0
0
DSA C++programming~10 mins

BST Inorder Predecessor in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - BST Inorder Predecessor
Start at root node
Search for target node
Found target node?
NoReturn None
Yes
Does target have left child?
YesGo to left child
Go to rightmost node in left subtree
Go up to ancestor where target is in right subtree
Return that ancestor as predecessor
Done
Start from root to find the target node. If it has a left child, predecessor is the rightmost node in left subtree. Otherwise, move up to find the ancestor where target is in right subtree.
Execution Sample
DSA C++
Node* inorderPredecessor(Node* root, Node* target) {
  if (!target) return nullptr;
  if (target->left) {
    Node* curr = target->left;
    while (curr->right) curr = curr->right;
    return curr;
  }
  Node* pred = nullptr;
  Node* curr = root;
  while (curr) {
    if (target->data > curr->data) {
      pred = curr;
      curr = curr->right;
    } else if (target->data < curr->data) {
      curr = curr->left;
    } else break;
  }
  return pred;
}
Finds the inorder predecessor of a target node in a BST by checking left subtree or ancestors.
Execution Table
StepOperationCurrent NodePointer ChangesVisual State
1Start search for target 1510 (root)pred=nullptr, curr=1010 / \ 5 15 / \ 12 20
215 > 10, move right15pred=10, curr=1510 / \ 5 15 / \ 12 20
3Found target 1515pred=10, curr=1510 / \ 5 15 / \ 12 20
4Target has left child 1215curr=1210 / \ 5 15 / \ 12 20
5Go to rightmost in left subtree12curr=1210 / \ 5 15 / \ 12 20
612 has no right child, stop12pred=1210 / \ 5 15 / \ 12 20
7Return predecessor 1212Return 1210 / \ 5 15 / \ 12 20
8Example: target 5 no left child10pred=nullptr, curr=1010 / \ 5 15 / \ 12 20
95 < 10, move left5pred=nullptr, curr=510 / \ 5 15 / \ 12 20
10Found target 55pred=nullptr, curr=510 / \ 5 15 / \ 12 20
11No left child, move up to find ancestor5pred=nullptr, curr=1010 / \ 5 15 / \ 12 20
125 < 10, move left5pred=nullptr, curr=510 / \ 5 15 / \ 12 20
13No ancestor where target is in right subtreenullReturn nullptr10 / \ 5 15 / \ 12 20
14EndnullStopPredecessor is nullptr (no predecessor)
💡 Search ends when target found and predecessor identified or none exists.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
curr1015121212
prednullptr10101212
target1515151515
Key Moments - 3 Insights
Why do we check the left child of the target node first?
Because if the target has a left subtree, the inorder predecessor is the rightmost node in that left subtree, as shown in steps 4-6 in the execution_table.
What if the target node has no left child? How do we find the predecessor?
We move up the tree from the root, tracking the last node smaller than the target (pred). This is shown in steps 8-13 where no left child exists.
Why do we update 'pred' only when moving right from current node?
Because moving right means current node is less than target, so it could be a predecessor candidate. This is shown in steps 2 and 11.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the predecessor when target is 15 at step 7?
ANode with value 12
BNode with value 10
CNode with value 5
Dnullptr
💡 Hint
Check step 7 in execution_table where predecessor is returned as 12.
At which step does the search realize the target 5 has no inorder predecessor?
AStep 9
BStep 11
CStep 13
DStep 14
💡 Hint
Look at step 13 where it returns nullptr because no ancestor is found.
If the target node had no left child but was greater than root, how would 'pred' change during search?
A'pred' would remain nullptr
B'pred' would update to root or nodes smaller than target
C'pred' would update to nodes greater than target
D'pred' would be set to target node
💡 Hint
Refer to variable_tracker and steps where 'pred' updates when moving right.
Concept Snapshot
BST Inorder Predecessor:
- If target has left child, predecessor = rightmost node in left subtree.
- Else, predecessor = ancestor where target is in right subtree.
- Search from root to find target and track predecessor.
- Return nullptr if no predecessor found.
- Useful for BST deletion and traversal.
Full Transcript
To find the inorder predecessor of a node in a BST, first locate the target node starting from the root. If the target has a left child, the predecessor is the rightmost node in that left subtree. If no left child exists, move up the tree from the root, tracking the last node smaller than the target, which becomes the predecessor. If no such ancestor exists, the predecessor is null. This method helps in BST operations like deletion and inorder traversal.