Homework: BinaryTree and Recursion I
This homework is necessary preparation for the lab.
-
Write a recursive body for the following static, generic method that computes and returns the size of a given
BinaryTree<T>. You can use any of theBinaryTreemethods except for the iterator and thesizekernel method. Note that theBinaryTreemust be restored, i.e., its outgoing value must be the same as its incoming value./** * Returns the size of the given {@code BinaryTree<T>}. * * @param <T> * the type of the {@code BinaryTree} node labels * @param t * the {@code BinaryTree} whose size to return * @return the size of the given {@code BinaryTree} * @ensures size = |t| */ public static <T> int size(BinaryTree<T> t) {...} -
Provide a second implementation of the
sizemethod above but this time make it an iterative (non-recursive) solution. You cannot use thesizekernel method in your solution.
Additional Questions
-
Write a recursive body for the following static, generic method that computes and returns the height of a given
BinaryTree<T>. You can use any of theBinaryTreemethods except for theheightmethod. Note that theBinaryTreemust be restored, i.e., its outgoing value must be the same as its incoming value./** * Returns the height of the given {@code BinaryTree<T>}. * * @param <T> * the type of the {@code BinaryTree} node labels * @param t * the {@code BinaryTree} whose height to return * @return the height of the given {@code BinaryTree} * @ensures height = ht(t) */ public static <T> int height(BinaryTree<T> t) {...}