Here’s the very simple general tree class we saw in lecture:
/** A general tree */
public class Tree {
int value;
List<Tree> children;
}
Implement the following method, which is the analogue of the binary tree findVal
method you saw in the lecture video.
/** Return true if tree T contains value v. */
boolean findVal(Tree t, int v) {
// your code here
}
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 \(n\) 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 \(h\). What’s the smallest number of nodes it could have? What’s the largest number of nodes it can have?