Lab: BinaryTree and Recursion
Objective
In this lab you will practice using the BinaryTree component and
recursion by writing the height and isInTree static, generic methods
for BinaryTree<T>.
Setup
To get started, import the project for this lab, BinaryTreeMethods,
from the BinaryTreeMethods.zip file available at this
link. If you don't remember how to do this, see
the Setup
instructions
in an earlier lab.
Method
-
Take a look at the provided code skeleton of
BinaryTreeMethods.javain thesrcfolder and make sure you understand it. You will notice that the main program makes use of a custom utility class,BinaryTreeUtility, that provides two useful static methods,treeFromStringandtreeToString, used to construct aBinaryTree<String>from aStringrepresentation and to convert aBinaryTree<T>to a specificStringrepresentation, respectively. Here is the documentation:/** * Constructs and returns the {@code BinaryTree<String>} corresponding to * the given {@code String}. * * @param str * the prefix representation of a {@code BinaryTree<String>} * @return the {@code BinaryTree<String>} corresponding to {@code str} * @requires <pre> * [str is the prefix representation of a BinaryTree<String> * where the String representations of the labels of the tree do not * contain the characters '(' and ')'] * </pre> * @ensures treeFromString = [the BinaryTree<String> corresponding to str] */ public static BinaryTree<String> treeFromString(String str) {...}/** * Returns the {@code String} prefix representation of the given * {@code BinaryTree<T>}. * * @param <T> * the type of the {@code BinaryTree} node labels * @param t * the {@code BinaryTree} to convert to a {@code String} * @return the prefix representation of {@code t} * @requires <pre> * [the String representations of the labels of t do not contain * the characters '(' and ')'] * </pre> * @ensures treeToString = [the String prefix representation of t] */ public static <T> String treeToString(BinaryTree<T> t) {...}The prefix representation of the empty tree is
"()", and the prefix representation of a non-emptyBinaryTree<T>is the string concatenation of the root, followed by'(', then by the prefix representation of the left subtree, the prefix representation of the right subtree, and finally')'. We assume that the labels in the tree cannot contain the characters '(' and ')'. Here are five examples of prefix representations of some binary trees. See if you can figure out and draw the binary trees being described.()a(()())a(b(()())c(()()))a(()b(()()))a(()b(c(()())()))
-
Complete the body of the
heightstatic, generic method without using theBinaryTreeheightmethod, of course. -
Run the main program to interactively test your implementation of
height. -
When you think it is working, run the
BinaryTreeMethodsTest.javaJUnit test fixture in thetestfolder. In addition to the test cases forheight, this fixture contains test cases for theisInTreemethod, some of which will fail given thatisInTreehas not been implemented yet. Fix any problems with your implementation ofheightuntil it passes all its test cases. -
Complete the body of the
isInTreestatic, generic method and try it out by running the main program. -
Then run the JUnit test fixture and fix any problems with your implementation of
isInTreeuntil it passes all its test cases. -
Once your first implementation of
isInTreepasses all test cases, provide an alternative implementation that is recursive if your first implementation was not recursive, or make your second implementation not recursive if the first one was recursive. You can just comment out the code of your first implementation (select the code to comment out and press CTRL-/, i.e., the Control key and the '/' key at the same time). -
Run the JUnit test fixture and test your second implementation of
isInTree.
Additional Activities
- Copy and paste your implementation(s) of the
sizemethod from your homework intoBinaryTreeMethods.java. Update the main program so that it callssizeand outputs the result. Run the main program to test each implementation ofsize(recursive and iterative). - Add appropriate test cases to
BinaryTreeMethodsTest.javato test your implementation(s) ofsize. Run the JUnit test fixture on each implementation ofsize(recursive and iterative).