Site icon

Find InOrder successor of a node in binary search tree using java (example)

Fig 1: Find InOrder Successor of BST

The inorder traversal (DFS) of binary search tree is :
25 50 75 100 120 125 140 150 160 175 190
InOrder traversal of binary search tree, gives the sorted output. So, inorder successor is next higher number of input number.

Examples to find successor of input node:

S. No.Input node Successor node
1Node H (120) Node F (125)
2Node E (75) Node A(100)
3Node F (125) Node I (140)
4Node C (150) Node J(160)
Let us analyse above InOrder successors with the help of examples:

Example 1: InOrder successor of Node H (120)?

Fig 2: InOrder Successor(H) = Node F

Example 2: InOrder successor of  Node E (75) ?

Fig 3: InOrder Predecessor(E) = Node A

How many parents nodes we need to traverse to find the successors?
In example 1, we had travel just one parent. By keeping in mind the properties of binary search tree, we need the first parent, from where current node, is lying on the left side of it. Let us traverse the tree to understand it better.

Arise of First Condition to find Inorder Successor:
If a node do not have any right child then we will to traverse to parent nodes to find inorder successor of binary tree.

Example 3: InOrder successor of 125 (Node F) ?

Fig 4: InOrder Successor(F) = Node I

Example 4: InOrder successor of 150 (Node C) ?

Fig 5: InOrder Predecessor(C) = Node J

Arise of Second (and last) Condition to find Inorder Successor
Example 3 and example 4 will be our second or last condition to find the inorder successor of binary search tree.
Conclusion to find InOrder Successor in BST: We can categorize the algorithm into following categories

Algorithm: find Inorder successor in a BST using java

Program – find InOrder successor of given node in a BST using java

1.) InorderSuccessor class:

package org.learn.Question;
import org.learn.PrepareTree.Node;

public class InorderSuccessor {
	private static Node min(Node root) {
		//we found the min node
		if(root.left == null) {
			return root;
		}
		return min(root.left);
	}
	
	public static Node successor(Node root, Node node) {
		//Example 3 or Example 4
		if(node.right != null)
			return min(node.right);
		
		//Example 1 or Example 2
		Node successor = null;
	    // Start from root and search for successor down the tree
	    while (root != null)
	    {
	    	if(node.data == root.data) {
	    		//by now we might found our successor
	    		break;
	    	} else if (node.data < root.data) {
	        	successor = root;
	            root = root.left;
	        } else if (node.data > root.data)
	            root = root.right;	       
	    }
	   return successor;
	}
}

2.) Node class:

package org.learn.PrepareTree;

public class Node {
	public int data;
	public Node left;
	public Node right;

	public Node(int num) {
		this.data = num;
		this.left = null;
		this.right = null;
	}

	public Node() {
		this.left = null;
		this.right = null;
	}
	public static Node createNode(int number) {
		return new Node(number);
	}
}

3.) App class:

package org.learn.Client;

import org.learn.PrepareTree.Node;
import org.learn.Question.InorderSuccessor;

public class App 
{
    public static void main( String[] args )
    {  
       //root level 0
       Node A = Node.createNode(100);
       //Level 1
       Node B = Node.createNode(50);
       Node C = Node.createNode(150);
       //Level 2
       Node D = Node.createNode(25);
       Node E = Node.createNode(75);
       Node F = Node.createNode(125);
       Node G = Node.createNode(175);
       //Level 3
       Node H = Node.createNode(120);
       Node I = Node.createNode(140);
       Node J = Node.createNode(160);
       Node K = Node.createNode(190);
             
       //connect Level 0 and 1
       A.left = B;
       A.right = C;
       //connect level 1 and level 2
       B.left = D;
       B.right = E;
       C.left = F;
       C.right = G;
       //Connect level 2 and level 3
       F.left = H;
       F.right = I;
       G.left = J;
       G.right = K;
         
       Node successor = InorderSuccessor.successor(A, H);
       System.out.printf("Inorder successor of Node %d is %d\n",H.data, successor.data);     
       
       successor = InorderSuccessor.successor(A, E);
       System.out.printf("Inorder successor of Node %d is %d\n",E.data, successor.data);     
       
       successor = InorderSuccessor.successor(A, F);
       System.out.printf("Inorder successor of Node %d is %d\n",F.data, successor.data);     
       
       successor = InorderSuccessor.successor(A, C);
       System.out.printf("Inorder successor of Node %d is %d\n",C.data, successor.data); 
       
       successor = InorderSuccessor.successor(A, A);
       System.out.printf("Inorder successor of Node %d is %d\n",A.data, successor.data);  
    }
}

Output – InOrder successors of a given node using java

InOrder successor of Node 120 is 125
InOrder successor of Node 75 is 100
InOrder successor of Node 125 is 140
InOrder successor of Node 150 is 160
InOrder successor of Node 100 is 120<

Download code – find InOrder Successor in BST using java

Exit mobile version