-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVLTest2.java
508 lines (467 loc) · 17.8 KB
/
AVLTest2.java
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
package cpsc331.assignment2;
import java.util.NoSuchElementException;
import cpsc331.collections.Dictionary;
import cpsc331.assignment2.AVLDictionary;
import java.util.ArrayList;
import cpsc331.assignment2.AVLUtilities;
public class AVLTest2 {
public static void main(String[] args) {
System.out.println("");
System.out.print("Initializing a new AVLDictionary ");
System.out.print("with integers as keys and strings ");
System.out.println(" as values.");
System.out.println("");
AVLDictionary<Integer, String> D =
new AVLDictionary<Integer, String>();
System.out.println("Inserting key 20 and value twenty.");
D.set(20, "twenty");
System.out.println("Inserting key 19 and value nineteen.");
D.set(19, "nineteen");
System.out.println("Inserting key 18 and value eighteen.");
D.set(18, "eighteen");
System.out.println("Inserting key 17 and value seventeen.");
D.set(17, "seventeen");
System.out.println("Inserting key 16 and value sixteen.");
D.set(16, "sixteen");
System.out.println("Inserting key 15 and value fifteen.");
D.set(15, "fifteen");
System.out.println("Inserting key 14 and value fourteen.");
D.set(14, "fourteen");
System.out.println("Inserting key 13 and value thirteen.");
D.set(13, "thirteen");
System.out.println("Inserting key 12 and value twelve.");
D.set(12, "twelve");
System.out.println("Inserting key 11 and value eleven.");
D.set(11, "eleven");
System.out.println("Inserting key 10 and value ten.");
D.set(10, "ten");
System.out.println("Inserting key 9 and value nine.");
D.set(9, "nine");
System.out.println("Inserting key 8 and value eight.");
D.set(8, "eight");
System.out.println("Inserting key 7 and value seven.");
D.set(7, "seven");
System.out.println("Inserting key 6 and value six.");
D.set(6, "six");
System.out.println("Inserting key 5 and value five.");
D.set(5, "five");
System.out.println("Inserting key 4 and value four.");
D.set(4, "four");
System.out.println("Inserting key 3 and value three.");
D.set(3, "three");
System.out.println("Inserting key 2 and value two.");
D.set(2, "two");
System.out.println("Inserting key 1 and value one.");
D.set(1, "one");
System.out.println("");
System.out.println("Getting the value with key 1.");
try {
String value = D.get(1);
if (value == "one") {
System.out.println("Value was correctly reported to be one.");
} else {
System.out.println("Value was incorrectly reported.");
};
} catch (NoSuchElementException e) {
System.out.println("get incorrectly threw a NotFoundException.");
};
System.out.println("");
System.out.println("Checking whether result is an AVL tree.");
AVLUtilities<Integer, String> tester =
new AVLUtilities<Integer, String>();
boolean isAVL = tester.isAVLTree(D, true);
if (isAVL) {
System.out.println("This is an AVL tree.");
} else {
System.out.println("This is not an AVL tree.");
};
System.out.println("");
System.out.println("Removing the node with key 20.");
try {
String value = D.remove(20);
if (value == "twenty") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was twenty.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" twenty.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Removing the node with key 1.");
try {
String value = D.remove(1);
if (value == "one") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was one.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" one.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Checking whether result is an AVL tree.");
isAVL = tester.isAVLTree(D, true);
if (isAVL) {
System.out.println("This is an AVL tree.");
} else {
System.out.println("This is not an AVL tree.");
};
System.out.println("");
System.out.println("Removing the node with key 19.");
try {
String value = D.remove(19);
if (value == "nineteen") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was nineteen.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" nineteen.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Removing the node with key 2.");
try {
String value = D.remove(2);
if (value == "two") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was two.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" two.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Checking whether result is an AVL tree.");
isAVL = tester.isAVLTree(D, true);
if (isAVL) {
System.out.println("This is an AVL tree.");
} else {
System.out.println("This is not an AVL tree.");
};
System.out.println("");
System.out.println("Removing the node with key 18.");
try {
String value = D.remove(18);
if (value == "eighteen") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was eighteen.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" eighteen.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Removing the node with key 3.");
try {
String value = D.remove(3);
if (value == "three") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was three.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" three.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Checking whether result is an AVL tree.");
isAVL = tester.isAVLTree(D, true);
if (isAVL) {
System.out.println("This is an AVL tree.");
} else {
System.out.println("This is not an AVL tree.");
};
System.out.println("");
System.out.println("Removing the node with key 17.");
try {
String value = D.remove(17);
if (value == "seventeen") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was seventeen.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" seventeen.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Removing the node with key 4.");
try {
String value = D.remove(4);
if (value == "four") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was four.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" four.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Checking whether result is an AVL tree.");
isAVL = tester.isAVLTree(D, true);
if (isAVL) {
System.out.println("This is an AVL tree.");
} else {
System.out.println("This is not an AVL tree.");
};
System.out.println("");
System.out.println("Removing the node with key 16.");
try {
String value = D.remove(16);
if (value == "sixteen") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was sixteen.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" sixteen.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Removing the node with key 5.");
try {
String value = D.remove(5);
if (value == "five") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was five.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" five.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Checking whether result is an AVL tree.");
isAVL = tester.isAVLTree(D, true);
if (isAVL) {
System.out.println("This is an AVL tree.");
} else {
System.out.println("This is not an AVL tree.");
};
System.out.println("");
System.out.println("Removing the node with key 15.");
try {
String value = D.remove(15);
if (value == "fifteen") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was fifteen.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" fifteen.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Removing the node with key 6.");
try {
String value = D.remove(6);
if (value == "six") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was six.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" six.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Checking whether result is an AVL tree.");
isAVL = tester.isAVLTree(D, true);
if (isAVL) {
System.out.println("This is an AVL tree.");
} else {
System.out.println("This is not an AVL tree.");
};
System.out.println("");
System.out.println("Removing the node with key 14.");
try {
String value = D.remove(14);
if (value == "fourteen") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was fourteen.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" fourteen.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Removing the node with key 7.");
try {
String value = D.remove(7);
if (value == "seven") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was seven.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" seven.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Checking whether result is an AVL tree.");
isAVL = tester.isAVLTree(D, true);
if (isAVL) {
System.out.println("This is an AVL tree.");
} else {
System.out.println("This is not an AVL tree.");
};
System.out.println("");
System.out.println("Removing the node with key 13.");
try {
String value = D.remove(13);
if (value == "thirteen") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was thirteen.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" thirteen.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Removing the node with key 8.");
try {
String value = D.remove(8);
if (value == "eight") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was eight.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" eight.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Checking whether result is an AVL tree.");
isAVL = tester.isAVLTree(D, true);
if (isAVL) {
System.out.println("This is an AVL tree.");
} else {
System.out.println("This is not an AVL tree.");
};
System.out.println("");
System.out.println("Removing the node with key 12.");
try {
String value = D.remove(12);
if (value == "twelve") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was twelve.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" twelve.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Removing the node with key 9.");
try {
String value = D.remove(9);
if (value == "nine") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was nine.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" nine.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Checking whether result is an AVL tree.");
isAVL = tester.isAVLTree(D, true);
if (isAVL) {
System.out.println("This is an AVL tree.");
} else {
System.out.println("This is not an AVL tree.");
};
System.out.println("");
System.out.println("Removing the node with key 11.");
try {
String value = D.remove(11);
if (value == "eleven") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was eleven.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" eleven.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Removing the node with key 10.");
try {
String value = D.remove(10);
if (value == "ten") {
System.out.print("remove correctly reported that the");
System.out.println(" associated value was ten.");
} else {
System.out.print("remove incorrectly reported that the");
System.out.print(" associated value was different than");
System.out.println(" ten.");
}
} catch (NoSuchElementException e) {
System.out.println("Remove incorrectly threw an exception.");
};
System.out.println("");
System.out.println("Checking whether result is an AVL tree.");
isAVL = tester.isAVLTree(D, true);
if (isAVL) {
System.out.println("This is an AVL tree.");
} else {
System.out.println("This is not an AVL tree.");
};
System.out.println("");
System.out.println("Checking whether this is now an empty tree.");
if (D.root() == null) {
System.out.println("The tree is now empty, as required.");
} else {
System.out.println("The tree is, incorrectly, still nonempty.");
};
System.out.println("");
}
}