// Program to test solutions to problem #10 on the cse143 final, winter 2014. // Fill in your solution to the constructor, then compile and run the program import java.util.*; class IntTree { public IntTree(int n) { // fill in your solution here and include any private methods below // you can also rename the parameter above } private IntTreeNode overallRoot; // this is the sample solution public IntTree(boolean dummy, int n) { if (n < 0) { throw new IllegalArgumentException(); } overallRoot = makeTree(1, n); } private IntTreeNode makeTree(int low, int high) { if (low > high) { return null; } else { int mid = (low + high - 1) / 2; return new IntTreeNode(high, makeTree(low, mid), makeTree(mid + 1, high - 1)); } } // post: constructs an empty tree public IntTree() { overallRoot = null; } // post: prints the data fields of the tree, one per line, following // an inorder traversal and using indentation to indicate node depth; // prints right to left so that it looks correct when the output is // rotated. public void printStructure() { printStructure(overallRoot, 0); } // post: prints in preorder the data fields of the given tree indenting // each line to the given level private void printStructure(IntTreeNode root, int level) { if (root != null) { printStructure(root.right, level + 1); for (int i = 0; i < level; i++) System.out.print(" "); System.out.println(root.data); printStructure(root.left, level + 1); } } // returns true if this tree has the same structure as the other tree public boolean sameStructure(IntTree other) { return sameStructure(overallRoot, other.overallRoot); } private boolean sameStructure(IntTreeNode root1, IntTreeNode root2) { if (root1 == null || root2 == null) return root1 == null && root2 == null; else return sameStructure(root1.left, root2.left) && sameStructure(root1.right, root2.right); } // returns true if this tree equals other tree public boolean equals(IntTree other) { return equals(overallRoot, other.overallRoot); } private boolean equals(IntTreeNode root1, IntTreeNode root2) { if (root1 == null || root2 == null) return root1 == null && root2 == null; else return root1.data == root2.data && equals(root1.left, root2.left) && equals(root1.right, root2.right); } } public class FinalTest10 { private static int testCount; private static int failCount; private static int structureCount; public static void main(String[] args) { for (int i = 0; i < 30; i++) test(i); if (failCount == 0) { System.out.println("passed all tests"); } else { System.out.println("failed " + failCount + " of " + testCount + " tests"); if (structureCount > 0) { System.out.println("structure matched in " + structureCount + " of the failed cases"); } } } public static void test(int n) { System.out.println("IntTree(" + n + ") should produce this tree:"); IntTree t1 = new IntTree(true, n); t1.printStructure(); IntTree t2 = new IntTree(); boolean fail = false; try { t2 = new IntTree(n); } catch (RuntimeException e) { int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" threw " + e + " at line #" + line); fail = true; } if (!fail && (!t1.equals(t2))) { System.out.println("actual result:"); t2.printStructure(); fail = true; } testCount++; if (fail) { System.out.println("failed"); failCount++; if (t1.sameStructure(t2)) { structureCount++; } } else { System.out.println("passed"); } System.out.println("---------------------------------------------"); } } class IntTreeNode { public int data; // data stored at this IntTreeNode public IntTreeNode left; // reference to left subtree public IntTreeNode right; // reference to right subtree // post: constructs a IntTreeNode with the given data and links public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) { this.data = data; this.left = left; this.right = right; } }