Because the code is too long the link is here ->http://pastebin.com/jXgbE6bB
Because i am not that good at recursions i just can't find the right recursion function for this problem.
(P.S.I am new to this forum and i know i am going to have a lot of hate comments like why not go find tutorials on recursions and other things,but believe me i have done everything but i just can't understand the logic of the recursions)
My question is What's the recursive function for in-order successor of a given element in Binary search tree?
I made it this far but it just returns the parent of the node it's supposed to print:
public E getSuccessor(BNode<E> t, E x) {
if(t.left!=null && x.equals(t.info)){
return t.left.info;
}
else if(x.compareTo(t.info)<0){
return (getSuccessor(t.left, x));
}
else {
return (getSuccessor(t.right, x));
}
}
public E getSuccessor(E x) {
return getSuccessor(root, x);
}
The inorder successor of a given node is the lowest node in the right subtree of that node. To understand otherwise, it is the next node that will be printed in a simple in order traversal of the tree.
Here is the recursive way to solve this. While calling the method, pass root as the root of the tree, the node who's successor is needed as t and null as the successor because the method will calculate it. Something like the following -
BinaryTreeNode successor=tree.inorderSuccessor(root,node,null);