-
Notifications
You must be signed in to change notification settings - Fork 1
/
btree.c
345 lines (283 loc) · 6.79 KB
/
btree.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
/* btree.c - A binary tree implementation */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "btree.h"
#include "btreep.h"
/* ---------- Private function declarations ---------- */
static btnode node_Minimum(btnode);
static btnode node_Maximum(btnode);
static btnode node_Search(BTREE, btnode, void *);
static void node_Close(btnode);
static btnode node_Successor(btnode);
static btnode node_Make(BTREE tree, void *data);
static void btree_reallyPrint(BTREE, btnode, FILE *, unsigned int);
/* ---------------------------------------------------------------------- */
BTREE btree_Create(size_t size, int (*cmp)(const void *, const void *))
{
BTREE ret;
ret = malloc(sizeof(struct _btree_struct));
if (ret) {
ret->root = NULL;
ret->node_size = sizeof(struct node);
ret->elem_size = size;
ret->cmp = cmp;
}
return ret;
}
/* ---------------------------------------------------------------------- */
int btree_Search(BTREE tree, void *key, void **ret)
{
btnode node;
node = root(tree);
node = node_Search(tree, node, key);
if (node) {
if (ret) *ret = data(tree, node); /*data_copy(tree, ret, data(tree, node));*/
return 0;
}
else {
return 1;
}
}
/* ---------------------------------------------------------------------- */
static btnode node_Search(BTREE tree, btnode node, void *key)
{
int result;
if (node) {
while (node && (result = data_compare(tree, data(tree, node), key))) {
if (result > 0) {
node = left(node);
}
else {
node = right(node);
}
}
}
return node;
}
/* ---------------------------------------------------------------------- */
int btree_Minimum(BTREE tree, void *ret)
{
btnode node;
node = node_Minimum(root(tree));
if (node) {
data_copy(tree, ret, data(tree, node));
return 0;
}
return 1;
}
/* ---------------------------------------------------------------------- */
static btnode node_Minimum(btnode node)
{
if (node) {
while (left(node)) {
node = left(node);
}
}
return node;
}
/* ---------------------------------------------------------------------- */
int btree_Maximum(BTREE tree, void *ret)
{
btnode node;
node = node_Maximum(root(tree));
if (node) {
data_copy(tree, ret, data(tree, node));
return 0;
}
return 1;
}
/* ---------------------------------------------------------------------- */
static btnode node_Maximum(btnode node)
{
if (node) {
while (right(node)) {
node = right(node);
}
}
return node;
}
/* ---------------------------------------------------------------------- */
int btree_Empty(BTREE tree)
{
return root(tree) ? 0 : 1;
}
/* ---------------------------------------------------------------------- */
int btree_Successor(BTREE tree, void *key, void *ret)
{
btnode node;
if (!root(tree)) return 1;
node = node_Search(tree, root(tree), key);
if (!node) return 1;
node = node_Successor(node);
if (node) {
data_copy(tree, ret, data(tree, node));
return 0;
}
return 1;
}
/* ---------------------------------------------------------------------- */
static btnode node_Successor(btnode node)
{
btnode node2;
if (right(node)) {
node = node_Minimum(right(node));
}
else {
node2 = parent(node);
while (node2 && node == right(node2)) {
node = node2;
node2 = parent(node);
}
node = node2;
}
return node;
}
/* ---------------------------------------------------------------------- */
int btree_Predecessor(BTREE tree, void *key, void *ret)
{
btnode node, node2;
if (!root(tree)) return 1;
node = node_Search(tree, root(tree), key);
if (!node) return 1;
if (left(node)) {
node = node_Maximum(left(node));
}
else {
node2 = parent(node);
while (node2 && node == left(node2)) {
node = node2;
node2 = parent(node);
}
node = node2;
}
if (node) {
data_copy(tree, ret, data(tree, node));
return 0;
}
return 1;
}
/* ---------------------------------------------------------------------- */
int btree_Insert(BTREE tree, void *data)
{
btnode parent, node, newnode;
newnode = node_Make(tree, data);
if (!newnode) {
return 1;
}
parent = NULL;
node = root(tree);
while (node) {
parent = node;
if (data_compare(tree, data, data(tree, node)) < 0) {
node = left(node);
}
else {
node = right(node);
}
}
parent(newnode) = parent;
if (!parent) {
root(tree) = newnode;
}
else {
if (data_compare(tree, data, data(tree, parent)) < 0) {
left(parent) = newnode;
}
else {
right(parent) = newnode;
}
}
return 0;
}
/* ---------------------------------------------------------------------- */
int btree_Delete(BTREE tree, void *data)
{
btnode node;
btnode remove;
btnode other;
if (!root(tree)) return 1;
node = node_Search(tree, root(tree), data);
if (!node) return 1;
if (!left(node) || !right(node)) {
remove = node;
}
else {
remove = node_Successor(node);
}
if (left(remove)) {
other = left(remove);
}
else {
other = right(remove);
}
if (other) {
parent(other) = parent(remove);
}
if (!parent(remove)) {
root(tree) = other;
}
else {
if (remove == left(parent(remove))) {
left(parent(remove)) = other;
}
else {
right(parent(remove)) = other;
}
}
if (node != remove) {
data_copy(tree, data(tree, node), data(tree, remove));
}
free(node);
return 0;
}
/* ---------------------------------------------------------------------- */
void btree_Destroy(BTREE tree)
{
if (root(tree)) {
node_Close(root(tree));
}
free(tree);
}
/* ---------------------------------------------------------------------- */
static void node_Close(btnode node)
{
if (left(node)) {
node_Close(left(node));
}
if (right(node)) {
node_Close(right(node));
}
free(node);
}
/* ---------------------------------------------------------------------- */
static btnode node_Make(BTREE tree, void *data)
{
btnode ret;
ret = malloc(node_size(tree) + elem_size(tree));
if (ret) {
data_copy(tree, data(tree, ret), data);
parent(ret) = left(ret) = right(ret) = NULL;
}
return ret;
}
/* ------------------------------------------------------------------------ */
/*
Note that this routine assumes that the data elements are char *'s
*/
void btree_print(BTREE tree, void *where)
{
btree_reallyPrint(tree, root(tree), (FILE *)where, 2);
}
/* ------------------------------------------------------------------------ */
void btree_reallyPrint(BTREE tree, btnode node, FILE *where, unsigned int spaces)
{
fprintf(where, "%*s%s\n", spaces, " ",
(node ?
*(char **)data(tree, node) :
"NULL"));
if (node) {
btree_reallyPrint(tree, left(node), where, spaces + 4);
btree_reallyPrint(tree, right(node), where, spaces + 4);
}
}