The in-order successor of a node in a binary search tree is the node with the smallest key greater than the key of the given node.
Key takeaways:
The in-order successor in a binary search tree is the node with the smallest key greater than the current node's key.
Finding the in-order successor is a common task when performing deletions, updates, or search operations in BSTs.
The time complexity of finding an in-order successor is proportional to the height of the tree, with
being the best and worst cases for balanced and unbalanced trees, respectively. The space complexity of this algorithm is constant,
, as it only uses a fixed amount of extra space.
In a binary search tree (BST), the in-order successor of a node is the one that appears immediately after it in an in-order traversal. This concept is crucial for navigating BSTs, particularly during operations like deletion, updating, or searching. Since an in-order traversal of a BST produces nodes in sorted order, the in-order successor is the next greater node. Identifying this successor is a key step in many tree-based algorithms. Before delving into this Answer, it would be helpful to look at an overview of binary search trees.
The in-order successor of a node in a binary search tree is the node with the smallest key that is larger than the key of the given node. In this task, the goal is to find and return this successor when a specific node in the tree is provided.
Constraints:
nodes
nodes.data
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re understood the concept of in-order successor correctly:
In-order successor in a binary search tree
What is the in-order successor of a 9 in the following tree?
6
/ \
2 9
/ / \
1 10 13
6
10
13
2
This solution finds the inorder successor of a given node in the binary search tree. Because a binary search tree maintains the property where left children are smaller and right children are larger than the parent node, the solution uses this property to find the in-order successor without visiting all nodes. To locate the successor:
If the target node has a right child, its successor is the smallest node in the right subtree.
If there is no right child, the successor lies in one of the ancestors. We must traverse the tree, keeping track of the last node where we turned left, as that node will be the closest greater value.
Here’s the step-by-step implementation of the solution:
Initialize an empty node to return the result. This variable will store the in-order successor.
Begin at the root of the binary search tree.
Loop through the tree:
If the node value we need to find the successor of is greater than or equal to the current node’s value, move to the right subtree (no need to check the left subtree as the value is greater).
If the value is less than the current node’s value, update the result node to the current root node and move to the left subtree.
The result node will eventually hold the in-order successor. If the result remains None
after the loop, there is no in-order successor in the binary search tree.
Let’s look at the Python code for the algorithm we just discussed.
# Class definition for a binary tree nodeclass Tree:def __init__(self, data=0, left=None, right=None):self.data = data # The value stored in the nodeself.left = left # Pointer to the left childself.right = right # Pointer to the right child# Function to find the inorder successor of a given node in a BSTdef inorder(root, node):# Variable to store the potential inorder successorresult = None# Traverse the tree starting from the rootwhile root:if node.data >= root.data:# If the node's value is greater than or equal to the current root's value,# move to the right subtree (no inorder successor in the left subtree)root = root.rightelse:# If the node's value is less than the current root's value,# update the result as the current root (potential successor)# and move to the left subtree to check for a smaller successorresult = rootroot = root.left# Return the inorder successor if found, otherwise Nonereturn result# Driver codedef main():root = Tree(10)root.left = Tree(5)root.right = Tree(20)root.left.left = Tree(3)root.left.right = Tree(8)root.right.left = Tree(15)root.right.right = Tree(25)node = root.left.rightsuccessor = inorder(root, node)if successor:print(f"The inorder successor of {node.data} is", successor.data)else:print(f"There is no inorder successor of {node}")if __name__ == '__main__':main()
The time complexity of this code is
The space complexity of this code is
If you further want to explore the binary search tree problems, here are some of the problems solved by different coding patterns:
Haven’t found what you were looking for? Contact Us
Free Resources