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
|
#include "avl_tree.h"
#include <stdio.h>
#include <stdlib.h>
/**
* Print a visual representation of an AVL Tree.
*
* @param tree The subtree to print
* @param level The level in the tree that this subtree is in
*/
static void print_tree(AVLTree *tree, int level) {
for (int i = 0; i < level; i++)
printf(" ");
if (tree) {
printf("(%d:%d)\n", tree->value, tree->height);
if (!tree->left && !tree->right)
return;
print_tree(tree->left, level + 1);
print_tree(tree->right, level + 1);
} else {
printf("( )\n");
}
}
/*
* Determines if a integer value is evenly divisibly by 2.
*
* @param value The integer to check
* @return true when the value is even otherwise false
*/
static bool is_even(int value) { return value % 2 == 0; }
/*
* Determines if a integer value is an odd number
*
* @param value The integer to check
* @return true when the value is odd otherwise false
*/
static bool is_odd(int value) { return !is_even(value); }
/**
* Converts an AVL tree to a Red Black tree with all
* nodes in the tree coloured black.
*
* @param tree The AVL subtree to convert
* @param parent The parent node of the current subtree. Use NULL for the root.
* @return The converted Red Black tree
*/
static RBTree *to_rb_tree(AVLTree *tree, AVLTree *parent) {
if (!tree)
return NULL;
RBTree *rb_tree = rb_tree_initialize(tree->value);
rb_tree->left = to_rb_tree(tree->left, tree);
if (rb_tree->left)
rb_tree->left->parent = rb_tree;
rb_tree->right = to_rb_tree(tree->right, tree);
if (rb_tree->right)
rb_tree->right->parent = rb_tree;
return rb_tree;
}
/**
* Applies the correct colouring to each descendant node in a Red Black tree.
*
* @param tree The Red Black subtree to colour
* @param colour The colour to apply to the provided subtree node.
*/
static void change_colour(RBTree *tree, enum Colour colour) {
if (!tree)
return;
int l = rb_tree_height(tree->left);
int r = rb_tree_height(tree->right);
tree->colour = colour;
change_colour(tree->left, l < r || is_odd(l) ? black : red);
change_colour(tree->right, r < l || is_odd(r) ? black : red);
}
/**
* Returns the larger integer between the two provided as arguments.
*
* @param a An integer value to compare
* @param b Another integer value to compare
* @return Returns the larger value
*/
static int max(int a, int b) { return (a > b) ? a : b; }
/**
* Returns the height of an AVL subtree.
*
* @param tree The subtree to interrogate.
* @return The height of the subtree
*/
static int height_of(AVLTree *tree) { return tree == NULL ? 0 : tree->height; }
/**
* Returns the smallest value stored in the AVL tree.
*
* @param tree The subtree to traverse to find the smallest value.
* @return The subtree node containing the smallest value in the tree.
*/
static AVLTree *smallest(AVLTree *tree) {
AVLTree *current = tree;
while (current && current->left != NULL)
current = current->left;
return current;
}
/**
* Performs a right rotation on an AVL subtree
*
* @param y The subtree to perform the rotation on
* @return The new root after the rotation is performed.
*/
static AVLTree *rotate_right(AVLTree *y) {
AVLTree *x = y->left;
AVLTree *t = x->right;
x->right = y;
y->left = t;
y->height = max(height_of(y->left), height_of(y->right)) + 1;
x->height = max(height_of(x->left), height_of(x->right)) + 1;
return x;
}
/**
* Performs a left rotation on an AVL subtree
*
* @param x The subtree to perform the rotation on
* @return The new root after the rotation is performed.
*/
static AVLTree *rotate_left(AVLTree *x) {
AVLTree *y = x->right;
AVLTree *t = y->left;
y->left = x;
x->right = t;
x->height = max(height_of(x->left), height_of(x->right)) + 1;
y->height = max(height_of(y->left), height_of(y->right)) + 1;
return y;
}
/**
* Calculates the balance of a subtree by taking the difference
* of the height of the left subtree and the right subtree.
*
* @param tree The tree to investigate.
* @return The balace
*/
static int balance_of(AVLTree *tree) {
return (tree == NULL) ? 0 : height_of(tree->left) - height_of(tree->right);
}
/**
* Compares two integers and returns -1, 0, 1.
* If a is equal to b then 0 is returned.
* If a is greater than b then 1 is returned.
* If a is less than b then -1 is returned.
*
* @param a An integer
* @param b Another integer
* @return Returns 0, 1, or -1.
*/
static int compare(int a, int b) { return (a < b) ? -1 : ((a > b) ? 1 : 0); }
/**
* Initializes an instance of an AVL tree.
*
* @param value The value to assign to the new node in the tree.
* @return Returns the new AVL tree node instance.
*/
AVLTree *avl_tree_initialize(int value) {
AVLTree *tree = malloc(sizeof(AVLTree));
tree->value = value;
tree->left = NULL;
tree->right = NULL;
tree->height = 1;
return tree;
}
/**
* Computes the # of nodes stored in an AVL subtree.
*
* @param tree The subtree to investigate.
* @return Returns the # of descendant nodes found in the subtree.
*/
int avl_tree_size(AVLTree *tree) {
int total = 0;
if (tree == NULL)
return total;
if (tree->left)
total += avl_tree_size(tree->left);
if (tree->right)
total += avl_tree_size(tree->right);
return total + 1;
}
/**
* Inserts a new value into an AVL subtree.
*
* @param tree The subtree to attempt to insert a new value into.
* @param value The value to insert.
* @return Returns the new root of the subtree.
*/
AVLTree *avl_tree_insert(AVLTree *tree, int value) {
if (tree == NULL)
return avl_tree_initialize(value);
switch (compare(value, tree->value)) {
case -1:
tree->left = avl_tree_insert(tree->left, value);
break;
case 1:
tree->right = avl_tree_insert(tree->right, value);
break;
default:
return tree;
}
tree->height = 1 + max(height_of(tree->left), height_of(tree->right));
int balance = balance_of(tree);
if (balance > 1 && value < tree->left->value)
return rotate_right(tree);
if (balance < -1 && value > tree->right->value)
return rotate_left(tree);
if (balance > 1 && value > tree->left->value) {
tree->left = rotate_left(tree->left);
return rotate_right(tree);
}
if (balance < -1 && value < tree->right->value) {
tree->right = rotate_right(tree->right);
return rotate_left(tree);
}
return tree;
}
/**
* Deletes a value from an AVL subtree.
*
* @param tree The subtree to search to find the value to delete.
* @param value The value to search for.
* @return Returns the new root of the subtree.
*/
AVLTree *avl_tree_delete(AVLTree *tree, int value) {
if (tree == NULL)
return tree;
switch (compare(value, tree->value)) {
case -1:
tree->left = avl_tree_delete(tree->left, value);
break;
case 1:
tree->right = avl_tree_delete(tree->right, value);
break;
default:
if (tree->left && tree->right) {
AVLTree *min = smallest(tree->right);
tree->value = min->value;
tree->right = avl_tree_delete(tree->right, min->value);
} else {
AVLTree *tmp = tree->left ? tree->left : tree->right;
if (tmp) {
*tree = *tmp;
free(tmp);
} else {
free(tree);
return NULL;
}
}
break;
}
tree->height = 1 + max(height_of(tree->left), height_of(tree->right));
int balance = balance_of(tree);
if (balance > 1 && balance_of(tree->left) >= 0)
return rotate_right(tree);
if (balance > 1 && balance_of(tree->left) < 0) {
tree->left = rotate_left(tree->left);
return rotate_right(tree);
}
if (balance < -1 && balance_of(tree->right) <= 0)
return rotate_left(tree);
if (balance < -1 && balance_of(tree->right) > 0) {
tree->right = rotate_right(tree->right);
return rotate_left(tree);
}
return tree;
}
/**
* Converts an AVL tree to a Red Black tree.
*
* @param tree The AVL tree to convert.
* @return Returns a new Red Black tree.
*/
RBTree *avl_tree_to_rb_tree(AVLTree *tree) {
if (!tree)
return NULL;
RBTree *rb_tree = to_rb_tree(tree, NULL);
change_colour(rb_tree, black);
return rb_tree;
}
/**
* Prints a visual inspection of
* an AVL tree for debugging purposes to stdout.
*
* @param tree The tree to visualize
*/
void avl_tree_inspect(AVLTree *tree) { print_tree(tree, 0); }
|