-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVLTest4.java
107 lines (95 loc) · 3.75 KB
/
AVLTest4.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
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 AVLTest4 {
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 15 and value fifteen.");
D.set(15, "fifteen");
System.out.println("Inserting key 8 and value eight.");
D.set(8, "eight");
System.out.println("Inserting key 20 and value twenty.");
D.set(20, "twenty");
System.out.println("Inserting key 5 and value five.");
D.set(5, "five");
System.out.println("Inserting key 12 and value twelve.");
D.set(12, "twelve");
System.out.println("Inserting key 18 and value eighteen.");
D.set(18, "eighteen");
System.out.println("Inserting key 22 and value twenty-two.");
D.set(22, "twenty-two");
System.out.println("Inserting key 3 and value three.");
D.set(3, "three");
System.out.println("Inserting key 7 and value seven.");
D.set(7, "seven");
System.out.println("Inserting key 10 and value ten.");
D.set(10, "ten");
System.out.println("Inserting key 14 and value fourteen.");
D.set(14, "fourteen");
System.out.println("Inserting key 17 and value seventeen.");
D.set(17, "seventeen");
System.out.println("Inserting key 19 and value nineteen.");
D.set(19, "nineteen");
System.out.println("Inserting key 21 and value twenty-one.");
D.set(21, "twenty-one");
System.out.println("Inserting key 2 and value two.");
D.set(2, "two");
System.out.println("Inserting key 4 and value four.");
D.set(4, "four");
System.out.println("Inserting key 6 and value six.");
D.set(6, "six");
System.out.println("Inserting key 9 and value nine.");
D.set(9, "nine");
System.out.println("Inserting key 11 and value eleven.");
D.set(11, "eleven");
System.out.println("Inserting key 13 and value thirteen.");
D.set(13, "thirteen");
System.out.println("Inserting key 16 and value sixteen.");
D.set(16, "sixteen");
System.out.println("Inserting key 1 and value one.");
D.set(1, "one");
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 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("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("");
};
}