-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWAVLTree.java
More file actions
845 lines (744 loc) · 21.3 KB
/
WAVLTree.java
File metadata and controls
845 lines (744 loc) · 21.3 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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
package project_m;
import java.util.Arrays;
import javax.management.openmbean.KeyAlreadyExistsException;
import javax.swing.text.TabableView;
/**
*
* WAVLTree
*
* An implementation of a WAVL Tree with
* distinct integer keys and info
*
*/
public class WAVLTree {
private int size;
// public static void main(String[] args){
// WAVLTree t = new WAVLTree();
// t.insert(58, "a");
// t.insert(7, "b");
// t.insert(160, "d");
// t.insert(899, "c");
// t.insert(56, "e");
// t.insert(2, "j");
// t.insert(1, "i");
// t.insert(90, "h");
// t.insert(89, "g");
// t.insert(7, "f");
// t.insert(45, "k");
// t.insert(3443534, "l");
// t.insert(100, "hh");
// t.delete(20);
// t.delete(45);
//// WAVLNode test = t.findNode(64);
//// test = t.replaceInnerNode(test);
//// t.deleteLeaf(test);
//
// t.delete(64);
// t.delete(15);
// t.delete(100);
// t.delete(89);
// t.delete(90);
// t.delete(7);
// t.delete(5);
// t.delete(1);
// t.delete(132);
// t.delete(2);
// t.insert(132,"h");
//// t.delete(100);
// WAVLNode x = t.findNode(3443534);
// //System.out.println(x.key);
//
// t.delete(3443534);
// //System.out.println(t.root == t.extLeaf);
// printBinaryTree(t.root, 0);
// System.out.println(Arrays.toString(t.infoToArray()));
// System.out.println(t.size);
//
// }
WAVLNode root; // here we DON'T have a sentinel - I had some doubts about this, and if you feel like we're better off with one - do tell me, it's fairly easy to create.
WAVLNode extLeaf; // External Leaf - only one
WAVLNode sentinel;
//-------------------------TREE BUILDER 1- empty-------------------------//
public WAVLTree(){
root = this.new WAVLNode();
extLeaf = this.new WAVLNode();
sentinel = this.new WAVLNode();
root.parent = sentinel;
sentinel.rank = -1005;
sentinel.right = root;
sentinel.left = root;
root.rank = -2;
extLeaf.rank = -1;
size=0;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Printing ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
public static void printBinaryTree(WAVLNode root, int level){
if(root==null)
return;
printBinaryTree(root.right, level+1);
if(level!=0){
for(int i=0;i<level-1;i++)
System.out.print("|\t");
System.out.println("|-------"+root.key + "(" + root.rank + ")");
}
else
System.out.println(root.key + "(" + root.rank + ")");
printBinaryTree(root.left, level+1);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Is Empty ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/**
* public boolean empty()
*
* returns true if and only if the tree is empty
*
*/
public boolean empty() { //O(1)
return root.rank == -2;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Search ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/**
* 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) //O(logn)
{
if (this.empty())
return null;
WAVLNode current = root;
while ((current != null)&&(current != extLeaf)){
if (current.key == k){
return current.info;
}
if (current.key > k)
current = current.left;
if (current.key < k)
current = current.right;
}
return null;
}
//-------------------------Help method- Insert to an empty tree-------------------------//
private void InsertFirst(int key, String info){ //O(1)
root.rank = 0;
root.info = info;
root.key = key;
root.parent = null;
root.left = extLeaf;
root.right = extLeaf;
}
//-------------------------Help method- insert to the place-------------------------//
private void insertInPlace(WAVLNode newnode){
WAVLNode current = root;
while(true)
{
if (current.key > newnode.key)
{ // go left
if (current.left == extLeaf)
{ // if this is a leaf;
current.left = newnode;
newnode.parent = current;
break;
}
else
current = current.left; // if we still have something to the left
}
if (current.key < newnode.key)
{
if (current.right == extLeaf)
{
current.right = newnode;
newnode.parent = current;
break;
}
else
current = current.right;
}
}
newnode.rank=0;
// newnode.parent.rank+=1;
newnode.left=newnode.right= extLeaf; //the node we added is a LEAF now!
}
//-------------------------Help method- A Leaf? -------------------------//
private boolean IsLeaf(WAVLNode n){
if (n.rank==0)
{
return true;
}
return false;
}
//-------------------------Help method- Which Node ("0-1", "1-1"...)- left first, right after? -------------------------//
private String DiffType(WAVLNode n){
String Nodetype="";
int leftdif=n.rank-n.left.rank;
int rightdif=n.rank-n.right.rank;
Nodetype+= Integer.toString(leftdif)+ Integer.toString(rightdif);
return Nodetype;
}
//-------------------------Help method- Legal-------------------------//
private boolean Islegal(WAVLNode n){
if((DiffType(n).equals("22"))||(DiffType(n).equals("11"))||(DiffType(n).equals("21"))||(DiffType(n).equals("12")))
{
return true;
}
return false;
}
//-------------------------Help method- Rotation right-------------------------//
private WAVLNode RotateRight(WAVLNode origin){
boolean check=false;
WAVLNode temp = null;
if(origin==root)
{
check=true;
}
else
temp = origin.parent;
WAVLNode other=origin.left;
origin.left=other.right;
other.right=origin;
Demote(origin);
if(origin.left.parent != extLeaf)
origin.left.parent = origin;
other.parent=origin.parent;
origin.parent=other;
if(check==true)
{
root=other;
root.parent = sentinel;
}
else{
if (temp.left == origin)
temp.left = other;
if (temp.right == origin)
temp.right = other;
}
return other;
}
//-------------------------Help method- Rotation left-------------------------//
private WAVLNode RotateLeft(WAVLNode origin){
boolean check=false;
WAVLNode temp = null;
if(origin==root)
{
check=true;
temp=sentinel;
}
else{
temp = origin.parent;
}
WAVLNode other=origin.right;
origin.right=other.left;
other.left=origin;
Demote(origin);
origin.right.parent = origin;
other.parent=origin.parent;
origin.parent=other;
if(check==true)
{
root=other;
root.parent = sentinel;
}
else{
if (temp.left == origin)
temp.left = other;
if (temp.right == origin)
temp.right = other;
}
return other;
}
//-------------------------Help method- Double Rotate Left-Right-------------------------//
private WAVLNode RotateLeftRight(WAVLNode origin){
boolean check = false;
WAVLNode temp = extLeaf;
if (origin == root)
{
check = true;
origin.parent = sentinel;
}
temp = origin.parent;
origin.left = RotateLeft(origin.left);
WAVLNode newNode = (RotateRight(origin));
if (temp.left == origin)
temp.left = newNode;
if (temp.right == origin)
temp.right = newNode;
Promote(newNode);
newNode.parent = temp;
if (check)
root = newNode;
return newNode;
}
//-------------------------Help method- Double Rotate Right-Left-------------------------//
private WAVLNode RotateRightLeft(WAVLNode origin){
boolean check = false;
WAVLNode temp = extLeaf;
if (origin == root)
{
check = true;
origin.parent = sentinel;
}
temp = origin.parent;
origin.right = RotateRight(origin.right);
WAVLNode newNode= (RotateLeft(origin));
if (temp.left == origin)
temp.left = newNode;
if (temp.right == origin)
temp.right = newNode;
Promote(newNode);
newNode.parent = temp;
if (check)
root = newNode;
return newNode;
}
//-------------------------Help method- Promote-------------------------//
private void Promote(WAVLNode n){
n.rank+=1;
}
//-------------------------Help method- Demote-------------------------//
private void Demote(WAVLNode n){
n.rank-=1;
}
//-------------------------Help method- INSERT Cases-------------------------//
private int insertCases(WAVLNode n){
WAVLNode pointer=n;
int counter=0;
while( ((pointer!=root)) && ( (DiffType(pointer.parent).equals("01") )||( DiffType(pointer.parent).equals("10")))) //Case1
{
Promote(pointer.parent);
pointer=pointer.parent;
counter++;
}
if((pointer==root)||(Islegal(pointer.parent)))//Case1 solve the problem
{
return counter;
}
else
{
while(pointer!=root)
{
if((DiffType(pointer.parent).equals("02")) & (DiffType(pointer).equals("12"))) // Case2
{
pointer= RotateRight(pointer.parent);
counter+=1; //1 rotate, without- 1 demote
return counter;
}
else if(DiffType(pointer.parent).equals("20") & DiffType(pointer).equals("12")) // Case2 symetric
{
pointer= RotateRightLeft(pointer.parent);
counter+=2; //2 rotates, without the inner- 2 demotes, 1 promote
return counter;
}
else if(DiffType(pointer.parent).equals("02") & DiffType(pointer).equals("21")) // Case3
{
pointer = RotateLeftRight(pointer.parent);
counter+=2; //2 rotates, without- 2 demotes, 1 promote
return counter;
}
else if(DiffType(pointer.parent).equals("20") & DiffType(pointer).equals("21")) // Case3 symetric
{
pointer= RotateLeft(pointer.parent);
counter+=1; //1 rotate, without- 1 demote
return counter;
}
}
return counter;
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Insert ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/**
* 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) {
size+=1;
int numOfBalance=0;
if(this.empty())
this.InsertFirst(k, i);
else if (this.search(k) != null) //already in the tree
{
size-=1;
return -1;
}
else
{
WAVLNode newnode= new WAVLNode(k,i); //insert to the tree- after this we'll update ranks
insertInPlace(newnode);
if (newnode.parent == root){
Promote(newnode.parent);
// System.out.println("when insert "+ k + " num of balance operations was 1");
return 1;
}
if(newnode.parent.rank!=0){ //No-Leaf Parent- you jast need to Insert. the ranks are update!
// System.out.println("when insert "+ k + " num of balance operations was 0");
return 0;
}
if(newnode.parent.rank-newnode.rank==0)
numOfBalance=insertCases(newnode);
}
// System.out.println("when insert "+ k + " num of balance operations was "+ numOfBalance);
return numOfBalance;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Delete ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/**
* 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 int delete(int k)
{
if (size == 0){ // empty tree
System.out.println("Deleting from empty WAVLTree!");
return -1;
}
WAVLNode current = findNode(k);
if (current == null){
System.out.println("No such key in WAVLTree!");
return -1;
}
if ((this.size == 2) && (current == root)){
if(current.left == extLeaf)
root = current.right;
if(current.right == extLeaf)
root = current.left;
root.parent = sentinel;
size = 1;
return 0;
}
if (size == 1) { // delete root.
System.out.println("WAVLTree is now empty.");
root = this.new WAVLNode();
root.rank = -2;
root.left = extLeaf;
root.right = extLeaf;
size = 0;
return 0;
}
if (!((current.left.rank == -1) && (current.right.rank == -1))) {
current = replaceInnerNode(current);
}
WAVLNode parent = current.parent;
if (((current.left.rank == -1) && (current.right.rank == -1))) {
deleteLeaf(current);
}
if (((current.left.rank == -1) || (current.right.rank == -1))) {
deleteUnary(current);
}
size--;
int rot = 0; // number of rotations
rot = deleteCases(parent);
return rot;
}
//-------------------------Help method- DELETE cases-------------------------//
private int deleteCases(WAVLNode current){
int reb = 0;
while (true)
{
if((DiffType(current).equals("22")) && (current.right==extLeaf) && (current.left==extLeaf))// if 2-2 && extleaf rec != root
{
Demote(current);
if(current==root)
return reb++;
else{
current = current.parent;
continue;
}
}
if(Islegal(current)) // if legal - return
return reb;
if((DiffType(current).equals("32"))||(DiffType(current).equals("23"))) // if 3-2/2-3 demote rec != root
{
Demote(current);
if(current==root)
return reb++;
else{
current = current.parent;
continue;
}
}
if((DiffType(current).equals("31"))&&(DiffType(current.right).equals("22"))) // if 3-1&&R2-2 DD rec notroot
{
Demote(current);
Demote(current.right);
if(current==root)
return reb+2;
else{
current = current.parent;
continue;
}
}
if((DiffType(current).equals("13"))&&(DiffType(current.left).equals("22"))) // // if 1-3&&L2-2 DD rec notroot- symetric
{
Demote(current);
Demote(current.left);
if(current==root)
return reb+2;
else{
current = current.parent;
continue;
}
}
if((DiffType(current).equals("31"))&&((DiffType(current.right).equals("21"))||(DiffType(current.right).equals("11")))) // if 3-1&&R$-1 RL ret
{
current = RotateLeft(current);
Promote(current);
WAVLNode temp=current.left;
if((DiffType(temp).equals("22")) && (temp.right==extLeaf) && (temp.left==extLeaf))
{
Demote(temp);
}
reb++;
return reb;
}
if((DiffType(current).equals("13"))&&((DiffType(current.left).equals("12"))||(DiffType(current.left).equals("11")))) // if 1-3&&L1-$ RR ret symetric
{
current = RotateRight(current);
Promote(current);
WAVLNode temp=current.right;
if((DiffType(temp).equals("22")) && (temp.right==extLeaf) && (temp.left==extLeaf))
{
Demote(temp);
}
reb++;
return reb;
}
if((DiffType(current).equals("31"))&&(DiffType(current.right).equals("12"))) // if 3-1&&R1-2 RRL ret
{
Demote(current);
current= RotateRightLeft(current);
Promote(current);
reb+=2;
return reb;
}
if((DiffType(current).equals("13"))&&(DiffType(current.left).equals("21"))) // if 1-3&&L2-1 RLR ret symetric
{
Demote(current);
current= RotateLeftRight(current);
Promote(current);
reb+=2;
return reb;
}
System.out.println("CASES EROR" );
}
}
//-------------------------Help method- like search, but returns a pointer.-------------------------//
private WAVLNode findNode(int k)
{
WAVLNode current = root;
while ((current != null)&&(current != extLeaf)){
if (current.key == k)
{
return current;
}
if (current.key > k)
current = current.left;
if (current.key < k)
current = current.right;
}
return null;
}
//-------------------------Help method- deletes a given leaf, no re-balancing-------------------------//
private void deleteLeaf(WAVLNode current)
{
if (current.parent.left == current)
current.parent.left = extLeaf;
if (current.parent.right == current)
current.parent.right = extLeaf;
}
private void deleteUnary(WAVLNode current){
if (current.parent.left == current){
if (current.left != extLeaf){
current.parent.left = current.left;
current.left.parent = current.parent;
}
if (current.right != extLeaf){
current.parent.left = current.right;
current.right.parent = current.parent;
}
}
if (current.parent.right == current){
if (current.left != extLeaf){
current.parent.right = current.left;
current.left.parent = current.parent;
}
if (current.right != extLeaf){
current.parent.right = current.right;
current.right.parent = current.parent;
}
}
}
//-------------------------Help method- "rolling down" the problem-------------------------//
private WAVLNode replaceInnerNode(WAVLNode current){ // @pre - node isn't a leaf.
WAVLNode origin = current;
if ((current.right.rank != -1) && (current.left.rank != -1)){ //Successor
current = current.right;
while (current.left.rank != -1)
current = current.left;
}
swapData(origin, current);
return current;
}
//-------------------------Help method- swap Data-------------------------//
private void swapData(WAVLNode a, WAVLNode b){
String s = a.info;
int k = a.key;
a.info = b.info;
a.key = b.key;
b.info = s;
b.key = k;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Min ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/**
* public String min()
*
* Returns the ifo of the item with the smallest key in the tree,
* or null if the tree is empty
*/
public String min()
{
if (this.empty()) // if empty
return null;
WAVLNode current = root;
while (current.left != extLeaf){ // go left all the way.
current = current.left;
}
return current.info;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Max ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/**
* 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()
{
if (this.empty()) // if empty
return null;
WAVLNode current = root;
while (current.right != extLeaf){ // go right all the way.
current = current.right;
}
return current.info;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Keys -> array ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/**
* public int[] keysToArray()
*
* Returns a sorted array which contains all keys in the tree,
* or an empty array if the tree is empty.
*/
public int[] keysToArray()
{
WAVLNode[] arr = new WAVLNode[size];
int[] resultKey=new int[size];
if(empty())
{
return resultKey;
}
else
{
WAVLNode n=root;
int i=0;
i=Sort(i,n,arr);
}
for(int i=0; i<size; i++)
{
resultKey[i]= arr[i].key;
}
return resultKey;
}
//-------------------------Help method- Sort-------------------------//
private int Sort(int i, WAVLNode n, WAVLNode[] arr) {
if(IsLeaf(n))
{
arr[i]=n;
return (i+1);
}
else if(n.rank==-1)
{
return i;
}
else
{
i=Sort(i,n.left,arr);
arr[i]=n;
i=Sort(i+1,n.right,arr);
}
return i;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Info -> array ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/**
* 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()
{
WAVLNode[] arr = new WAVLNode[size];
String[] resultinfo=new String[size];
if(empty())
{
return resultinfo;
}
else
{
WAVLNode n=root;
int i=0;
i=Sort(i,n,arr);
}
for(int i=0; i<size; i++)
{
resultinfo[i]= arr[i].info;
}
return resultinfo;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Size ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/**
* public int size()
*
* Returns the number of nodes in the tree.
*
* precondition: none
* postcondition: none
*/
public int size()
{
return size;
}
/**
* 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.
*/
//-------------------------WAVL NODE CLASS-------------------------//
public class WAVLNode{ // inner class node - currently just one type. morWAVLNode, alexWAVLNode and arnonEvronWAVLNode are currently in developing and will be introduced in v2.0.
public WAVLNode left = null;
public WAVLNode right = null;
public WAVLNode parent = null;
public int rank;
public int key;
public String info;
//-------------------------NODE BUILDER 1- empty-------------------------//
public WAVLNode(){
}
//-------------------------NODE BUILDER 2- key & info-------------------------//
public WAVLNode(int key, String info){
this.rank = 0;
this.left = this.right = extLeaf;
this.key = key;
this.info = info;
}
}
}