Lecture 8 - Problems

  1. Here’s the very simple general tree class we saw in lecture:

    Implement the following method, which is the analogue of the binary tree findVal method you saw in the lecture video.

    Pro tip: the easiest way to loop through a List is using an “enhanced for loop”, also known as a “foreach loop”, such as for (Thing t : listOfThings) { // do stuff with t }

  2. What’s the worst-case runtime of findVal if t has n nodes in it?

  3. What’s the worst-case runtime of a pre-order, in-order, and post-order traversal in which whatever is being done to each node takes constant time?

  4. Suppose that you have two binary trees. You print each tree’s nodes using a pre-order traversal and find that the output is identical for both trees. Are the trees identical? Make an argument for why this is the case if so, or give a counterexample if not.

  5. Suppose a binary tree has height h. What’s the smallest number of nodes it could have? What’s the largest number of nodes it can have?