-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVLNode.java
332 lines (285 loc) · 8.94 KB
/
AVLNode.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
/*
Course: Comp 282
Semester: Summer 2016
Assignment: AVLMap
FileName: AVLNode.java
Author: Piechota, Michael
*/
import java.util.ArrayList;
public class AVLNode<K extends Comparable<K>, V> implements Comparable<K>
{
private int height;
private K key;
private V value;
private AVLNode<K, V> left;
private AVLNode<K, V> right;
public AVLNode(K k)
{
key = k;
}
public AVLNode(K k, V v)
{
key = k;
value = v;
}
public int getHeight()
{
return height;
}
public void setHeight(int height)
{
this.height = height;
}
public K getKey()
{
return key;
}
public void setKey(K key)
{
this.key = key;
}
public V getValue()
{
return value;
}
public void setValue(V value)
{
this.value = value;
}
public AVLNode<K, V> getLeft()
{
return left;
}
public void setLeft(AVLNode<K, V> left)
{
this.left = left;
}
public AVLNode<K, V> getRight()
{
return right;
}
public void setRight(AVLNode<K, V> right)
{
this.right = right;
}
@Override
public boolean equals(Object obj)
{
return key.equals((AVLNode) obj);
}
@Override
public int compareTo(K o)
{
return key.compareTo(o);
}
@Override
public String toString()
{
return "<" + key + ", " + value + ">";
}
//this method fixes the tree by restoring balance to the tree
public void fixMe(K o, AVLNode<K, V> rootArg) {
//create an array list called "path" of nodes
ArrayList<AVLNode<K, V>> path = new ArrayList<AVLNode<K, V>>();
AVLNode<K, V> current = rootArg; // start from the root
while (current != null)
{
path.add(current); //this adds a node to the list "path"
if (o.compareTo(current.getKey()) < 0)
{
current = current.getLeft();
}
else if (o.compareTo(current.getKey()) > 0)
{
current = current.getRight();
}
else
{
break;
}
}
//"root" needed to pass the root from AVLMap class
AVLNode<K, V> root = rootArg;
for (int i = path.size() - 1; i >= 0; i--) {
AVLNode<K, V> A = (AVLNode<K, V>) (path.get(i));
A.updateHeight(A);
AVLNode<K, V> parentOfA = (A == root) ? null :
(AVLNode<K, V>) (path.get(i - 1));
switch (A.nodeBalanceFactor(A))
{
case -2:
if(A.nodeBalanceFactor((AVLNode<K, V>) A.getLeft()) <= 0)
{ // Perform left left rotation
A.balanceLeftLeft(A, parentOfA, root);
}
else
{ // Perform a left right rotation
A.balanceLeftRight(A, parentOfA, root);
}
break;
case +2:
if (A.nodeBalanceFactor((AVLNode<K, V>)
A.getRight()) >= 0)
{
//do a right right rotation
A.balanceRightRight(A, parentOfA, root);
}
else //do a right left rotation
{
A.balanceRightLeft(A, parentOfA, root);
}
}
}
}
/*Finds the balance factor of a node which is defined to be
the height difference:
BalanceFactor(N) := –Height(LeftSubtree(N)) + Height(RightSubtree(N))*/
public int nodeBalanceFactor(AVLNode<K, V> node)
{
if (node.getRight() == null) //if the node has no right subtree
{
return -node.getHeight();
}
else if (node.getLeft() == null)//if the node has no left subtree
{
return +node.getHeight();
}
else
{
return ((AVLNode<K, V>) node.getRight()).getHeight()
- ((AVLNode<K, V>) node.getLeft()).getHeight();
}
}
/*performs a right right rotation */
private void balanceRightRight(AVLNode<K, V> A_rightHeavy,
AVLNode<K, V> parentOfA,
AVLNode<K, V> rootArg)
{
AVLNode<K, V> B_rightHeavy = A_rightHeavy.right;
AVLNode<K, V> root = rootArg;
if(A_rightHeavy == root)
{
root = B_rightHeavy;
}
else if(parentOfA.left == A_rightHeavy)
{
parentOfA.left = B_rightHeavy;
}
else
{
parentOfA.right = B_rightHeavy;
}
//B_rightHeavy is now the subtree of A_rightHeavy
A_rightHeavy.right = B_rightHeavy.left;
B_rightHeavy.left = A_rightHeavy;
updateHeight((AVLNode<K, V>) A_rightHeavy);
updateHeight((AVLNode<K, V>) B_rightHeavy);
}
/*performs a left left rotation */
public void balanceLeftLeft(AVLNode<K, V> A_leftHeavy,
AVLNode<K, V> parentOfA,
AVLNode<K, V> rootArg)
{
AVLNode<K, V> B_leftHeavy = A_leftHeavy.getLeft();
AVLNode<K, V> root = rootArg;
if (A_leftHeavy == root)
{
root = B_leftHeavy;
}
else if (parentOfA.getLeft() == A_leftHeavy)
{
parentOfA.setLeft(B_leftHeavy);
}
else
{
parentOfA.setRight(B_leftHeavy);
}
A_leftHeavy.setLeft(B_leftHeavy.getRight());
//now make A_leftHeavy the left child of B_leftHeavy
B_leftHeavy.setRight(A_leftHeavy);
A_leftHeavy.updateHeight((AVLNode<K, V>) A_leftHeavy);
A_leftHeavy.updateHeight((AVLNode<K, V>) B_leftHeavy);
}
/*
Performs a left right rotation
*/
public void balanceLeftRight(AVLNode<K, V> nodeA,
AVLNode<K, V> parentOfA,
AVLNode<K, V> rootArg)
{
AVLNode<K, V> nodeB = nodeA.getLeft(); // A is left-heavy
AVLNode<K, V> nodeC = nodeB.getRight(); // B is right-heavy
AVLNode<K, V> root = rootArg;
if (nodeA == root) {
root = nodeC;
} else if (parentOfA.getLeft() == nodeA) {
parentOfA.setLeft(nodeC);
} else {
parentOfA.setRight(nodeC);
}
//now change subtrees
nodeA.setLeft(nodeC.getRight());
nodeB.setRight(nodeC.getLeft());
nodeC.setLeft(nodeB);
nodeC.setRight(nodeA);
//now adjust the heights after the rotations
nodeA.updateHeight((AVLNode<K, V>) nodeA);
nodeB.updateHeight((AVLNode<K, V>) nodeB);
nodeC.updateHeight((AVLNode<K, V>) nodeC);
}
/*performs a right left rotation */
public void balanceRightLeft(AVLNode<K, V> nodeA,
AVLNode<K, V> parentOfA,
AVLNode<K, V> rootArg)
{
AVLNode<K, V> nodeB = nodeA.getRight(); // A is right-heavy
AVLNode<K, V> nodeC = nodeB.getLeft(); // B is left-heavy
AVLNode<K, V> root = rootArg;
if (nodeA == root)
{
root = nodeC;
}
else if (parentOfA.getLeft() == nodeA)
{
parentOfA.setLeft(nodeC);
}
else
{
parentOfA.setRight(nodeC);
}
nodeA.setRight(nodeC.getLeft());
nodeB.setLeft(nodeC.getRight());
nodeC.setLeft(nodeA);
nodeC.setRight(nodeB);
// Adjust heights
nodeA.updateHeight((AVLNode<K, V>) nodeA);
nodeB.updateHeight((AVLNode<K, V>) nodeB);
nodeC.updateHeight((AVLNode<K, V>) nodeC);
}
public void updateHeight(AVLNode<K, V> node)
{
//check if the node is a leaf
if (node.getLeft() == null && node.getRight() == null)
{
node.setHeight(0);
}
//check if the node has no left subtree
else if (node.getLeft() == null)
{
node.setHeight(1 + ((AVLNode<K, V>)
(node.getRight())).getHeight());
}
//check if the node has no right subtree
else if (node.getRight() == null)
{
node.setHeight(1 + ((AVLNode<K, V>)
(node.getLeft())).getHeight());
}
else
{
node.setHeight(1 + Math.max(((AVLNode<K, V>)
(node.getRight())).getHeight(), ((AVLNode<K, V>)
(node.getLeft())).getHeight()));
}
}
}