binary tree iterators · returns the premous element in the list previouslndex ( ) returns the...
TRANSCRIPT
![Page 1: Binary Tree Iterators · Returns the premous element in the list previouslndex ( ) Returns the index of the element that would be returned by a subsequent call to hasNext Returns](https://reader034.vdocuments.site/reader034/viewer/2022050208/5f5b5c9e83d9101fca657671/html5/thumbnails/1.jpg)
Binary Tree Iterators
After today, you should be able to…… implement a simple iterator for trees… implement _lazy_ iterators for trees
0
![Page 2: Binary Tree Iterators · Returns the premous element in the list previouslndex ( ) Returns the index of the element that would be returned by a subsequent call to hasNext Returns](https://reader034.vdocuments.site/reader034/viewer/2022050208/5f5b5c9e83d9101fca657671/html5/thumbnails/2.jpg)
} Exams not graded yet
} Partner Evaluation done?◦ We had a mishap with the survey◦ Moodle knows who hasn’t completed it…
} Doublets progress?◦ Time for Q/A now.
![Page 3: Binary Tree Iterators · Returns the premous element in the list previouslndex ( ) Returns the index of the element that would be returned by a subsequent call to hasNext Returns](https://reader034.vdocuments.site/reader034/viewer/2022050208/5f5b5c9e83d9101fca657671/html5/thumbnails/3.jpg)
What if we want to iterate over the elements in the nodes of the tree one-at-a-time instead of just printing all of them?
![Page 4: Binary Tree Iterators · Returns the premous element in the list previouslndex ( ) Returns the index of the element that would be returned by a subsequent call to hasNext Returns](https://reader034.vdocuments.site/reader034/viewer/2022050208/5f5b5c9e83d9101fca657671/html5/thumbnails/4.jpg)
} In Java, specified by java.util.Iterator<E>
![Page 5: Binary Tree Iterators · Returns the premous element in the list previouslndex ( ) Returns the index of the element that would be returned by a subsequent call to hasNext Returns](https://reader034.vdocuments.site/reader034/viewer/2022050208/5f5b5c9e83d9101fca657671/html5/thumbnails/5.jpg)
} Pros: easy to write.
} So let’s recall or write toArrayList() now and use it.
} Cons? We’ll see shortly!
2
![Page 6: Binary Tree Iterators · Returns the premous element in the list previouslndex ( ) Returns the index of the element that would be returned by a subsequent call to hasNext Returns](https://reader034.vdocuments.site/reader034/viewer/2022050208/5f5b5c9e83d9101fca657671/html5/thumbnails/6.jpg)
} In Java, specified by java.util.Iterator<E>
} ListIterator<E> adds:
![Page 7: Binary Tree Iterators · Returns the premous element in the list previouslndex ( ) Returns the index of the element that would be returned by a subsequent call to hasNext Returns](https://reader034.vdocuments.site/reader034/viewer/2022050208/5f5b5c9e83d9101fca657671/html5/thumbnails/7.jpg)
ag can be any Collection of Integers, since all Collections implement Iterable
The Java compiler translates the above into this:
![Page 8: Binary Tree Iterators · Returns the premous element in the list previouslndex ( ) Returns the index of the element that would be returned by a subsequent call to hasNext Returns](https://reader034.vdocuments.site/reader034/viewer/2022050208/5f5b5c9e83d9101fca657671/html5/thumbnails/8.jpg)
} Consider a tree with 1 million elements.} What is the runtime of iterating over only the
first 100 elements?
} (example on board)
} To improve efficiency, the iterator should only get as few elements as possible◦ The one time where being lazy has a reward!
2
![Page 9: Binary Tree Iterators · Returns the premous element in the list previouslndex ( ) Returns the index of the element that would be returned by a subsequent call to hasNext Returns](https://reader034.vdocuments.site/reader034/viewer/2022050208/5f5b5c9e83d9101fca657671/html5/thumbnails/9.jpg)
} What are they?
} How would you make a lazy pre-orderiterator? (brainstorm an algorithm now)
} What do you need to add to create the other recursive iterators?
} What about the last iterator?◦ A quick change. Magic? Not really…
3-6
![Page 10: Binary Tree Iterators · Returns the premous element in the list previouslndex ( ) Returns the index of the element that would be returned by a subsequent call to hasNext Returns](https://reader034.vdocuments.site/reader034/viewer/2022050208/5f5b5c9e83d9101fca657671/html5/thumbnails/10.jpg)
A good goal would be to complete Milestone 1 of
BinarySearchTrees by next class