-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVLUtilities.java
225 lines (177 loc) · 5.82 KB
/
AVLUtilities.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
package cpsc331.assignment2;
import java.util.NoSuchElementException;
import cpsc331.collections.Dictionary;
import cpsc331.assignment2.AVLDictionary;
import java.util.ArrayList;
/**
*
* Provides utilities that can be used to test implementations
* of AVLDictionary.
*
*/
public class AVLUtilities<K extends Comparable<K>, V> {
/**
*
* Checks whether an input AVLDictionary is a valid AVL tree.<br>
*
* A second Boolean input is used to indicate whether
* diagnostic messages should be printed if it is determined
* that this is not, in fact, an AVL tree
*
*/
public boolean isAVLTree (AVLDictionary<K, V> D, boolean verbose) {
boolean isBST = isBST(D, verbose);
boolean isBalanced = isBalanced(D, verbose);
return (isBST && isBalanced);
}
/**
*
* Checks whether an input AVLDictionary is a valid
* binary search tree.<br>
*
* A second Boolean input is used to indicate whether
* diagonstic messages should be printed if it is determined
* that input tree is not, in fact, a binary search tree
*
*/
public boolean isBST (AVLDictionary<K, V> D, boolean verbose) {
ArrayList<K> A = makeArray(D, verbose);
int length = A.size();
int i = 0;
while (i < length - 1) {
int result = (A.get(i)).compareTo(A.get(i+1));
if (result >= 0) {
return false;
};
i = i+1;
};
return true;
}
// Produces an ArrayList storing the keys in an input AVLDictionary.
//
// A second Boolean input is used as for isBST
//
// Based on the algorithm described in Tutorial Exercise #11
private ArrayList<K> makeArray
(AVLDictionary<K, V> D, boolean verbose) {
ArrayList<K> A = new ArrayList<K>();
if (D.root() != null) {
addToArray(D.root(), A, verbose);
};
return A;
}
// Adds the entries of a subtree with an input node to an
// input ArrayList.
//
// A second Boolean input is used as for isBST
//
// Based on the algorithm described in Tutorial Exercise #11 -
// but also includes checks to ensure that the ArrayList
// being produced is sorted
private void addToArray (AVLDictionary<K, V>.AVLNode x,
ArrayList<K> A, boolean verbose) {
if (x.left() != null) {
addToArray(x.left(), A, verbose);
int length = A.size();
int leftResult = (x.key()).compareTo(A.get(length - 1));
if ((leftResult <= 0) && verbose) {
System.out.print((A.get(length-1)).toString());
System.out.print(" is in the left subtree of ");
System.out.println(x.key().toString());
};
};
A.add(x.key());
int length = A.size();
if (x.right() != null) {
addToArray(x.right(), A, verbose);
int rightResult = (x.key()).compareTo(A.get(length));
if ((rightResult >= 0) && verbose) {
System.out.print(A.get(length).toString());
System.out.print(" is in the right subtree of ");
System.out.println(x.key().toString());
};
};
}
/**
*
* Checks whether all nodes in a (supposed) AVL tree are,
* indeed, balanced.<br>
*
* A second Boolean input is used to indicated whether
* diagonstic messages should be printed if it is determined
* that input tree is not, in fact, a binary search tree
*
*/
public boolean
isBalanced(AVLDictionary<K, V> D, boolean verbose) {
if (D.root() == null) {
return true;
} else {
return isSubtreeBalanced(D.root(), verbose);
}
}
// Slow method to correctly check the height of a non-null node
private int correctHeight(AVLDictionary<K, V>.AVLNode x) {
int leftHeight = -1;
if (x.left() != null) {
leftHeight = correctHeight(x.left());
};
int rightHeight = -1;
if (x.right() != null) {
rightHeight = correctHeight(x.right());
};
return Math.max(leftHeight, rightHeight) + 1;
}
private int correctBalance(AVLDictionary<K, V>.AVLNode x) {
int leftHeight = -1;
if (x.left() != null) {
leftHeight = correctHeight(x.left());
};
int rightHeight = -1;
if (x.right() != null) {
rightHeight = correctHeight(x.right());
};
return leftHeight - rightHeight;
}
// Slow method to correctly report the balance factor of a
// non-null node
//
// Checks whether all nodes in the subtree of a given node
// are, indeed, balanced - and that heights of nodes are
// correctly defined
//
// A second Boolean input, verbose, is as for isBalanced
//
private boolean isSubtreeBalanced
(AVLDictionary<K, V>.AVLNode x, boolean verbose) {
boolean leftBalanced = true;
if (x.left() != null) {
leftBalanced = isSubtreeBalanced(x.left(), verbose);
};
boolean rightBalanced = true;
if (x.right() != null) {
rightBalanced = isSubtreeBalanced(x.right(), verbose);
};
boolean correctHeight = true;
if (correctHeight(x) != x.height()) {
correctHeight = false;
};
if ((!correctHeight) && verbose) {
System.out.print("Height of node with key ");
System.out.print(x.key().toString());
System.out.println(" is incorrect.");
};
int balanceFactor = correctBalance(x);
boolean nodeBalanced = (((balanceFactor == 1)
|| (balanceFactor == 0)
|| (balanceFactor == -1))
&& correctHeight);
if ((!nodeBalanced) && verbose) {
System.out.print("Node with key ");
System.out.print(x.key().toString());
System.out.print(" has balance factor ");
System.out.println(balanceFactor);
};
return (leftBalanced && rightBalanced && nodeBalanced);
}
}