-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAugmented_AVL.cpp
681 lines (513 loc) · 23 KB
/
Augmented_AVL.cpp
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
#include<bits/stdc++.h>
using namespace std;
template <class T>
class avl{
/****************************** STRUCTURE OF AVL TREENODE *************************************/
struct TreeNode
{
T data;
int height;
int count, lc, rc;
struct TreeNode *left, *right;
};
struct TreeNode *root = NULL;
T nextgreater = {}; T nextsmaller = {};
int noOfElementsOutsideRange = 0;
public:
/****************************** RETURN MIN VALUE OF AVL **************************************/
TreeNode* minValue( TreeNode* root){
if(!root) return root;
if(!root->left) return root;
return minValue(root->left);
}
/****************************** RETURN MAX VALUE OF AVL **************************************/
TreeNode* maxValue( TreeNode* root){
if(!root) return root;
if(!root->right) return root;
return maxValue(root->right);
}
/************************************* INIT **************************************************/
void init(){
noOfElementsOutsideRange = 0;
nextgreater = {};
nextsmaller = {};
}
/****************************** CREATE NEW NODE **********************************************/
TreeNode* createTreeNode(T key)
{
TreeNode* newNode = new TreeNode;
newNode->data = key;
newNode->left = newNode->right = NULL;
newNode->height = 0; newNode->lc = 0; newNode->rc = 0;
newNode->count = 1;
return newNode;
}
/****************************** FUNCTION TO CALCULATE HEIGHT OF NODE **************************/
int height(TreeNode *node){
if(!node) return -1;
return node->height;
}
/****************************** ROTATE RIGHT FUNCTION ****************************************/
TreeNode* rotateRight(TreeNode* root){
//cout<<endl<<"=======INSIDE ROTATE RIGHT======="<<endl;
TreeNode *pivot = root->left;
root->left = pivot->right; root->lc = pivot->rc;
pivot->right = root; pivot->rc = root->lc + root->rc + root->count;
root->height = max( height(root->left), height(root->right) ) + 1;
pivot->height = max( height(pivot->left), height(pivot->right) ) + 1;
return pivot;
}
/****************************** ROTATE LEFT FUNCTION ******************************************/
TreeNode* rotateLeft(TreeNode* root){
//cout<<endl<<"=======INSIDE ROTATE LEFT======="<<endl;
TreeNode *pivot = root->right;
root->right = pivot->left; root->rc = pivot->lc;
pivot->left = root; pivot->lc = root->lc + root->rc + root->count;
root->height = max( height(root->left), height(root->right) ) + 1;
pivot->height = max( height(pivot->left), height(pivot->right) ) + 1;
return pivot;
}
/****************************** INSERTION OF NODE IN AVL **************************************/
TreeNode* insert(TreeNode* node, T key)
{
if (node == NULL) return createTreeNode(key);
if(key == node->data){
node->count = node->count + 1;
return node;
}
if (key < node->data)
{node->lc++; node->left = insert(node->left, key);}
else if (key > node->data)
{node->rc++; node->right = insert(node->right, key);}
int heightDifference = height(node->left) - height(node->right);
if (heightDifference < -1){
if(height(node->right->right) >= height(node->right->left)){
return rotateLeft(node);
}
else{
node->right = rotateRight(node->right);
return rotateLeft(node);
}
}
else if(heightDifference > 1){
if(height(node->left->left) >= height(node->left->right)){
return rotateRight(node);
}
else{
node->left = rotateLeft(node->left);
return rotateRight(node);
}
}
node->height = max(height(node->left), height(node->right)) + 1;
return node;
}
void insertKey(T key){
root = insert(root, key);
}
/****************************** LEVEL ORDER TRAVERSAL ****************************************/
void levelorder_helper(TreeNode* root){
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
TreeNode* node = q.front();
q.pop();
cout<<node->data<<"|"<<node->count<<"|"<<node->lc<<"|"<<node->rc<<" ";
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
}
}
void levelOrder(){
levelorder_helper(root);
}
/****************************** PRE ORDER TRAVERSAL ****************************************/
void preorder_helper(TreeNode* root){
if(root){
cout<<root->data<<"|"<<root->count<<"|"<<root->lc<<"|"<<root->rc<<" ";
preorder_helper(root->left);
preorder_helper(root->right);
}
}
void preorder(){
preorder_helper(root);
}
/****************************** SEARCH GIVEN DATA IN AVL *************************************/
TreeNode* search(TreeNode* root, T key){
if(!root) return NULL;
if(root->data == key) return root;
if(root->data > key) return search(root->left, key);
return search(root->right, key);
}
bool searchKey(T key){
TreeNode* node = search(root, key);
if(!node) return false;
return true;
}
/****************************** DELETE NODE FROM AVL *****************************************/
TreeNode* recurrDelete(TreeNode* node, T key){
if(!node) return NULL;
if(key < node->data){
node->lc--;
node->left = recurrDelete(node->left, key);
}
else if(key > node->data){
node->rc--;
node->right = recurrDelete(node->right, key);
}
else{
/*if(node->count > 1){
node->count = node->count - 1 ;
return node;
}*/
if(!node->left && !node->right){
return NULL;
}
if(!node->left){
TreeNode* temp = node->right;
delete(node);
return temp;
}
if(!node->right){
TreeNode* temp = node->left;
delete(node);
return temp;
}
else{
TreeNode* temp = minValue(node->right);
node->data = temp->data;
node->count = temp->count;
node->right = recurrDelete(node->right, node->data);
}
}
int heightDifference = height(node->left) - height(node->right);
if (heightDifference < -1){
if(height(node->right->right) >= height(node->right->left)){
return rotateLeft(node);
}
else{
node->right = rotateRight(node->right);
return rotateLeft(node);
}
}
else if(heightDifference > 1){
if(height(node->left->left) >= height(node->left->right)){
return rotateRight(node);
}
else{
node->left = rotateLeft(node->left);
return rotateRight(node);
}
}
node->height = max(height(node->left), height(node->right)) + 1;
return node;
}
TreeNode* deleteNode(TreeNode* node, T key){
if(!node) return NULL;
if(key < node->data){
node->lc--;
node->left = deleteNode(node->left, key);
}
else if(key > node->data){
node->rc--;
node->right = deleteNode(node->right, key);
}
else{
if(node->count > 1){
node->count = node->count - 1 ;
return node;
}
if(!node->left && !node->right){
return NULL;
}
if(!node->left){
TreeNode* temp = node->right;
delete(node);
return temp;
}
if(!node->right){
TreeNode* temp = node->left;
delete(node);
return temp;
}
else{
TreeNode* temp = minValue(node->right);
node->data = temp->data;
node->count = temp->count;
node->rc -= temp->count ;
node->right = recurrDelete(node->right, node->data);
}
}
int heightDifference = height(node->left) - height(node->right);
if (heightDifference < -1){
if(height(node->right->right) >= height(node->right->left)){
return rotateLeft(node);
}
else{
node->right = rotateRight(node->right);
return rotateLeft(node);
}
}
else if(heightDifference > 1){
if(height(node->left->left) >= height(node->left->right)){
return rotateRight(node);
}
else{
node->left = rotateLeft(node->left);
return rotateRight(node);
}
}
node->height = max(height(node->left), height(node->right)) + 1;
return node;
}
void deleteKey(T key){
TreeNode* nodeToBeDelete = search(root, key);
if(!nodeToBeDelete){
cout<<endl<<endl<<"Key "<<key<<" doesn't exist"<<endl<<endl;
return;
}
else root = deleteNode(root, key);
}
/****************************** COUNT OCCURRANCES OF ELEMENT ********************************/
int countOccurrance_helper(TreeNode* root, T key){
if(!root) return 0;
// cout<<endl<<"count out : "<<count<<endl;
if(root->data == key){
return root->count;
// cout<<endl<<"count in : "<<count<<endl;
}
if(root->data >= key) return countOccurrance_helper(root->left, key);
return countOccurrance_helper(root->right, key);
}
int countOccurrance(T key){
return countOccurrance_helper(root, key);
}
/****************************** NEXT GREATER IN AVL *****************************************/
void nextGreater(TreeNode* root, T key){
if(!root) return;
if(key < root->data){
nextgreater = root->data;
nextGreater(root->left, key);
}
else nextGreater(root->right, key);
}
/****************************** NEXT SMALLER IN AVL *****************************************/
void nextSmaller(TreeNode* root, T key){
if(!root) return;
if(key >= root->data){
nextsmaller = root->data;
nextSmaller(root->right, key);
}
else nextSmaller(root->left, key);
}
/****************************** LOWER BOUND OF GIVEN DATA IN AVL *****************************/
T lowerBound(TreeNode* root, T key){
TreeNode *node = search(root, key);
if(node){return key;}
nextGreater(root, key);
return nextgreater;
}
T lowerbound(T key){
init();
return lowerBound(root, key);
}
/****************************** UPPER BOUND OF GIVEN DATA IN AVL *****************************/
T upperBound(TreeNode* root, T key){
nextGreater(root, key);
return nextgreater;
}
T upperbound(T key){
init();
return upperBound(root, key);
}
/**************************** CLOSEST ELEMENT OF GIVEN DATA IN AVL ***************************/
T closest(T key){
init();
if(key<=minValue(root)->data) return minValue(root)->data;
else if (key >= maxValue(root)->data){return maxValue(root)->data;}
T n1 = lowerbound(key);
nextSmaller(root, key);
T n2 = nextsmaller;
if(key-n2 > n1-key) return n1;
else return n2;
}
/************************* Kth LARGEST ELEMENT IN LOG-N TIME IN AVL *************************/
T klargest_helper(TreeNode* root, int k){
if(root->rc >= k){
return klargest_helper(root->right, k);
}
else if(root->rc + root->count >= k){
return root->data;
}
return klargest_helper(root->left, k - root->rc - root->count);
}
T klargest(int k){
return klargest_helper(root, k);
}
/***************************** NO OF NODES IN A GIVEN RANGE *******************************/
void rejectMin(TreeNode* root, T a){
if(!root) return;
if(root->data < a){
noOfElementsOutsideRange += root->lc + root->count;
//cout<<"ll : "<<noOfElementsOutsideRange<<endl;
return rejectMin(root->right, a);
}
else if(root->data ==a){
noOfElementsOutsideRange += root->lc;
//cout<<"ll : "<<noOfElementsOutsideRange<<endl;
return rejectMin(root->right, a);
}
return rejectMin(root->left, a);
}
void rejectMax(TreeNode* root, T b){
if(!root) return;
if(root->data > b){
noOfElementsOutsideRange += root->rc + root->count;
//cout<<"ll+rr : "<<noOfElementsOutsideRange<<endl;
return rejectMax(root->left, b);
}
else if(root->data == b){
noOfElementsOutsideRange += root->rc;
//cout<<"ll+rr : "<<noOfElementsOutsideRange<<endl;
return rejectMax(root->left, b);
}
return rejectMax(root->right, b);
}
int range(T a, T b){
if(a<minValue(root)->data && b<minValue(root)->data) return 0;
else if(a>maxValue(root)->data && b>maxValue(root)->data) return 0;
T left = lowerbound(a);
nextSmaller(root, b);
T right = nextsmaller;
init(); rejectMin(root, a);rejectMax(root, b);
int total = root->count + root->lc + root->rc;
return total-noOfElementsOutsideRange;
}
};
/****************************** MAIN FUNCTION ***********************************************/
int main()
{ cout<<endl<<"********************************************** WELCOME TO AVL WORLD ********************************************"<<endl;
/* 1.insertKey(T key)
2.deleteKey(T key)
3.searchKey(T key)
3.countOccurrance(T key)
4.lowerbound(T key)
5.upperbound(T key)
6.closest(T key)
7.klargest(int k)
8.range(T a, T b)*/
avl<int> tree1;
avl<float> tree2;
avl<string> tree3;
avl<char> tree4;
cout<<endl<<endl<<"----------------------------------------- TEST CASE : 1 ---------------------------------------"<<endl<<endl;
tree1.insertKey(5);
tree1.insertKey(10);
tree1.insertKey(32);
tree1.insertKey(25);
tree1.insertKey(32);
tree1.insertKey(41);
tree1.insertKey(50);
tree1.insertKey(55);
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<"preorder : ";tree1.preorder();
cout<<endl<<"levelorder : ";tree1.levelOrder();
cout<<endl<<endl<<"search 32 : "<<tree1.searchKey(32)<<endl;
cout<<"search 40 : "<<tree1.searchKey(40)<<endl;
cout<<"search 55 : "<<tree1.searchKey(55)<<endl;
cout<<"search 50 : "<<tree1.searchKey(50)<<endl;
cout<<"search 64 : "<<tree1.searchKey(64)<<endl;
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<endl<<"lowerbound 32 : "<<tree1.lowerbound(32)<<endl;
cout<<"lowerbound 40 : "<<tree1.lowerbound(40)<<endl;
cout<<"lowerbound 56 : "<<tree1.lowerbound(56)<<endl;
cout<<"lowerbound 50 : "<<tree1.lowerbound(50)<<endl;
cout<<"lowerbound 64 : "<<tree1.lowerbound(64)<<endl;
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<endl<<"closest 32 : "<<tree1.closest(32)<<endl;
cout<<"closest 40 : "<<tree1.closest(40)<<endl;
cout<<"closest 56 : "<<tree1.closest(56)<<endl;
cout<<"closest 50 : "<<tree1.closest(50)<<endl;
cout<<"closest 0 : "<<tree1.closest(0)<<endl;
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<endl<<"upperbound 32 : "<<tree1.upperbound(32)<<endl;
cout<<"upperbound 40 : "<<tree1.upperbound(40)<<endl;
cout<<"upperbound 56 : "<<tree1.upperbound(56)<<endl;
cout<<"upperbound 50 : "<<tree1.upperbound(50)<<endl;
cout<<"upperbound 0 : "<<tree1.upperbound(0)<<endl;
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<endl<<"countOccurance 32 : "<<tree1.countOccurrance(32)<<endl;
cout<<"countOccurance 40 : "<<tree1.countOccurrance(40)<<endl;
cout<<"countOccurance 56 : "<<tree1.countOccurrance(56)<<endl;
cout<<"countOccurance 50 : "<<tree1.countOccurrance(50)<<endl;
cout<<"countOccurance 0 : "<<tree1.countOccurrance(0)<<endl;
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<endl<<"klargest 4 : "<<tree1.klargest(4)<<endl;
cout<<"klargest 1 : "<<tree1.klargest(1)<<endl;
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<endl<<"range 5 50 : "<<tree1.range(5, 50)<<endl;
cout<<"range 1 2 : "<<tree1.range(1, 2)<<endl;
cout<<endl<<endl<<"--------------------------------------- TEST CASE : 2 --------------------------------------"<<endl<<endl;
tree2.insertKey(5.34);
tree2.insertKey(10.2);
tree2.insertKey(32.2341);
tree2.insertKey(25.55);
tree2.insertKey(32.2341);
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<"preorder : ";tree2.preorder();
cout<<endl<<"levelorder : ";tree2.levelOrder();
cout<<endl<<endl<<"search 32.2341 : "<<tree2.searchKey(32.2341)<<endl;
cout<<"search 10.2 : "<<tree2.searchKey(10.2)<<endl;
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<endl<<"lowerbound 32 : "<<tree2.lowerbound(32)<<endl;
cout<<"lowerbound 10 : "<<tree2.lowerbound(10)<<endl;
cout<<"lowerbound 16 : "<<tree2.lowerbound(16)<<endl;
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<endl<<"closest 32 : "<<tree2.closest(32)<<endl;
cout<<"closest 20 : "<<tree2.closest(20)<<endl;
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<endl<<"upperbound 12 : "<<tree2.upperbound(12)<<endl;
cout<<"upperbound 4: "<<tree2.upperbound(4)<<endl;
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<endl<<"countOccurance 32.2341 : "<<tree2.countOccurrance(32.2341)<<endl;
cout<<"countOccurance 40 : "<<tree2.countOccurrance(40)<<endl;
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<endl<<"klargest 4 : "<<tree2.klargest(4)<<endl;
cout<<"klargest 1 : "<<tree2.klargest(1)<<endl;
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<endl<<"range 5 50 : "<<tree2.range(5, 50)<<endl;
cout<<"range 1 2 : "<<tree2.range(1, 2)<<endl;
cout<<endl<<endl<<"--------------------------------------- TEST CASE : 3 --------------------------------------"<<endl<<endl;
tree3.insertKey("na");
tree3.insertKey("msd");
tree3.insertKey("dc");
tree3.insertKey("sd");
tree3.insertKey("hello");
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<"preorder : ";tree3.preorder();
cout<<endl<<"levelorder : ";tree3.levelOrder();
cout<<endl<<endl<<"search hello : "<<tree3.searchKey("hello")<<endl;
cout<<"search dc : "<<tree3.searchKey("dc")<<endl;
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<endl<<"lowerbound dc : "<<tree3.lowerbound("dc")<<endl;
cout<<"lowerbound cd : "<<tree3.lowerbound("cd")<<endl;
cout<<"lowerbound hi : "<<tree3.lowerbound("hi")<<endl;
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<endl<<"upperbound rt : "<<tree3.upperbound("rt")<<endl;
cout<<"upperbound gg: "<<tree3.upperbound("gg")<<endl;
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<endl<<"klargest 4 : "<<tree3.klargest(4)<<endl;
cout<<"klargest 1 : "<<tree3.klargest(1)<<endl;
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<endl<<"range a z : "<<tree3.range("a", "z")<<endl;
cout<<"range bye hi : "<<tree3.range("bye", "hi")<<endl;
cout<<endl<<endl<<"--------------------------------------- TEST CASE : 4 --------------------------------------"<<endl<<endl;
tree4.insertKey('d');
tree4.insertKey('a');
tree4.insertKey('s');
tree4.insertKey('a');
tree4.insertKey('@');
cout<<endl<<endl<<"******************************************************************"<<endl<<endl;
cout<<endl<<"preorder : ";tree3.preorder();
cout<<endl<<"levelorder : ";tree3.levelOrder();
cout<<endl<<endl;
return 0;
}