-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWAVLTree
More file actions
562 lines (490 loc) · 11 KB
/
WAVLTree
File metadata and controls
562 lines (490 loc) · 11 KB
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/**
*
* WAVLTree
*
* An implementation of a WAVL Tree with distinct integer keys and info
*
*/
public class WAVLTree {
private WAVLNode root;
private int size;
private WAVLNode min;
private WAVLNode max;
private int pos;
public WAVLTree() {} // Constructor of an empty tree with null WABLNode as a root
public WAVLNode getRoot() {
return root;
}
public String toString() {
return "Tree with root " + root.toString();
}
/**
* public boolean empty()
*
* returns true if and only if the tree is empty
*
*/
public boolean empty() {
return root == null ? true : false;
}
/**
* public String min()
*
* Returns the info of the item with the smallest key in the tree, or null if
* the tree is empty
*/
public String min() {
return empty() ? null : min.getInfo();
}
/**
* public String max()
*
* Returns the info of the item with the largest key in the tree, or null if
* the tree is empty
*/
public String max() {
return empty() ? null : max.getInfo();
}
/**
* public int size()
*
* Returns the number of nodes in the tree.
*
* precondition: none postcondition: none
*/
public int size() {
return size;
}
/**
* public String search(int k)
*
* returns the info of an item with key k if it exists in the tree
* otherwise, returns null
*/
public String search(int k) {
WAVLNode x = root;
while (x.rank != -1) {
if (x.key == k) {
return x.info;
} else if (x.key > k) {
x = x.left;
} else {
x = x.right;
}
}
return null;
}
/**
* public String search(int k)
*
* returns the info of an item with key k if it exists in the tree
* otherwise, returns null
*/
public WAVLNode treePosition(int k) {
WAVLNode x = root;
WAVLNode y = new WAVLNode();
while (x.rank != -1) {
y = x;
if (x.key == k) {
return x;
} else if (x.key > k) {
x = x.left;
} else {
x = x.right;
}
}
return y;
}
public WAVLNode successor(WAVLNode x) {
return max == x ? null : x.successor();
}
public WAVLNode predecessor(WAVLNode x) {
return min == x ? null : x.predecessor();
}
/**
* public int insert(int k, String i)
*
* inserts an item with key k and info i to the WAVL tree. the tree must
* remain valid (keep its invariants). returns the number of rebalancing
* operations, or 0 if no rebalancing operations were necessary. returns -1
* if an item with key k already exists in the tree.
*/
public int insert(int k, String i) {
if (this.empty()) {
return startTree(new WAVLNode(k,i));
}
WAVLNode y = this.treePosition(k);
if (y.key == k) {
System.out.println("key is already in Tree!");
return -1;
}
WAVLNode z = new WAVLNode(k, i);
z.insertAsSon(y);
insMaintainTreeFields(z);
return balance(y); // to be replaced by student code
}
private int balance(WAVLNode x) {
boolean notBalanced = true;
WAVLNode currNode = x;
int numOfSteps = 0;
while (notBalanced) {
if (currNode.isCase01()) {
numOfSteps += currNode.parent.promote();
currNode = currNode.parent;
}else if(currNode.isCase02()){
if (currNode.isCase0212()){
}else if(currNode.isCase0221()){
}
}
else{
notBalanced=false;
}
}
return numOfSteps;
}
public int startTree(WAVLNode first){
min=max=root=first;
size++;
return 0;
}
public void insMaintainTreeFields(WAVLNode newNode){
if (newNode.key<min.key){
min=newNode;
}
else if(newNode.key>max.key){
max=newNode;
}
size++;
}
public int delete(int k) {
WAVLNode x = treePosition(k);
if (x.getKey() != k) {
return -1;
//k is in tree and x is indeed the node to be deleted
}
if (size == 1) {
resetTree();
return 0;
}
if (!x.isBinary()) {
if (x!=root){
x.deleteNonBin();
}
delMaintainTreeFields(x);
} else {
WAVLNode successor=x.successor();
x.replace(successor);
if (x==root){
root=successor;
}
}
size--;
return 42;
}
//to be noticed- deletion of root is responsibility of delMaintainTreeFields.NonBin does not delete root!
public void delMaintainTreeFields(WAVLNode x){
if (x == max) {
max = predecessor(x);
} else if (x == min) {
min = successor(x);
}if (x==root){
root=x.onlySon();
}
}
/**
* public int delete(int k)
*
* deletes an item with key k from the binary tree, if it is there; the tree
* must remain valid (keep its invariants). returns the number of
* rebalancing operations, or 0 if no rebalancing operations were needed.
* returns -1 if an item with key k was not found in the tree.
*/
public void resetTree(){
size=0;
max=min=root=null;}
/**
* public int[] keysToArray()
*
* Returns a sorted array which contains all keys in the tree, or an empty
* array if the tree is empty.
*/
public void keyAppend(WAVLNode x, int index, int[] array) {
if (array.length>0 && x.rank != -1 ) {
keyAppend(x.left, index, array);
array[pos++] = x.key;
keyAppend(x.right, index, array);
}
}
public void infoAppend(WAVLNode x, int index, String[] array) {
if (array.length>0 && x.rank != -1) {
infoAppend(x.left, index, array);
array[pos++] = x.getInfo();
infoAppend(x.right, index, array);
}
}
public int[] keysToArray() {
int[] arr = new int[size];
keyAppend(root, 0, arr);
pos = 0;
return arr; // to be replaced by student code
}
/**
* public String[] infoToArray()
*
* Returns an array which contains all info in the tree, sorted by their
* respective keys, or an empty array if the tree is empty.
*/
public String[] infoToArray() {
String[] arr = new String[size];
infoAppend(root, 0, arr);
pos = 0;
return arr;
}
/**
* public int rotate(WAVLNode x, String dir)
* rotates a WAVLNode x to direction dir.
* The method assumes such node exists in the tree.
* It returns the number 1 for further balancing operations calculations.
*/
public int rotate(WAVLNode x, String dir) {
WAVLNode y = x.parent;
WAVLNode a = x.left;
WAVLNode b = x.right;
WAVLNode c;
if (x.isRightChild()) {
c = y.left;
} else {
c = y.right;
}
if (y != root) {
if (y.isRightChild()) {
y.parent.right = x;
} else {
y.parent.left = x;
}
x.parent = y.parent;
} else {
root = x;
}
if (dir.equals("R")) {
y.left = b;
b.parent = y;
x.right = y;
} else {
y.right = a;
a.parent = y;
x.left = y;
}
y.parent = x;
a.promote();
x.promote();
c.demote();
y.demote();
return 1;
}
/**
* public int doubleRotate(WAVLNode x, String dir)
* Rotate the WAVLNode x twice to the same direction dir
* Assumes x indeed exists
*/
public int doubleRotate(WAVLNode x, String dir) {
return rotate(x, dir) + rotate(x, dir);
}
/**
* public class WAVLNode
*
* If you wish to implement classes other than WAVLTree (for example
* WAVLNode), do it in this file, not in another file. This is an example
* which can be deleted if no such classes are necessary.
*/
public class WAVLNode {
private int key;
private String info;
private int rank;
private WAVLNode parent = null;
private WAVLNode left = null;
private WAVLNode right = null;
public WAVLNode() { rank = -1; } // Constructor of an external leaf
public void deleteNonBin(){
WAVLNode son = onlySon();
son.parent = parent;
if (parent.right == this) {
parent.right = son;
} else {
parent.left = son;
}
}
public WAVLNode(int newKey, String newValue) {
key = newKey;
info = newValue;
left = new WAVLNode();
left.setParent(this);
right = new WAVLNode();
right.setParent(this);
}
public void insertAsSon(WAVLNode newParent){
parent=newParent;
String dir;
if (key < newParent.key) {
newParent.left=this;
dir="left";
} else {
newParent.right=this;
dir="right";
}
System.out.println(this.key+" inserted successfully as "+dir+" son of "+newParent.key);
}
public WAVLNode min() {
if (rank == -1) {
return parent;
} else {
return left.min();
}
}
public WAVLNode max() {
if (rank == -1) {
return parent;
} else {
return right.max();
}
}
public WAVLNode successor() {
WAVLNode x = this;
if (right.rank != -1) {
return right.min();
}
WAVLNode y = parent;
while (y.right == this && y != null) {
x = y;
y = x.parent;
System.out.println("y is " + y + " " + y.parent);
}
return y;
}
public WAVLNode predecessor() {
WAVLNode x = this;
if (left.rank != -1) {
return left.max();
}
WAVLNode y = parent;
while (y.left == this && y != null) {
x = y;
y = x.parent;
}
return y;
}
public int promote() {
rank++;
return 1;
}
public int doublePromote() {
rank += 2;
return 2;
}
public int demote() {
rank--;
return 1;
}
public int doubleDemote() {
rank -= 2;
return 2;
}
public boolean isBinary() {
return right.rank != -1 && left.rank != -1;
}
public WAVLNode onlySon() {
if (left.rank != -1) {
return left;
} else {
return right;
}
}
public void replace(WAVLNode other) {
// Is successor left or right child
if (other == other.parent.left) {
other.parent.left = other.right; // there is no left child
} else {
other.parent.right = other.right;
}
// Replacement
System.out.println("replacing "+other.key+" with "+key);
other.parent = this.parent;
other.left = this.left;
other.right = this.right;
// Editing original parent and children
if (this.parent.left == this) {
this.parent.left = other;
} else {
this.parent.right = other;
}
this.left.parent = other;
this.right.parent = other;
}
public boolean isUnary() {
return (!isBinary() && !isLeaf());
}
public boolean isLeaf() {
return right.rank == -1 && left.rank == -1;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public int getKey() {
return key;
}
public String getInfo() {
return info;
}
public WAVLNode getParent() {
return parent;
}
public void setParent(WAVLNode parent) {
this.parent = parent;
}
public WAVLNode getLeft() {
return left;
}
public void setLeft(WAVLNode left) {
this.left = left;
}
public WAVLNode getRight() {
return right;
}
public void setRight(WAVLNode right) {
this.right = right;
}
public String toString() {
String result = "TreeNode " + key;
return result;
}
public boolean isRightChild() {
return parent.right == this;
}
public String postorder() {
String result = "";
if (rank == -1) {
return "";
} else {
result += left.postorder();
result += right.postorder();
result += toString();
}
return result;
}
public boolean isCase01() {
return (parent.rank-rank) + (parent.rank - parent.right.rank) == 1;
}
public boolean isCase02() {
return (parent.rank-rank) + (parent.rank - parent.right.rank) == 2;
}
public boolean isCase0212() {
return left.rank==1 && right.rank == 2;
}
public boolean isCase0221() {
return left.rank==2 && right.rank == 1;
}
}
}