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.
Examples:
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:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int key;
struct Node *left, *right;
};
void findPreSuc(Node* root, Node*& pre, Node*& suc, int key)
{
if (root == NULL)
return ;
while (root != NULL) {
if (root->key == key) {
if (root->right) {
suc = root->right;
while (suc->left)
suc = suc->left;
}
if (root->left) {
pre = root->left;
while (pre->right)
pre = pre->right;
}
return ;
}
else if (root->key < key) {
pre = root;
root = root->right;
}
else {
suc = root;
root = root->left;
}
}
}
Node* newNode( int item)
{
Node* temp = new Node;
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
Node* insert(Node* node, int key)
{
if (node == NULL)
return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
int main()
{
int key = 65;
Node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
Node *pre = NULL, *suc = NULL;
findPreSuc(root, pre, suc, key);
if (pre != NULL)
cout << "Predecessor is " << pre->key << endl;
else
cout << "-1" ;
if (suc != NULL)
cout << "Successor is " << suc->key;
else
cout << "-1" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int key;
Node left, right;
};
static Node pre;
static Node suc;
static void findPreSuc(Node root, int key)
{
if (root == null )
return ;
while (root != null )
{
if (root.key == key)
{
if (root.right != null )
{
suc = root.right;
while (suc.left != null )
suc = suc.left;
}
if (root.left != null )
{
pre = root.left;
while (pre.right != null )
pre = pre.right;
}
return ;
}
else if (root.key < key)
{
pre = root;
root = root.right;
}
else
{
suc = root;
root = root.left;
}
}
}
static Node newNode( int item)
{
Node temp = new Node();
temp.key = item;
temp.left = temp.right = null ;
return temp;
}
static Node insert(Node node, int key)
{
if (node == null )
return newNode(key);
if (key < node.key)
node.left = insert(node.left, key);
else
node.right = insert(node.right, key);
return node;
}
public static void main(String[] args)
{
int key = 65 ;
Node root = null ;
root = insert(root, 50 );
insert(root, 30 );
insert(root, 20 );
insert(root, 40 );
insert(root, 70 );
insert(root, 60 );
insert(root, 80 );
findPreSuc(root, key);
if (pre != null )
System.out.println( "Predecessor is " +
pre.key);
else
System.out.print( "-1" );
if (suc != null )
System.out.print( "Successor is " + suc.key);
else
System.out.print( "-1" );
}
}
|
Python3
class newNode:
def __init__( self , data):
self .key = data
self .left = None
self .right = None
def findPreSuc(root, pre, suc, key):
if root = = None :
return
while root ! = None :
if root.key = = key:
if root.right:
suc[ 0 ] = root.right
while suc[ 0 ].left:
suc[ 0 ] = suc[ 0 ].left
if root.left:
pre[ 0 ] = root.left
while pre[ 0 ].right:
pre[ 0 ] = pre[ 0 ].right
return
elif root.key < key:
pre[ 0 ] = root
root = root.right
else :
suc[ 0 ] = root
root = root.left
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
if __name__ = = '__main__' :
key = 65
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[ 0 ] ! = None :
print ( "Predecessor is" , pre[ 0 ].key)
else :
print ( "-1" )
if suc[ 0 ] ! = None :
print ( "Successor is" , suc[ 0 ].key)
else :
print ( "-1" )
|
C#
using System;
class GFG
{
class Node
{
public int key;
public Node left, right;
};
static Node pre;
static Node suc;
static void findPreSuc(Node root, int key)
{
if (root == null )
return ;
while (root != null )
{
if (root.key == key)
{
if (root.right != null )
{
suc = root.right;
while (suc.left != null )
suc = suc.left;
}
if (root.left != null )
{
pre = root.left;
while (pre.right != null )
pre = pre.right;
}
return ;
}
else if (root.key < key)
{
pre = root;
root = root.right;
}
else
{
suc = root;
root = root.left;
}
}
}
static Node newNode( int item)
{
Node temp = new Node();
temp.key = item;
temp.left = temp.right = null ;
return temp;
}
static Node insert(Node node, int key)
{
if (node == null )
return newNode(key);
if (key < node.key)
node.left = insert(node.left, key);
else
node.right = insert(node.right, key);
return node;
}
public static void Main(String[] args)
{
int key = 65;
Node root = null ;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
findPreSuc(root, key);
if (pre != null )
Console.WriteLine( "Predecessor is " +
pre.key);
else
Console.Write( "-1" );
if (suc != null )
Console.Write( "Successor is " + suc.key);
else
Console.Write( "-1" );
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .key = val;
this .left = null ;
this .right = null ;
}
}
var pre;
var suc;
function findPreSuc(root , key)
{
if (root == null )
return ;
while (root != null )
{
if (root.key == key)
{
if (root.right != null )
{
suc = root.right;
while (suc.left != null )
suc = suc.left;
}
if (root.left != null )
{
pre = root.left;
while (pre.right != null )
pre = pre.right;
}
return ;
}
else if (root.key < key)
{
pre = root;
root = root.right;
}
else
{
suc = root;
root = root.left;
}
}
}
function newNode(item)
{
var temp = new Node();
temp.key = item;
temp.left = temp.right = null ;
return temp;
}
function insert(node , key)
{
if (node == null )
return newNode(key);
if (key < node.key)
node.left = insert(node.left, key);
else
node.right = insert(node.right, key);
return node;
}
var key = 65;
var root = null ;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
findPreSuc(root, key);
if (pre != null )
document.write( "Predecessor is " +
pre.key+ "<br/>" );
else
document.write( "-1<br/>" );
if (suc != null )
document.write( "Successor is " + suc.key);
else
document.write( "-1" );
</script>
|
Output:
Predecessor is 60
Successor is 70
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Article: https://www.geeksforgeeks.org/inorder-predecessor-successor-given-key-bst/
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Jan, 2022
Like Article
Save Article