In-order Traversal: Iterative Implementation
Exercise Open the starter code and complete the implementation of BstOrderedSet.iterator
.
Hint 1: You cannot do this recursively (in Java). The iterator needs to "pause," so to speak, and wait for a call to next
to generate (retrieve) the next element.
Hint 2: You will need an auxiliary data structure in the iterator class to keep track of some of the nodes as you traverse the tree so that you can visit those nodes later. This auxiliary data structure will make a less efficient iterator (space complexity greater than $O(1)$).
Solution
Please refer to the posted solution code.
Resources
You may find this article on Medium useful: Binary Search Tree Iterator.