-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAVL.cpp
More file actions
430 lines (341 loc) · 12.9 KB
/
AVL.cpp
File metadata and controls
430 lines (341 loc) · 12.9 KB
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
/*
Generic AVL Tree Implementation.
Guidelines: 1) Comparator function must be implemented in the inheriting child class.
- Conventions:
Return 0 if values are equal.
Return <0 value for less than boolean result.
Return >0 value for greater than boolean result.
2) toString method for the object being stored must be defined for printing.
*/
#include <stack>
template <class type>
class AVL
{
private:
// AVL struct
struct AVLNode
{
AVLNode* left;
AVLNode* right;
type data;
int height;
int balance_factor;
};
// Initialize root
AVLNode* root = NULL;
// Allocate memory for new node
AVLNode* GetNewNode()
{
AVLNode* node = new AVLNode;
node -> left = NULL;
node -> right = NULL;
node -> balance_factor = 0;
node -> height = -1;
return node;
}
// Delete a given node.
void delete_node(AVLNode* node)
{
// De-allocate memory. Return memory back to the OS.
delete node;
// Point the node to null.
node = NULL;
}
// Rebuild the tree after insertion and deletion updating heights and rebalancing the tree.
void rebuild(std::stack<AVLNode*>* stk)
{
while (!stk -> empty() && stk -> top() != NULL)
{
// Update height and balance factor for each node.
update(stk -> top());
// Perform rotation if needed based on current node's balance factor.
root = balance(stk -> top());
stk -> pop();
// Establish parent links in each step.
if (!stk -> empty() && stk -> top() != NULL)
{
// Value of node is less than its parent
if ( compare(root -> data, stk -> top() -> data) == 0 ||
compare(root -> data, stk -> top() -> data) < 0 )
stk -> top() -> left = root;
// Value of node is greater
else
stk -> top() -> right = root;
}
}
return;
}
// Returns the greater of two input numbers.
int MAX(int num1, int num2)
{
if (num1 > num2) return num1;
return num2;
}
// Assigns height and balance factor values to each node.
void update(AVLNode* node)
{
// Initialize left and right heights.
int left_height = -1, right_height = -1;
// If child exist, update left and right heights.
if (node -> left != NULL) left_height = node -> left -> height;
if (node -> right != NULL) right_height = node -> right -> height;
// Increment the maximum of the two heights and set it as node's own height.
node -> height = 1 + MAX(left_height, right_height);
// Update balance factor of the input node.
node -> balance_factor = left_height - right_height;
}
// Right rotation about the given node.
AVLNode* rightRotation(AVLNode* node)
{
// Right rotation steps
AVLNode* left_child = node -> left;
node -> left = left_child -> right;
left_child -> right = node;
// Update heights and balance factors after rotation.
update(node);
update(left_child);
return left_child;
}
// Left rotation about the given node.
AVLNode* leftRotation(AVLNode* node)
{
// Left rotation steps.
AVLNode* right_child = node -> right;
node -> right = right_child -> left;
right_child -> left = node;
// Update heights and balance factors after rotation.
update(node);
update(right_child);
return right_child;
}
// Balance tree based on the balance_factor value of a given node.
AVLNode* balance(AVLNode* node)
{
// CASE-I Left heavy case
if (node -> balance_factor == 2)
{
// CASE-(a) LeftLeftCase
if (node -> left -> balance_factor > 0)
return LeftLeftCase(node);
// CASE-(b) LeftRightCase
return LeftRightCase(node);
}
// CASE-II Right heavy case
else if (node -> balance_factor == -2)
{
// CASE-(a) RightRightCase
if (node -> right -> balance_factor < 0)
return RightRightCase(node);
// CASE-(b) RightLeftCase
return RightLeftCase(node);
}
return node;
}
// Perform Right rotation.
AVLNode* LeftLeftCase(AVLNode* node)
{
return rightRotation(node);
}
// Perform Left Rotation
AVLNode* RightRightCase(AVLNode* node)
{
return leftRotation(node);
}
// First perform left and then right rotation.
AVLNode* LeftRightCase(AVLNode* node)
{
// First perform left rotation about the middle node.
node -> left = leftRotation(node -> left);
return LeftLeftCase(node);
}
// First perform right and then left rotation.
AVLNode* RightLeftCase(AVLNode* node)
{
// First perform right rotation about the middle node.
node -> right = rightRotation(node -> right);
return RightRightCase(node);
}
// Print tree level by level horizontally.
void print(std::string prefix, AVLNode* root, bool isLeft)
{
if( root == NULL ) return;
std::cout << prefix;
std::cout << (isLeft ? "├──" : "└──" );
// Print the value of the node
std::cout << root -> data.toString() << std::endl;
// Enter the next tree level - left and right branch
print( prefix + (isLeft ? "│ " : " "), root -> left, true);
print( prefix + (isLeft ? "│ " : " "), root -> right, false);
}
public:
// Pure virtual function to be implemented in inheriting child classes.
virtual int compare(type obj1, type obj2) = 0;
// Insert data according to BST invariant and perform rotations if required.
// Time Complexity = 2 * log n + constant time for rotations = log n
void insert(type data)
{
// Allocate memory for new node.
AVLNode* new_node = GetNewNode();
// Declaring pointers
AVLNode* parent;
AVLNode* current;
// Creating a stack to keep track of visited nodes.
std::stack<AVLNode*> stk;
// Insert root node.
if (root == NULL)
{
new_node -> data = data;
root = new_node;
stk.push(root);
}
// Insert according to BST condition.
else
{
parent = NULL;
current = root;
stk.push(parent);
// Find the position of node to be inserted.
while (current != NULL)
{
parent = current;
// Smaller than root values go in left subtree.
if ( compare(data, current -> data) == 0 ||
compare(data, current -> data) < 0 )
current = current -> left;
// Greater than root values go in right subtree.
else
current = current -> right;
// Push parent pointers into the stack.
stk.push(parent);
}
// Insert data in appropriate position
new_node -> data = data;
// Establish parent link.
if ( compare(data, parent -> data) == 0 ||
compare(data, parent -> data) < 0 )
parent -> left = new_node;
else
parent -> right = new_node;
}
// Update the newly inserted node height value
update(new_node);
// Rebuild the tree from the traversed path updating height and balance_factor in each step.
rebuild(&stk);
}
// Remove a node from AVL keeping the BST invariant intact. Perform rotations where required.
// Time Complexity = 2 * log n + constant time for rotations = log n
void remove(type data)
{
// Stack for update and balance methods
std::stack<AVLNode*> stk;
// Locate the node to be deleted.
AVLNode* parent = NULL;
AVLNode* current = root;
stk.push(parent);
while (current != NULL)
{
// Element found.
if (compare(data, current -> data) == 0)
break;
// Else keep traversing
parent = current;
if (compare(data, current -> data) < 0)
current = current -> left;
else
current = current -> right;
// Push parent pointers into the stack.
stk.push(parent);
}
// Element does not exist
if (current == NULL)
std::cout << "Element does not exist in the tree." << '\n';
// If element to be deleted exists in the tree
else
{
// CASE-I Leaf node
if (current -> left == NULL && current -> right == NULL)
{
type value = current -> data;
delete_node(current);
// Adjust parent links
if ( compare(value, parent -> data) == 0 ||
compare(value, parent -> data) < 0 )
parent -> left = NULL;
else
parent -> right = NULL;
std::cout << "Node " << value.toString() << " (leaf node) deleted from the tree." << '\n';
}
// CASE-II Node has one child only.
else if (current -> left == NULL || current -> right == NULL)
{
// Current immidiate left node is to be connected with parent.
if (current -> left != NULL)
{
// Current is on the left side of parent.
if ( compare(current -> data, parent -> data) == 0 ||
compare(current -> data, parent -> data) < 0 )
parent -> left = current -> left;
// Current is on the right side of parent.
else
parent -> right = current -> left;
}
// Current immidiate right node is to be connected with parent.
else
{
// Current is on the left side of parent.
if ( compare(current -> data, parent -> data) == 0 ||
compare(current -> data, parent -> data) < 0 )
parent -> left = current -> right;
// Current is on the right side of parent.
else
parent -> right = current -> right;
}
type value = current -> data;
// Delete the node at the end.
delete_node(current);
std::cout << "Node " << value.toString() << " (one child node) deleted from the tree." << '\n';
}
// CASE-III Node has two children
else
{
type value = current -> data;
AVLNode* successsor = current -> right;
AVLNode* parent_successor = current;
// Find in-order successor.
while (successsor -> left != NULL)
{
parent_successor = successsor;
successsor = successsor -> left;
}
// Copy over the contents from successor node.
current -> data = successsor -> data;
// CASE-(a) One chid exists to the right of successor node.
if (successsor -> right != NULL)
{
// Attach parent with immidiate right node.
parent_successor -> left = successsor -> right;
delete_node(successsor);
}
// CASE-(b) No child exists of the successor node.
else
{
delete_node(successsor);
// Adjust parent links
if ( compare(parent_successor -> data, value) == 0 ||
compare(parent_successor -> data, value) < 0 )
parent_successor -> left = NULL;
else
parent_successor -> right = NULL;
}
std::cout << "Node " << value.toString() << " (two child node) deleted from the tree." << '\n';
}
// Rebuild Tree from the traversed path updating height and BF values in each step.
rebuild(&stk);
}
}
// Print Binary Tree Level-by-Level Horizontally.
void print()
{
print("", root, false);
}
};