https://github.com/mirek/rb_tree
Red-black tree C implementation
https://github.com/mirek/rb_tree
c-language library
Last synced: 10 months ago
JSON representation
Red-black tree C implementation
- Host: GitHub
- URL: https://github.com/mirek/rb_tree
- Owner: mirek
- Created: 2012-05-14T17:52:35.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2018-12-03T22:25:09.000Z (about 7 years ago)
- Last Synced: 2025-03-24T03:12:18.829Z (10 months ago)
- Topics: c-language, library
- Size: 93.8 KB
- Stars: 37
- Watchers: 4
- Forks: 12
- Open Issues: 2
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
# Red-black tree C implementation
Based on Julienne Walker's red-black tree implementation.
## Usage
First, grab the source into your project, you'll need just those two:
wget https://raw.github.com/mirek/rb_tree/master/rb_tree.h
wget https://raw.github.com/mirek/rb_tree/master/rb_tree.c
Let's say you want to store `struct iovec` objects (defined in `sys/uio.h`) with the following definition:
struct iovec {
void *iov_base; // [XSI] Base address of I/O memory region
size_t iov_len; // [XSI] Size of region iov_base points to
};
You'll need to provide comparison callback for your object. Let's say you want to keep track
of the length of the iovec buffers, your callback should look similar to:
int
my_cmp_cb (struct rb_tree *self, struct rb_node *node_a, struct rb_node *node_b) {
struct iovec *a = (struct iovec *) node_a->value;
struct iovec *b = (struct iovec *) node_b->value;
return (a->iov_len > b->iov_len) - (a->iov_len < b->iov_len);
}
To create your red-black tree:
struct rb_tree *tree = rb_tree_create(my_cmp_cb);
if (tree) {
// Use the tree here...
for (int i = 0; i < 10; i++) {
struct iovec *v = malloc(sizeof *v);
v->iov_base = (void *) i;
v->iov_len = i * i;
// Default insert, which allocates internal rb_nodes for you.
rb_tree_insert(tree, v);
}
// To f
struct iovec *f = rb_tree_find(tree, & (struct iovec) { .iov_base = (void *) 7, .iov_len = 0 });
if (r) {
fprintf(stdout, "found iovec(.iov_base = %p, .iov_len = %zu)\n", f->iov_base, f->iov_len)
} else {
printf("not found\n");
}
// Dealloc call can take optional parameter to notify on each node
// being deleted so you can free the node and/or your object:
rb_tree_dealloc(tree, NULL);
}
Above example will leak because malloc is not balanced with free.
If you want to iterate over elements (here in reverse order):
struct rb_iter *iter = rb_iter_create();
if (iter) {
for (struct iovec *v = rb_iter_last(iter, tree); v; v = rb_iter_prev(iter)) {
printf("- %p %zu\n", v->iov_base, v->iov_len);
}
rb_iter_dealloc(iter);
}
## License
None (Public Domain).