Inorder Successor in Binary Search Tree:

In Binary Tree, Inorder successor of a node is the next node in Inorder traversal of the Binary Tree. Inorder Successor is NULL for the last node in Inorder traversal.


In Binary Search Tree, Inorder Successor of an input node can also be defined as the node with the smallest key greater than the key of input node. So, it is sometimes important to find next node in sorted order.

 binary search trees

In the above diagram, inorder successor of 8 is 10, inorder successor of 10 is 12 and inorder successor of 14 is 20.

 

[ad type=”banner”]

Method 1 (Uses Parent Pointer)

In this method, we assume that every node has parent pointer.

The Algorithm is divided into two cases on the basis of right subtree of the input node being empty or not.

Input: node, root // node is the node whose Inorder successor is needed.
output: succ // succ is Inorder successor of node.

1) If right subtree of node is not NULL, then succ lies in right subtree. Do following.
Go to right subtree and return the node with minimum key value in right subtree.
2) If right subtree of node is NULL, then succ is one of the ancestors. Do following.
Travel up using the parent pointer until you see a node which is left child of it’s parent. The parent of such a node is the succ.

Implementation of Python Program:

Note that the function to find InOrder Successor is highlighted (with gray background) in below code.

[pastacode lang=”python” manual=”%23%20Python%20program%20to%20find%20the%20inroder%20successor%20in%20a%20BST%0A%20%0A%23%20A%20binary%20tree%20node%20%0Aclass%20Node%3A%0A%20%0A%20%20%20%20%23%20Constructor%20to%20create%20a%20new%20node%0A%20%20%20%20def%20__init__(self%2C%20key)%3A%0A%20%20%20%20%20%20%20%20self.data%20%3D%20key%20%0A%20%20%20%20%20%20%20%20self.left%20%3D%20None%0A%20%20%20%20%20%20%20%20self.right%20%3D%20None%0A%20%0Adef%20inOrderSuccessor(root%2C%20n)%3A%0A%20%20%20%20%20%0A%20%20%20%20%23%20Step%201%20of%20the%20above%20algorithm%0A%20%20%20%20if%20n.right%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20return%20minValue(n.right)%0A%20%0A%20%20%20%20%23%20Step%202%20of%20the%20above%20algorithm%0A%20%20%20%20p%20%3D%20n.parent%0A%20%20%20%20while(%20p%20is%20not%20None)%3A%0A%20%20%20%20%20%20%20%20if%20n%20!%3D%20p.right%20%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20n%20%3D%20p%20%0A%20%20%20%20%20%20%20%20p%20%3D%20p.parent%0A%20%20%20%20return%20p%0A%20%0A%23%20Given%20a%20non-empty%20binary%20search%20tree%2C%20return%20the%20%0A%23%20minimum%20data%20value%20found%20in%20that%20tree.%20Note%20that%20the%0A%23%20entire%20tree%20doesn’t%20need%20to%20be%20searched%0Adef%20minValue(node)%3A%0A%20%20%20%20current%20%3D%20node%0A%20%0A%20%20%20%20%23%20loop%20down%20to%20find%20the%20leftmost%20leaf%0A%20%20%20%20while(current%20is%20not%20None)%3A%0A%20%20%20%20%20%20%20%20if%20current.left%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20current%20%3D%20current.data%0A%20%0A%20%20%20%20return%20current%0A%20%0A%20%0A%23%20Given%20a%20binary%20search%20tree%20and%20a%20number%2C%20inserts%20a%0A%23%20new%20node%20with%20the%20given%20number%20in%20the%20correct%20place%0A%23%20in%20the%20tree.%20Returns%20the%20new%20root%20pointer%20which%20the%0A%23%20caller%20should%20then%20use(%20the%20standard%20trick%20to%20avoid%20%0A%23%20using%20reference%20parameters)%0Adef%20insert(%20node%2C%20data)%3A%0A%20%0A%20%20%20%20%23%201)%20If%20tree%20is%20empty%20then%20return%20a%20new%20singly%20node%0A%20%20%20%20if%20node%20is%20None%3A%0A%20%20%20%20%20%20%20%20return%20Node(data)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%202)%20Otherwise%2C%20recur%20down%20the%20tree%0A%20%20%20%20%20%20%20%20if%20data%20%3C%3D%20node.data%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20insert(node.left%2C%20data)%0A%20%20%20%20%20%20%20%20%20%20%20%20node.left%20%3D%20temp%20%0A%20%20%20%20%20%20%20%20%20%20%20%20temp.parent%20%3D%20node%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20insert(node.right%2C%20data)%0A%20%20%20%20%20%20%20%20%20%20%20%20node.right%20%3D%20temp%20%0A%20%20%20%20%20%20%20%20%20%20%20%20temp.parent%20%3D%20node%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20return%20%20the%20unchanged%20node%20pointer%0A%20%20%20%20%20%20%20%20return%20node%0A%20%0A%20%0A%23%20Driver%20progam%20to%20test%20above%20function%0A%20%0Aroot%20%20%3D%20None%0A%20%0A%23%20Creating%20the%20tree%20given%20in%20the%20above%20diagram%20%0Aroot%20%3D%20insert(root%2C%2020)%0Aroot%20%3D%20insert(root%2C%208)%3B%0Aroot%20%3D%20insert(root%2C%2022)%3B%0Aroot%20%3D%20insert(root%2C%204)%3B%0Aroot%20%3D%20insert(root%2C%2012)%3B%0Aroot%20%3D%20insert(root%2C%2010)%3B%20%20%0Aroot%20%3D%20insert(root%2C%2014)%3B%20%20%20%20%0Atemp%20%3D%20root.left.right.right%20%0A%20%0Asucc%20%3D%20inOrderSuccessor(%20root%2C%20temp)%0Aif%20succ%20is%20not%20None%3A%0A%20%20%20%20print%20%22%5CnInorder%20Successor%20of%20%25d%20is%20%25d%20%22%20%5C%0A%20%20%20%20%20%20%20%20%20%20%20%20%25(temp.data%20%2C%20succ.data)%0Aelse%3A%0A%20%20%20%20print%20%22%5CnInorder%20Successor%20doesn’t%20exist%22%0A%20%0A%23%20This%20code%20is%20contributed%20by%20Nikhil%20Kumar%20Singh(nickzuck_007)” message=”Python Programming” highlight=”” provider=”manual”/]

Output:

Inorder Successor of 14 is 20

Time Complexity: O(h) where h is height of tree.

[ad type=”banner”]

Method 2 (Search from root)

Parent pointer is NOT needed in this algorithm. The Algorithm is divided into two cases on the basis of right subtree of the input node being empty or not.

Input: node, root // node is the node whose Inorder successor is needed.
output: succ // succ is Inorder successor of node.

Step 1: If right subtree of node is not NULL, then successor lies in right subtree. Do following.
Go to right subtree and return the node with minimum key value in right subtree.
Step 2: If right subtree of node is NULL, then start from root and us search like technique. Do following.
Travel down the tree, if a node’s data is greater than root’s data then go right side, otherwise go to left side.

[ad type=”banner”]