ode inorder_succ (Node root, Node p) {
if (root == null || p == null)
return null;
if(p.right != null) {
// if has right subtree
// then succ is smallest in right subtree
Node tmp = p.right;
while(tmp.left != null){
tmp = tmp.left;
}
return tmp;
}
Node tmp = null;
// Else we move down from root to find the node
// Save the left parent. the last left parent is succ
while(root!= null && root.data != p.data) {
if(root.data >= p.data) {
//moving left save the node
tmp = root;
root = root.left;
} else {
root = root.right
}
}
return tmp;
}