Click to reopen instructions.

Recursion over trees. A tree is a recursive data structure made up of other trees. This assignment uses two different kinds of trees, but in both each tree object holds an int value and also has child trees. The starter code imports both tree classes for you. The objects are immutable as is common when writing recursive methods.

A BinaryTree has an int value and at most two children. value() returns the value, and left() and right() return the children, each of which is either another BinaryTree or null. A null represents an empty tree with no value and no children. Thus a BinaryTree whose whose children are both null is a leaf. Note that a method that takes a BinaryTree argument should handle null as the representation of an empty tree. You can construct a BinaryTree with new BinaryTree(value, left, right) or new BinaryTree(value) for a leaf. (You can also make a leaf with new BinaryTree(value, null, null).)

A Tree has an int value and any number of children. value() returns the value and children() returns a List<Tree> of the children, in order. children() always returns a non-null value, which will be an empty list for a leaf. A Tree argument should never be null: there is always at least one node with one value.

In the test results a tree is written in a compact form: a leaf is just its value, and any other node is its value followed by its children in parentheses, with . for a missing child in a binary tree. So (5 3 (8 . 9)) is the binary tree whose root is 5, with a leaf 3 on the left and on the right a node 8 with no left child and a leaf 9 on the right. And (5 3 (8 9 2) 1) is the general tree whose root 5 has three children: the leaf 3, a node 8 with two leaf children, and the leaf 1.

The BinaryTree methods have exactly two children to recurse on, so they need no loops at all, and none are allowed. For the methods on Tree you may use a loop to iterate over a node’s children but the method must still be recursive. Or if you really want you can recurse over the list too, with a helper.

Put definitions here.

Revisions:

Test results show up here.

Errors