0
0
DSA Typescriptprogramming~10 mins

BST Iterator Design in DSA Typescript - 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
Repeat check for next element
The iterator uses a stack to store nodes. It pushes all left children first, then pops nodes to return values, pushing left children of right subtrees as it goes.
Execution Sample
DSA Typescript
class BSTIterator {
  stack: TreeNode[] = [];
  constructor(root: TreeNode) {
    this.pushLeft(root);
  }
  pushLeft(node: TreeNode | null) {
    while (node) {
      this.stack.push(node);
      node = node.left;
    }
  }
  next(): number {
    const node = this.stack.pop()!;
    this.pushLeft(node.right);
    return node.val;
  }
  hasNext(): boolean {
    return this.stack.length > 0;
  }
}
This code creates an iterator over a BST that returns nodes in ascending order using a stack to track left nodes.
Execution Table
StepOperationStack Nodes (top to bottom)Pointer ChangesVisual State
1Initialize iterator with root=7[1, 3, 7]Push 7, then 3, then 1Stack: 1(top) -> 3 -> 7 -> null
2Call hasNext()Stack unchangedNo pointer changeStack: 1(top) -> 3 -> 7 -> null
3Call next()Pop 1Pop 1; push left nodes of 1.right (null)Returned 1; Stack: 3(top) -> 7 -> null
4Call hasNext()Stack unchangedNo pointer changeStack: 3(top) -> 7 -> null
5Call next()Pop 3Pop 3; push left nodes of 3.right (5)Returned 3; Stack: 5(top) -> 7 -> null
6Call hasNext()Stack unchangedNo pointer changeStack: 5(top) -> 7 -> null
7Call next()Pop 5Pop 5; push left nodes of 5.right (null)Returned 5; Stack: 7(top) -> null
8Call hasNext()Stack unchangedNo pointer changeStack: 7(top) -> null
9Call next()Pop 7Pop 7; push left nodes of 7.right (9)Returned 7; Stack: 9(top) -> null
10Call hasNext()Stack unchangedNo pointer changeStack: 9(top) -> null
11Call next()Pop 9Pop 9; push left nodes of 9.right (null)Returned 9; Stack: empty
12Call hasNext()Stack emptyNo pointer changeStack: empty
13Call next()No nodes to popNo pointer changeNo next element, iteration ends
💡 Stack becomes empty after all nodes are visited; hasNext() returns false and next() cannot proceed.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 7After Step 9After Step 11Final
stack[][7,3,1][7,3][7,5][7][9][][]
current node poppednullnull13579null
returned valuenullnull13579null
Key Moments - 3 Insights
Why do we push all left children onto the stack at initialization and after popping a node?
Because the smallest values are always in the left subtree, pushing all left children ensures the next smallest node is on top of the stack, as shown in steps 1 and 9 in the execution_table.
What happens if the popped node has no right child?
No new nodes are pushed onto the stack after popping, so the stack size decreases, as seen in steps 3 and 7 where no nodes are pushed after popping nodes 1 and 5.
Why does hasNext() check if the stack is empty?
Because the stack holds all nodes yet to be visited; if empty, no next node exists. This is clear in step 12 where the stack is empty and hasNext() returns false.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the top node on the stack after the next() call?
ANode with value 5
BNode with value 3
CNode with value 7
DStack is empty
💡 Hint
Refer to the 'Stack Nodes' column at step 5 in the execution_table.
At which step does the stack become empty, indicating no more elements to iterate?
AStep 11
BStep 12
CStep 13
DStep 10
💡 Hint
Check the 'Stack Nodes' column and 'exit_note' in the execution_table.
If the BST had an additional left child under node 1, how would the stack change after initialization (step 1)?
AStack would include the new left child on top
BStack would remain the same
CStack would only have the root node
DStack would be empty
💡 Hint
Refer to the 'pushLeft' operation in the code and how left children are pushed onto the stack.
Concept Snapshot
BST Iterator uses a stack to traverse nodes in ascending order.
Initialize by pushing all left children from root.
next() pops top node, pushes left children of its right subtree.
hasNext() checks if stack is not empty.
This simulates in-order traversal without recursion.
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 starting from the root onto the stack. Each call to next() pops the top node from the stack, returns its value, and then pushes all left children of the popped node's right child. The hasNext() method returns true if the stack is not empty, indicating more nodes to visit. This approach allows iteration over the BST in ascending order without recursion, maintaining the current traversal state in the stack.