There is BST given with root node with key part as integer only. The structure of each node is as follows: Show C++
Java
Python3
C#
Javascript
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. Following is the algorithm to reach the desired result. It is 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. return 3. If key is smaller than root node set the successor as root search recursively into left subtree else set the predecessor as root search recursively into right subtree Following is the implementation of the above algorithm: C++
Java
Python
C#
Javascript
Output: Predecessor is 60 Successor is 70 Complexity Analysis: Time
Complexity: O(h), where h is the height of the tree. In the worst case as explained above we travel the whole height of the tree. Another Approach: We can also find the inorder successor and inorder predecessor using inorder traversal. Check if the current node is smaller than the given key for the predecessor and for a successor, check if it is greater than the given key. If it is greater than the given key then, check if it is smaller than the already stored value in the successor then, update it. At last, get the predecessor and successor stored in q(successor) and p(predecessor). C++
Java
Python3
C#
Javascript
Output : 50 60 Complexity Analysis: Time Complexity:
O(n), where n is the total number of nodes in the tree. In the worst case as explained above we travel the whole tree. ?list=PLqM7alHXFySHCXD7r1J0ky9Zg_GBB1dbk This article is contributed by algoLover. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Where is the inorder successor of a node A in a 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 the input node.
Where is the predecessor in a binary search tree?Where is the predecessor of a node in a tree, assuming all keys are distinct? If X has two children, its predecessor is the maximum value in its left subtree and its successor the minimum value in its right subtree. If it does not have a left child, a node's predecessor is its rst left ancestor.
Where are inorder predecessor and inorder successor stored in a binary search tree?When you do the inorder traversal of a binary tree, the neighbors of given node are called Predecessor(the node lies behind of given node) and Successor (the node lies ahead of given node). Approach: Say you have to find the inorder predecessor and successor node 15. First compare the 15 with root (25 here).
What is inorder predecessor in binary tree?The inorder predecessor of a node p is the node q that comes just before p in the binary tree's inorder traversal. Given the root node of a binary search tree and the node p , find the inorder predecessor of node p . If it does not exist, return null .
|