forked from iamshubhamg/Leet-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl_tree.cpp
150 lines (135 loc) · 3.51 KB
/
avl_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
#include<iostream>
using namespace std;
struct Node {
struct Node *left, *right;
int data, height;
}*root=NULL;
int NodeHeight(struct Node *t){
int hl, hr;
hl = t && t->left ? t->left->height : 0;
hr = t && t->right ? t->right->height : 0;
return hl > hr ? hl+1:hr+1;
}
int balanceFactor(struct Node *p){
int hl, hr;
hl = p && p->left ? p->left->height : 0;
hr = p && p->right ? p->right->height : 0;
return hl-hr;
}
struct Node * LLRotation(struct Node *p){
struct Node *pl = p->left;
struct Node *plr = pl->right;
pl->right = p;
p->left = plr;
p->height = NodeHeight(p);
pl->height = NodeHeight(pl);
if(root == p)
root = pl;
return pl;
}
struct Node * RRRotation(struct Node *p){
struct Node *pr = p->right;
struct Node *prl = pr->left;
pr->left = p;
p->right = prl;
p->height = NodeHeight(p);
pr->height = NodeHeight(pr);
if(root == p)
root = pr;
return pr;
}
struct Node * LRRotation(struct Node *p){
struct Node *pl = p->left;
struct Node *plr = pl->right;
pl->right = plr->left;
p->left = plr->right;
plr->left = pl;
plr->right = p;
p->height = NodeHeight(p);
pl->height = NodeHeight(pl);
plr->height = NodeHeight(plr);
if(p == root)
root = plr;
return plr;
}
struct Node *RLRotation(struct Node *p){
struct Node *pr = p->right;
struct Node *prl = pr->left;
pr->left = prl->right;
p->right = prl->left;
prl->right = pr;
prl->left = p;
p->height = NodeHeight(p);
pr->height = NodeHeight(pr);
prl->height = NodeHeight(prl);
if(p == root)
root= prl;
return prl;
}
struct Node *RInsert(struct Node *p, int key){
struct Node *t = NULL;
if(p == NULL){
t = new struct Node;
t->data = key;
t->height = 1;
t->left = t->right = NULL;
return t;
}
if(key < p->data)
p->left = RInsert(p->left, key);
else if(key > p->data)
p->right = RInsert(p->right, key);
p->height = NodeHeight(p);
if(balanceFactor(p) == 2 && balanceFactor(p->left) == 1){
return LLRotation(p);
}
else if(balanceFactor(p) == 2 && balanceFactor(p->left) == -1){
return LRRotation(p);
}
else if(balanceFactor(p) == -2 && balanceFactor(p->right) == -1){
return RRRotation(p);
}
else if(balanceFactor(p) == -2 && balanceFactor(p->right) == 1){
return RLRotation(p);
}
return p;
}
void preOrder(struct Node *p){
if(p){
cout << p->data << " ";
preOrder(p->left);
preOrder(p->right);
}
}
void inOrder(struct Node *p){
if(p){
inOrder(p->left);
cout << p->data << " ";
inOrder(p->right);
}
}
int main(){
int x = 0;
cout << "Enter Node Value: ";
cin >> x;
if(x != -1){
root = RInsert(root, x);
while (true)
{
cout << "Enter Node Value: ";
cin >> x;
if(x == -1)
break;
RInsert(root, x);
}
cout << "\nPreOrder Sequence is: ";
preOrder(root);
cout << "\nInOrder Sequence is: ";
inOrder(root);
// cout << root->data << " Left Child: " << root->left->data << " Right Child: " << root->right->data;
}
else{
cout << "\nNo Root value found\n";
}
return 0;
}