Rev 50 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
// Written by Ira Snyderimport java.io.*;class Driver {public static void main ( String [] args ) throws Exception {testBasicConstructor();System.out.println();testArrayConstructor();System.out.println();testGetters();System.out.println();testContains();System.out.println();testGet();System.out.println();testEquals();System.out.println();testRemove();System.out.println();testHeight();System.out.println();testPrint();System.out.println();}//creates an array of Integers from an array of ints. The returned array//will have all of the values multiplied by 200 (just to give my data//different values from the keyspublic static Integer[] makeIntegers( int[] a ) {Integer[] answer = new Integer[a.length];for( int i=0; i<a.length; i++ ) {answer[i] = new Integer(a[i]*200);}return answer;}//method to make a test treepublic static AVLTree createTestTree( int size ) {int[] nums = new int[30];for( int i=0; i<30; i++ ) { nums[i] = i; }//returns the int[] as a Integer[] with the values//multiplied by 200Integer[] data = makeIntegers(nums);return new AVLTree(nums,data);}//method to test the basic AVLTree constructorpublic static void testBasicConstructor() {//this should make a pole if the tree is not balanced (IE: a plain BST)//if the tree is _not_ balanced, this will print height = 7AVLTree poleTree = new AVLTree(1,new Integer(1));poleTree.add(2,new Integer(2));poleTree.add(3,new Integer(3));poleTree.add(4,new Integer(4));poleTree.add(5,new Integer(5));poleTree.add(6,new Integer(6));poleTree.add(7,new Integer(7));System.out.print("Should print 2: ");System.out.println("height = " + poleTree.getHeight());}//method to test the AVLTree array constructorpublic static void testArrayConstructor() {int[] nums = new int[30];for( int i=0; i<30; i++ ) { nums[i] = i; }//returns the int[] as a Integer[] with the values//multiplied by 200Integer[] data = makeIntegers(nums);AVLTree tree = new AVLTree(nums,data);System.out.print("size should be 30: ");System.out.println("size = " + tree.size());System.out.print("should be 4 or 5: ");System.out.println("height = " + tree.getHeight());}//method to test the getter methods from Prog. Prob. 13.3public static void testGetters() {AVLTree tree = new AVLTree(1,new Integer(1));tree.add(2,new Integer(2));tree.add(3,new Integer(3));System.out.println("tree = " + tree);System.out.println("tree.getRoot() = " + tree.getRoot());System.out.println("tree.getLeft() = " + tree.getLeft());System.out.println("tree.getRight() = " + tree.getRight());}//method to test the contains method from Prog. Prob. 13.4public static void testContains() {AVLTree tree = createTestTree(30);System.out.println("tree.contains(20) = " + tree.contains(20));System.out.println("tree.contains(50) = " + tree.contains(50));}//get the get() method from Prog. Prob. 13.5public static void testGet() {AVLTree tree = createTestTree(30);System.out.println("tree.get(10) = " + tree.get(10));System.out.println("tree.get(50) = " + tree.get(50));}//test the equals method from Prog. Prob. 13.6public static void testEquals() {AVLTree tree1 = createTestTree(10);AVLTree tree2 = createTestTree(10);int[] nums = { 0,10,20,30,40,50,60,70,80,90 };Integer[] data = makeIntegers(nums);AVLTree tree3 = new AVLTree(nums,data);System.out.println("tree1 = " + tree1);System.out.println("tree2 = " + tree2);System.out.println("tree3 = " + tree3);System.out.println("tree1.equals(tree2) = " + tree1.equals(tree2));System.out.println("tree1.equals(tree3) = " + tree1.equals(tree3));}//method to test the remove method from Project 2public static void testRemove() {AVLTree tree = createTestTree(30);//print the original treeSystem.out.println("tree = " + tree);//remove the first 10 elementsfor( int i=0; i<10; i++ ) {System.out.println("tree.remove(" + i + ") = " + tree.remove(i));}//remove some elements not in the treefor( int i=50; i<55; i++ ) {System.out.println("tree.remove(" + i + ") = " + tree.remove(i));}//print the tree after everything has been removedSystem.out.println("tree = " + tree);}//method to test some heightspublic static void testHeight() {AVLTree tree = createTestTree(30);for( int i=0; i<30; i++ ) {System.out.println("Node with key: " + i + " has height: "+ tree.getHeight(i));}}public static void testPrint() {AVLTree tree = createTestTree(30);System.out.println("Printing InOrder:");AVLTree.printInOrder(tree);System.out.println();System.out.println("Printing PreOrder:");AVLTree.printPreOrder(tree);}}