-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraal_avl.c
371 lines (301 loc) · 7.71 KB
/
graal_avl.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
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
/************************************************************************
* A big part of this code come from http://www.eternallyconfuzzled.com. *
* Thanks to the author of this website for its sharing. *
* *
* Copyright © 2016 Matthieu Dien <[email protected]> *
* This work is free. You can redistribute it and/or modify it under the *
* terms of the Do What The Fuck You Want To Public License, Version 2, *
* as published by Sam Hocevar. See the COPYING file for more details. *
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef struct _avl_node {
int balance;
void* data;
struct _avl_node* sons[2];
}* avl_node;
typedef struct _avl_tree {
avl_node root;
}* avl_tree;
/*******************
* Constructors *
*******************/
avl_node make_node(void* data)
{
avl_node r = (avl_node)malloc(sizeof(struct _avl_node));
r->data = data;
r->balance = 0;
r->sons[0] = r->sons[1] = NULL;
return r;
}
avl_tree avl_make_empty_tree(void)
{
avl_tree r = (avl_tree)malloc(sizeof(struct _avl_tree));
r->root = NULL;
return r;
}
/*******************
* Destructor *
*******************/
void erase_tree(avl_node root)
{
if(root != NULL) {
avl_node l = root->sons[0], r = root->sons[1];
free(root);
erase_tree(l);
erase_tree(r);
}
}
void avl_erase_tree(avl_tree tree)
{
erase_tree(tree->root);
free(tree);
}
/*******************
* Search *
*******************/
void* search_r(avl_node root, void* data)
{
if (root == NULL)
return NULL;
else
if (data == root->data)
return root->data;
else
{
int dir = (intptr_t)root->data < (intptr_t)data;
return search_r(root->sons[dir], data);
}
}
void* avl_search(avl_tree tree, void* data)
{
return search_r(tree->root, data);
}
/*******************
* Rotation *
*******************/
// dir == 0 means left rotation and 1 means right
avl_node single_rotation(avl_node root, int dir)
{
avl_node save = root->sons[!dir];
root->sons[!dir] = save->sons[dir];
save->sons[dir] = root;
return save;
}
// dir == 0 means right-left and 1 means left-right
avl_node double_rotation(avl_node root, int dir)
{
root->sons[!dir] = single_rotation(root->sons[!dir], !dir);
return single_rotation(root, dir);
/* avl_node save = root->sons[!dir]->sons[dir]; */
/* root->sons[!dir]->sons[dir] = save->sons[!dir]; */
/* save->sons[!dir] = root->sons[!dir]; */
/* root->sons[!dir] = save; */
/* save = root->sons[!dir]; */
/* root->sons[!dir] = save->sons[dir]; */
/* save->sons[dir] = root; */
/* return save; */
}
/* Adjust the balances before the application of double_rotation
(right-left rotation on the following example)
x
/ \
A y
/ \
z D
/ \
B C
*/
void adjust_balance(avl_node x, int dir, int bal)
{
avl_node y = x->sons[dir];
avl_node z = y->sons[!dir];
// Case: (A ,(x, +2), ((B, (z, 0), C), (y, -1), D))
if (z->balance == 0)
{
x->balance = y->balance = 0;
}
// Case: (A ,(x, +2), ((B, (z, +1), C), (y, -1), D))
else if (z->balance == bal)
{
x->balance = -bal;
y->balance = 0;
}
// Case: (A ,(x, +2), ((B, (z, -1), C), (y, -1), D))
else
{
x->balance = 0;
y->balance = bal;
}
z->balance = 0;
}
/*******************
* Insertion *
*******************/
avl_node insert_balance(avl_node root, int dir)
{
avl_node n = root->sons[dir];
int bal = dir == 0 ? -1 : +1;
if (n->balance == bal)
{
root->balance = n->balance = 0;
root = single_rotation(root, !dir);
}
else /* n->balance == -bal */
{
adjust_balance(root, dir, bal);
root = double_rotation(root, !dir);
}
return root;
}
avl_node insert_node(avl_node root, void* data)
{
/* Empty tree case */
if(root == NULL)
root = make_node(data);
else
{
avl_node head = make_node((void*)0); /* False tree root */
avl_node s, t; /* Place to rebalance and parent */
avl_node p, q; /* Iterator and save pointer */
int dir;
t = head;
t->sons[1] = root;
/* Search down the tree, saving rebalance points */
for (s = p = t->sons[1];; p = q)
{
dir = p->data < data;
q = p->sons[dir];
if (q == NULL)
break;
if (q->balance != 0)
{
t = p;
s = q;
}
}
/* Insert the new node */
p->sons[dir] = q = make_node(data);
/* Update balance factors */
for (p = s; p != q; p = p->sons[dir])
{
dir = p->data < data;
p->balance += dir == 0 ? -1 : +1;
}
q = s; /* Save rebalance point for parent fix */
/* Rebalance if necessary */
if (abs(s->balance) > 1)
{
dir = s->data < data;
s = insert_balance(s, dir);
}
/* Fix parent */
if (q == head->sons[1])
root = s;
else
t->sons[q == t->sons[1]] = s;
}
return root;
}
avl_tree avl_insert(avl_tree tree, void* data)
{
avl_node root = insert_node(tree->root, data);
tree->root = root;
return tree;
}
/*******************
* Deletion *
*******************/
avl_node remove_balance(avl_node root, int dir, int *done)
{
avl_node n = root->sons[!dir];
int bal = dir == 0 ? -1 : +1;
if (n->balance == -bal)
{
root->balance = n->balance = 0;
root = single_rotation(root, dir);
}
else if (n->balance == bal)
{
adjust_balance(root, !dir, -bal);
root = double_rotation(root, dir);
}
else /* n->balance == 0 */
{
root->balance = -bal;
n->balance = bal;
root = single_rotation(root, dir);
*done = 1;
}
return root;
}
avl_node remove_node(avl_node root, void* data, int* done)
{
if (root != NULL)
{
int dir;
/* Remove node */
if (root->data == data)
{
/* Unsons and fix parent */
if (root->sons[0] == NULL || root->sons[1] == NULL)
{
avl_node save;
dir = root->sons[0] == NULL;
save = root->sons[dir];
free(root);
return save;
}
else
{
/* Find inorder predecessor */
avl_node heir = root->sons[0];
while (heir->sons[1] != NULL)
heir = heir->sons[1];
/* Copy and set new search data */
root->data = heir->data;
data = heir->data;
}
}
dir = root->data < data;
root->sons[dir] = remove_node(root->sons[dir], data, done);
if (!*done)
{
/* Update balance factors */
root->balance += dir != 0 ? -1 : +1;
/* Terminate or rebalance as necessary */
if (abs(root->balance) == 1)
*done = 1;
else if (abs(root->balance) > 1)
root = remove_balance(root, dir, done);
}
}
else
*done = 1;
return root;
}
avl_tree avl_remove(avl_tree tree, void* data)
{
int done = 0;
tree->root = remove_node(tree->root, data, &done);
return tree;
}
/*******************
* Debug *
*******************/
void traverse(avl_node root)
{
if(root != NULL)
{
traverse(root->sons[0]);
printf("%d ", (int)(intptr_t)root->data);
traverse(root->sons[1]);
}
}
void avl_traverse_and_print(avl_tree tree)
{
printf("[ ");
traverse(tree->root);
printf("]\n");
}