A tree Node
’s predecessor is the largest child Node
that is smaller than the current Node
.
Finding the Predecessor
To find a Node
’s predecessor, read the rightmost node that is a child of the current Node
’s left child.
static Node getPredecessor(Node node) {
if(node == null || node.left == null) return null;
return rightmostChild(node.left);
}
static Node rightmostChild(Node node) {
if(node.right == null) return node;
return rightmostChild(node.right);
}