add rbtree
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
#include "fast_ping.h"
|
||||
#include "hashtable.h"
|
||||
#include "list.h"
|
||||
#include "rbtree.h"
|
||||
#include "tlog.h"
|
||||
#include "util.h"
|
||||
#include <errno.h>
|
||||
@@ -246,6 +247,57 @@ void sig_handle(int sig)
|
||||
_exit(0);
|
||||
}
|
||||
|
||||
#if 0
|
||||
struct data_rbtree {
|
||||
struct rb_node list;
|
||||
int value;
|
||||
};
|
||||
|
||||
int rbtree_test()
|
||||
{
|
||||
struct rb_root root = RB_ROOT;
|
||||
struct rb_node *n;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
struct data_rbtree *r = malloc(sizeof(struct data_rbtree));
|
||||
struct rb_node **new = &root.rb_node, *parent = NULL;
|
||||
r->value = i;
|
||||
|
||||
while (*new) {
|
||||
parent = *new;
|
||||
if (i < rb_entry(parent, struct data_rbtree, list)->value)
|
||||
new = &parent->rb_left;
|
||||
else
|
||||
new = &parent->rb_right;
|
||||
}
|
||||
|
||||
rb_link_node(&r->list, parent, new);
|
||||
rb_insert_color(&r->list, &root);
|
||||
}
|
||||
|
||||
n = root.rb_node;
|
||||
int num = 5;
|
||||
while (n) {
|
||||
struct data_rbtree *r = rb_entry(n, struct data_rbtree, list);
|
||||
if (r->value > num) {
|
||||
n = n->rb_left;
|
||||
} else if (r->value < num) {
|
||||
n = n->rb_right;
|
||||
} else {
|
||||
printf("n = %d\n", r->value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
struct rb_node *node;
|
||||
for (node = rb_first(&root); node; node = rb_next(node))
|
||||
printf("V = %d\n", rb_entry(node, struct data_rbtree, list)->value);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ret;
|
||||
|
||||
Reference in New Issue
Block a user