Recitation 13

Background

When traversing trees, there are two techniques for traversing trees. We saw one technique with depth-first traversals (pre-order, in-order, and post-order). Another technique is breadth-first traversal. Here, you visit all of the nodes at one level before going to nodes at the next level. For example, in the tree below, the breadth-first traversal would lead to this order: 50, 30, 80, 10, 40, 90, 5, 20, 45, 85, 95.

Breadth-first traversals are useful for a number of situations, many of which will be covered in CS/COE 1501. It is also a foundational traversal technique that other traversals are based on (many are covered in an Artificial Intelligence class or a Networking class).

Implementation

Breadth-first traversals need to use a queue to keep track of which nodes have been seen, but not visited yet. For example, in the tree above, when we are at node 50, we can see its two children (30 and 80) and so add them to the queue. To go to the next node, you dequeue the next node from the queue and visit it (adding its children to the queue). This repeats until all of the nodes have been visited (i.e. dequeued from the queue).

Design and implement a breadth-first iterator for binary trees. You should only need to modify the BinaryTree class; the BinaryNode class should not need to be modified. Use the classes and package provided below.

Submission and Grading

This is for your practice. You should be able to complete the iterator by the end of recitation. If you complete a function, call the TA over to look over your iterator to make sure it is correct. If you do not have time to complete the programs before the end of recitation, you have the option of uploading it to CourseWeb once you are finished to receive feedback on it from the TA. If you upload it after Monday, December 14, please email the TA so he knows to look at it.