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