forked from krish-ag/HacktoberFest22-Repo-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVL_Trees_StepWise
213 lines (172 loc) · 3.52 KB
/
AVL_Trees_StepWise
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
#include<bits/stdc++.h>
using namespace std;
int flagg=0; // global variable to check if any rotations are performed so as to output later.
int dup=0; //global variable to check for duplicates
// Tree Node
class Node
{
public:
int key;
Node *left;
Node *right;
int height;
};
//max of 2 integers
int max(int a, int b);
// height of the AVL tree
int height(Node *N)
{
if (N == NULL)
return 0;
return N->height;
}
//max of 2 int
int max(int a, int b)
{
return (a > b)? a : b;
}
//New node
Node* newNode(int key)
{
Node* node = new Node();
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1;
return(node);
}
//right rotate
Node *rightRotate(Node *y)
{
cout<<" After Right Rotation around Node "<<y->key<<",";
Node *x = y->left;
Node *T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(height(y->left),
height(y->right)) + 1;
x->height = max(height(x->left),
height(x->right)) + 1;
return x;
}
//left rotate
Node *leftRotate(Node *x)
{
cout<<" After Left Rotation around Node "<<x->key<<",";
Node *y = x->right;
Node *T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(height(x->left),
height(x->right)) + 1;
y->height = max(height(y->left),
height(y->right)) + 1;
return y;
}
// Get balance factor
int getBalance(Node *N)
{
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}
void Baalance(Node* N) //to check if any unbalance remains throughout
{
int balance;
if(N==NULL)
{
balance=0;
}
if(N==NULL)
{
return;
}
balance=height(N->left)-height(N->right);
if(balance>1)
{
cout<<"Node "<<N->key<<" BF becomes 2.";
}
if(balance<-1)
{
cout<<"Node "<<N->key<<" BF becomes -2.";
}
Baalance(N->left);
Baalance(N->right);
}
//insert node
Node* insert(Node* node, int key)
{
if (node == NULL)
{
cout<<"Node "<<key<<" inserted into tree.";
return(newNode(key));
}
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else
{
dup++;
cout<<"Duplicate element, hence discarded.";
return node;
}
node->height = 1 + max(height(node->left),
height(node->right));
int balance = getBalance(node);
if(!(balance<=1 && balance>=-1))
{
cout<<" Node "<<node->key<<" BF becomes "<<balance<<".";
}
if (balance > 1 && key < node->left->key)
{
flagg++;
return rightRotate(node);
}
if (balance < -1 && key > node->right->key)
{
flagg++;
return leftRotate(node);
}
if (balance > 1 && key > node->left->key)
{
flagg++;
node->left = leftRotate(node->left);
Baalance(node);
return rightRotate(node);
}
if (balance < -1 && key < node->right->key)
{
flagg++;
node->right = rightRotate(node->right);
Baalance(node);
return leftRotate(node);
}
return node;
}
int main()
{
Node *root = NULL;
int n;cin>>n;
for(int i=0;i<n;i++)
{
flagg=0; //global var
dup=0;// global var
string s;
cin>>s;
int num;
cin>>num;
cout<<"I "<<num<<": ";
root = insert(root, num);
if(flagg!=0)
{
cout<<" AVL tree property restored.";
}
else if(dup==0)
{
cout<<" No issue with AVL property.";
}
cout<<endl;
}
return 0;
}