-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathavl-self-balancing-tree.cpp
771 lines (595 loc) · 14.2 KB
/
avl-self-balancing-tree.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
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
//
// beautiful code
//
#include <algorithm> // max, max
#include <bitset>
#include <cassert> // assert
#include <cmath>
#include <csignal>
#include <deque> // deque
#include <fstream> // ifstream
#include <functional> // greater
#include <iomanip> // setw, setfill
#include <iostream>
#include <limits> // numeric_limits
#include <map> // map, multimap
#include <numeric>
#include <queue> // priority_queue (greatest on top, by default, use greater for smallest on top)
#include <set> // set
#include <sstream> // stringstream
#include <stack> // stack
#include <tuple> // tuple
#include <unordered_map>
#include <unordered_set> // unordered_set
#include <vector> // vector
using namespace std;
bool ON = 1;
bool OFF = 0;
#ifdef DEBUGG
bool DEBUG_MODE = ON;
bool LOGS = ON;
#else
bool DEBUG_MODE = OFF;
bool LOGS = OFF;
#endif
template <typename Arg, typename... Args>
void debug(Arg&& arg, Args&&... args) {
if (LOGS) {
std::ostream& out = std::cout;
out << std::forward<Arg>(arg);
using expander = int[];
(
void)expander {
0, (void(out << ' ' << std::forward<Args>(args)), 0)...
};
}
}
template <typename T>
ostream & operator<<(ostream &o1, const vector<T> &c) {
for (auto it = c.begin(); it != c.end(); it++) {
o1 << setw(4) << *it << " ";
}
return o1;
}
template <typename T>
ostream & operator<<(ostream &o1, const deque<T> &c) {
for (auto it = c.begin(); it != c.end(); it++) {
o1 << setw(4) << *it << " ";
}
return o1;
}
template <typename T>
vector <T> range(T N1, T N2) {
vector<T> numbers(N2 - N1);
iota(numbers.begin(), numbers.end(), N1);
return numbers;
}
template <typename T>
vector <T> zero_till(T N) {
vector<T> numbers(N);
iota(numbers.begin(), numbers.end(), 0);
return numbers;
}
template <typename T>
vector <T> one_till(T N) {
vector<T> numbers(N);
iota(numbers.begin(), numbers.end(), 1);
return numbers;
}
template <typename NType>
void get_log_table(NType N) {
vector<NType> _log2floor_table_;
// See: http://www.rapidtables.com/math/algebra/logarithm/Logarithm_Table.htm
_log2floor_table_.resize(N + 1, 0);
for (NType i = 2; i < _log2floor_table_.size(); i++) {
_log2floor_table_[i] = _log2floor_table_[i / 2] + 1;
}
}
// -----------------------------------
// -----------------------------------
// -----------------------------------
// #define PTypeVal short // 1 to 50
//
//#define NTypeValue int // 1 to 10^5
//#define MTypeValue int // 1 to 10^5
//#define XTypeValue int // 1 to 10^9
//#define VTypeValue int // 0 to 10^6
//#define TypeValue unsigned long long // 0 to 10^6
//#define PTypeVal unsigned long long
//#define VTypeVal unsigned short // long long
//#define PTypeVal long long
//#define DTypeVal long long
//#define TTypeVal unsigned long long
//#define LTypeVal unsigned long long
//#define SumOfLTypeVal unsigned long long
/*
struct node
{
NTypeVal i;
EleTypeVal d;
node *l, *r, *p; // lefti, righti, parent
};
*/
// #define PTypeVal unsigned long long
// #define EleTypeVal unsigned long long
// #define EleTypeVeryLargVal unsigned long long
// #define SizeT unsigned int
#define QTypeVal unsigned long
#define NTypeVal unsigned long
#define EleTypeVal unsigned long
#define NTypeValBig unsigned long
#define EleTypeValBig unsigned long
typedef struct node {
public:
int val;
struct node*left;
struct node*right;
int ht;
} node;
template <class NType, class EleType, class NTypeBig, class EleTypeBig>
class avl__self_balancing_tree {
private:
NType i, j;
NType NNew;
NType startX, startY, goalX, goalY;
NTypeBig count_when_0 = numeric_limits<NType>::max();
unordered_map<NType, NTypeBig> m1;
stack<pair<NType, NTypeBig>> s1;
// vector <string> v1;
// set<pair<NType, NType>> s1; // visited;
// unordered_set<pair<NType, NType>> s1; // visited;
// queue<pair<NType, NType>> q1; // adjacents;
// multimap<pair<NType, NType>, pair<NType, NType>> m1; // current to parent;
// unordered_set <EleType> s1;
// unordered_multimultimap <NTypeVal, pair<NTypeVal, EleTypeVal>> m1;
// unordered_multimap <pair<NTypeVal, NTypeVal >, EleTypeVal> m1;
// unordered_multimultimap <NTypeVal, pair<NTypeVal, EleTypeVal>>
// pair<NTypeVal, EleTypeVal> least_gr_eq;
// NType least_gr_eq_ri;
// EleType least_gr_eq_rmax;
// HNType n;
// MType m;
// deque <pair<VType, NType > > p1;
// XType x;
// string S;
// LenType ai;
// TType type;
// HVType v;
// VType v;
// PType P;
// DType D;
// KType K;
// Heap<NType, pair<LType, TType>> h1;
// multiset<HVType> se1;
// deque<HVType> p1;
public:
avl__self_balancing_tree(istream &cin) {
// c(
return;
// v1.resize(N);
// v1 = { 1,3,5,7,9,11 };
// v1 = { 5,6,4 ,1,7, 1,3,2 };
// v1 = { 1,4,2,3 };
// cou t << one_till(N) << endl;
// cou t << v1 << endl;
// cou t << setw(4) << ai << " ";
// build__v1ment_tree__for_Range_Query_Max__wrapper(cin);
/*
v1.resize(N);
for (size_t i = 0; i < v1.size(); i++) {
cin >> v1[i];
}
*/
}
void dfs(node* root) {
if (root == NULL) {
return;
}
dfs(root->left);
update_height_due_to_change_at(root->left);
dfs(root->right);
update_height_due_to_change_at(root->left);
update_height_due_to_change_at(root);
}
void bfs_print(node* root) {
// dfs(root);
int ht = root->ht;
int spaces = 2 * ht;
queue<node*> q1;
q1.push(root);
q1.push(NULL);
string str1;
cout << setw(spaces) << " ";
spaces -= 2;
while (!q1.empty()) {
node* temp = q1.front();
if (temp == NULL) {
cout << endl;
cout << setw(spaces) << " ";
spaces -= 2;
while ((temp == NULL)) {
q1.pop();
if (q1.empty()) {
break;
}
temp = q1.front();
}
}
if (q1.empty()) {
break;
}
cout << setw(2) << temp->val << "(" << temp->ht << ")";
q1.pop();
q1.push(temp->left);
q1.push(temp->right);
q1.push(NULL);
}
cout << endl << endl;
}
node* get_new_node(int val1, int ht1 = 0) { // 0, because, in BST, a new key is always inserted at leaf
node* new1 = new node();
new1->val = val1;
new1->left = NULL;
new1->right = NULL;
new1->ht = ht1;
return new1;
}
int height(node* root) {
if (root == NULL) {
return -1;
}
return max(height(root->left), height(root->right)) + 1;
}
/*
3
/ \
2 4
\
5
*/
void update_height_due_to_change_at(node* root) {
if (root == NULL) {
return;
}
int new_height = height(root);
if (new_height == root->ht) {
return;
}
root->ht = new_height;
// update_height_due_to_change_at(root->parent);
}
// --------------------------------------------
//
// balance factor (balFac) = right child height - left child height
//
// --------------------------------------------
node* single__left_rotation(node* root, bool should_update_height = true) {
// y +2
// x +1
// z
// tree hight reduces
// root balFac +n ex: +2
// root->right balFac +(n-1) ex: +1
// then
// 1. single left rotation on root
auto new_root = root->right;
auto save = root->right->left;
root->right->left = root;
root->right = save;
root = new_root;
if (should_update_height) {
update_height_due_to_change_at(root->left);
update_height_due_to_change_at(root);
}
// x +1
// y z
return root;
}
node* single__right_rotation(node* root, bool should_update_height = true) {
// z -2
// x -1
// y
// tree hight reduces
// root balFac -n ex: -2
// root->left balFac -(n-1) ex: -1
// then
// 1. single right rotation on root
auto new_root = root->left;
auto save = root->left->right;
root->left->right = root;
root->left = save;
root = new_root;
if (should_update_height) {
update_height_due_to_change_at(root->right);
update_height_due_to_change_at(root);
}
// x -1
// y z
return root;
}
// --------------------------------------------
node* double__left_right_rotation(node* root) {
// z -2
// y +1
// x
// tree hight reduces
// root balFac -n ex: -2
// root->left balFac +(n-1) ex: +1
// then
// 1. single left rotation on left
// 2. single right on root
root->left = single__left_rotation(root->left, true);
// z -2
// x -1
// y
root = single__right_rotation(root, true);
// x -1
// y z
return root;
}
node* double__right_left_rotation(node* root) {
// y +2
// z -1
// x
// tree hight reduces
// root balFac +n ex: +2
// root->left balFac -(n-1) ex: -1
// then
// 1. single right rotation on right
// 2. single left on root
root->right = single__right_rotation(root->right, true);
// y +2
// x +1
// z
root = single__left_rotation(root, true);
// x +1
// y z
return root;
}
// --------------------------------------------
node* rebalance(node* root, int &balance_factor, int &left_ht, int &right_ht) {
// heights of the two child subtrees of any node can differ by at most one
// balance factor = height(right subtree) - height(left subtree)
// that is, allowed range [-1,+1], else unbalanced so rotation is needed
// rorate if required
if (balance_factor >= 2) {
int right__left_ht = (root->right->left) ? root->right->left->ht : -1;
int right__right_ht = (root->right->right) ? root->right->right->ht : -1;
int balance_factor_of_right = right__right_ht - right__left_ht;
if (balance_factor_of_right > 0) {
// if (root->right->right) {
return single__left_rotation(root);
}
else {
return double__right_left_rotation(root);
}
}
else {
int left__left_ht = (root->left->left) ? root->left->left->ht : -1;
int left__right_ht = (root->left->right) ? root->left->right->ht : -1;
int balance_factor_of_left = left__right_ht - left__left_ht;
if (balance_factor_of_left < 0) {
return single__right_rotation(root);
}
else {
return double__left_right_rotation(root);
}
}
}
node* insert(node* root, int val) {
if (root == NULL) {
return get_new_node(val);
}
else {
if (val <= root->val) {
root->left = insert(root->left, val);
// update height
update_height_due_to_change_at(root->left);
}
else {
root->right = insert(root->right, val);;
// update height
update_height_due_to_change_at(root->right);
}
// update height
update_height_due_to_change_at(root);
int left_ht = (root->left) ? root->left->ht : -1;
int right_ht = (root->right) ? root->right->ht : -1;
// calculate balance factor
int balance_factor = right_ht - left_ht;
if ((balance_factor <= -2) || (balance_factor >= 2)) {
// rotation necessary
root = rebalance(root, balance_factor, left_ht, right_ht);
}
return root;
}
}
};
// testsss
#define ReturnCountTypeValue char
vector < pair < vector<string>, vector<ReturnCountTypeValue >>> tests = {
// good cases
// bad cases
// see input condition - starts with 0? or 1?
// global variables are a issue - create a dummpy class put your function in it, create more dummy classes if you need
{
{
"5",
"7 4 8 3 5", // add 6
},
{ 0, 0, 0, 0, 0, 0, 0, 0 }
},
/*
{
{
"5",
"9 8 10 7 11", // add 6
},
{ 0, 0, 0, 0, 0, 0, 0, 0 }
},
*/
/*
{
{
"3",
"1 2", // add 6
},
{ 0, 0, 0, 0, 0, 0, 0, 0 }
},
{
{
"2",
"1 2", // add 6
},
{ 0, 0, 0, 0, 0, 0, 0, 0 }
},
*/
/*
{
{
"1",
"1", // add 6
},
{ 0, 0, 0, 0, 0, 0, 0, 0 }
},
{
{
"1",
"7", // add 6
},
{ 0, 0, 0, 0, 0, 0, 0, 0 }
},
*/
/*
{
{
"4",
"10 9 8 7", // add 6
},
{ 0, 0, 0, 0, 0, 0, 0, 0 }
},
{
{
"4",
"7 8 9 10", // add 6
},
{ 0, 0, 0, 0, 0, 0, 0, 0 }
},
{
{
"5",
"9 8 10 7 11", // add 6
},
{ 0, 0, 0, 0, 0, 0, 0, 0 }
},
*/
/*
{
{
"4",
"4 3 2 1", // add 6
},
{ 0, 0, 0, 0, 0, 0, 0, 0 }
},
{
{
"4",
"1 2 3 4", // add 6
},
{ 0, 0, 0, 0, 0, 0, 0, 0 }
},
*/
/*
{
{
"4",
"3 2 4 5", // add 6
},
{ 0, 0, 0, 0, 0, 0, 0, 0 }
},*/
/*
{
{
"7",
"3 2 1 5 4 6 7", // a 12 e 10
// "180", // 6
},
{ 0, 0, 0, 0, 0, 0, 0, 0 } // 2
},
*/
};
// LOGS = 0;
// debug("testFunction - begin\n\n");
template <class NType, class EleType, class NTypeBig, class EleTypeBig>
class Cls1 {
QTypeVal Q;
NType N;
public:
Cls1() {
}
vector <ReturnCountTypeValue> testFunction(istream & cin) {
// m(
// cin >> Q;
// for (size_t q = 0; q < Q; q++) {
cin >> N;
avl__self_balancing_tree <NTypeVal, EleTypeVal, NTypeValBig, EleTypeValBig> tree1(cin);
int val;
node* root = NULL;
for (size_t i = 0; i < N; i++) {
cin >> val;
root = tree1.insert(root, val);
}
tree1.bfs_print(root);
root = tree1.insert(root, 6);
tree1.bfs_print(root);
// int height = tree1.height(root);
// cout << height << endl;
// }
vector <ReturnCountTypeValue> res;
auto actual_result = 0;
res.push_back(actual_result);
return res;
}
};
int main() {
if (!DEBUG_MODE) {
Cls1 <NTypeVal, EleTypeVal, NTypeValBig, EleTypeValBig> o1;
o1.testFunction(cin);
return 0;
}
else {
for (unsigned long i = 0; i < tests.size(); i++) {
// debug("----------------------- input getting ready ----------------------------- ", "\n");
auto input = tests[i].first;
auto expected_output = tests[i].second;
std::stringstream ss;
istream &cin = ss;
for (size_t i = 0; i < input.size(); i++) {
// debug(input[i], "\n");
ss << input[i] << endl;
}
/*
ifstream ifs;
// ifs.open("../lr_input09_dummy.txt");
ifs.open("../lr_input09.txt");
string temp;
vector<string> a;
getline(ifs, temp); ss << temp << endl;
getline(ifs, temp); ss << temp << endl;
*/
// debug("----------------------- input ready ----------------------------- ", "\n");
Cls1 <NTypeVal, EleTypeVal, NTypeValBig, EleTypeValBig> o1;
// auto actual_result = o.testFunction(cin, q)[0];
auto actual_result = o1.testFunction(cin)[0];
// for (PTypeVal ai = 0; ai < q; ai++) {
// Cls1<NTypeVal, LTypeVal, TTypeVal> o;
// // // debug("\tactual_result ", actual_result, " ", "expected_output ", expected_output[ai], "\n");
//
// // assert(actual_result == expected_output[ai]);
// }
// break;
} // for tests.size()
return 0;
}
return 0;
}