## Binary Search Tree Iterator

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.

Calling next() will return the next smallest number in the BST.

Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.

## Analysis

So this problem is a little tricky if you do not familar with recursion and stack. I had an answer using recursion until recently I tried to solve this problem again by using a stack. The advantage of using a stack is making the solution pretty neat and clean. However, the disadvantage now becomes of how could we get the idea of such solution.

Firstly, let's review the basic concept of binary search tree. In short, the binary search tree is an binary tree with order, by traverse the whole tree, you will get the nodes in order. The traversal is defined how you search the tree, here in BST, we could just use the pre-order traversal. The figure below illustrates a BST and its traversal procedure.

For this specific problem, note that, the starting node for the iterator may NOT be the root node! Yes, although it is initialized from the root node, actually the starting node should be the node with smallest value (it's possible the smallest node is the root.) So what is the smallest node in the BST, it is the **left-most node**.

Secondly, I draw the firgure to show how the preorder traversal is done in recursion way. Specifically, in each recursion, we have 3 steps, (1) search the left node, (2)print the value, and (3) search the right node. In the figure, I show the current value and all 3 steps in blue color, this is what actually is stored in the function stack. The green arrow is our function call each time, while the organe arrow is the recursion. The red arrow and number is getting the node value.

From the figure we can see that, the whole procedue acts like the following way:

- keep searching the left node.
- if there is no left node OR the left node has already been visited, we do the following steps:
- pop/print the current node
- keep searching the right node

If we tried to simulate the function stack using a regular stack, we could see from the figure that, green arrow acts like the push to stack operation, and red arrow acts like the pop operation. The property of stack itself servers the orange arrow.

Notice that, now the top element of stack is alway the smallest node. Every time we pop the top node, at the same time we push the right child and all its left children. Because for each node, the next node in its preorder traversal will be only two cases:

- The left-most node of its right child (if current node has right child)
- It current node doese have right child. Then search up to its parent, until the parent node has right child. The left-most node of that right child is the next node we want.

The above two steps becomes quite easy if we are using a stack. The first step in stack is just pushing nodes into stack, while the second step could be viewed as the following steps in stack:

- If current node has right child, we keep pushing the left child of that node (include the right child itself).
- If there is no right child, we do nothing. The top node in the stack is what we want. Why? because once we visited the node, we pop it form the stack, so all the previous nodes are no longer existed in the stack.

In this way, we could design the iterator very easily, as the following code:

Your C++ code will give a problem when the question asked on some OJ does include the variables. Rest is really good code.

ReplyDeleteYour iteration takes log (n) foreach step in iteration. Over all it takes n log n to iterate over all elements.

ReplyDeleteInstead if u can sacrifice on using a stack, it's additional O(log n) space (assuming balanced tree) but each call to iterator is constant operation O(1)

This comment has been removed by the author.

ReplyDeleteI like your blog, I read this blog please update more content on python, further check it once at

ReplyDeletepython online training