http://hi.baidu.com/flower_mlh/item/1839a59049aea4d31b49dfe7·
http://blog.csdn.net/v_JULY_v/article/details/6105630
http://ds.fzu.edu.cn/fine/resources/FlashContent.asp?id=70
自己写的代码,目前只有insert操作。
#include <stdio.h>
struct rb_node {
struct rb_node *left;
struct rb_node *right;
struct rb_node *father;
#define COLOR_RED 0
#define COLOR_BLACK 1
int color;
int value;
};
struct rb_tree {
struct rb_node *root;
int num;
struct rb_node nil;
};
void init_tree(struct rb_tree *tree) {
tree->root = NULL;
tree->num = 0;
tree->nil.left = NULL;
tree->nil.right = NULL;
tree->nil.father = NULL;
tree->nil.color = COLOR_BLACK;
tree->nil.value = 0;
}
void clear_tree(struct rb_tree *tree) {
}
void left_rotate(struct rb_tree *tree, struct rb_node *node) {
printf("left rotate %d\n", node->value);
struct rb_node *father;
struct rb_node *right_son = node->right;
struct rb_node *grandson = right_son->left;
if (node != tree->root) {
if (node->father->left == node) {
node->father->left = right_son;
} else {
node->father->right = right_son;
}
}
right_son->father = node->father;
right_son->left = node;
node->father = right_son;
node->right = grandson;
grandson->father = node;
}
void right_rotate(struct rb_tree *tree, struct rb_node *node) {
printf("right rotate %d\n", node->value);
struct rb_node *father = node->father;
struct rb_node *left_son = node->left;
struct rb_node *grandson = left_son->right;
if (node != tree->root) {
if (node->father->left != NULL) {
node->father->left = left_son;
} else {
node->father->right = left_son;
}
}
left_son->father = node->father;
left_son->right = node;
node->father = left_son;
node->left = grandson;
grandson->father = node;
}
void insert_node(struct rb_tree *tree, int value) {
printf(">>>> insert node %d\n", value);
struct rb_node *node = (struct rb_node *)malloc(sizeof(struct rb_node));
node->value = value;
node->color = COLOR_RED;
node->left = &tree->nil;
node->right = &tree->nil;
if (tree->num == 0) {
node->color = COLOR_BLACK;
tree->root = node;
tree->num = 1;
return;
}
tree->num++;
//insert node
struct rb_node *search = tree->root;
while (1) {
if (value < search->value) {
if (search->left == &tree->nil) {
search->left = node;
node->father = search;
break;
}
search = search->left;
} else {
if (search->right == &tree->nil) {
search->right = node;
node->father = search;
break;
}
search = search->right;
}
}
printf("father point = %d\n", search->value);
if (search->color == COLOR_BLACK) {
printf("point is black, ok\n");
return;
}
//shift up problem
struct rb_node *grandfather;
struct rb_node *father = search;
struct rb_node *uncle;
struct rb_node *me = node;
while (1) {
grandfather = father->father;
if (father->value < grandfather->value) {
uncle = grandfather->right;
} else {
uncle = grandfather->left;
}
if (uncle->color == COLOR_BLACK)
break;
father->color = COLOR_BLACK;
uncle->color = COLOR_BLACK;
grandfather->color = COLOR_RED;
printf("shift up, grandfather = %d\n", grandfather->value);
if (grandfather == tree->root) {
grandfather->color = COLOR_BLACK;
printf("grandfather is root, set black, return!\n");
return;
}
me = father;
father = me->father;
}
if (father->value < grandfather->value) { //left sub tree
printf("left subtree\n");
if (me->value > father->value) {
left_rotate(tree, father);
father = me->father;
me->color = COLOR_BLACK;
father->color = COLOR_RED;
right_rotate(tree, father);
if (father == tree->root) tree->root = me;
} else {
father->color = COLOR_BLACK;
grandfather->color = COLOR_RED;
right_rotate(tree, grandfather);
if (grandfather == tree->root) tree->root = father;
}
} else { //right sub tree
printf("right subtree\n");
if (me->value < father->value) {
right_rotate(tree, father);
father = me->father;
me->color = COLOR_BLACK;
father->color = COLOR_RED;
left_rotate(tree, father);
if (father == tree->root) tree->root = me;
} else {
father->color = COLOR_BLACK;
grandfather->color = COLOR_RED;
left_rotate(tree, grandfather);
if (grandfather == tree->root) tree->root = father;
}
}
return;
}
void do_list_tree(struct rb_tree *tree, struct rb_node *node) {
if (node == &tree->nil)return;
do_list_tree(tree, node->left);
printf("%d ", node->value);
do_list_tree(tree, node->right);
}
void list_tree(struct rb_tree *tree) {
printf("list tree, num = %d:\n", tree->num);
do_list_tree(tree, tree->root);
printf("\n");
}
int main(int argc, char **argv) {
struct rb_tree tree;
init_tree(&tree);
insert_node(&tree, 1);
insert_node(&tree, 2);
insert_node(&tree, 3);
insert_node(&tree, 4);
insert_node(&tree, 5);
insert_node(&tree, 6);
insert_node(&tree, 7);
insert_node(&tree, 8);
insert_node(&tree, 9);
insert_node(&tree, 10);
list_tree(&tree);
return 0;
}
http://blog.csdn.net/v_JULY_v/article/details/6105630
http://ds.fzu.edu.cn/fine/resources/FlashContent.asp?id=70
自己写的代码,目前只有insert操作。
#include <stdio.h>
struct rb_node {
struct rb_node *left;
struct rb_node *right;
struct rb_node *father;
#define COLOR_RED 0
#define COLOR_BLACK 1
int color;
int value;
};
struct rb_tree {
struct rb_node *root;
int num;
struct rb_node nil;
};
void init_tree(struct rb_tree *tree) {
tree->root = NULL;
tree->num = 0;
tree->nil.left = NULL;
tree->nil.right = NULL;
tree->nil.father = NULL;
tree->nil.color = COLOR_BLACK;
tree->nil.value = 0;
}
void clear_tree(struct rb_tree *tree) {
}
void left_rotate(struct rb_tree *tree, struct rb_node *node) {
printf("left rotate %d\n", node->value);
struct rb_node *father;
struct rb_node *right_son = node->right;
struct rb_node *grandson = right_son->left;
if (node != tree->root) {
if (node->father->left == node) {
node->father->left = right_son;
} else {
node->father->right = right_son;
}
}
right_son->father = node->father;
right_son->left = node;
node->father = right_son;
node->right = grandson;
grandson->father = node;
}
void right_rotate(struct rb_tree *tree, struct rb_node *node) {
printf("right rotate %d\n", node->value);
struct rb_node *father = node->father;
struct rb_node *left_son = node->left;
struct rb_node *grandson = left_son->right;
if (node != tree->root) {
if (node->father->left != NULL) {
node->father->left = left_son;
} else {
node->father->right = left_son;
}
}
left_son->father = node->father;
left_son->right = node;
node->father = left_son;
node->left = grandson;
grandson->father = node;
}
void insert_node(struct rb_tree *tree, int value) {
printf(">>>> insert node %d\n", value);
struct rb_node *node = (struct rb_node *)malloc(sizeof(struct rb_node));
node->value = value;
node->color = COLOR_RED;
node->left = &tree->nil;
node->right = &tree->nil;
if (tree->num == 0) {
node->color = COLOR_BLACK;
tree->root = node;
tree->num = 1;
return;
}
tree->num++;
//insert node
struct rb_node *search = tree->root;
while (1) {
if (value < search->value) {
if (search->left == &tree->nil) {
search->left = node;
node->father = search;
break;
}
search = search->left;
} else {
if (search->right == &tree->nil) {
search->right = node;
node->father = search;
break;
}
search = search->right;
}
}
printf("father point = %d\n", search->value);
if (search->color == COLOR_BLACK) {
printf("point is black, ok\n");
return;
}
//shift up problem
struct rb_node *grandfather;
struct rb_node *father = search;
struct rb_node *uncle;
struct rb_node *me = node;
while (1) {
grandfather = father->father;
if (father->value < grandfather->value) {
uncle = grandfather->right;
} else {
uncle = grandfather->left;
}
if (uncle->color == COLOR_BLACK)
break;
father->color = COLOR_BLACK;
uncle->color = COLOR_BLACK;
grandfather->color = COLOR_RED;
printf("shift up, grandfather = %d\n", grandfather->value);
if (grandfather == tree->root) {
grandfather->color = COLOR_BLACK;
printf("grandfather is root, set black, return!\n");
return;
}
me = father;
father = me->father;
}
if (father->value < grandfather->value) { //left sub tree
printf("left subtree\n");
if (me->value > father->value) {
left_rotate(tree, father);
father = me->father;
me->color = COLOR_BLACK;
father->color = COLOR_RED;
right_rotate(tree, father);
if (father == tree->root) tree->root = me;
} else {
father->color = COLOR_BLACK;
grandfather->color = COLOR_RED;
right_rotate(tree, grandfather);
if (grandfather == tree->root) tree->root = father;
}
} else { //right sub tree
printf("right subtree\n");
if (me->value < father->value) {
right_rotate(tree, father);
father = me->father;
me->color = COLOR_BLACK;
father->color = COLOR_RED;
left_rotate(tree, father);
if (father == tree->root) tree->root = me;
} else {
father->color = COLOR_BLACK;
grandfather->color = COLOR_RED;
left_rotate(tree, grandfather);
if (grandfather == tree->root) tree->root = father;
}
}
return;
}
void do_list_tree(struct rb_tree *tree, struct rb_node *node) {
if (node == &tree->nil)return;
do_list_tree(tree, node->left);
printf("%d ", node->value);
do_list_tree(tree, node->right);
}
void list_tree(struct rb_tree *tree) {
printf("list tree, num = %d:\n", tree->num);
do_list_tree(tree, tree->root);
printf("\n");
}
int main(int argc, char **argv) {
struct rb_tree tree;
init_tree(&tree);
insert_node(&tree, 1);
insert_node(&tree, 2);
insert_node(&tree, 3);
insert_node(&tree, 4);
insert_node(&tree, 5);
insert_node(&tree, 6);
insert_node(&tree, 7);
insert_node(&tree, 8);
insert_node(&tree, 9);
insert_node(&tree, 10);
list_tree(&tree);
return 0;
}