-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavl.c
More file actions
347 lines (317 loc) · 8.15 KB
/
avl.c
File metadata and controls
347 lines (317 loc) · 8.15 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
/* AVL Tree Implementation */
#define QSIZE 50
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct avlnode {
int m_key;
char m_height;
struct avlnode *m_left;
struct avlnode *m_right;
};
struct avltree {
struct avlnode *m_root;
int m_cnt;
};
struct avlnode *initavlnode(int);
struct avltree *initavltree();
struct avlnode *avltreeput(struct avltree *, int);
struct avlnode *avltreeputn(struct avltree *, struct avlnode *, int);
void freeavltree(struct avltree *);
void freeavlnode(struct avlnode *);
int max(int a, int b);
char getbalance(struct avlnode *n);
char getheight(struct avlnode *n);
void setheight(struct avlnode *n);
struct avlnode *balance(struct avlnode *n);
struct avlnode *rotateleft(struct avlnode *n);
struct avlnode *rotateright(struct avlnode *n);
int avltreeisvalid(struct avltree *);
int avltreeisvalidn(struct avlnode *);
void avltreeprint(struct avltree *);
void preorder(struct avlnode *);
void inorder(struct avlnode *);
void postorder(struct avlnode *);
struct avlnode *avlremovemin(struct avltree *t);
struct avlnode *avlremoveminn(struct avltree *t, struct avlnode *n);
struct avlnode *avlremovemax(struct avltree *t);
struct avlnode *avlremovemaxn(struct avltree *t, struct avlnode *n);
struct avlnode *avltreeremove(struct avltree *, int);
struct avlnode *avltreeremoven(struct avltree *, struct avlnode *, int);
int main(int agrc, char *argv[])
{
struct avltree *t = initavltree();
srand(time(0));
for (int i=0; i<30; i++) {
avltreeput(t, rand() % 100 +1);
if (!avltreeisvalid(t))
break;
}
/* postorder(t->m_root); */
/* printf("\n"); */
avltreeprint(t);
printf("%d\n", avltreeisvalid(t));
while (t->m_cnt && avltreeisvalid(t)) {
/* avltreeremove(t, t->m_root->m_key); */
avlremovemin(t);
/* avlremovemax(t); */
avltreeprint(t);
}
printf("%d\n", avltreeisvalid(t));
freeavltree(t);
return 0;
}
struct avlnode *initavlnode(int key)
{
struct avlnode *n = (struct avlnode *) malloc(sizeof(struct avlnode));
if (n != NULL) {
n->m_key = key;
n->m_height = 1;
n->m_left = n->m_right = NULL;
}
return n;
}
struct avltree *initavltree()
{
struct avltree *t = (struct avltree *) malloc(sizeof(struct avltree));
if (t) {
t->m_root = NULL;
t->m_cnt = 0;
}
return t;
}
void freeavltree(struct avltree *t)
{
freeavlnode(t->m_root);
free(t);
}
void freeavlnode(struct avlnode *n)
{
if (n == NULL)
return;
freeavlnode(n->m_left);
freeavlnode(n->m_right);
free(n);
}
int avltreeisvalid(struct avltree *t)
{
return avltreeisvalidn(t->m_root) != -1;
}
int avltreeisvalidn(struct avlnode *n)
{
if (n == NULL)
return 0;
if ((n->m_left != NULL && n->m_left->m_key > n->m_key) || (n->m_right != NULL && n->m_right->m_key < n->m_key)) {
printf("ERROR => not a BST. %d\n", n->m_key);
return -1;
}
int lh, rh;
lh = avltreeisvalidn(n->m_left);
rh = avltreeisvalidn(n->m_right);
if (lh == -1 || rh == -1)
return -1;
if (abs(getbalance(n)) > 1) {
printf("ERROR => balance is out or range -1..1. %d\n", n->m_key);
return -1;
}
if (getheight(n) != 1 + max(lh, rh)) {
printf("ERROR => heights don't match. %d\n", n->m_key);
return -1;
}
return 1 + max(lh, rh);
}
void preorder(struct avlnode *n)
{
if (n == NULL)
return;
printf("%d,%d ", n->m_key, n->m_height);
preorder(n->m_left);
preorder(n->m_right);
}
void inorder(struct avlnode *n)
{
if (n == NULL)
return;
inorder(n->m_left);
printf("%d,%d ", n->m_key, n->m_height);
inorder(n->m_right);
}
void postorder(struct avlnode *n)
{
if (n == NULL)
return;
postorder(n->m_left);
postorder(n->m_right);
printf("%d,%d ", n->m_key, n->m_height);
}
void avltreeprint(struct avltree *t)
{
int qc = 0, qh = -1, qt = -1;
struct avlnode *qq[QSIZE];
if (t->m_root == NULL)
return;
qq[++qh % QSIZE] = t->m_root; qc++;
while (qc > 0) {
int lcnt = qc;
while (lcnt > 0) {
struct avlnode *n = qq[++qt % QSIZE]; qc--;
if (n != NULL) {
printf("%d,%d ", n->m_key, getheight(n));
qq[++qh % QSIZE] = n->m_left; qc++;
qq[++qh % QSIZE] = n->m_right; qc++;
}
else
printf("*,* ");
lcnt--;
}
printf("\n");
}
}
int max(int a, int b)
{
return (a > b ? a : b);
}
char getbalance(struct avlnode *n)
{
return (getheight(n->m_right) - getheight(n->m_left));
}
char getheight(struct avlnode *n)
{
return (n == NULL ? 0 : n->m_height);
}
void setheight(struct avlnode *n)
{
n->m_height = 1 + max(getheight(n->m_left), getheight(n->m_right));
}
struct avlnode *balance(struct avlnode *n)
{
char bal = getbalance(n);
if (bal == -2) {
if (getbalance(n->m_left) == 1)
n->m_left = rotateleft(n->m_left);
n = rotateright(n);
}
else if (bal == 2) {
if (getbalance(n->m_right) == -1)
n->m_right = rotateright(n->m_right);
n = rotateleft(n);
}
else
setheight(n);
return n;
}
struct avlnode *rotateright(struct avlnode *n)
{
printf("Rotate right. %d\n", n->m_key);
struct avlnode *t = n->m_left;
n->m_left = t->m_right;
t->m_right = n;
setheight(n);
setheight(t);
return t;
}
struct avlnode *rotateleft(struct avlnode *n)
{
printf("Rotate left. %d\n", n->m_key);
struct avlnode *t = n->m_right;
n->m_right = t->m_left;
t->m_left = n;
setheight(n);
setheight(t);
return t;
}
struct avlnode *avltreeput(struct avltree *t, int key)
{
printf("** Inserting => %d\n", key);
t->m_root = avltreeputn(t, t->m_root, key);
return t->m_root;
}
struct avlnode *avltreeputn(struct avltree *t, struct avlnode *n, int key)
{
if (n == NULL) {
n = initavlnode(key);
if (n != NULL)
t->m_cnt++;
return n;
}
if (key == n->m_key)
return n;
if (key < n->m_key)
n->m_left = avltreeputn(t, n->m_left, key);
else
n->m_right = avltreeputn(t, n->m_right, key);
return balance(n);
}
struct avlnode *avlremovemin(struct avltree *t)
{
printf("** Removing min\n");
if (t->m_root == NULL)
return NULL;
t->m_root = avlremoveminn(t, t->m_root);
return NULL;
}
struct avlnode *avlremoveminn(struct avltree *t, struct avlnode *n)
{
if (n->m_left == NULL) {
struct avlnode *q = n->m_right;
t->m_cnt--;
free(n);
return q;
}
n->m_left = avlremoveminn(t, n->m_left);
return balance(n);
}
struct avlnode *avlremovemax(struct avltree *t)
{
printf("** Removing max\n");
if (t->m_root == NULL)
return NULL;
t->m_root = avlremovemaxn(t, t->m_root);
return NULL;
}
struct avlnode *avlremovemaxn(struct avltree *t, struct avlnode *n)
{
if (n->m_right == NULL) {
struct avlnode *q = n->m_left;
t->m_cnt--;
free(n);
return q;
}
n->m_right = avlremovemaxn(t, n->m_right);
return balance(n);
}
struct avlnode *avltreeremove(struct avltree *t, int key)
{
printf("** Removing => %d\n", key);
t->m_root = avltreeremoven(t, t->m_root, key);
return NULL;
}
struct avlnode *avltreeremoven(struct avltree *t, struct avlnode *n, int key)
{
if (n == NULL)
return n; // not found
if (key < n->m_key)
n->m_left = avltreeremoven(t, n->m_left, key);
else if (key > n->m_key)
n->m_right = avltreeremoven(t, n->m_right, key);
else {
if (n->m_left != NULL && n->m_right != NULL) {
struct avlnode *s = n->m_right;
while (s->m_left != NULL)
s = s->m_left;
n->m_key = s->m_key;
n->m_right = avltreeremoven(t, n->m_right, s->m_key);
}
else {
struct avlnode *q = n;
if (n->m_left == NULL)
n = n->m_right;
else
n = n->m_left;
free(q);
t->m_cnt--;
return n;
}
}
return balance(n);
}