To find the successor of x in a BST
Page 1 of 1
To find the successor of x in a BST
The last left turn is the successor(x):
public E successor(E x){
return succAux(root, x, null);
}
public E succAux(Node root, E x, E best){
if(root==null)
return best;
int c=x.compartTo(root);
if(c>0)
return succAux(root.right, X, best);
else
return succAux(root.l, X, root.v);
}
public E successor(E x){
return succAux(root, x, null);
}
public E succAux(Node root, E x, E best){
if(root==null)
return best;
int c=x.compartTo(root);
if(c>0)
return succAux(root.right, X, best);
else
return succAux(root.l, X, root.v);
}
Similar topics
» Find whether two BST are the same. Recursively and Iteratively
» Find if a singly linked List has loop or not. How to find out middle element from a looped single linked list
» Find the height of BST
» Find pairs in an array that sum to y
» Given a string, find all of its permutations
» Find if a singly linked List has loop or not. How to find out middle element from a looped single linked list
» Find the height of BST
» Find pairs in an array that sum to y
» Given a string, find all of its permutations
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|