Subversion Repositories programming

Rev

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

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