Given a BST and a key. The task is to find the inorder successor and predecessor of the given key. In case, if either of predecessor or successor is not present, then print -1.
Input: 50 / \ / \ 30 70 / \ / \ / \ / \ 20 40 60 80 key = 65 Output: Predecessor : 60 Successor : 70 Input: 50 / \ / \ 30 70 / \ / \ / \ / \ 20 40 60 80 key = 100 Output: predecessor : 80 successor : -1 Explanation: As no node in BST has key value greater than 100 so -1 is printed for successor.
In the previous post, a recursive solution has been discussed. The problem can be solved using an iterative approach. To solve the problem, the three cases while searching for the key has to be dealt with which are as described below:
- Root is the given key: In this case, if the left subtree is not NULL, then predecessor is the rightmost node in left subtree and if right subtree is not NULL, then successor is the leftmost node in right subtree.
- Root is greater than key: In this case, the key is present in left subtree of root. So search for the key in left subtree by setting root = root->left. Note that root could be an inorder successor of given key. In case the key has no right subtree, the root will be its successor.
- Root is less than key: In this case, key is present in right subtree of root. So search for the key in right subtree by setting root = root->right. Note that root could be an inorder predecessor of given key. In case the key has no left subtree, the root will be its predecessor.
Below is the implementation of above approach:
# Python3 program to find predecessor
# and successor in a BST
# A utility function to create a
# new BST node
# Constructor to create a new node
def __init__(self, data):
self.key = data
self.left = None
self.right = None
# Function that finds predecessor and
# successor of key in BST.
def findPreSuc(root, pre, suc, key):
if root == None:
# Search for given key in BST.
while root != None:
# If root is given key.
if root.key == key:
# the minimum value in right
# subtree is predecessor.
suc = root.right
suc = suc.left
# the maximum value in left
# subtree is successor.
pre = root.left
pre = pre.right
# If key is greater than root, then
# key lies in right subtree. Root
# could be predecessor if left
# subtree of key is null.
elif root.key < key: pre = root root = root.right # If key is smaller than root, then # key lies in left subtree. Root # could be successor if right # subtree of key is null. else: suc = root root = root.left # A utility function to insert # a new node with given key in BST def insert(node, key): if node == None: return newNode(key) if key < node.key: node.left = insert(node.left, key) else: node.right = insert(node.right, key) return node # Driver Code if __name__ == '__main__': key = 65 # Key to be searched in BST # Let us create following BST # 50 # / \ # / \ # 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) pre, suc = [None], [None] findPreSuc(root, pre, suc, key) if pre != None: print("Predecessor is", pre.key) else: print("-1") if suc != None: print("Successor is", suc.key) else: print("-1") # This code is contributed by PranchalK [tabbyending]
Predecessor is 60 Successor is 70
Time Complexity: O(N)
Auxiliary Space: O(1)
- Inorder predecessor and successor for a given key in BST
- Inorder Successor in Binary Search Tree
- Largest number less than or equal to N in BST (Iterative Approach)
- Iterative approach to check if a Binary Tree is Perfect
- Reverse a Linked List in groups of given size (Iterative Approach)
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Iterative approach to check for children sum property in a Binary Tree
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Level Order Predecessor of a node in Binary Tree
- Level Order Successor of a node in Binary Tree
- Sum of the mirror image nodes of a complete binary tree in an inorder way
- Check if an array represents Inorder of Binary Search tree or not
- Iterative Postorder traversal | Set 3
- Level order traversal with direction change after every two levels | Recursive Approach
- Iterative searching in Binary Search Tree
Improved By : PranchalKatiyar