Subversion Repositories programming

Rev

Rev 100 | Blame | Compare with Previous | Last modification | View Log | RSS feed

// Written by Ira Snyder
// License: Public Domain (added 07-11-2005)

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();
        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 keys
    public 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 tree
    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 200
        Integer[] data = makeIntegers(nums);

        return new AVLTree(nums,data);
    }

    //method to test the basic AVLTree constructor
    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 = 7
        AVLTree 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 constructor
    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 200
        Integer[] 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.3
    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());
    }

    //method to test the contains method from Prog. Prob. 13.4
    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));
    }

    //get the get() method from Prog. Prob. 13.5
    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));
    }

    //test the equals method from Prog. Prob. 13.6
    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));
    }

    //method to test the remove method from Project 2
    public static void testRemove() {
        AVLTree tree = createTestTree(30);
        
        //print the original tree
        System.out.println("tree = " + tree);
        
        //remove the first 10 elements
        for( int i=0; i<10; i++ ) { 
            System.out.println("tree.remove(" + i + ") = " + tree.remove(i)); 
        }
        
        //remove some elements not in the tree
        for( int i=50; i<55; i++ ) { 
            System.out.println("tree.remove(" + i + ") = " + tree.remove(i));
        }
        
        //print the tree after everything has been removed
        System.out.println("tree = " + tree);
    }
    
    //method to test some heights
    public 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);
    }
        
}