Due by 10am, Monday 09 October 2017
In this prelab, you will familiarize yourself with some of the design and implementation issues in the upcoming lab 5. Please write or type up your solutions, and hand in a paper copy before class on Monday. Remember, late prelabs receive zero credit.
As you may recall from class, a binary tree is either:
Therefore, you will have three classes:
private T data; private BinaryTree<T> left; private BinaryTree<T> right;
For each of the binary tree methods, you will do the following:
For example, for the height method, you would:
public abstract int height( );
public int height( ) { return -1; }
public int height( ) { return 1 + Math.max(left.height(),right.height()); }
public int nodeCount( )that returns the number of nodes / vertices in the tree (including the root). Write the method body for both the EmptyTree and ConsTree classes.
public int leafCount( )that returns the number of leaves in the tree. Write the method body for this method, presumably using calls to your isLeaf method. Write it for both classes.
public BinaryTree<T> mirrorImage()that returns a new tree that looks like the mirror image of the current tree (that is, a tree "flipped" around a vertical line through the root node). Don't forget to also flip the sub-trees. Write the method body for both classes.
Probably the trickiest method you will write is a method that reads in a binary tree from a file, and returns a new BinaryTree according to the file's contents. It should have the following method signature:
public BinaryTree<String> loadFromFile(String filename)
The files in question will list the nodes of the tree in postorder sequence, one node per line. Each line contains a string, which is the data value stored in the node, and two tag bits. The first tag bit indicates whether or not this node has a left child or not. (1=oui,0=non). The second tag bit indicates whether the node has a right child or not.
For example, the tree to the right is represented as
32 0 0 74 0 1 29 0 0 63 1 0 18 0 0 83 1 1 34 1 1
The information in the file is sufficient to uniquely determine a binary tree. The tree can be constructed from the file using the following pseudocode:
create an empty stack of binary trees (i.e. Stack<BinaryTree<String>>
)
while there is another line of input in the file {
read a line (containing data and two tags)
if right tag is 1, pop an element from the stack and call it right
if left tag is 1, pop an element from the stack and call it left
create a new binary tree with data as its root and
left and right as its subtrees (if they exist)
push the new tree onto your stack
}
when you exit the while loop, the stack should contain one element
this element is the the tree represented by the file data (so return it!)
If the stack is empty, the tree has no nodes, so you know what kind of tree it is.
NOTE: the input file is left then right, but the algorithm
uses the right value first, then the left. Not a typo.
E 0 0 F 0 0 D 1 1 B 1 0 C 0 0 A 1 1