-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexample.c
43 lines (35 loc) · 1.12 KB
/
example.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
#include <stdio.h>
#define BGEN_NAME bt // The namespace for the btree structure.
#define BGEN_TYPE int // The data type for all items in the btree
#define BGEN_LESS return a < b; // A code fragment for comparing items
#include "../bgen.h" // Include "bgen.h" to generate the btree
int main() {
// Create an empty btree instance.
struct bt *tree = 0;
// Insert some items into the btree
bt_insert(&tree, 3, 0, 0);
bt_insert(&tree, 8, 0, 0);
bt_insert(&tree, 2, 0, 0);
bt_insert(&tree, 5, 0, 0);
// Print items in tree
struct bt_iter *iter;
bt_iter_init(&tree, &iter, 0);
for (bt_iter_scan(iter); bt_iter_valid(iter); bt_iter_next(iter)) {
int item;
bt_iter_item(iter, &item);
printf("%d ", item);
}
printf("\n");
// Delete an item
bt_delete(&tree, 3, 0, 0);
// Print again
for (bt_iter_scan(iter); bt_iter_valid(iter); bt_iter_next(iter)) {
int item;
bt_iter_item(iter, &item);
printf("%d ", item);
}
printf("\n");
bt_iter_release(iter);
bt_clear(&tree, 0);
return 0;
}