Rev 44 | Blame | Last modification | View Log | RSS feed
// Written by Ira Snyder//import 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();}//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;}public 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);}public 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());}public 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());}public 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());}public 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));}public 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));}public 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));}public 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);}}