Subversion Repositories programming

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
41 irasnyd 1
// Written by Ira Snyder
49 irasnyd 2
 
41 irasnyd 3
import java.io.*;
4
 
42 irasnyd 5
class Driver {
41 irasnyd 6
 
7
    public static void main ( String [] args ) throws Exception {
42 irasnyd 8
 
45 irasnyd 9
        testBasicConstructor();
10
        System.out.println();
41 irasnyd 11
 
45 irasnyd 12
        testArrayConstructor();
13
        System.out.println();
42 irasnyd 14
 
45 irasnyd 15
        testGetters();
16
        System.out.println();
42 irasnyd 17
 
45 irasnyd 18
        testContains();
42 irasnyd 19
        System.out.println();
20
 
45 irasnyd 21
        testGet();
42 irasnyd 22
        System.out.println();
23
 
45 irasnyd 24
        testEquals();
42 irasnyd 25
        System.out.println();
26
 
45 irasnyd 27
        testRemove();
43 irasnyd 28
    }
29
 
30
    //creates an array of Integers from an array of ints. The returned array
31
    //will have all of the values multiplied by 200 (just to give my data
32
    //different values from the keys
33
    public static Integer[] makeIntegers( int[] a ) {
34
        Integer[] answer = new Integer[a.length];
35
        for( int i=0; i<a.length; i++ ) {
36
            answer[i] = new Integer(a[i]*200);
37
        }
42 irasnyd 38
 
43 irasnyd 39
        return answer;
41 irasnyd 40
    }
43 irasnyd 41
 
49 irasnyd 42
    //method to make a test tree
45 irasnyd 43
    public static AVLTree createTestTree( int size ) {
44
        int[] nums = new int[30];
45
 
46
        for( int i=0; i<30; i++ ) { nums[i] = i; }
47
 
48
        //returns the int[] as a Integer[] with the values
49
        //multiplied by 200
50
        Integer[] data = makeIntegers(nums);
51
 
52
        return new AVLTree(nums,data);
53
    }
54
 
49 irasnyd 55
    //method to test the basic AVLTree constructor
45 irasnyd 56
    public static void testBasicConstructor() {
57
        //this should make a pole if the tree is not balanced (IE: a plain BST)
58
        //if the tree is _not_ balanced, this will print height = 7
59
        AVLTree poleTree = new AVLTree(1,new Integer(1));
60
        poleTree.add(2,new Integer(2));
61
        poleTree.add(3,new Integer(3));
62
        poleTree.add(4,new Integer(4));
63
        poleTree.add(5,new Integer(5));
64
        poleTree.add(6,new Integer(6));
65
        poleTree.add(7,new Integer(7));
66
 
67
        System.out.print("Should print 2: ");
68
        System.out.println("height = " + poleTree.getHeight());
69
    }
70
 
49 irasnyd 71
    //method to test the AVLTree array constructor
45 irasnyd 72
    public static void testArrayConstructor() {
73
        int[] nums = new int[30];
74
 
75
        for( int i=0; i<30; i++ ) { nums[i] = i; }
76
 
77
        //returns the int[] as a Integer[] with the values
78
        //multiplied by 200
79
        Integer[] data = makeIntegers(nums);
80
 
81
        AVLTree tree = new AVLTree(nums,data);
82
 
83
        System.out.print("size should be 30: ");
84
        System.out.println("size = " + tree.size());
85
 
86
        System.out.print("should be 4 or 5: ");
87
        System.out.println("height = " + tree.getHeight());
88
    }
89
 
49 irasnyd 90
    //method to test the getter methods from Prog. Prob. 13.3
45 irasnyd 91
    public static void testGetters() {
92
        AVLTree tree = new AVLTree(1,new Integer(1));
93
        tree.add(2,new Integer(2));
94
        tree.add(3,new Integer(3));
95
 
96
        System.out.println("tree = " + tree);
97
        System.out.println("tree.getRoot() = " + tree.getRoot());
98
        System.out.println("tree.getLeft() = " + tree.getLeft());
99
        System.out.println("tree.getRight() = " + tree.getRight());
100
    }
101
 
49 irasnyd 102
    //method to test the contains method from Prog. Prob. 13.4
45 irasnyd 103
    public static void testContains() {
104
        AVLTree tree = createTestTree(30);
105
 
106
        System.out.println("tree.contains(20) = " + tree.contains(20));
107
        System.out.println("tree.contains(50) = " + tree.contains(50));
108
    }
109
 
49 irasnyd 110
    //get the get() method from Prog. Prob. 13.5
45 irasnyd 111
    public static void testGet() {
112
        AVLTree tree = createTestTree(30);
113
 
114
        System.out.println("tree.get(10) = " + tree.get(10));
115
        System.out.println("tree.get(50) = " + tree.get(50));
116
    }
117
 
49 irasnyd 118
    //test the equals method from Prog. Prob. 13.6
45 irasnyd 119
    public static void testEquals() {
120
        AVLTree tree1 = createTestTree(10);
121
        AVLTree tree2 = createTestTree(10);
122
 
123
        int[] nums = { 0,10,20,30,40,50,60,70,80,90 };
124
        Integer[] data = makeIntegers(nums);
125
        AVLTree tree3 = new AVLTree(nums,data);
126
 
127
        System.out.println("tree1 = " + tree1);
128
        System.out.println("tree2 = " + tree2);
129
        System.out.println("tree3 = " + tree3);
130
 
131
        System.out.println("tree1.equals(tree2) = " + tree1.equals(tree2));
132
        System.out.println("tree1.equals(tree3) = " + tree1.equals(tree3));
133
    }
134
 
49 irasnyd 135
    //method to test the remove method from Project 2
45 irasnyd 136
    public static void testRemove() {
137
        AVLTree tree = createTestTree(30);
138
 
139
        //print the original tree
140
        System.out.println("tree = " + tree);
141
 
142
        //remove the first 10 elements
143
        for( int i=0; i<10; i++ ) { 
144
            System.out.println("tree.remove(" + i + ") = " + tree.remove(i)); 
145
        }
146
 
147
        //remove some elements not in the tree
148
        for( int i=50; i<55; i++ ) { 
149
            System.out.println("tree.remove(" + i + ") = " + tree.remove(i));
150
        }
151
 
152
        //print the tree after everything has been removed
153
        System.out.println("tree = " + tree);
154
    }
41 irasnyd 155
}
156