Binary Search Tree Coding PYTHON

Inorder predecessor and successor for a given key in BST

python Programming Inorder predecessor and successor for a given key in BST - I recently encountered with a question in an interview at e-commerce company.

Inorder predecessor and successor for a given key in BST

I recently encountered with a question in an interview at e-commerce company. The interviewer asked the following question:

There is BST given with root node with key part as integer only. The structure of each node is as follows:

struct Node
    int key;
    struct Node *left, *right ;

You need to find the inorder successor and predecessor of a given key. In case the given key is not found in BST, then return the two values within which this key will lie.

inorder successor and inorder predecessor

Following is the algorithm to reach the desired result. Its a recursive method:

Input: root node, key
output: predecessor node, successor node

1. If root is NULL
      then return
2. if key is found then
    a. If its left subtree is not null
        Then predecessor will be the right most 
        child of left subtree or left child itself.
    b. If its right subtree is not null
        The successor will be the left most child 
        of right subtree or right child itself.
3. If key is smaller then root node
        set the successor as root
        search recursively into left subtree
        set the predecessor as root
        search recursively into right subtree

Implementation of Python code:

Python Programming
# Python program to find predecessor and successor in a BST
# A BST node
class Node:
    # Constructor to create a new node
    def __init__(self, key):
        self.key  = key
        self.left = None
        self.right = None
# This fucntion finds predecessor and successor of key in BST
# It sets pre and suc as predecessor and successor respectively
def findPreSuc(root, key):
    # Base Case
    if root is None:
    # If key is present at root
    if root.key == key:
        # the maximum value in left subtree is predecessor
        if root.left is not None:
            tmp = root.left 
                tmp = tmp.right 
            findPreSuc.pre = tmp
        # the minimum value in right subtree is successor
        if root.right is not None:
            tmp = root.right
                tmp = tmp.left 
            findPreSuc.suc = tmp 
    # If key is smaller than root's key, go to left subtree
    if root.key > key :
        findPreSuc.suc = root 
        findPreSuc(root.left, key)
    else: # go to right subtree
        findPreSuc.pre = root
        findPreSuc(root.right, key)
# A utility function to insert a new node in with given key in BST
def insert(node , key):
    if node is None:
        return Node(key)
    if key < node.key:
        node.left = insert(node.left, key)
        node.right = insert(node.right, key)
    return node
# Driver program to test above function
key = 65 #Key to be searched in BST
""" Let us create following BST
           /     \
          30      70
         /  \    /  \
       20   40  60   80 
root = None
root = insert(root, 50)
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
# Static variables of the function findPreSuc 
findPreSuc.pre = None
findPreSuc.suc = None
findPreSuc(root, key)
if findPreSuc.pre is not None:
    print "Predecessor is", findPreSuc.pre.key
    print "No Predecessor"
if findPreSuc.suc is not None:
    print "Successor is", findPreSuc.suc.key
    print "No Successor"
# This code is contributed by Nikhil Kumar Singh(nickzuck_007)


Predecessor is 60
Successor is 70
READ  C Program Lowest Common Ancestor in a Binary Search Tree

About the author

Venkatesan Prabu

Venkatesan Prabu

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.