Ifkhas a left subtree, the predecessor ofkwill be the maximum integer in the left subtree ofk. From our preceding BST, ifk = 12,Predecessor(12)will be7since it's the maximum integer in the left subtree of12. Please take a look at the following diagram:
Ifkdoes not have a left subtree, we have to traverse the ancestors ofkuntil we find the first node, n, which is lower than nodek. After we find noden, we will see that nodenis the minimum element of the traversed elements. From our preceding BST, ifk = 29,Predecessor(29)will give us23since it's the first lower ancestor ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.