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 }
What’s the worst-case runtime of findVal
if t
has nodes in it?
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?
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.
Suppose a binary tree has height . What’s the smallest number of nodes it could have? What’s the largest number of nodes it can have?