0
0
DSA C++programming~5 mins

BST Iterator Design in DSA C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the main purpose of a BST Iterator?
A BST Iterator allows us to traverse a Binary Search Tree (BST) in sorted order (in-order traversal) one element at a time, without storing all elements at once.
Click to reveal answer
intermediate
How does a BST Iterator typically store its state internally?
It uses a stack to keep track of nodes. The stack stores the path from the root to the current node, allowing the iterator to move to the next smallest element efficiently.
Click to reveal answer
intermediate
Explain the role of the 'next()' function in a BST Iterator.
The 'next()' function returns the next smallest element in the BST. It pops the top node from the stack, processes it, and then pushes all the left children of the popped node's right child onto the stack.
Click to reveal answer
beginner
Why does the BST Iterator push left children onto the stack during initialization and after visiting a node?
Because in-order traversal visits left children first, pushing left children ensures the smallest elements are on top of the stack, ready to be returned by 'next()'.
Click to reveal answer
intermediate
What is the time complexity of the 'next()' and 'hasNext()' operations in a BST Iterator?
'hasNext()' runs in O(1) time. 'next()' runs in average O(1) amortized time because each node is pushed and popped exactly once.
Click to reveal answer
What data structure is commonly used inside a BST Iterator to track nodes?
AStack
BQueue
CArray
DLinked List
What does the 'next()' method of a BST Iterator return?
AThe last visited node value
BThe next smallest element in the BST
CThe root node value
DThe next largest element in the BST
When initializing a BST Iterator, which nodes are pushed onto the stack first?
AAll left children from the root down
BAll leaf nodes
CAll right children
DOnly the root node
What is the average time complexity of the 'next()' operation in a BST Iterator?
AO(n)
BO(log n)
CO(1) amortized
DO(n log n)
What does the 'hasNext()' method indicate in a BST Iterator?
AIf there is a next larger element
BIf the current node has a right child
CIf the BST is empty
DIf the stack is not empty, meaning more elements remain
Describe how a BST Iterator uses a stack to perform in-order traversal step-by-step.
Think about how in-order traversal visits nodes left-root-right.
You got /4 concepts.
    Explain why the BST Iterator's next() method has O(1) amortized time complexity.
    Consider how many times each node is handled during iteration.
    You got /4 concepts.