Bird
Raised Fist0
DSA Goprogramming~10 mins

BST Inorder Predecessor in DSA Go - Execution Trace

Choose your learning style10 modes available
Concept Flow - BST Inorder Predecessor
Start at root
↓
Search for node with given key
↓
Node found?
No→Return nil (no predecessor)
Yes↓
Does node have left child?
Yes→Go 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.

Practice

(1/5)
1. What is the inorder predecessor of a node in a Binary Search Tree (BST)?
easy
A. The node with the smallest value larger than the given node
B. The node with the largest value smaller than the given node
C. The left child of the given node
D. The right child of the given node

Solution

  1. Step 1: Understand inorder traversal in BST

    Inorder traversal visits nodes in ascending order of their values.
  2. Step 2: Define inorder predecessor

    The inorder predecessor of a node is the node that comes just before it in this ascending order, meaning it has the largest value smaller than the node.
  3. Final Answer:

    The node with the largest value smaller than the given node -> Option B
  4. Quick Check:

    Inorder predecessor = largest smaller value [OK]
Hint: Predecessor is largest smaller value in BST [OK]
Common Mistakes:
  • Confusing predecessor with successor
  • Thinking predecessor is always left child
  • Assuming predecessor is the parent node
2. Which of the following Go function signatures correctly represents a function to find the inorder predecessor in a BST node struct named Node?
easy
A. func inorderPredecessor(root *Node, key int) *Node
B. func inorderPredecessor(key int) Node
C. func inorderPredecessor(root Node, key int) Node
D. func inorderPredecessor(root *Node) int

Solution

  1. Step 1: Analyze function parameters and return type

    The function needs the BST root pointer and the key to find predecessor for, and returns a pointer to the predecessor node.
  2. Step 2: Match with options

    func inorderPredecessor(root *Node, key int) *Node matches this: it takes *Node root, int key, and returns *Node.
  3. Final Answer:

    func inorderPredecessor(root *Node, key int) *Node -> Option A
  4. Quick Check:

    Pointer root and key input, pointer node output [OK]
Hint: Use pointer receiver and return pointer for BST nodes [OK]
Common Mistakes:
  • Passing root by value instead of pointer
  • Returning node value instead of node pointer
  • Missing key parameter to identify target node
3. Given the following BST and code snippet, what will be printed?
type Node struct {
    Val int
    Left, Right *Node
}

func inorderPredecessor(root *Node, key int) *Node {
    var predecessor *Node
    curr := root
    for curr != nil {
        if key <= curr.Val {
            curr = curr.Left
        } else {
            predecessor = curr
            curr = curr.Right
        }
    }
    return predecessor
}

func main() {
    root := &Node{Val: 20}
    root.Left = &Node{Val: 10}
    root.Right = &Node{Val: 30}
    root.Left.Left = &Node{Val: 5}
    root.Left.Right = &Node{Val: 15}

    pred := inorderPredecessor(root, 15)
    if pred != nil {
        fmt.Println(pred.Val)
    } else {
        fmt.Println("No predecessor")
    }
}
medium
A. No predecessor
B. 5
C. 15
D. 10

Solution

  1. Step 1: Trace the inorderPredecessor function for key=15

    Start at root (20). Since 15 <= 20, move left to 10. Since 15 > 10, set predecessor=10, move right to 15. Since 15 <= 15, move left (nil). Loop ends.
  2. Step 2: Return predecessor node value

    Predecessor is node with value 10, so print 10.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    Largest smaller than 15 is 10 [OK]
Hint: Move left if key ≤ node, else update predecessor and move right [OK]
Common Mistakes:
  • Returning the key itself as predecessor
  • Confusing predecessor with successor
  • Not updating predecessor correctly when moving right
4. The following Go function is intended to find the inorder predecessor of a node with value key in a BST. What is the bug?
func inorderPredecessor(root *Node, key int) *Node {
    var predecessor *Node
    curr := root
    for curr != nil {
        if key < curr.Val {
            curr = curr.Left
        } else {
            predecessor = curr
            curr = curr.Right
        }
    }
    return predecessor
}
medium
A. The function should return curr instead of predecessor
B. The predecessor variable should be reset inside the loop
C. The condition should be key <= curr.Val to handle equal keys correctly
D. The loop should use recursion instead of iteration

Solution

  1. Step 1: Analyze the condition in the loop

    The current condition is key < curr.Val, which skips updating predecessor when key equals curr.Val.
  2. Step 2: Correct the condition to include equality

    Changing to key <= curr.Val ensures that when key equals curr.Val, we move left without updating predecessor, which is correct for inorder predecessor logic.
  3. Final Answer:

    The condition should be key <= curr.Val to handle equal keys correctly -> Option C
  4. Quick Check:

    Use <= to handle equal keys properly [OK]
Hint: Use <= in condition to handle equal keys correctly [OK]
Common Mistakes:
  • Using < instead of <= causes wrong predecessor for equal keys
  • Resetting predecessor inside loop loses previous valid value
  • Returning curr instead of predecessor returns wrong node
5. You have a BST where each node has a pointer to its parent. How would you find the inorder predecessor of a given node n efficiently without starting from the root? Choose the best approach.
hard
A. If n has a left child, find the rightmost node in its left subtree; else, move up using parent pointers until you find a node which is the right child of its parent
B. Always start from the root and do a full inorder traversal to find predecessor
C. If n has a right child, find the leftmost node in its right subtree; else, move up using parent pointers until you find a node which is the left child of its parent
D. Return the parent node directly as the predecessor

Solution

  1. Step 1: Check if node has left subtree

    If n has a left child, the predecessor is the rightmost node in that left subtree.
  2. Step 2: If no left child, move up using parent pointers

    Move up until you find a node that is the right child of its parent; that parent is the predecessor.
  3. Final Answer:

    If n has left child, find rightmost in left subtree; else move up until node is right child -> Option A
  4. Quick Check:

    Use left subtree or climb parents to find predecessor [OK]
Hint: Use left subtree or climb parents via right child link [OK]
Common Mistakes:
  • Starting from root wastes time
  • Confusing predecessor with successor logic
  • Returning parent directly without checks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard