Main {{ static void p(String s){ System.out.print(s); } static void p(){ System.out.println(); } public static void main(String args[]) throws Exception { NodeInt tree = NodeInt.parse(System.in); p(" Tree: "+tree+"\n"); tree.print(); p(); p(" Check: "+new Traversal(new Check()).traverse(tree)+"\n"); p(" Check result " + Check.check(tree)); p(" Sum: "+new Traversal(new Sum()).traverse(tree)+"\n"); p(" Diameter: "+new Traversal(new Diameter()).traverse(tree)+"\n"); DiameterPair pair = new Traversal(new Diameter()).traverse(tree); pair.print(); p(); p(" Diameter2: "+tree.diameter2() +"\n"); p(" ToStr: "+new Traversal(new ToStr()).traverse(tree)+"\n"); p(" Incr: "+new Traversal(new Incr()).traverse(tree)+"\n"); BSTInt incremented = new Traversal(new Incr()).traverse(tree); incremented.print(); p(); p("Height: "+new Traversal(new Height()).traverse(tree,0)+"\n"); p("Height2: "+new Traversal(new Height2()).traverse(tree,0)+"\n"); p("Height3: "+new Traversal(new Height3()).traverse(tree,new DownHeight(0))+"\n"); p("String: "+new Traversal(new Strs()).traverse(tree)+"\n"); // what does this do?? creates a tree of strings. Why not caught by type checking? p("Rev: "+new Traversal(new Rev()).traverse(tree)+"\n"); // BSTInt withText = new Traversal(new Rev()).traverse(tree); // Does not work with a tree of Integer BSTInt reversed = new Traversal(new Rev()).traverse(tree); reversed.print(); p(); System.out.println(" done "); } }} // /* BSTInt {{ abstract boolean isEmpty(); }} NodeInt {{ boolean isEmpty(){ return false;} }} EmptyInt {{ boolean isEmpty(){ return true;} }} BSTInt {{ abstract int diameter2(); abstract int height2(); }} NodeInt {{ int height2() { return 1 + Math.max(left.height2(), right.height2()); } int diameter2() { int lheight = left.height2(); int rheight = right.height2(); int ldiameter = left.diameter2(); int rdiameter = right.diameter2(); return Math.max(lheight + rheight + 1, Math.max(ldiameter, rdiameter)); } }} EmptyInt {{ int diameter2() {return 0; } int height2() {return 0; } }} // */ Pack {{ Pack(boolean g){ this(g, Integer.MAX_VALUE, Integer.MIN_VALUE); } }} AbstractClass {{ public Pack check2() { return new Pack(true);} }} Concrete2 {{ public Pack check2(){ Pack lp = left.check2(); Pack rp = right.check2(); return new Pack((lp.good && rp.good && (lp.max < data) && (data < rp.min)), Math.min(lp.min, data), Math.max(rp.max, data)); } }}