I'm trying to understand the rules for finding the inorder predecessor of a node in a binary search tree (BST).
If a node π₯ has a left subtree, the inorder predecessor is the largest value in that left subtree.
If node π₯ has no left subtree, the predecessor is one of its ancestors. But if node π₯ is the right child, then its parent p is its predecessor
Hereβs my specific question:
If node π₯ has no left subtree and is a left child of its parent, its parent π cannot be the predecessor because π is larger than π₯. In this case, we move up the tree to find the ancestor that is smaller than π₯, and that ancestor is π₯'s predecessor.
Take a look at this BST:
50
/ \
30 70
/ \
20 40
/
35
- Node π₯ = 35 has no left subtree and is the left child of its parent
- 40 (the parent) cannot be the inorder predecessor because it's larger than 35.
- We move up the tree to the parent of 40, which is 30.
- 30 is smaller than 35, so 30 is the inorder predecessor of 35.
Is it true that the inorder predecessor of node π₯ in this situation is always its grandparent, or could it be a more distant ancestor?