-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtree1.1.c
376 lines (348 loc) · 9.42 KB
/
btree1.1.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
372
373
374
375
376
/* btree1.1.c
*
* Author: Jimmy Pan
* Email: [email protected]
* Date: Mon May 20 23:24:44 CST 2013
*
* Copyright (C) 2013 Jimmy Pan
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* Algorithm
* Input:
* Output:
* Functions:
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "algorithms.h"
#define MIN_CHILD_NUM 10
#define MAX_CHILD_NUM (MIN_CHILD_NUM * 2)
#define MIN_KEY_NUM (MIN_CHILD_NUM - 1)
#define MAX_KEY_NUM (MAX_CHILD_NUM - 1)
#define KEY_NUM 1000
#define KEY_MAX 1000
enum leaf { NONLEAF = 0, LEAF = 1 };
struct btree_node {
int key_num;
int keys[MAX_KEY_NUM];
struct btree_node *children[MAX_CHILD_NUM];
int leaf;
};
static struct btree_node *bt_alloc_init_btree_node(int leaf)
{
struct btree_node *tmp;
tmp = malloc(sizeof *tmp);
memset(tmp, 0, sizeof *tmp);
tmp->leaf = leaf;
return tmp;
}
static int bt_btree_node_is_max_size(struct btree_node *n)
{
return n->key_num >= MAX_KEY_NUM;
}
static int bt_btree_node_is_min_size(struct btree_node *n)
{
return n->key_num <= MIN_KEY_NUM;
}
/* index is children index, index can be last child*/
static void
bt_btree_node_shift_left_from_index(struct btree_node *n, int index)
{
int i;
if (index == 0)
index = 1;
for (i = index; i < n->key_num; i++)
n->keys[i - 1] = n->keys[i];
if (!n->leaf)
for (i = index; i <= n->key_num; i++)
n->children[i - 1] = n->children[i];
n->key_num--;
}
/* index is children index, index can be last child*/
static void
bt_btree_node_shift_right_from_index(struct btree_node *n, int index)
{
int i, max_num;
max_num = n->key_num + 1 < MAX_KEY_NUM ? n->key_num + 1: MAX_KEY_NUM;
for (i = max_num - 1; i > index; i--)
n->keys[i] = n->keys[i - 1];
if (!n->leaf)
for (i = max_num; i > index; i--)
n->children[i] = n->children[i - 1];
n->key_num = max_num;
}
static void
bt_btree_node_pop_head(struct btree_node *n, int *rk, struct btree_node **rn)
{
*rk = n->keys[0];
if (n->leaf)
*rn = NULL;
else
*rn = n->children[0];
bt_btree_node_shift_left_from_index(n, 1);
}
static void
bt_btree_node_pop_tail(struct btree_node *n, int *rk, struct btree_node **rn)
{
*rk = n->keys[n->key_num - 1];
if (n->leaf)
*rn = NULL;
else
*rn = n->children[n->key_num];
n->key_num--;
}
static void bt_btree_node_transfer_from_left(struct btree_node *n, int pos)
{
int tmpk;
struct btree_node *tmpn, *child;
child = n->children[pos];
bt_btree_node_pop_tail(n->children[pos - 1], &tmpk, &tmpn);
bt_btree_node_shift_right_from_index(child, 0);
child->keys[0] = n->keys[pos - 1];
n->keys[pos - 1] = tmpk;
if (!child->leaf)
child->children[0] = tmpn;
}
static void bt_btree_node_transfer_from_right(struct btree_node *n, int pos)
{
int tmpk;
struct btree_node *tmpn, *child;
child = n->children[pos];
bt_btree_node_pop_head(n->children[pos + 1], &tmpk, &tmpn);
child->keys[child->key_num] = n->keys[pos];
n->keys[pos] = tmpk;
if (!child->leaf)
child->children[child->key_num + 1] = tmpn;
child->key_num++;
}
static int bt_btree_node_find_max(struct btree_node *n)
{
int tmp;
while (!n->leaf)
n = n->children[n->key_num];
return n->keys[n->key_num - 1];
}
static int bt_btree_node_find_min(struct btree_node *n)
{
int tmp;
while (!n->leaf)
n = n->children[0];
return n->keys[0];
}
static struct btree_node *
bt_btree_node_split_node(struct btree_node *n, int *newkey)
{
int i;
struct btree_node *tmp;
tmp = bt_alloc_init_btree_node(n->leaf);
for (i = 0; i < MIN_KEY_NUM; i++)
tmp->keys[i] = n->keys[MIN_KEY_NUM + 1 + i];
if (!n->leaf)
for (i = 0; i < MIN_CHILD_NUM; i++)
tmp->children[i] = n->children[MIN_CHILD_NUM + i];
*newkey = n->keys[MIN_KEY_NUM];
n->key_num = MIN_KEY_NUM;
tmp->key_num = MIN_KEY_NUM;
return tmp;
}
static void bt_btree_node_split_child(struct btree_node *n, int index)
{
int newkey;
struct btree_node *child, *sib;
child = n->children[index];
sib = bt_btree_node_split_node(child, &newkey);
bt_btree_node_shift_right_from_index(n, index + 1);
n->keys[index + 1] = n->keys[index];
n->keys[index] = newkey;
n->children[index + 1] = sib;
}
/* gurantee n is not of maximum size before you call */
static void bt_btree_node_insert_key(struct btree_node *n, int key)
{
int i, pos;
struct btree_node *tmp;
for (i = 0; i < n->key_num && key > n->keys[i]; i++);
pos = i;
if (n->leaf) {
bt_btree_node_shift_right_from_index(n, pos);
n->keys[pos] = key;
return;
}
/* children[i]'s keys are always larger than keys[i - 1],
* no larger than keys[i] */
if (bt_btree_node_is_max_size(n->children[pos])) {
bt_btree_node_split_child(n, pos);
if (key > n->keys[pos])
pos++;
}
bt_btree_node_insert_key(n->children[pos], key);
}
static void bt_btree_node_merge_children(struct btree_node *n, int pos)
{
int i;
struct btree_node *child, *sib, tmpn;
child = n->children[pos];
sib = n->children[pos + 1];
for (i = 0; i < MIN_KEY_NUM; i++)
child->keys[MIN_KEY_NUM + 1 + i] = sib->keys[i];
if (!child->leaf)
for (i = 0; i < MIN_CHILD_NUM; i++)
child->children[MIN_CHILD_NUM + i] = sib->children[i];
child->keys[MIN_KEY_NUM] = n->keys[pos];
child->key_num = MAX_KEY_NUM;
n->children[pos + 1] = child;
bt_btree_node_shift_left_from_index(n, pos + 1);
free(sib);
}
static void bt_btree_node_solve_child_minsize(struct btree_node *n, int pos)
{
if (pos > 0 && !bt_btree_node_is_min_size(n->children[pos - 1]))
bt_btree_node_transfer_from_left(n, pos);
else if (pos < n->key_num &&
!bt_btree_node_is_min_size(n->children[pos + 1]))
bt_btree_node_transfer_from_right(n, pos);
else if (pos < n->key_num)
bt_btree_node_merge_children(n, pos);
else
bt_btree_node_merge_children(n, pos - 1);
}
/* gurantee n is not of minimum size before you call */
static void bt_btree_node_delete_key(struct btree_node *n, int key)
{
int i, pos, tmp;
struct btree_node *pc, *nc;
for (i = 0; i < n->key_num && key > n->keys[i]; i++);
pos = i;
if (n->leaf && (pos >= n->key_num || n->keys[pos] != key)) {
printf("key %i don't exsits", key);
return;
}
/* delete key from itself */
if (pos < n->key_num && n->keys[pos] == key) {
if (n->leaf) {
bt_btree_node_shift_left_from_index(n, pos + 1);
} else if (!bt_btree_node_is_min_size(n->children[pos])) {
tmp = bt_btree_node_find_max(n->children[pos]);
n->keys[pos] = tmp;
bt_btree_node_delete_key(n->children[pos], tmp);
} else if (!bt_btree_node_is_min_size(n->children[pos + 1])) {
tmp = bt_btree_node_find_min(n->children[pos + 1]);
n->keys[pos] = tmp;
bt_btree_node_delete_key(n->children[pos + 1], tmp);
} else {
bt_btree_node_merge_children(n, pos);
bt_btree_node_delete_key(n->children[pos], key);
}
} else {
/* delete key from child */
if (bt_btree_node_is_min_size(n->children[pos])) {
bt_btree_node_solve_child_minsize(n, pos);
bt_btree_node_delete_key(n, key);
} else {
bt_btree_node_delete_key(n->children[pos], key);
}
}
}
static void bt_btree_node_print(struct btree_node *n)
{
int i;
printf("%d keys: ", n->key_num);
for (i = 0; i < n->key_num; i++)
printf("%i ", n->keys[i]);
if (n->leaf) {
printf(" it's a leaf\n");
return;
}
printf("\n");
for (i = 0; i <= n->key_num; i++) {
printf("subtree for %p at %p:\n", n, n->children[i]);
bt_btree_node_print(n->children[i]);
}
}
struct btree {
struct btree_node *root;
};
static struct btree *bt_alloc_init_btree()
{
struct btree *tmp;
struct btree_node *tmpn;
tmp = malloc(sizeof *tmp);
memset(tmp, 0, sizeof *tmp);
tmpn = bt_alloc_init_btree_node(LEAF);
tmp->root = tmpn;
return tmp;
}
static void bt_btree_insert_key(struct btree *t, int key)
{
int newkey;
struct btree_node *root, *sib, *newroot;
root = t->root;
if (bt_btree_node_is_max_size(root)) {
sib = bt_btree_node_split_node(root, &newkey);
newroot = bt_alloc_init_btree_node(NONLEAF);
newroot->key_num = 1;
newroot->keys[0] = newkey;
newroot->children[0] = root;
newroot->children[1] = sib;
t->root = newroot;
root = newroot;
}
bt_btree_node_insert_key(root, key);
}
static void bt_btree_delete_key(struct btree *t, int key)
{
struct btree_node *root;
root = t->root;
bt_btree_node_delete_key(root, key);
if (root->key_num == 0) {
t->root = root->children[0];
free(root);
}
}
static void bt_btree_print(struct btree *t)
{
printf("\nstart printing new tree:\nroot at %p:\n", t->root);
if (t->root)
bt_btree_node_print(t->root);
}
int main(int argc, char **argv)
{
int i;
int keys[KEY_NUM];
struct btree *btree;
get_random_array(keys, KEY_NUM, KEY_MAX);
/*
*/
print_array(keys, KEY_NUM, "keys");
btree = bt_alloc_init_btree();
printf("inserting keys\n");
for (i = 0; i < KEY_NUM; i++) {
bt_btree_insert_key(btree, keys[i]);
}
printf("insertion finished\n");
bt_btree_print(btree);
printf("\ndeleting keys\n");
for (i = 0; i < KEY_NUM; i++) {
printf("\ndeleting %i\n", keys[i]);
bt_btree_delete_key(btree, keys[i]);
bt_btree_print(btree);
}
printf("deletion finished\n");
bt_btree_print(btree);
}