0
0
DSA C++programming~10 mins

BST Inorder Successor in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - BST Inorder Successor
Start at root
Find node with given key
Does node have right child?
YesGo to right child
Find leftmost node in right subtree
Go up to parent while node is right child
Return parent as successor or NULL if none
Find the node, then if it has right child, successor is leftmost in right subtree; else go up to find first ancestor where node is in left subtree.
Execution Sample
DSA C++
Node* inorderSuccessor(Node* root, Node* p) {
  if (p->right) {
    p = p->right;
    while (p->left) p = p->left;
    return p;
  }
  Node* succ = NULL;
  while (root) {
    if (p->val < root->val) {
      succ = root;
      root = root->left;
    } else root = root->right;
  }
  return succ;
}
Finds the inorder successor of node p in BST by checking right subtree or climbing up ancestors.
Execution Table
StepOperationCurrent NodePointer ChangesVisual State
1Start at root to find node 510root=1010 / \ 5 15
2Go left since 5 < 105root=510 / \ 5 15
3Node 5 found, check right child5p=510 / \ 5 15
4Node 5 has right child 7, go right7p=710 / \ 5 15 \ 7
5Find leftmost in right subtree7p=710 / \ 5 15 \ 7
67 has no left child, successor is 77return 710 / \ 5 15 \ 7
7Return successor 77returnSuccessor: 7
8Alternate case: Node 15 with no right child15p=1510 / \ 5 15
9Go up to parent while node is right child15root=1010 / \ 5 15
1015 is right child of 10, move up10root=NULL10 / \ 5 15
11No ancestor where node is left child, successor NULLNULLreturn NULLSuccessor: NULL
💡 Traversal ends when successor found or no ancestor left where node is left child.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 9After Step 11
root1057710NULL
p55771515
succNULLNULLNULL7NULLNULL
Key Moments - 3 Insights
Why do we check the right child of the node first?
Because if the node has a right child, the inorder successor is the leftmost node in that right subtree, as shown in steps 3-6 of the execution_table.
What if the node has no right child? How do we find the successor?
We move up the tree to find the first ancestor where the node is in the left subtree, as shown in steps 8-11 where we climb from node 15 up to root 10 and find no successor.
Why do we stop when root becomes NULL in the upward traversal?
Because it means we reached the top of the tree without finding an ancestor where the node is a left child, so the node has no inorder successor (step 11).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the successor of node 5 at step 6?
ANode 7
BNode 10
CNode 15
DNULL
💡 Hint
Check the Visual State and Operation columns at step 6 where successor is returned as 7.
At which step does the traversal end with no successor found for node 15?
AStep 7
BStep 9
CStep 11
DStep 4
💡 Hint
Look at the exit_note and rows 10-11 where root becomes NULL and successor is NULL.
If node 5 had no right child, which operation would the algorithm perform next?
AReturn node 7 as successor
BGo up to parent to find ancestor where node is left child
CReturn NULL immediately
DGo to left child
💡 Hint
Refer to key_moments and execution_table steps 8-11 for the case when right child is absent.
Concept Snapshot
BST Inorder Successor:
- If node has right child, successor = leftmost node in right subtree
- Else, climb ancestors to find first node where current node is in left subtree
- Return NULL if no such ancestor
- Used in BST traversal and deletion
- Time: O(h) where h = tree height
Full Transcript
To find the inorder successor of a node in a BST, first check if the node has a right child. If yes, the successor is the leftmost node in that right subtree. If no right child exists, move up the tree to find the first ancestor where the node is in the left subtree. If none found, successor is NULL. This process is shown step-by-step in the execution table with pointer changes and visual tree states. Key moments clarify why we check right child first and how upward traversal works. The visual quiz tests understanding of these steps.