The successor of an element in a BST is the element's successor in the sorted order determined by the inorder traversal. Finding the successor when each node has a pointer to its parent node is presented in CLRS's algorithm textbook (Introduction to Algorithms by MIT press).
The idea to find the successor here is - if the right subtree of node x
is nonempty, the successor of x
is the minimum element in the right subtree. Otherwise, the successor is the lowest ancestor of x
whose left child is also an ancestor of x
(assuming a node is an ancestor of itself).
Can we find the successor without using the pointer to the parent node?
Sometimes our tree node does not have this pointer. I struggled a couple of hours but cannot write the correct code.