What if you could flip through a huge sorted list one item at a time, without losing your place or wasting time?
Why BST Iterator Design in DSA Javascript?
Imagine you have a huge family tree drawn on paper, and you want to look at each member in order from youngest to oldest. You try to scan the whole paper every time you want the next person, but it takes forever and you lose your place easily.
Manually scanning the entire tree each time is slow and confusing. You might miss someone or look at the same person twice. It's hard to remember where you left off without a clear system.
A BST Iterator acts like a bookmark and a guide. It remembers where you are and quickly gives you the next member in order without scanning everything again. It makes moving through the tree smooth and easy.
function getNextInOrder(root) {
let nodes = [];
function inorder(node) {
if (!node) return;
inorder(node.left);
nodes.push(node.val);
inorder(node.right);
}
inorder(root);
return nodes.shift();
}class BSTIterator { constructor(root) { this.stack = []; this._pushLeft(root); } _pushLeft(node) { while (node) { this.stack.push(node); node = node.left; } } next() { let node = this.stack.pop(); this._pushLeft(node.right); return node.val; } hasNext() { return this.stack.length > 0; } }
This design lets you explore a binary search tree step-by-step in order, using little memory and no repeated work.
Think of browsing a sorted list of contacts on your phone. Instead of loading all contacts at once, the phone shows one contact at a time as you scroll, remembering where you left off.
Manual scanning of trees is slow and error-prone.
BST Iterator remembers position and efficiently returns next item.
It enables smooth, ordered traversal with minimal memory.
Practice
BST Iterator in a binary search tree?Solution
Step 1: Understand BST Iterator functionality
A BST Iterator is designed to traverse the tree in sorted order, typically ascending, without using recursion.Step 2: Identify the main purpose
It provides a way to get the next smallest element efficiently, which matches To traverse the BST in ascending order without recursion.Final Answer:
To traverse the BST in ascending order without recursion -> Option AQuick Check:
BST Iterator = Ascending traversal without recursion [OK]
- Confusing iterator with insertion or deletion
- Thinking it balances the tree automatically
- Assuming it uses recursion internally
Solution
Step 1: Identify correct class syntax and initialization
The BST Iterator is usually implemented as a class with a stack to store nodes and a helper method to push left children.Step 2: Check each option
class BSTIterator { constructor(root) { this.stack = []; this.pushLeft(root); } } correctly uses a class, initializes a stack, and calls pushLeft on root. Others use queue or pushRight which are incorrect for BST Iterator.Final Answer:
class BSTIterator { constructor(root) { this.stack = []; this.pushLeft(root); } } -> Option BQuick Check:
Use stack and pushLeft in constructor [OK]
- Using queue instead of stack
- Calling pushRight instead of pushLeft
- Using function instead of class syntax
const iterator = new BSTIterator(root); console.log(iterator.next()); console.log(iterator.next()); console.log(iterator.hasNext());
Solution
Step 1: Trace the first next() call
First next() returns the smallest element, which is 3.Step 2: Trace the second next() call
Second next() returns the next smallest, which is 7.Step 3: Check hasNext()
After two next() calls, there are still nodes (9, 15, 20), so hasNext() returns true.Final Answer:
[3, 7, true] -> Option CQuick Check:
next() returns ascending nodes, hasNext() true if more nodes [OK]
- Assuming next() returns root first
- Confusing hasNext() return value
- Ignoring left subtree nodes
next() {
const node = this.stack.pop();
if (node.right) {
this.pushRight(node.right);
}
return node.val;
}
pushRight(node) {
while (node) {
this.stack.push(node);
node = node.right;
}
}Solution
Step 1: Analyze pushRight method
pushRight pushes right children, but BST Iterator needs to push left children to get next smallest.Step 2: Understand correct pushLeft usage
Correct method pushes left children of right subtree to stack to maintain ascending order.Final Answer:
pushRight should push left children, not right -> Option DQuick Check:
Push left children to stack for correct order [OK]
- Pushing right children instead of left
- Using queue instead of stack
- Returning wrong node property
next() and hasNext() in O(1) average time and uses O(h) space, where h is tree height. Which approach best achieves this?Solution
Step 1: Understand time and space constraints
O(1) average time per next() and O(h) space means no full traversal storage and no recursion per call.Step 2: Evaluate each approach
Use a stack to store left children and push left nodes of right subtree on next() uses a stack to store left children and pushes left nodes of right subtree on next(), achieving O(1) average time and O(h) space. Store all nodes in an array by inorder traversal at initialization uses O(n) space. Use recursion to traverse nodes on each next() call uses recursion, not O(1) time. Use a queue to store nodes level by level uses queue level order, not inorder.Final Answer:
Use a stack to store left children and push left nodes of right subtree on next() -> Option AQuick Check:
Stack + push left children = O(1) avg time, O(h) space [OK]
- Storing all nodes wastes space
- Using recursion causes slow next()
- Using queue gives wrong traversal order
Ready to prove it?
Pass the test and this pattern moves unknown
