0
0
DSA Goprogramming~10 mins

BST Inorder Predecessor in DSA Go - Execution Trace

Choose your learning style9 modes available
Concept Flow - BST Inorder Predecessor
Start at root
Search for node with given key
Node found?
NoReturn nil (no predecessor)
Yes
Does node have left child?
YesGo to left child
Go to rightmost node in left subtree
This is inorder predecessor
No
Go up to ancestor where node is in right subtree
That ancestor is inorder predecessor
Return predecessor node
Find the node with the given key, then find its inorder predecessor by either going to the rightmost node in its left subtree or moving up to the nearest ancestor where the node is in the right subtree.
Execution Sample
DSA Go
func inorderPredecessor(root, key *Node) *Node {
  node := search(root, key.val)
  if node == nil {
    return nil
  }
  if node.left != nil {
    return rightmost(node.left)
  }
  return ancestorPredecessor(root, node)
}
This code finds the inorder predecessor of a node in a BST by searching the node, then checking its left subtree or ancestors.
Execution Table
StepOperationNode VisitedPointer ChangesVisual State
1Start at root50head=root(50)50 / \ 30 70 / \ / \ 20 40 60 80
2Search for node with key=4050current=50Same as above
3Move left (40 < 50)30current=30Same as above
4Move right (40 > 30)40current=40Same as above
5Node found40node=40Same as above
6Check if node.left exists40node.left=nilSame as above
7No left child, find ancestor predecessor40start=root(50)Same as above
8Move down from root to find ancestor50predecessor=nilSame as above
940 < 50, move left30predecessor=nilSame as above
1040 > 30, set predecessor=30, move right40predecessor=30Same as above
11Reached node, stop40predecessor=30Same as above
12Return predecessor node30return 30Same as above
💡 Node 40 has no left child; predecessor is ancestor 30 found by moving up.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 10Final
currentroot(50)50304040404040
nodenilnilnilnil40404040
predecessornilnilnilnilnilnil3030
Key Moments - 3 Insights
Why do we check if the node has a left child first?
Because if the node has a left child, its inorder predecessor is the rightmost node in that left subtree (see step 6 and 7). If no left child, we look for ancestor predecessor.
Why do we move up to ancestors when there is no left child?
Because the inorder predecessor is the nearest ancestor where the node lies in the right subtree (see steps 7 to 11). This ancestor is the predecessor.
Why do we stop searching ancestor when we reach the node itself?
Because once we reach the node during ancestor traversal, we have found the predecessor stored in 'predecessor' variable (step 11).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'predecessor' after step 10?
A30
B40
C50
Dnil
💡 Hint
Check the 'predecessor' column in variable_tracker after step 10.
At which step do we confirm that the node has no left child?
AStep 5
BStep 6
CStep 7
DStep 10
💡 Hint
Look at the 'Operation' column in execution_table where left child check happens.
If the node had a left child, which step would we go to next?
AStep 7
BStep 6
CStep 4
DStep 12
💡 Hint
Refer to the decision at step 6 in execution_table about left child presence.
Concept Snapshot
BST Inorder Predecessor:
- Find node with given key.
- If node has left child, predecessor is rightmost node in left subtree.
- Else, predecessor is nearest ancestor where node is in right subtree.
- Return predecessor node or nil if none.
- Used in BST deletion and traversal.
Full Transcript
To find the inorder predecessor in a BST, first search for the node with the given key starting from the root. If the node is found and has a left child, the predecessor is the rightmost node in its left subtree. If there is no left child, move up the tree to find the nearest ancestor where the node lies in the right subtree; that ancestor is the predecessor. If no such ancestor exists, the node has no inorder predecessor. This process is shown step-by-step in the execution table and variable tracker.