Assignment #3

DUE : Thursday 10/14.


The first part of this assignment is inspired by item 4 on page 152 of the text, regarding support for iterators for binary search trees. The suggestion is to maintain two extra links in each node, one to the inorder predecessor and one to the inorder successor in the tree.
  1. Implement this idea, and write code to demonstrate that your iterator works. The iterator is easy to write in light of the last assignment (one can make it a ListIterator, since the links go in both directions, but it's fine to restrict yourself to hasNext(), hasPrevious(), next() and previous()). The assertion "it is easy to maintain these links" is true, but only easy once you have the basic idea. I'll give you some hints in class.
  2. Use the iterator to write a method to merge two binary search trees into one sorted linked list. The method must have run-time O(n) where n is the combined size of the two trees.


  3. There's a purely written part to this assignment, too:

  4. Explain why the method you wrote in part (2) has run-time O(n). Could you have easily done this without the iterator you implemented? Explain.
  5. Consider the problem of merging two binary search trees into a single binary search tree. In pseudocode, write down at least two distinct algorithms for doing this. Discuss any issues that may arise (e.g., modifications in data structures). Analyze both the run-time of your algorithms and give the worst-case tree depths that may result, assuming both of the given trees are approximately balanced.
Back to CS160 Home Page