-
Notifications
You must be signed in to change notification settings - Fork 39
/
avl_balanced_tree.c
248 lines (216 loc) · 5.91 KB
/
avl_balanced_tree.c
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
/*
* Date: 2017-11-15
*
* Description:
* Implement AVL tree with insert and delete operations.
* AVL tree is a balanced tree in which height of left and right sub trees is
* not more than 1 for every node. This can be achieved by rotating the tree
* after every insert and delete operations if not balanced.
*
* Reference:
* http://www.geeksforgeeks.org/avl-tree-set-1-insertion/
* http://www.geeksforgeeks.org/avl-tree-set-2-deletion/
*
* Complexity:
* All operations can be done in log(n) time complexity in AVL trees.
*/
#include "stdio.h"
#include "stdlib.h"
typedef struct node {
int key;
struct node *left;
struct node *right;
int height;
}node;
int height(node *N) {
if (N == NULL)
return 0;
else
return N->height;
}
int get_balance_factor(node *N) {
if (N == NULL)
return 0;
else
return (height(N->left) - height(N->right));
}
void inorder(node *root) {
if (root != NULL) {
inorder(root->left);
printf("%d[%d] ", root->key, get_balance_factor(root));
inorder(root->right);
}
}
node *new_node(int key) {
node *new = (node *)malloc(sizeof(node));
new->key = key;
new->left = NULL;
new->right = NULL;
new->height = 1;
return new;
}
int max(int a, int b) {
return (a > b ? a : b);
}
node* right_rotate(node *y) {
node *x = y->left;
node *t = x->right;
// Update pointers to perform rotation.
x->right = y;
y->left = t;
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
// New root
return x;
}
node* left_rotate(node *x) {
node *y = x->right;
node *t = y->left;
y->left = x;
x->right = t;
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
// New root
return y;
}
node* insert(node *root, int k) {
int balance_factor = 0;
if (root == NULL)
return new_node(k);
else if (root->key > k)
root->left = insert(root->left, k);
else if (root->key < k)
root->right = insert(root->right, k);
else {
printf("[ERROR]: Duplicate key[%d] not allowed\n", k);
return root;
}
// Update height of each node traversed while insertion.
root->height = max(height(root->left), height(root->right)) + 1;
// Get balance factor to this ancestor node.
balance_factor = get_balance_factor(root);
printf("After adding node: %d, Balance factor of node: %d is %d\n",
k, root->key, balance_factor);
if (balance_factor > 1) { // Left sub-tree is heavy
if(k < root->left->key) { // Case 1: left-left case
printf("Left Left Case\n");
return right_rotate(root); // Right rotate
}
else { // Case 2: left-right case
printf("Left Right Case\n");
root->left = left_rotate(root->left); // Left rotate
return right_rotate(root); // Right rotate
}
}
else if(balance_factor < -1) { // Right sub-tree is heavy
if(k > root->right->key) { // Case 3: right-right case
printf("Right Right Case\n");
return left_rotate(root); // Left rotate
}
else { // Case 4: right-left case
printf("Right Left Case\n");
root->right = right_rotate(root->right); // Right rotate
return left_rotate(root); // Left rotate
}
}
else
printf("Node: %d already balanced\n", root->key);
return root;
}
node* find_min(node *N) {
while (N->left != NULL)
N = N->left;
return N;
}
node* delete(node *root, int k) {
if (root == NULL)
return root;
else if (root->key > k)
root->left = delete(root->left, k);
else if (root->key < k)
root->right = delete(root->right, k);
else
{
if (root->left == NULL) {
node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL) {
node *temp = root->left;
free(root);
return root;
}
else {
// Find in-order successor
node *min = find_min(root->right);
root->key = min->key;
root->right = delete(root->right, min->key);
}
}
// After deletion if tree is empty then return
if (root == NULL)
return root;
root->height = max(height(root->left), height(root->right)) + 1;
int balance_factor = get_balance_factor(root);
if (balance_factor > 1) { // Left sub-tree is heavy
if (k > root->left->key) { // Case 1: Left Left case
printf("Left Left Case\n");
return right_rotate(root); // Right rotate
}
else { // Case 2: Left Right case
printf("Left Right Case\n");
root->left = left_rotate(root->left); // Left rotate
return right_rotate(root); // Right rotate
}
}
else if (balance_factor < -1) { // Right subtree is heavy
if (k < root->right->key) { // Case 3: Right right case
printf("Right Right Case\n");
return left_rotate(root); // Left rotate
}
else { // Case 4: Right left case
printf("Right Left Case\n");
root->right = right_rotate(root->right); // Right rotate
return left_rotate(root); // Left rotate
}
}
else
printf("Node: %d already balanced\n", root->key);
return root;
}
void print_array(int arr[], int n, char *msg) {
int i = 0;
printf("*********** %s *****************\n", msg);
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n\n");
}
int main() {
int i = 0;
int n = 0;
int *a = NULL;
int element = 0;
node *root = NULL;
printf("Enter number of elements : ");
scanf("%d",&n);
a = (int *)malloc(sizeof(int)*n);
for (i = 0; i < n; i++) {
printf("Enter element [%d] : ", i);
scanf("%d",&a[i]);
}
print_array(a, n, "Inserted Array");
// Insert in AVL and balance it.
for (i = 0; i < n; i++)
root = insert(root, a[i]);
printf("***************** Inorder traversal *************\n");
inorder(root);
printf("\n\n");
printf("Enter element to be deleted: ");
scanf("%d",&element);
root = delete(root, element);
printf("***************** Inorder traversal *************\n");
inorder(root);
printf("\n\n");
return 0;
}