// Program to test solutions to problem #9 on the cse143x final, fall 2018. // Fill in your solution to limitPathSum, then compile and run the program import java.util.*; class IntTree { public void limitPathSum(int max) { // 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 void limitPathSum2(int max) { overallRoot = limitPathSum2(overallRoot, max, 0); } private IntTreeNode limitPathSum2(IntTreeNode root, int max, int current) { if (root != null) { current += root.data; if (current > max) root = null; else { root.left = limitPathSum2(root.left, max, current); root.right = limitPathSum2(root.right, max, current); } } return root; } // post: constructs an empty tree public IntTree() { overallRoot = null; } // pre : the tree satisfies the binary search tree property // post: value is added so as to preserve the binary search tree property public void add(int value) { overallRoot = add(overallRoot, value); } // pre : the tree with given root satisfies the binary search tree property // post: value is added so as to preserve the binary search tree property private IntTreeNode add(IntTreeNode root, int value) { if (root == null) root = new IntTreeNode(value); else if (value <= root.data) root.left = add(root.left, value); else root.right = add(root.right, value); return root; } // 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 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 FinalTest9 { private static int testCount; private static int failCount; 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"); } } public static void test(int n) { IntTree t1 = new IntTree(); IntTree t2 = new IntTree(); Random r = new Random(); for (int i = 0; i < n; i++) { int next = r.nextInt(100); t1.add(next); t2.add(next); } int max; if (n == 0) max = 0; else max = (int) (60 * Math.log(n) / Math.log(2)); System.out.println("Limiting path sum to " + max + " and testing this tree :"); t1.printStructure(); System.out.println("Expected result:"); t1.limitPathSum2(max); t1.printStructure(); boolean fail = false; try { t2.limitPathSum(max); } 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++; } 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 leaf node with given data public IntTreeNode(int data) { this(data, null, null); } // 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; } }