Find the successor node of a node in the binary tree public class getnextnode{//node definition public static class node{public int value;
Public Node left;
public Node right;
public Node parent;
public Node (int data) {this.value=data;
}///Given a node to obtain its successor node public static node Getnextnode (node node) {//if (node==null) {return node;
//If there is a subtree, then the successor node is the leftmost node in the right subtree if (node.right!=null) {return getleftmost (node.right);
The}//node is moved up to find the parent node else{nodes parent=node.parent;
while (Parent!=null&&parent.left!=node) {node=parent;
Parent=node.parent;
return to parent;
The public static Node Getleftmost (node node) {if (node==null) {return node;
while (node.left!=null) {node=node.left;
} return node; public static void Main (String[]args) {/** constructed two-fork Tree 6 3 9 1
4 8 2 5 7/Node head = new node (6);
Head.parent = null; Head.left = new NoDe (3);
Head.left.parent = head;
Head.left.left = new Node (1);
Head.left.left.parent = Head.left;
Head.left.left.right = new Node (2);
Head.left.left.right.parent = Head.left.left;
Head.left.right = new Node (4);
Head.left.right.parent = Head.left;
Head.left.right.right = new Node (5);
Head.left.right.right.parent = Head.left.right;
Head.right = new Node (9);
Head.right.parent = head;
Head.right.left = new Node (8);
Head.right.left.parent = Head.right;
Head.right.left.left = new Node (7);
Head.right.left.left.parent = Head.right.left;
Head.right.right = new Node (10);
Head.right.right.parent = Head.right;
Node test = Head.left.left;
System.out.println (Test.value + "Next:" + getnextnode (test). Value);
Test = Head.left.left.right;
System.out.println (Test.value + "Next:" + getnextnode (test). Value);
Test = Head.left;
System.out.println (Test.value + "Next:" + getnextnode (test). Value);
Test = Head.left.right; System.out.println (Test.value + "nExt: "+ getnextnode (test). Value);
Test = Head.left.right.right;
System.out.println (Test.value + "Next:" + getnextnode (test). Value);
Test = head;
System.out.println (Test.value + "Next:" + getnextnode (test). Value);
Test = Head.right.left.left;
System.out.println (Test.value + "Next:" + getnextnode (test). Value);
Test = Head.right.left;
System.out.println (Test.value + "Next:" + getnextnode (test). Value);
Test = Head.right;
System.out.println (Test.value + "Next:" + getnextnode (test). Value); Test = Head.right.right;
The ' s next is null System.out.println (Test.value + "Next:" + getnextnode (test)); }
}