-
Notifications
You must be signed in to change notification settings - Fork 617
/
avl_tree.cpp
428 lines (347 loc) · 9.8 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
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
/*
AVL tree is basically a self-balancing binary search tree in which the balancing factor
i.e difference between the height of left-subtree and right-subtrees is either 0 or 1 or -1
(not more than one). If the balance factor is disturbed then we need to rotate the nodes
to maintain the balance factor.
*/
#include <bits/stdc++.h>
using namespace std;
struct node
{
int info;
node *left , *right;
int height;
};
//print in-order traversal of the tree
void inorder(node* root)
{
if(root == NULL)
return;
inorder(root->left);
cout << root->info<<" ";
inorder(root->right);
}
//print pre-order traversal of the tree
void preorder(node* root)
{
if(root == NULL)
return;
cout << root->info<<" ";
preorder(root->left);
preorder(root->right);
}
//print post-order traversal of the tree
void postorder(node* root)
{
if(root == NULL)
return;
preorder(root->left);
preorder(root->right);
cout << root->info<<" ";
}
//height() will calculate the height of the tree
int height(node *root)
{
if(root == NULL)
return 0;
else
return root->height;
}
//left-left rotation function
node* LLrotation(node *root)
{
node *p1; //for adjustments
node *p2;
p1 = root; //A
p2 = root->left; //B
p1->left = p2->right; //A->left = B->right;
p2->right = p1; //B->right = A;
return p2; //new pointer i.e. B
}
//right-right rotation function
node* RRrotation(node *root)
{
node *p1; //for adjustments
node *p2;
p1 = root; //A
p2 = root->right; //B
p1->right = p2->left; //A->right = B->left;
p2->left = p1; //B->left = A;
return p2; //new pointer i.e. B
}
//left-right rotation function
node* LRrotation(node *root)
{
node *p1;
node *p2;
node *p3;
p1 = root; //A
p2 = root->left; //B
p3 = root->left->right; //C
p2->right = p3->left; //B->right = C->left
p1->left = p3->right; //A->left = C->right
p3->right = p1; //C->right = A
p3->left = p2; //C->left = B
return p3; //C is the new root
}
//right-left rotation function
node* RLrotation(node *root)
{
node *p1;
node *p2;
node *p3;
p1 = root; //A
p2 = root->right; //B
p3 = root->right->left; //C
p1->right = p3->left; //A->right = C->left
p2->left = p3->right; //B->left = C->right
p3->right = p2; //C->right = B
p3->left = p1; //C->left =
return p3; //C is the new root
}
//return the height of a particular node
int nodeheight(node* root)
{
if(root && root->left)
{
if(root->left->height > root->right->height)
return (root->left->height + 1);
}
else if(root && root->right)
{
if(root->right->height > root->left->height)
return (root->right->height + 1);
}
}
//returns the balance factor of a node
int BalanceFactor(node* root)
{
if(root == NULL)
return 0;
else
return(height(root->left) - height(root->right));
}
//creates a new node of the given data
node* create(int data)
{
node *newnode = new node;
newnode->info = data;
newnode->left = NULL;
newnode->right = NULL;
newnode->height = 1;
return newnode;
}
//inserts a node in the AVL tree
node* AVLinsertion(node* root , int data)
{
if(root == NULL)
{
root = create(data);
return root;
}
else if(data < root->info)
root->left = AVLinsertion(root->left , data);
else
root->right = AVLinsertion(root->right , data);
//node's height will be max of (l-height,r-height) + 1
root->height = (max(height(root->left) , height(root->right))+1);
//Insertion is done now conditions for rotation if needed.
if(BalanceFactor(root) == 2 && BalanceFactor(root->left) == 1)
return LLrotation(root);
if(BalanceFactor(root) == 2 && BalanceFactor(root->left) == -1)
return LRrotation(root);
if(BalanceFactor(root) == -2 && BalanceFactor(root->right) == -1)
return RRrotation(root);
if(BalanceFactor(root) == -2 && BalanceFactor(root->right) == 1)
return RLrotation(root);
return root;
}
//return the minimum node of the tree
node* minimum_node(node *root)
{
if(root == NULL)
return NULL;
if(root->left == NULL)
return root;
else
minimum_node(root->left);
}
//return the maximum node of the tree
node* maximum_node(node *root)
{
if(root == NULL)
return NULL;
if(root->right == NULL)
return root;
else
maximum_node(root->right);
}
//deletes an element from tree
node* delete_element(node *root , int data)
{
if(root == NULL)
return root;
if(data < root->info)
root->left = delete_element(root->left , data);
else if(data > root->info)
root->right = delete_element(root->right , data);
else
{
if(root->left != NULL && root->right != NULL)
{
node* temp = minimum_node(root->right);
root->info = temp->info;
root->right = delete_element(root->right , temp->info);
}
else if(root->left != NULL)
return (root->left);
else if(root->right != NULL)
return(root->right);
else //both left and left is null
return NULL;
}
if(root == NULL)
return root;
root->height = 1 + (max(height(root->left) , height(root->right)));
if(BalanceFactor(root) == 2 && BalanceFactor(root->left) == 1)
return LLrotation(root);
else if(BalanceFactor(root) == 2 && BalanceFactor(root->left) == -1)
return LRrotation(root);
else if(BalanceFactor(root) == -2 && BalanceFactor(root->right) == -1)
return RRrotation(root);
else if(BalanceFactor(root) == -2 && BalanceFactor(root->right) == 1)
return RLrotation(root);
return root;
}
//checks if the tree formed is AVL or not
int checkAVL(node* root)
{
if(root == NULL)
return 1;
int left = checkAVL(root->left);
int right = checkAVL(root->right);
if(BalanceFactor(root) >= -1 && BalanceFactor(root) <= 1)
return 1;
return 0;
}
int main()
{
node *root = NULL;
int data,choice;
while(1)
{
cout << "\n1. Insertion";
cout << "\n2. Deletion";
cout << "\n3. Traversal";
cout << "\n4. Check for AVL";
cout << "\n5. EXIT";
cout << "\nENTER CHOICE : ";
cin >> choice;
switch(choice)
{
case 1:
{
while(1)
{
cout << "\nEnter element or press -1 : ";
cin >> data;
if(data == -1)
break;
else
root = AVLinsertion(root , data);
}
break;
}
case 2:
{
cout << "\nEnter element to be deleted : ";
cin >> data;
delete_element(root , data);
break;
}
case 3:
{
cout << "\nPre-order traversal of the tree formed = ";
preorder(root);
cout << "\nIn-order traversal of the tree formed = ";
inorder(root);
cout << "\nPost-order traversal of the tree formed = ";
postorder(root);
break;
}
case 4:
{
if(checkAVL(root) == 1)
cout << "\nTree is AVL";
else
cout << "\nTree is not AVL";
break;
}
case 5:
{
exit(0);
break;
}
default:
cout << "\nINVALID CHOICE\n";
}
}
return 0;
}
/*
Complexity -
Insertion : O (log n), because it takes account of Balance Factor
Deletion : O (log n), because it takes account of Balance Factor
Traversal : O (n)
Output:
1. Insertion
2. Deletion
3. Traversal
4. Check for AVL
5. EXIT
ENTER CHOICE : 1
Enter element or press -1 : 12
Enter element or press -1 : 8
Enter element or press -1 : 20
Enter element or press -1 : -1
1. Insertion
2. Deletion
3. Traversal
4. Check for AVL
5. EXIT
ENTER CHOICE : 3
Pre-order traversal of the tree formed = 12 8 20
In-order traversal of the tree formed = 8 12 20
Post-order traversal of the tree formed = 8 20 12
1. Insertion
2. Deletion
3. Traversal
4. Check for AVL
5. EXIT
ENTER CHOICE : 4
Tree is AVL
1. Insertion
2. Deletion
3. Traversal
4. Check for AVL
5. EXIT
ENTER CHOICE : 2
Enter element to be deleted : 20
1. Insertion
2. Deletion
3. Traversal
4. Check for AVL
5. EXIT
ENTER CHOICE : 3
Pre-order traversal of the tree formed = 12 8
In-order traversal of the tree formed = 8 12
Post-order traversal of the tree formed = 8 12
1. Insertion
2. Deletion
3. Traversal
4. Check for AVL
5. EXIT
ENTER CHOICE : 5
Process returned 0 (0x0) execution time : 30.580 s
Press any key to continue.
*/