-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVLTree.java
467 lines (383 loc) · 12.1 KB
/
AVLTree.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
import java.util.HashSet;
import java.util.Stack;
// Class: Height balanced AVL Tree
// Binary Search Tree
public class AVLTree extends BSTree {
private AVLTree left, right; // Children.
private AVLTree parent; // Parent pointer.
private int height; // The height of the subtree
public AVLTree() {
super();
// This acts as a sentinel root node
// How to identify a sentinel node: A node with parent == null is SENTINEL NODE
// The actual tree starts from one of the child of the sentinel node !.
// CONVENTION: Assume right child of the sentinel node holds the actual root! and left child will always be null.
}
public AVLTree(int address, int size, int key) {
super(address, size, key);
this.height = 0;
}
// Implement the following functions for AVL Trees.
// You need not implement all the functions.
// Some of the functions may be directly inherited from the AVLTree class and nothing needs to be done for those.
// Remove the functions, to not override the inherited functions.
private AVLTree getRootSentinel() {
AVLTree cur = this;
while (cur.parent != null) {
cur = cur.parent;
}
return cur;
}
private int getHeight(AVLTree node)
{
if (node == null) {
return 0;
}
return node.height;
}
private int getBalance()
{
return getHeight(this.left) - getHeight(this.right);
}
private void updateHeight()
{
this.height = 1 + Math.max(getHeight(this.left), getHeight(this.right));
}
private AVLTree rightRotate()
{
AVLTree cur = this, leftChild = cur.left;
AVLTree parent = cur.parent;
cur.left = leftChild.right;
if (cur.left != null) {
cur.left.parent = cur;
}
leftChild.right = cur;
cur.parent = leftChild;
if (parent.left == cur) {
parent.left = leftChild;
leftChild.parent = parent;
} else {
parent.right = leftChild;
leftChild.parent = parent;
}
leftChild.right.updateHeight();
leftChild.updateHeight();
parent.updateHeight();
return leftChild;
}
private AVLTree leftRotate()
{
AVLTree cur = this, rightChild = cur.right;
AVLTree parent = cur.parent;
cur.right = rightChild.left;
if (cur.right != null) {
cur.right.parent = cur;
}
rightChild.left = cur;
cur.parent = rightChild;
if (parent.left == cur) {
parent.left = rightChild;
rightChild.parent = parent;
} else {
parent.right = rightChild;
rightChild.parent = parent;
}
rightChild.left.updateHeight();
rightChild.updateHeight();
parent.updateHeight();
return rightChild;
}
// Rebalance the tree and update the changed heights
private void rebalance()
{
AVLTree cur = this;
while (cur != null) {
cur.updateHeight();
if (cur.parent == null) {
return;
}
int balance = cur.getBalance();
if (balance > 1) {
if (cur.left.getBalance() >= 0) {
cur = cur.rightRotate();
} else {
cur.left.leftRotate();
cur = cur.rightRotate();
}
}
else if (balance < -1) {
if (cur.right.getBalance() > 0) {
cur.right.rightRotate();
cur = cur.leftRotate();
} else {
cur = cur.leftRotate();
}
}
cur = cur.parent;
}
}
@Override
public AVLTree Insert(int address, int size, int key)
{
/*if (!this.sanity()) {
System.out.println("Error");
}*/
AVLTree insertedNode = null;
AVLTree cur = this.getRootSentinel();
if (cur.right == null) {
cur.right = new AVLTree(address, size, key);
cur.right.parent = cur;
insertedNode = cur.right;
}
cur = cur.right;
while (insertedNode == null) {
if (key < cur.key || (key == cur.key && address < cur.address)) {
if (cur.left == null) {
cur.left = new AVLTree(address, size, key);
cur.left.parent = cur;
insertedNode = cur.left;
break;
} else {
cur = cur.left;
}
} else {
if (cur.right == null) {
cur.right = new AVLTree(address, size, key);
cur.right.parent = cur;
insertedNode = cur.right;
break;
} else {
cur = cur.right;
}
}
}
insertedNode.rebalance();
return insertedNode;
}
private AVLTree getMin() {
AVLTree cur = this;
while (cur.left != null) {
cur = cur.left;
}
return cur;
}
private boolean isLeft() {
return (this.parent.left == this);
}
private void assignValues(AVLTree cur) {
this.address = cur.address;
this.key = cur.key;
this.size = cur.size;
}
public boolean Delete(Dictionary e)
{
/*if (!this.sanity()) {
System.out.println("Error");
}*/
AVLTree cur = this.getRootSentinel().right;
if (cur == null) {
return false;
}
while (cur != null) {
if (cur.key == e.key && cur.address == e.address) {
if (cur.left == null && cur.right == null) {
if (cur.isLeft()) {
cur.parent.left = null;
} else {
cur.parent.right = null;
}
cur.parent.rebalance();
cur = null; // Making cur eligible for garbage constructor
return true;
}
else if (cur.right == null) {
if (cur.isLeft()) {
cur.parent.left = cur.left;
cur.left.parent = cur.parent;
} else {
cur.parent.right = cur.left;
cur.left.parent = cur.parent;
}
cur.parent.rebalance();
cur = null; // Making cur eligible for garbage constructor
return true;
}
else { // cur.left != null and cur.right != null
AVLTree succ = cur.right.getMin();
cur.assignValues(succ);
succ.assignValues((AVLTree)e);
cur = succ;
}
}
else if (e.key < cur.key || (e.key==cur.key && e.address < cur.address)) {
cur = cur.left;
}
else {
cur = cur.right;
}
}
return false;
}
@Override
public AVLTree Find(int k, boolean exact)
{
AVLTree cur = this.getRootSentinel().right;
AVLTree ans = null;
while (cur != null) {
if (cur.key == k) {
if (ans == null) {
ans = cur;
}
else if (ans.key > k) {
ans = cur;
}
else if (ans.address > cur.address) {
ans = cur;
}
cur = cur.left;
}
else if (cur.key < k) {
cur = cur.right;
}
else {
if (!exact) {
if (ans == null) {
ans = cur;
}
else if (ans.key > cur.key) {
ans = cur;
}
else if (ans.key == cur.key && ans.address > cur.address) {
ans = cur;
}
cur = cur.left;
}
}
}
return ans;
}
@Override
public AVLTree getFirst()
{
AVLTree cur = this.getRootSentinel().right;
while (cur != null && cur.left != null) {
cur = cur.left;
}
return cur;
}
@Override
public AVLTree getNext()
{
if (this.parent == null) {
return null;
}
if (this.right != null) {
return this.right.getMin();
}
AVLTree cur = this;
while (cur.parent != null && !cur.isLeft()) {
cur = cur.parent;
}
if (cur.parent != null) {
return cur.parent;
}
return null;
}
// If root sentinel exist, then, return root sentinel, otherwise, return null
private AVLTree rootExist()
{
HashSet<AVLTree> h = new HashSet<AVLTree>();
// Find root sentinel and check if this path from root to "this" is cycle or not
AVLTree cur = this;
while (cur.parent != null) {
if (h.contains(cur)) {
return null;
}
h.add(cur);
cur = cur.parent;
}
return cur;
}
// Function to check cycle in subtree rooted at "this"
private boolean containCycle()
{
HashSet<AVLTree> h = new HashSet<AVLTree>();
Stack<AVLTree> s = new Stack<AVLTree>();
s.push(this);
// DFS (preorder traversal)
while (!s.empty()) {
AVLTree cur = s.pop();
if (h.contains(cur)) {
return true;
}
h.add(cur);
if (cur.right != null) {
s.push(cur.right);
}
if (cur.left != null) {
s.push(cur.left);
}
}
return false;
}
// Function to check BST order property and BST structural property, AVL properties
private boolean checkBST()
{
if (this.height != 1+Math.max(getHeight(this.left), getHeight(this.right))) {
return false;
}
if (Math.abs(this.getBalance()) > 1 && this.parent != null) {
return false;
}
if (this.left != null) {
if (this.left.parent != this) {
return false;
}
if (this.left.key > this.key || (this.left.key == this.key && this.left.address > this.address)) {
return false;
}
if (!this.left.checkBST()) {
return false;
}
}
if (this.right != null) {
if (this.right.parent != this) {
return false;
}
if (this.right.key < this.key || (this.right.key == this.key && this.right.address < this.address)) {
return false;
}
if (!this.right.checkBST()) {
return false;
}
}
return true;
}
@Override
public boolean sanity()
{
AVLTree root = this.rootExist();
if (root == null) {
return false;
}
// INV :- root.left == null
if (root.left != null) {
return false;
}
// INV :- empty tree
if (root.right == null) {
return true;
}
// INV :- BST does not contains cycle
if (root.right.containCycle()) {
return false;
}
// INV :- BST follows structure and order property and AVL properties
// Order property :- keys in left subtree <= root.key < key in right subtree
// Structural property :- node.left.parent == node and node.right.parent == node
if (!root.right.checkBST()) {
return false;
}
return true;
}
}