最近使用到红黑树写了一个电话本,使用的linux内核中的标准接口,然后加上了一些自己写的接口和测试程序,记录如下,方便后续使用,以下代码网上几乎都可以找到,只是根据自己所做的项目做了一点修正:
rbtree.h
/*
Red Black Trees
(C) 1999 Andrea Arcangeli <andrea@suse.de>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
linux/include/linux/rbtree.h
To use rbtrees you'll have to implement your own insert and search cores.
This will avoid us to use callbacks and to drop drammatically performances.
I know it's not the cleaner way, but in C (not in C++) to get
performances and genericity...
Some example of insert and search follows here. The search is a plain
normal search over an ordered tree. The insert instead must be implemented
in two steps: First, the code must insert the element in order as a red leaf
in the tree, and then the support library function rb_insert_color() must
be called. Such function will do the not trivial work to rebalance the
rbtree, if necessary.
*/
#ifndef _LINUX_RBTREE_H
#define _LINUX_RBTREE_H
#define MAX_LEN 32
#define RECORD_MAX 512
//定义数据记录
struct PBookData
{
char name[MAX_LEN];
char num1[MAX_LEN];
char num2[MAX_LEN];
}RecordData[RECORD_MAX];
typedef struct PBookData st_PBookRecord;
struct rb_node
{
unsigned long rb_parent_color;
#define RB_RED 0
#define RB_BLACK 1
struct rb_node *rb_right;
struct rb_node *rb_left;
} __attribute__((aligned(sizeof(long))));
struct rb_root
{
struct rb_node *rb_node;
};
//自定义类型
struct mytype {
struct rb_node my_node;
//int num;
st_PBookRecord *my_pbook;
};
typedef struct mytype * ElemDataNode;
//取r节点的父节点的地址
#define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3))
//获取r节点的颜色:1是黑,0是红。
#define rb_color(r) ((r)->rb_parent_color & 1)
//测试节点是否为红色
#define rb_is_red(r) (!rb_color(r))
//测试节点是否为黑色
#define rb_is_black(r) rb_color(r)
#define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0)
#define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0)
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
//设置节点父节点地址为p所指地址
static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
{
rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
}
//设置节点color
static inline void rb_set_color(struct rb_node *rb, int color)
{
rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
}
#define RB_ROOT (struct rb_root) { NULL, } //初始根节点指针
#define rb_entry(ptr, type, member) container_of(ptr, type, member) //包含ptr的结构体指针
#define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL) //判断树是否空
#define RB_EMPTY_NODE(node) (rb_parent(node) == node) //判断节点是否空,父亲是否等于自身
#define RB_CLEAR_NODE(node) (rb_set_parent(node, node)) //设置节点为空,父亲等于自身
int rb_pbook_cmp(const char *s1, const char *s2);
void rb_pbook_cp(st_PBookRecord * src, st_PBookRecord * dest);
struct mytype *rb_search(struct rb_root *root, const char *name);
int rb_insert(struct rb_root *root, struct mytype *data);
void rb_delete(struct rb_root *root, const char *name);
void rb_print(struct rb_root *tree);
void rb_free(struct rb_root *tree);
//把新插入的节点进行着色,并且修正红黑树使其达到平衡,其效果就是前文的insertFixup的效果
void rb_insert_color(struct rb_node *, struct rb_root *);
//To remove an existing node from a tree
void rb_erase(struct rb_node *, struct rb_root *);
/*
rb_next:返回node在书中的后继,这个稍微复杂一点。如果node的右孩子不为空,他只要返回node的