-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWAVLTree_Tester.java
More file actions
231 lines (204 loc) · 7.01 KB
/
WAVLTree_Tester.java
File metadata and controls
231 lines (204 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package project_m;
import java.util.Arrays;
import java.util.Random;
import project_m.WAVLTree.WAVLNode;
public class WAVLTree_Tester {
public static void main(String[] args) {
//create a random tree and test if there are any error while inserting the items.
WAVLTree test_tree = new WAVLTree();
test_tree = example_tree_creator2();
// for(int i= 0; i<155 ; i++){
// example_random_tree_deletor(test_tree,20,20);
// }
insDelTest(5000,500,500,500);
System.out.println("__________________________________________________________________");
System.out.println("");
// creating a tree and testing it for min, max, search, keysToArray, infoToArray, size.
test_tree = example_tree_creator();
if(!test_tree.min().equals("1")){
System.out.println("error 1: in min function");
}
if(!test_tree.max().equals("9")){
System.out.println("error 2: in max function");
}
if(!test_tree.search(3).equals("3")){
System.out.println("error 3: in search function");
}
if(!(test_tree.search(4) == null)){
System.out.println("error 4: in search function");
}
int [] sorted_keys_array = {1,2,3,5,6,7,9};
if(!Arrays.equals(test_tree.keysToArray(),sorted_keys_array)){
System.out.println("error 5: in keys to array function");
}
String [] sorted_info_array = {"1","2","3","5","6","7","9"};
if(!Arrays.equals(test_tree.infoToArray(),sorted_info_array)){
System.out.println("error 6: in info to array function");
}
WAVLTree empty_tree = new WAVLTree();
if(empty_tree.empty() != true){
System.out.println("error 7: in empty function");
}
if(test_tree.empty() == true){
System.out.println("error 8: in empty function");
}
if(empty_tree.size() != 0){
System.out.println("error 9: in size function");
}
if(test_tree.size() != 7){
System.out.println("error 10: in size function");
}
System.out.println("Done!");
}
public static WAVLTree example_tree_creator(){
WAVLTree tree = new WAVLTree();
tree.insert(5, "5");
tree.insert(3, "3");
tree.insert(6, "6");
tree.insert(7, "7");
tree.insert(1, "1");
tree.insert(9, "9");
tree.insert(2, "2");
return tree;
}
public static void insDelTest(int iter, int startTreeSize, int numOfTests, int maxNum){
Random random = new Random();
int in;
for (int i = 0; i<iter; i++){
WAVLTree tree = new WAVLTree();
for(int j =0; j<startTreeSize; j++){
in = random.nextInt(maxNum);
tree.insert(in, "" + in);
}
//printBinaryTree(tree.root, 0);
for(int j = 0; j < numOfTests; j++){
in = random.nextInt(maxNum);
tree.insert(in, "" + in);
int[] array = tree.keysToArray();
int rnd = new Random().nextInt(array.length);
in = array[rnd];
tree.delete(in);
if(!testTreeRanks(tree.root)){
System.out.println("Error in rebalancing after inserting item key " + in + " " + i);
printBinaryTree(tree.root, 0);
}
// else
// //System.out.println("delete!");
//
}
System.out.println("DDone!");
}
}
public static WAVLTree example_tree_creator2(){
WAVLTree tree = new WAVLTree();
tree.insert(1, "1");
tree.insert(13, "13");
tree.insert(14, "14");
tree.insert(15, "15");
tree.insert(7, "7");
tree.insert(8, "8");
tree.insert(16, "16");
tree.insert(17, "17");
tree.insert(18, "18");
tree.insert(19, "19");
tree.insert(3, "3");
tree.insert(4, "4");
tree.insert(6, "6");
tree.insert(9, "9");
tree.insert(10, "10");
tree.insert(11, "11");
tree.insert(2, "2");
tree.insert(12, "12");
tree.insert(5, "5");
tree.insert(20, "20");
return tree;
}
public static WAVLTree example_random_tree_insertor(WAVLTree tree,int randomInsertionNumber, int randomMaxInteger){
Random random = new Random();
int lastKey;
int lastTreeSize =tree.size();
tree = example_tree_creator2();
for(int i = 0; i < randomInsertionNumber; i++){
tree.insert(lastKey = random.nextInt(randomMaxInteger), "info");
if(!testTreeRanks(tree.root)){
System.out.println("Error in rebalancing after inserting item key " + lastKey + " " + i);
printBinaryTree(tree.root, 0);
return tree;
}
}
System.out.println("Finished inserting " + (tree.size()-lastTreeSize) + " random items to tree without any errors.");
System.out.println("DDone!");
return tree;
}
public static WAVLTree example_random_tree_deletor(WAVLTree tree,int randomDeletionNumber, int randomMaxInteger){
Random random = new Random();
int lastKey;
int lastTreeSize =tree.size();
// printBinaryTree(tree.WAVL_root, 0,tree.WAVL_emptyNode);
for(int i = 0; i < randomDeletionNumber; i++){
tree = example_tree_creator2();
if(tree.delete(lastKey = random.nextInt(randomMaxInteger))!=-1){
// System.out.println("__________________________________________________________________");
// System.out.println("");
// System.out.println("deleting item " + lastKey);
// System.out.println("__________________________________________________________________");
// System.out.println("");
//printBinaryTree(tree.WAVL_root, 0,tree.WAVL_emptyNode);
if(!testTreeRanks(tree.root)){
System.out.println("Error in rebalancing after deleting item key " + lastKey);
printBinaryTree(tree.root, 0);
return tree;
}
}
System.out.println("DDone!");
}
// System.out.println("Finished deleting " + (lastTreeSize-tree.size()) + " random items to tree without any errors.");
return tree;
}
// public static void printBinaryTree(WAVLTree.WAVLNode root, int level, WAVLTree.WAVLNode empty){
// if(root==null || root == empty)
// return;
// printBinaryTree(root.right, level+1,empty);
// if(level!=0){
// for(int i=0;i<level-1;i++)
// System.out.print("|\t");
// System.out.println("|---" + root.rank + "---"+root.key);
// }
// else
// System.out.println(root.key);
// printBinaryTree(root.left, level+1,empty);
// }
public static void printBinaryTree(WAVLNode root, int level){
if(root==null)
return;
printBinaryTree(root.right, level+1);
if(level!=0){
for(int i=0;i<level-1;i++)
System.out.print("|\t");
System.out.println("|-------"+root.key + "(" + root.rank + ")");
}
else
System.out.println(root.key + "(" + root.rank + ")");
printBinaryTree(root.left, level+1);
}
public static boolean testTreeRanks(WAVLTree.WAVLNode root){
int rank = root.rank;
int left_rank = root.left.rank;
int right_rank = root.right.rank;
if (root.left.key > root.right.key)
return false;
if (rank - left_rank > 2)
return false;
if (rank - left_rank < 1)
return false;
if (rank - right_rank < 1)
return false;
if (rank - right_rank > 2)
return false;
if (root.right.rank != -1)
testTreeRanks(root.right);
if (root.left.rank != -1)
testTreeRanks(root.left);
return true;
}
}