-
Notifications
You must be signed in to change notification settings - Fork 46
/
avl_tree.cpp
307 lines (233 loc) · 7.21 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
// Copyright 2019 Souvik Biswas
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <iostream>
using namespace std;
// Functions for inserting an element in the tree
// and creating the tree by calling insert method repetadly
struct node *insert(node *, int );
struct node *create(node *);
// Function for RR rotation
struct node *RRrotation(node *);
// Function for LL rotation
struct node *LLrotation(node *);
// Function for traversing the tree
void inorderTraversal(node *);
// Function for finding height of the tree
int avlHeight(node *);
// Function for finding the maximum value
int max(int , int);
// Functon for getting the Balance Factor of the avl tree
// i.e., left height - right height
int getBalanceFactor(node *);
/*
Declaring a structure called node
having four members:
1) left block (which stores the address of the left node)
2) right block (which stores the address of the right node)
3) data block (which stores the value)
4) height block (which stores the height of the tree)
*/
struct node {
node *left;
node *right;
int data;
int height;
};
// Function for finding the height of the tree
// If tree is empty return 0, otherwise return height of the tree
int avlHeight(node *tree) {
if(tree == NULL) {
return 0;
}
else {
return tree->height;
}
}
// Function for finding the max value
int max(int a, int b) {
return (a > b) ? a : b;
}
// Function to calculate the balance factor of a node
// Balance factor = left height - right height
int getBalanceFactor(node *tree) {
if(tree == NULL) {
return 0;
}
return avlHeight(tree->left) - avlHeight(tree->right);
}
// ALGORITHM FOR LL ROTATION
node *LLrotation(node *x) {
node *y = x->right;
node *T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(avlHeight(x->left), avlHeight(x->right)) + 1;
y->height = max(avlHeight(y->left), avlHeight(y->right)) + 1;
return y;
}
// ALGORITHM FOR RR ROTATION
node *RRrotation(node *y) {
node *x = y->left;
node *T2 = x->right;
x->right = y;
y->left = T2;
x->height = max(avlHeight(x->left), avlHeight(x->right)) + 1;
y->height = max(avlHeight(y->left), avlHeight(y->right)) + 1;
return x;
}
/*
ALGORITHM TO INSERT AN ELEMENT IN THE TREE:
1) IF AVAIL = NULL
WRITE Overflow
EXIT
2) NEW_NODE = AVAIL
3) AVAIL = AVAIL -> LEFT
4) NEW_NODE -> DATA = VALUE
5) NEW_NODE -> LEFT = NULL
6) NEW_NODE -> RIGHT = NULL
7) NEW_NODE -> HEIGHT = 1
8) IF TREE = NULL
RETRUN NEW_NODE
9) IF VALUE < TREE -> DATA
TREE -> LEFT = RECURSIVE CALL PASSING TREE -> LEFT & VALUE
10) ELSE IF VALUE > TREE -> DATA
TREE -> RIGHT = RECURSIVE CALL PASSING TREE -> RIGHT & VALUE
11) ELSE
RETURN TREE
12) TREE -> HEIGHT = MAX(HEIGHT OF TREE -> LEFT, HEIGHT OF TREE -> RIGHT) + 1
13) BALANCE_FACTOR = GET BALANCE FACTOR PASSING TREE
14) IF BALANCE_FACTOR > 1 && VALUE < TREE -> LEFT -> DATA
RETURN RRrotation(TREE);
15) IF BALANCE_FACTOR < -1 && VALUE > TREE -> RIGHT -> DATA
RETURN LLrotation(TREE);
16) IF BALANCE_FACTOR > 1 && VALUE > TREE -> LEFT -> DATA
TREE -> LEFT = LLrotation(TREE -> LEFT)
RETURN RRrotation(TREE);
17) IF BALANCE_FACTOR < -1 && VALUE < TREE -> RIGHT -> DATA
TREE -> RIGHT = RRrotation(TREE -> RIGHT)
RETURN LLrotation(TREE);
18) RETURN TREE
19) EXIT
*/
node *insert(node *tree, int value) {
// Alocating space for a new node
node *new_node = new node;
// Storing data in new node
new_node->data = value;
new_node->left = NULL;
new_node->right = NULL;
new_node->height = 1;
// When node is empty
if(tree == NULL) {
return new_node;
}
// When the value to be inserted is less then tree -> data
if(value < tree->data) {
tree->left = insert(tree->left, value);
}
// When the value to be inserted is greater then tree -> data
else if(value > tree->data) {
tree->right = insert(tree->right, value);
}
// When the value to be inserted is equal to tree -> data
else {
return tree;
}
// Calculating the height of the tree
tree->height = max(avlHeight(tree->left), avlHeight(tree->right)) + 1;
// Calculating the balance factor of the tree
int balanceFactor = getBalanceFactor(tree);
// For RR rotation
if(balanceFactor > 1 && value < tree->left->data) {
return RRrotation(tree);
}
// For LL rotation
if(balanceFactor < -1 && value > tree->right->data) {
return LLrotation(tree);
}
// For LR rotation
if(balanceFactor > 1 && value > tree->left->data) {
tree->left = LLrotation(tree->left);
return RRrotation(tree);
}
// For RL rotation
if(balanceFactor < -1 && value < tree->right->data) {
tree->right = RRrotation(tree->right);
return LLrotation(tree);
}
return tree;
}
// Creating the AVL tree by calling insert method repetadly
node *create(node *tree) {
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
while(value != -1) {
tree = insert(tree, value);
cout<<"Enter the value to be inserted: ";
cin>>value;
}
return tree;
}
void inorderTraversal(node *tree) {
if(tree != NULL) {
inorderTraversal(tree->left);
cout<<tree->data<<" ";
inorderTraversal(tree->right);
}
}
// MAIN FUNCTION
int main() {
node *root = NULL;
int option, value;
do {
cout<<"\n******* MENU *******\n"
<<"1. Create\n"
<<"2. Insert\n"
<<"3. Inorder Traversal\n"
<<"4. Height\n"
<<"5. Exit\n";
cout<<"Enter your option: ";
cin>>option;
switch(option) {
case 1: root = create(root);
break;
case 2: cout<<"Enter the value to be inserted: ";
cin>>value;
root = insert(root, value);
break;
case 3: inorderTraversal(root);
cout<<endl;
break;
case 4: value = avlHeight(root);
cout<<"The height of the tree is "<<value<<endl;
break;
case 5: break;
default: cout<<"Wrong option !\n";
break;
}
} while(option != 5);
if(option == 5) {
// Freeing the space for root, after execution of the program
delete root;
cout<< "\nTHANK YOU for using the program !\n"
<<"Have a good day.\n\n";
}
}