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
|
#include <stdlib.h>
#include <search.h>
/*
avl tree implementation using recursive functions
the height of an n node tree is less than 1.44*log2(n+2)-1
(so the max recursion depth in case of a tree with 2^32 nodes is 45)
*/
struct node {
const void *key;
struct node *left;
struct node *right;
int height;
};
static int delta(struct node *n) {
return (n->left ? n->left->height:0) - (n->right ? n->right->height:0);
}
static void updateheight(struct node *n) {
n->height = 0;
if (n->left && n->left->height > n->height)
n->height = n->left->height;
if (n->right && n->right->height > n->height)
n->height = n->right->height;
n->height++;
}
static struct node *rotl(struct node *n) {
struct node *r = n->right;
n->right = r->left;
r->left = n;
updateheight(n);
updateheight(r);
return r;
}
static struct node *rotr(struct node *n) {
struct node *l = n->left;
n->left = l->right;
l->right = n;
updateheight(n);
updateheight(l);
return l;
}
static struct node *balance(struct node *n) {
int d = delta(n);
if (d < -1) {
if (delta(n->right) > 0)
n->right = rotr(n->right);
return rotl(n);
} else if (d > 1) {
if (delta(n->left) < 0)
n->left = rotl(n->left);
return rotr(n);
}
updateheight(n);
return n;
}
static struct node *find(struct node *n, const void *k,
int (*cmp)(const void *, const void *))
{
int c;
if (!n)
return 0;
c = cmp(k, n->key);
if (c == 0)
return n;
if (c < 0)
return find(n->left, k, cmp);
else
return find(n->right, k, cmp);
}
static struct node *insert(struct node *n, const void *k,
int (*cmp)(const void *, const void *), struct node **found)
{
struct node *r;
int c;
if (!n) {
n = malloc(sizeof *n);
if (n) {
n->key = k;
n->left = n->right = 0;
n->height = 1;
}
*found = n;
return n;
}
c = cmp(k, n->key);
if (c == 0) {
*found = n;
return 0;
}
r = insert(c < 0 ? n->left : n->right, k, cmp, found);
if (r) {
if (c < 0)
n->left = r;
else
n->right = r;
r = balance(n);
}
return r;
}
static struct node *remove_rightmost(struct node *n, struct node **rightmost)
{
if (!n->right) {
*rightmost = n;
return n->left;
}
n->right = remove_rightmost(n->right, rightmost);
return balance(n);
}
static struct node *remove(struct node **n, const void *k,
int (*cmp)(const void *, const void *), struct node *parent)
{
int c;
if (!*n)
return 0;
c = cmp(k, (*n)->key);
if (c == 0) {
struct node *r = *n;
if (r->left) {
r->left = remove_rightmost(r->left, n);
(*n)->left = r->left;
(*n)->right = r->right;
*n = balance(*n);
} else
*n = r->right;
free(r);
return parent;
}
if (c < 0)
parent = remove(&(*n)->left, k, cmp, *n);
else
parent = remove(&(*n)->right, k, cmp, *n);
if (parent)
*n = balance(*n);
return parent;
}
void *tdelete(const void *restrict key, void **restrict rootp,
int(*compar)(const void *, const void *))
{
if (!rootp)
return 0;
struct node *n = *rootp;
struct node *ret;
/* last argument is arbitrary non-null pointer
which is returned when the root node is deleted */
ret = remove(&n, key, compar, n);
*rootp = n;
return ret;
}
void *tfind(const void *key, void *const *rootp,
int(*compar)(const void *, const void *))
{
if (!rootp)
return 0;
return find(*rootp, key, compar);
}
void *tsearch(const void *key, void **rootp,
int (*compar)(const void *, const void *))
{
struct node *update;
struct node *ret;
if (!rootp)
return 0;
update = insert(*rootp, key, compar, &ret);
if (update)
*rootp = update;
return ret;
}
static void walk(const struct node *r, void (*action)(const void *, VISIT, int), int d)
{
if (r == 0)
return;
if (r->left == 0 && r->right == 0)
action(r, leaf, d);
else {
action(r, preorder, d);
walk(r->left, action, d+1);
action(r, postorder, d);
walk(r->right, action, d+1);
action(r, endorder, d);
}
}
void twalk(const void *root, void (*action)(const void *, VISIT, int))
{
walk(root, action, 0);
}
|