Last active
June 26, 2020 17:44
-
-
Save anil477/b2c74903d41757b5f24321212eb76e90 to your computer and use it in GitHub Desktop.
Inorder predecessor and successor for a given key in BST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://www.geeksforgeeks.org/inorder-predecessor-successor-given-key-bst/ | |
// http://code.geeksforgeeks.org/NvtCcP | |
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 then root node | |
set the successor as root | |
search recursively into left subtree | |
else | |
set the predecessor as root | |
search recursively into right subtree | |
*/ | |
class P03_FindPredecessorAndSuccessor { | |
static Node root; | |
static Node pre; | |
static Node suc; | |
static class Node{ | |
private int data; | |
private Node left; | |
private Node right; | |
Node(int data){ | |
this.data = data; | |
left = right = null; | |
} | |
} | |
public void insert(int key){ | |
root = insert(root,key); | |
} | |
public Node insert(Node node,int key){ | |
if(node==null){ | |
node = new Node(key); | |
return node; | |
} | |
if(key < node.data) | |
node.left = insert(node.left, key); | |
else if(key > node.data) | |
node.right = insert(node.right, key); | |
return node; | |
} | |
public void printTree(Node node) | |
{ | |
if(node!=null){ | |
printTree(node.left); | |
System.out.print(node.data+" "); | |
printTree(node.right); | |
} | |
} | |
public void predSucc(Node node,int key) | |
{ | |
if(node == null) return; | |
if(node.data == key) { | |
if(node.left != null){ | |
// maximum element in the left subtree - it be the rightmost element | |
Node current = node.left; | |
while(current.right != null) { | |
current = current.right; | |
} | |
pre = current; | |
} | |
if(node.right != null){ | |
// minimum element in the right subtree - it be the lefttmost element | |
Node current = node.right; | |
while(current.left != null) { | |
current = current.left; | |
} | |
suc = current; | |
} | |
} | |
if(node.data > key) { | |
suc = node; | |
predSucc(node.left, key); | |
} else if (node.data < key){ | |
pre = node; | |
predSucc(node.right, key); | |
} | |
} | |
public static void main(String[] args){ | |
P03_FindPredecessorAndSuccessor tree = new P03_FindPredecessorAndSuccessor(); | |
tree.insert(70); | |
tree.insert(20); | |
tree.insert(50); | |
tree.insert(10); | |
tree.insert(30); | |
tree.insert(60); | |
tree.insert(40); | |
tree.insert(5); | |
tree.printTree(root); | |
tree.predSucc(root,40); | |
System.out.println(""); | |
if(pre == null){ | |
System.out.println(" no predecessor"); | |
} else { | |
System.out.println("predecessor : "+pre.data); | |
} | |
if(suc == null){ | |
System.out.println(" no successor"); | |
} else{ | |
System.out.println("sucsessor : "+suc.data); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment