0
0
DSA Goprogramming~10 mins

BST Iterator Design in DSA Go - Execution Trace

Choose your learning style9 modes available
Concept Flow - BST Iterator Design
Initialize stack empty
Push all left nodes from root
Check if stack empty?
YesNo next element
No
Pop top node from stack
Return popped node's value
Push all left nodes from popped node's right child
Back to Check if stack empty?
The iterator uses a stack to store nodes. It pushes all left children from the root initially. On next(), it pops the top node, returns its value, then pushes all left children of the popped node's right child.
Execution Sample
DSA Go
func (it *BSTIterator) Next() int {
    node := it.stack[len(it.stack)-1]
    it.stack = it.stack[:len(it.stack)-1]
    val := node.Val
    it.pushLeft(node.Right)
    return val
}
This code returns the next smallest value in the BST by popping the top node from the stack and pushing all left children of its right subtree.
Execution Table
StepOperationNodes in Stack (Top to Bottom)Pointer ChangesVisual State
1Initialize iterator with root=7[7]Push 7Stack: [7]
2Push all left nodes from 7[3, 7]Push 3Stack: [3, 7]
3Push all left nodes from 3[1, 3, 7]Push 1Stack: [1, 3, 7]
4Call Next()Pop 1Pop 1Stack: [3, 7] → Output: 1
5Push left nodes from 1's right (nil)[3, 7]No pushStack: [3, 7]
6Call Next()Pop 3Pop 3Stack: [7] → Output: 3
7Push left nodes from 3's right (5)[5, 7]Push 5Stack: [5, 7]
8Push left nodes from 5[5, 7]No left childStack: [5, 7]
9Call Next()Pop 5Pop 5Stack: [7] → Output: 5
10Push left nodes from 5's right (nil)[7]No pushStack: [7]
11Call Next()Pop 7Pop 7Stack: [] → Output: 7
12Push left nodes from 7's right (15)[15]Push 15Stack: [15]
13Push left nodes from 15[13, 15]Push 13Stack: [13, 15]
14Call Next()Pop 13Pop 13Stack: [15] → Output: 13
15Push left nodes from 13's right (nil)[15]No pushStack: [15]
16Call Next()Pop 15Pop 15Stack: [] → Output: 15
17Check if stack empty[]No nodesStack empty → No next element
💡 Stack is empty, no more elements to iterate
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 9After Step 11After Step 13Final
stack[][1, 3, 7][5, 7][7][][13, 15][]
current_node7357151315
output_values[][1][1,3][1,3,5][1,3,5,7][1,3,5,7,13][1,3,5,7,13,15]
Key Moments - 3 Insights
Why do we push all left nodes from the root initially?
Because the smallest element in a BST is the leftmost node. Pushing all left nodes ensures the top of the stack is always the next smallest element (see execution_table rows 1-3).
Why do we push left nodes from the popped node's right child after Next()?
Because after visiting a node, the next smallest elements are in its right subtree. We push all left children of that right subtree to maintain the correct order (see execution_table rows 6-7 and 12-13).
What happens when the stack becomes empty?
It means we have visited all nodes in ascending order. The iterator has no more elements to return (see execution_table row 17).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4. What is the top node in the stack after popping?
A3
B7
C1
D5
💡 Hint
Check the 'Nodes in Stack' column at Step 4 after popping 1
At which step does the iterator push the node with value 13 onto the stack?
AStep 7
BStep 13
CStep 12
DStep 9
💡 Hint
Look at the 'Operation' and 'Pointer Changes' columns around Steps 12 and 13
If the BST had no right children, how would the stack change after calling Next()?
AIt would push left nodes from the right child
BIt would remain unchanged after popping
CIt would become empty after popping
DIt would push the root node again
💡 Hint
Refer to the 'Push left nodes from popped node's right child' action in execution_table rows 5 and 10
Concept Snapshot
BST Iterator uses a stack to traverse nodes in ascending order.
Initialize by pushing all left nodes from root.
Next() pops top node, returns its value.
Then pushes all left nodes from popped node's right child.
HasNext() checks if stack is empty.
This simulates in-order traversal lazily.
Full Transcript
The BST Iterator design uses a stack to simulate in-order traversal of a binary search tree. Initially, it pushes all left children from the root onto the stack, so the smallest element is on top. Each call to Next() pops the top node, returns its value, and then pushes all left children of the popped node's right subtree. This ensures the next smallest element is always on top of the stack. When the stack is empty, there are no more elements to iterate. This approach efficiently returns elements in ascending order without traversing the entire tree at once.