CS 201 - Homework 6 - IntTrees

Due: Wednesday 11/1 at 8am

This assignment consists of one programming problem, related to IntTrees, which are similar to the IntLists from HW 5 (and different from the object-oriented tree class discussed in Bailey's text). When you have completed the assignment, please submit your program (HW6IntTreeOps.java) using the HW 6 submission page.


Lab exercises:

Start by downloading hw6.zip, and extract this archive in your cs201 directory. This will create a new directory hw6 containing the following files:
HW6IntTreeOps.java   IntTreeOps.java
IntTree.java         IntTreeOpsTest.java
Then, as before, start Eclipse, go to File->Import, select General->Existing Projects into Workspace, and select your hw6 folder as "root directory".

All your code for this homework should be added to the file HW6IntTreeOps.java. Start by studying the code in IntTree.java and IntTreeOps.java. Then, compile and run IntTreeOpsTest.java. Make sure you understand what's going on, and in particular the way the method treePrint displays a tree.

During lab we suggest that you should start by making progress on problem 5 below, the main method used for testing. Here are some specific steps to get you started:

  1. In your main method, declare 7 variables of type IntTree and assign the 7 trees shown below to these variables. Look at IntTreeOps.java for example code on how to do this.
  2. Call printTree on each of your 7 trees to make sure your trees are correct.
  3. Instead of using 7 variables, now store your 7 trees in an array of IntTrees, and write a loop that calls printTree on each.
  4. Also call some of the other functions from IntTreeOps.java and print their results. Make sure you understand what's going on.
At this point, you are in a good position to tackle problems 1-4. Each time you've written one, add code to your main method to test it. Once all your methods work, change your main program so it obeys the requirements of problem 5, i.e., get the tree number(s) as command-line arguments, instead of running test on all trees.


Homework problems: IntTree operations

Problem 1: Weighted Trees

Consider the following method defined for integer trees:

public static IntTree weightedTree(IntTree t)

Returns a new tree that has the same "shape" as t but in which every node has a value that is the product of the value of the corresponding node in t and the level of that node. The original tree should be left unchanged.

For the purpose of this question, let us define the level of a tree node to be the number of nodes on the path from the node to the root of the tree. So the level of the root is 1, the level of root's children is 2, the level of the root's grandchildren is 3, and so on. (The level numbers according to this definition are one higher than the level numbers as defined by Bailey). For example, in the figure below, the tree C' shown on the right is the result of calling weightedTree on the tree C shown on the left.

Sample tree C C' = weightedTree(C)

Write a definition of weightedTree.

Hint: it may be helpful to define weightedTree in terms of an auxiliary recursive function that takes two arguments - a tree and a level number - and returns a new tree (similar to the method partialSumDown in IntTreeOps.java).

Problem 2: Comparing the Shapes of Two Trees

Write a method compareTrees that has two inputs that are each of type IntTree, and which returns a boolean value. compareTrees should return true if the two trees have the same shape, that is, the same number of nodes with the same geometric arrangement of the nodes. The content of the nodes themselves need not be the same. For example, given the two trees C and C' in Problem 1, compareTrees should return true. When the two trees are not of the same shape, this method should return false.

Problem 3: Testing Whether a Tree is Full

A binary tree of a particular height h is defined to be "full" if it has the maximum number of nodes at each level of the tree. The trees C and C' in Problem 1, for example, are full trees, because they have 1 node at level 1, 2 nodes at level 2, and 4 nodes at level 3. A full tree of height 4 would also have 8 nodes at the 4th level of the tree, and so on. Write a method testFullTree that has a single input of type IntTree, and which returns a boolean value that should be true if the tree if full and false otherwise.

Problem 4: Testing Whether the Contents of a Tree are Ordered

A binary tree is defined to be "ordered" if, for every node of the tree, all values stored in the entire left subtree of this node are less or equal than the value stored in this node, and all values stored in the right subtree are greater or equal than the value stored in this root node. Write a method testOrderedTree that has a single input of type IntTree, and which returns a boolean value that should be true if the values stored in the input tree are ordered and false otherwise.

Problem 5: A Main Method to Test it All

An important part of this assignment is to write a main method that tests your methods from problems 1-4. Your main method should first initialize an array of IntTrees to contain the seven trees shown below, and then let the user select one or two of them to test your methods with:

  1. The empty tree.

  2. A tree with a single (leaf) node with value three:
  3. The tree:
  4. The tree:
  5. The tree:

  6. The tree:

  7. The tree C from problem 1.
The behavior of the main method is summarized in the usage message (which, as usual, should be printed when the wrong number of arguments is supplied):
      > java HW6IntTreeOps 
      usage: java HW6IntTreeOps <t1>
	  or java HW6IntTreeOps <t1> <t2>
      
	Tests certain operations on selected tree(s).
	If one tree is specified, runs 'weightedTree()',
	  'testFullTree()', and 'testOrderedTree()'.
	If two trees are specified, runs 'compareTrees()'.
	Valid tree numbers are 1 through 7.
Here are two sample runs if one tree is specified. Your program should produce similar output (note that the tree should be printed first).
      > java HW6IntTreeOps 1
      Tree 1:
        .
      weightedTree:
        .
      testFullTree:    true
      testOrderedTree: true
      
      > java HW6IntTreeOps 7
      Tree 7:
        1   3   7
	        6
	    2   5
	        4
      weightedTree:
        1   6  21
	       18
	    4  15
	       12
      testFullTree:    true
      testOrderedTree: false
Here is a sample run if two trees are specified:
      > java HW6IntTreeOps 2 3
      Tree 2:
        3
      Tree 3:
        1   3
	    2
      compareTrees:   false
Your program should be robust to erroneous input. In particular, it should output an error message if a tree number is not within the valid range 1-7.

Problem 6 - extra credit: Level-order traversal

Write a method levelOrderWrite that takes one parameter of type IntTree and prints all its values in level-order. For example, when called on tree 5 your method should print

  3  1  4  2  5
and when called on tree 7 your method should print
  1  2  3  4  5  6  7
For full (extra) credit, your method should "visit" each node only once.