Homework: Tree and Recursion
-
Write a recursive body for the following static, generic method that computes and returns the size of a given
Tree<T>. You can use any of theTreemethods except for the iterator and thesizekernel method. Note that theTreemust be restored, i.e., its outgoing value must be the same as its incoming value./** * Returns the size of the given {@code Tree<T>}. * * @param <T> * the type of the {@code Tree} node labels * @param t * the {@code Tree} whose size to return * @return the size of the given {@code Tree} * @ensures size = |t| */ public static <T> int size(Tree<T> t) {...} -
Provide a second implementation of the
sizemethod above but this time make it an iterative (non-recursive) solution. You still cannot use thesizekernel method in your solution. -
Write a recursive body for the following static, generic method that computes and returns the height of a given
Tree<T>. You can use any of theTreemethods except for theheightkernel method (in particular, you can use thesizemethod). Note that theTreemust be restored, i.e., its outgoing value must be the same as its incoming value./** * Returns the height of the given {@code Tree<T>}. * * @param <T> * the type of the {@code Tree} node labels * @param t * the {@code Tree} whose height to return * @return the height of the given {@code Tree} * @ensures height = ht(t) */ public static <T> int height(Tree<T> t) {...} -
Write a recursive body for the following static method that computes and returns the largest integer in a given non-empty
Tree<Integer>. Note that theTreemust be restored, i.e., its outgoing value must be the same as its incoming value./** * Returns the largest integer in the given {@code Tree<Integer>}. * * @param t * the {@code Tree<Integer>} whose largest integer to return * @return the largest integer in the given {@code Tree<Integer>} * @requires |t| > 0 * @ensures <pre> * max is in labels(t) and * for all i: integer where (i is in labels(t)) (i <= max) * </pre> */ public static int max(Tree<Integer> t) {...}
Additional Questions
-
Write a recursive body for the following static, generic method that returns a
Stringrepresentation of a givenTree<T>. You cannot use theTreetoString method in your solution. Note that theTreemust be restored, i.e., its outgoing value must be the same as its incoming value./** * Returns the {@code String} prefix representation of the given * {@code Tree<T>}. * * @param <T> * the type of the {@code Tree} node labels * @param t * the {@code Tree} to convert to a {@code String} * @return the prefix representation of {@code t} * @ensures treeToString = [the String prefix representation of t] */ public static <T> String treeToString(Tree<T> t) {...}The prefix representation of the empty tree is
"()", and the prefix representation of a non-emptyTree<T>is the string concatenation of the root, followed by'(', then by the prefix representation of subtrees in the order in which they appear in the tree, and finally')'. Here are five examples of prefix representations of someTree<Character>s. See if you can figure out and draw the trees being described.()a(()()())a(b()c(d(e()))f(()))a(()b()()c())a(()b(c(()())()d()))
-
Write a recursive body for the following static method that copies and returns a given
Tree<Integer>. Note that the givenTreemust be restored, i.e., its outgoing value must be the same as its incoming value./** * Returns a copy of the given {@code Tree<Integer>}. * * @param t * the {@code Tree<Integer>} to copy * @return a copy of the given {@code Tree<Integer>} * @ensures copy = t */ public static Tree<Integer> copy(Tree<Integer> t) {...}