-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVLTree.java
468 lines (439 loc) · 14.8 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
466
467
468
import java.util.HashSet;
// 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 BSTree class and nothing needs to be done for those.
// Remove the functions, to not override the inherited functions.
public AVLTree Insert(int address, int size, int key)
{
AVLTree newnode = new AVLTree(address, size, key);
AVLTree curr = this.getRoot();
int comp=0;
if (curr ==null){
this.right = newnode;
newnode.parent = this;
return newnode;
}
while (true){
comp = curr.compareNode(newnode);
if (comp ==0) return null;
else if (comp==-1){
if (curr.right != null) curr = curr.right;
else{
curr.right = newnode;
newnode.parent = curr;
break;
}
}
else{
if (curr.left != null) curr = curr.left;
else{
curr.left = newnode;
newnode.parent = curr;
break;
}
}
}
// Newnode is inserted and now we have to update heights of all it's parents
newnode.updateheight();
// Done
return newnode;
}
private void updateheight(){
AVLTree curr = this;
int l=-1, r=-1;
while(curr.parent!=null){
l=-1;
r=-1;
if (curr.left!=null) l = curr.left.height;
if (curr.right!=null) r = curr.right.height;
if (l>r) curr.height = l+1;
else curr.height = r+1;
// Now we have to check whether height property is getting violated or not
if (l>r+1 || r>l+1){
// We will rebalance it (rotation or quiz method) and then update the heights.
// x,y,z can be easily obtained if z is known
curr = curr.rebalance();
}
curr =curr.parent;
}
}
private AVLTree getBig(){
// This fnction returns the child with more height
if (this.left==null && this.right==null) return null;
else if (this.right==null) return this.left;
else if (this.left==null) return this.right;
else{
if (this.left.height>this.right.height) return this.left;
else if (this.left.height<this.right.height) return this.right;
else{
if (this.parent!=null && this.parent.left==this) return this.left;
else return this.right;
}
}
}
private AVLTree rebalance(){
AVLTree Allfather = this.parent;
AVLTree x,y,z= this;
AVLTree[] xyz = new AVLTree[3];
AVLTree[] Ts = new AVLTree[4];
// Make x and y using the fact that they are the children's with bigger heights;
y= z.getBig();
x= y.getBig();
// Make the sorted xyz[] and Ts[] array by considering 4 cases;
if (y==z.left && x==y.left){
xyz[0] =x;
xyz[1] =y;
xyz[2] =z;
Ts[0] =x.left;
Ts[1] =x.right;
Ts[2] =y.right;
Ts[3] =z.right;
}
else if (y==z.left && x==y.right){
xyz[0] =y;
xyz[1] =x;
xyz[2] =z;
Ts[0] =y.left;
Ts[1] =x.left;
Ts[2] =x.right;
Ts[3] =z.right;
}
else if (y== z.right && x== y.left){
xyz[0] =z;
xyz[1] =x;
xyz[2] =y;
Ts[0] =z.left;
Ts[1] =x.left;
Ts[2] =x.right;
Ts[3] =y.right;
}
else{
xyz[0] =z;
xyz[1] =y;
xyz[2] =x;
Ts[0] =z.left;
Ts[1] =y.left;
Ts[2] =x.left;
Ts[3] =x.right;
}
// Do the labour of assigning pointers
if (Allfather.left!= null && Allfather.left.compareNode(z)==0) Allfather.left = xyz[1];
else if (Allfather.right!= null && Allfather.right.compareNode(z)==0) Allfather.right = xyz[1];
xyz[1].parent = Allfather;
xyz[1].left=(xyz[0]);
xyz[0].parent = xyz[1];
xyz[1].right = (xyz[2]);
xyz[2].parent = xyz[1];
xyz[0].right = (null);
xyz[0].left=(null);
xyz[2].left=(null);
xyz[2].right = (null);
for (int i=0;i<4;i++){
if (Ts[i]==null) continue;
if (i%2==0){
if(i<=1) xyz[0].left = Ts[i];
else xyz[2].left=Ts[i];
}
else{
if(i<=1) xyz[0].right = Ts[i];
else xyz[2].right=Ts[i];
}
if(i<=1) Ts[i].parent = xyz[0];
else Ts[i].parent = xyz[2];
}
// Do the labour of assigning heihgts;
int l,r;
l=-1;
r=-1;
if (xyz[0].left!=null) l= xyz[0].left.height;
if (xyz[0].right!=null) r= xyz[0].right.height;
if (l>r) xyz[0].height=l+1;
else xyz[0].height = r+1;
l=-1;
r=-1;
if (xyz[2].left!=null) l= xyz[2].left.height;
if (xyz[2].right!=null) r= xyz[2].right.height;
if (l>r) xyz[2].height=l+1;
else xyz[2].height = r+1;
l=-1;
r=-1;
if (xyz[1].left!=null) l= xyz[1].left.height;
if (xyz[1].right!=null) r= xyz[1].right.height;
if (l>r) xyz[1].height=l+1;
else xyz[1].height = r+1;
return xyz[1];
}
public boolean Delete(Dictionary e)
{
AVLTree curr = this.getRoot();
int comp;
int found = 0;
while(curr !=null){
comp = curr.compareDict(e);
if (comp ==0){
found = 1; // now I am gonna delete this node
break;
}
else if(comp ==-1){
curr = curr.right;
}
else{
curr = curr.left;
}
}
if (found==0) return false;
// Now i got reference to the node which must be deleted
// I must delete that node and store a reference to the parent of required leaf in start.
AVLTree start;
// The case when the node to be deleted is a leaf node
if (curr.left==null && curr.right ==null){
if (curr.parent.left!= null && curr.parent.left.compareNode(curr)==0){
curr.parent.left = null;
start = curr.parent;
curr.parent =null;
}
else{
curr.parent.right = null;
start = curr.parent;
curr.parent =null;
}
}
// When it has only one child
else if (curr.left==null || curr.right==null){
start = curr;
if (curr.left==null) curr = curr.right;
else curr = curr.left;
start.address = curr.address;
start.key= curr.key;
start.size= curr.size;
if (curr.parent.left!= null && curr.parent.left.compareNode(curr)==0){
curr.parent.left = null;
curr.parent =null;
}
else{
curr.parent.right = null;
curr.parent =null;
}
}
// When it has both child
else{
AVLTree temp = curr;
curr = curr.getNext();
temp.address =curr.address;
temp.key = curr.key;
temp.size = curr.size;
// deletingg the successor node ie curr node
if (curr.right ==null){
if (curr.parent.left!= null && curr.parent.left.compareNode(curr)==0){
curr.parent.left = null;
start = curr.parent;
curr.parent =null;
}
else{
curr.parent.right = null;
start = curr.parent;
curr.parent =null;
}
}
else {
start = curr;
curr = curr.right;
start.address = curr.address;
start.key= curr.key;
start.size= curr.size;
if (curr.parent.left!= null && curr.parent.left.compareNode(curr)==0){
curr.parent.left = null;
curr.parent =null;
}
else{
curr.parent.right = null;
curr.parent =null;
}
}
}
// Now I will just start the update height process on start node.
start.updateheight();
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public AVLTree Find(int key, boolean exact)
{
AVLTree elem = null;
if (exact ==true){
AVLTree curr = this.getRoot();
while(curr!=null){
if (curr.key == key){
elem = curr;
curr = curr.left;
}
else if (curr.key>key){
curr = curr.left;
}
else{
curr = curr.right;
}
}
}
else{
AVLTree curr = this.getRoot();
while(curr!=null){
if (curr.key >= key){
elem = curr;
curr = curr.left;
}
else if (curr.key<key){
curr = curr.right;
}
}
}
if (elem ==null) return null;
AVLTree duplicate = new AVLTree(elem.address, elem.size, elem.key);
return duplicate;
}
public AVLTree getFirst(){
AVLTree curr = this.getRoot();
if (this.getRoot()==null) return null;
while(curr.left!=null){
curr = curr.left;
}
return curr;
}
public AVLTree getNext()
{
// We will not go left since it has already been analysed
AVLTree curr = this;
if (curr.right == null){
// Now I am ready to go up till I find a node just successor of "this" node
while (curr.parent!=null){
if (curr.compareNode(this)==1) return curr;
else curr = curr.parent;
}
return null;
}
else{
// Must finish my business in the Right first
curr = curr.right;
while(curr.left !=null){
curr = curr.left;
}
return curr;
}
}
public boolean sanity(){
if (nothasloop(this,null)==false){
System.out.println("It has a loop");
return false;
}
s.clear();
if (check_height(this.getRoot())==false){
System.out.println("The height property is violated");
return false;
}
if (check_BST(this.getRoot())==false){
System.out.println("The BST property is violated");
return false;
}
if (checkpointers(this.getRoot())==false){
System.out.println("the pointers are not correctly defined");
return false;
}
return true;
}
private static HashSet <AVLTree> s= new HashSet<AVLTree>();
private boolean nothasloop(AVLTree n, AVLTree prev){
if (n==null) return true;
if (s.contains(n)) return false;
else{
s.add(n);
boolean l=true,r=true,p=true;
if (n.left!=null && n.left!=prev) l = nothasloop(n.left, n);
if (n.right!=null && n.right!=prev) r = nothasloop(n.right, n);
if (n.parent!=null && n.parent!=prev) p = nothasloop(n.parent, n);
return l && r && p;
}
}
private boolean check_height(AVLTree n){
if (n==null) return true;
int l=-1;
int r=-1;
if (n.left!=null) l= n.left.height;
if (n.right!=null) r = n.right.height;
if (l>r+1 || r>l+1) return false;
if (l>r && n.height!=l+1) return false;
if (l<=r && n.height!=r+1) return false;
else return check_height(n.left) && check_height(n.right);
}
private boolean check_BST(AVLTree n){
if (n== null) return true;
else if (n.left !=null && n.compareNode(n.left)!=1){
return false;
}
else if (n.right!=null && n.compareNode(n.right)!=-1){
return false;
}
else return check_BST(n.left) && check_BST(n.right);
}
private boolean checkpointers(AVLTree n){
// boolean l = true,r= true, p =true;
if (n== null) return true;
if (n.left!= null && n.left.parent!=n){
return false;
}
if (n.right!=null && n.right.parent!=n){
return false;
}
return checkpointers(n.left) && checkpointers(n.right);
}
private AVLTree getRoot(){
// returns the actual root of the tree, not the sentinel one.
AVLTree curr = this ;
if (curr.parent ==null){
return curr.right;
}
while (curr.parent.parent != null){
curr = curr.parent;
}
return curr;
}
private int compareNode(AVLTree d){
AVLTree b = this;
if (b.key != d.key){
if (b.key>d.key) return 1;
else return -1;
}
else {
if (b.address> d.address) return 1;
else if (b.address == d.address) return 0;
else return -1;
}
}
private int compareDict(Dictionary d){
AVLTree b = this;
if (b.key != d.key){
if (b.key>d.key) return 1;
else return -1;
}
else {
if (b.address> d.address) return 1;
else if (b.address == d.address) return 0;
else return -1;
}
}
}