Subversion Repositories programming

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
41 irasnyd 1
// Written by Ira Snyder
2
//
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
 
45 irasnyd 42
    public static AVLTree createTestTree( int size ) {
43
        int[] nums = new int[30];
44
 
45
        for( int i=0; i<30; i++ ) { nums[i] = i; }
46
 
47
        //returns the int[] as a Integer[] with the values
48
        //multiplied by 200
49
        Integer[] data = makeIntegers(nums);
50
 
51
        return new AVLTree(nums,data);
52
    }
53
 
54
 
55
    public static void testBasicConstructor() {
56
        //this should make a pole if the tree is not balanced (IE: a plain BST)
57
        //if the tree is _not_ balanced, this will print height = 7
58
        AVLTree poleTree = new AVLTree(1,new Integer(1));
59
        poleTree.add(2,new Integer(2));
60
        poleTree.add(3,new Integer(3));
61
        poleTree.add(4,new Integer(4));
62
        poleTree.add(5,new Integer(5));
63
        poleTree.add(6,new Integer(6));
64
        poleTree.add(7,new Integer(7));
65
 
66
        System.out.print("Should print 2: ");
67
        System.out.println("height = " + poleTree.getHeight());
68
    }
69
 
70
    public static void testArrayConstructor() {
71
        int[] nums = new int[30];
72
 
73
        for( int i=0; i<30; i++ ) { nums[i] = i; }
74
 
75
        //returns the int[] as a Integer[] with the values
76
        //multiplied by 200
77
        Integer[] data = makeIntegers(nums);
78
 
79
        AVLTree tree = new AVLTree(nums,data);
80
 
81
        System.out.print("size should be 30: ");
82
        System.out.println("size = " + tree.size());
83
 
84
        System.out.print("should be 4 or 5: ");
85
        System.out.println("height = " + tree.getHeight());
86
    }
87
 
88
    public static void testGetters() {
89
        AVLTree tree = new AVLTree(1,new Integer(1));
90
        tree.add(2,new Integer(2));
91
        tree.add(3,new Integer(3));
92
 
93
        System.out.println("tree = " + tree);
94
        System.out.println("tree.getRoot() = " + tree.getRoot());
95
        System.out.println("tree.getLeft() = " + tree.getLeft());
96
        System.out.println("tree.getRight() = " + tree.getRight());
97
    }
98
 
99
    public static void testContains() {
100
        AVLTree tree = createTestTree(30);
101
 
102
        System.out.println("tree.contains(20) = " + tree.contains(20));
103
        System.out.println("tree.contains(50) = " + tree.contains(50));
104
    }
105
 
106
    public static void testGet() {
107
        AVLTree tree = createTestTree(30);
108
 
109
        System.out.println("tree.get(10) = " + tree.get(10));
110
        System.out.println("tree.get(50) = " + tree.get(50));
111
    }
112
 
113
    public static void testEquals() {
114
        AVLTree tree1 = createTestTree(10);
115
        AVLTree tree2 = createTestTree(10);
116
 
117
        int[] nums = { 0,10,20,30,40,50,60,70,80,90 };
118
        Integer[] data = makeIntegers(nums);
119
        AVLTree tree3 = new AVLTree(nums,data);
120
 
121
        System.out.println("tree1 = " + tree1);
122
        System.out.println("tree2 = " + tree2);
123
        System.out.println("tree3 = " + tree3);
124
 
125
        System.out.println("tree1.equals(tree2) = " + tree1.equals(tree2));
126
        System.out.println("tree1.equals(tree3) = " + tree1.equals(tree3));
127
    }
128
 
129
    public static void testRemove() {
130
        AVLTree tree = createTestTree(30);
131
 
132
        //print the original tree
133
        System.out.println("tree = " + tree);
134
 
135
        //remove the first 10 elements
136
        for( int i=0; i<10; i++ ) { 
137
            System.out.println("tree.remove(" + i + ") = " + tree.remove(i)); 
138
        }
139
 
140
        //remove some elements not in the tree
141
        for( int i=50; i<55; i++ ) { 
142
            System.out.println("tree.remove(" + i + ") = " + tree.remove(i));
143
        }
144
 
145
        //print the tree after everything has been removed
146
        System.out.println("tree = " + tree);
147
    }
41 irasnyd 148
}
149