This commit is contained in:
yzd 2023-09-29 02:36:25 -07:00
parent 4aceaff0c0
commit 3e2208cbec
3 changed files with 252 additions and 282 deletions

View File

@ -3,13 +3,13 @@
#include "rbtree.h"
#define rb_parent(r) ((r)->parent)
#define rb_color(r) ((r)->color)
#define rb_is_red(r) ((r)->color==RED)
#define rb_is_black(r) ((r)->color==BLACK)
#define rb_set_black(r) do { (r)->color = BLACK; } while (0)
#define rb_set_red(r) do { (r)->color = RED; } while (0)
#define rb_color(r) ((r)->is_red)
#define rb_is_red(r) ((r)->is_red==RED)
#define rb_is_black(r) ((r)->is_red==BLACK)
#define rb_set_black(r) do { (r)->is_red = BLACK; } while (0)
#define rb_set_red(r) do { (r)->is_red = RED; } while (0)
#define rb_set_parent(r,p) do { (r)->parent = (p); } while (0)
#define rb_set_color(r,c) do { (r)->color = (c); } while (0)
#define rb_set_color(r,c) do { (r)->is_red = (c); } while (0)
/*
*
@ -22,23 +22,6 @@ RBRoot* create_rbtree()
return root;
}
/*
*
*/
static void preorder(RBTree tree)
{
if(tree != NULL)
{
printf("%d ", tree->key);
preorder(tree->left);
preorder(tree->right);
}
}
void preorder_rbtree(RBRoot *root)
{
if (root)
preorder(root->node);
}
/*
*
@ -47,36 +30,18 @@ static void inorder(RBTree tree)
{
if(tree != NULL)
{
inorder(tree->left);
inorder(tree->left_child);
printf("%d ", tree->key);
inorder(tree->right);
inorder(tree->right_child);
}
}
void inorder_rbtree(RBRoot *root)
void RBTreeTraversal(RBRoot *root)
{
if (root)
inorder(root->node);
}
/*
*
*/
static void postorder(RBTree tree)
{
if(tree != NULL)
{
postorder(tree->left);
postorder(tree->right);
printf("%d ", tree->key);
}
}
void postorder_rbtree(RBRoot *root)
{
if (root)
postorder(root->node);
}
/*
* key的节点
@ -87,36 +52,17 @@ static Node* search(RBTree x, Type key)
return x;
if (key < x->key)
return search(x->left, key);
return search(x->left_child, key);
else
return search(x->right, key);
return search(x->right_child, key);
}
int rbtree_search(RBRoot *root, Type key)
int RBTreeSearch(RBRoot *root, Type key)
{
if (root)
return search(root->node, key)? 0 : -1;
}
/*
* key的节点
*/
static Node* iterative_search(RBTree x, Type key)
{
while ((x!=NULL) && (x->key!=key))
{
if (key < x->key)
x = x->left;
else
x = x->right;
}
return x;
}
int iterative_rbtree_search(RBRoot *root, Type key)
{
if (root)
return iterative_search(root->node, key) ? 0 : -1;
}
/*
* tree为根结点的红黑树的最小结点
@ -126,8 +72,8 @@ static Node* minimum(RBTree tree)
if (tree == NULL)
return NULL;
while(tree->left != NULL)
tree = tree->left;
while(tree->left_child != NULL)
tree = tree->left_child;
return tree;
}
@ -145,42 +91,17 @@ int rbtree_minimum(RBRoot *root, int *val)
return 0;
}
/*
* tree为根结点的红黑树的最大结点
*/
static Node* maximum(RBTree tree)
{
if (tree == NULL)
return NULL;
while(tree->right != NULL)
tree = tree->right;
return tree;
}
int rbtree_maximum(RBRoot *root, int *val)
{
Node *node;
if (root)
node = maximum(root->node);
if (node == NULL)
return -1;
*val = node->key;
return 0;
}
/*
* (x)
*/
static Node* rbtree_successor(RBTree x)
static Node* FindSuccessor(RBTree x)
{
if (x->right != NULL)
return minimum(x->right);
if (x->right_child != NULL)
return minimum(x->right_child);
Node* y = x->parent;
while ((y!=NULL) && (x==y->right))
while ((y!=NULL) && (x==y->right_child))
{
x = y;
y = y->parent;
@ -189,32 +110,16 @@ static Node* rbtree_successor(RBTree x)
return y;
}
/*
* (x)
*/
static Node* rbtree_predecessor(RBTree x)
{
if (x->left != NULL)
return maximum(x->left);
Node* y = x->parent;
while ((y!=NULL) && (x==y->left))
{
x = y;
y = y->parent;
}
return y;
}
/*
* (x)
*/
static void rbtree_left_rotate(RBRoot *root, Node *x)
static void RBTreeLeftRotate(RBRoot *root, Node *x)
{
Node *y = x->right;
x->right = y->left;
if (y->left != NULL)
y->left->parent = x;
Node *y = x->right_child;
x->right_child = y->left_child;
if (y->left_child != NULL)
y->left_child->parent = x;
y->parent = x->parent;
if (x->parent == NULL)
@ -223,25 +128,25 @@ static void rbtree_left_rotate(RBRoot *root, Node *x)
}
else
{
if (x->parent->left == x)
x->parent->left = y;
if (x->parent->left_child == x)
x->parent->left_child = y;
else
x->parent->right = y;
x->parent->right_child = y;
}
y->left = x;
y->left_child = x;
x->parent = y;
}
/*
* (y)
*/
static void rbtree_right_rotate(RBRoot *root, Node *y)
static void RBTreeRightRotate(RBRoot *root, Node *y)
{
Node *x = y->left;
y->left = x->right;
if (x->right != NULL)
x->right->parent = y;
Node *x = y->left_child;
y->left_child = x->right_child;
if (x->right_child != NULL)
x->right_child->parent = y;
x->parent = y->parent;
if (y->parent == NULL)
@ -250,19 +155,19 @@ static void rbtree_right_rotate(RBRoot *root, Node *y)
}
else
{
if (y == y->parent->right)
y->parent->right = x;
if (y == y->parent->right_child)
y->parent->right_child = x;
else
y->parent->left = x;
y->parent->left_child = x;
}
x->right = y;
x->right_child = y;
y->parent = x;
}
/*
*
*/
static void rbtree_insert_fixup(RBRoot *root, Node *node)
static void InsertFixup(RBRoot *root, Node *node)
{
Node *parent, *gparent;
@ -272,11 +177,11 @@ static void rbtree_insert_fixup(RBRoot *root, Node *node)
gparent = rb_parent(parent);
//若“父节点”是“祖父节点的左孩子”
if (parent == gparent->left)
if (parent == gparent->left_child)
{
// Case 1条件叔叔节点是红色
{
Node *uncle = gparent->right;
Node *uncle = gparent->right_child;
if (uncle && rb_is_red(uncle))
{
rb_set_black(uncle);
@ -288,10 +193,10 @@ static void rbtree_insert_fixup(RBRoot *root, Node *node)
}
// Case 2条件叔叔是黑色且当前节点是右孩子
if (parent->right == node)
if (parent->right_child == node)
{
Node *tmp;
rbtree_left_rotate(root, parent);
RBTreeLeftRotate(root, parent);
tmp = parent;
parent = node;
node = tmp;
@ -300,13 +205,13 @@ static void rbtree_insert_fixup(RBRoot *root, Node *node)
// Case 3条件叔叔是黑色且当前节点是左孩子。
rb_set_black(parent);
rb_set_red(gparent);
rbtree_right_rotate(root, gparent);
RBTreeRightRotate(root, gparent);
}
else//若“z的父节点”是“z的祖父节点的右孩子”
{
// Case 1条件叔叔节点是红色
{
Node *uncle = gparent->left;
Node *uncle = gparent->left_child;
if (uncle && rb_is_red(uncle))
{
rb_set_black(uncle);
@ -318,10 +223,10 @@ static void rbtree_insert_fixup(RBRoot *root, Node *node)
}
// Case 2条件叔叔是黑色且当前节点是左孩子
if (parent->left == node)
if (parent->left_child == node)
{
Node *tmp;
rbtree_right_rotate(root, parent);
RBTreeRightRotate(root, parent);
tmp = parent;
parent = node;
node = tmp;
@ -330,7 +235,7 @@ static void rbtree_insert_fixup(RBRoot *root, Node *node)
// Case 3条件叔叔是黑色且当前节点是右孩子。
rb_set_black(parent);
rb_set_red(gparent);
rbtree_left_rotate(root, gparent);
RBTreeLeftRotate(root, gparent);
}
}
@ -341,7 +246,7 @@ static void rbtree_insert_fixup(RBRoot *root, Node *node)
/*
*
*/
static void rbtree_insert(RBRoot *root, Node *node)
static void insert_node(RBRoot *root, Node *node)
{
Node *y = NULL;
Node *x = root->node;
@ -349,41 +254,41 @@ static void rbtree_insert(RBRoot *root, Node *node)
{
y = x;
if (node->key < x->key)
x = x->left;
x = x->left_child;
else
x = x->right;
x = x->right_child;
}
rb_parent(node) = y;
if (y != NULL)
{
if (node->key < y->key)
y->left = node;
y->left_child = node;
else
y->right = node;
y->right_child = node;
}
else
{
root->node = node;
}
node->color = RED;
rbtree_insert_fixup(root, node);
node->is_red = RED;
InsertFixup(root, node);
}
/*
*
*/
static Node* create_rbtree_node(Type key, Node *parent, Node *left, Node* right)
static Node* create_rbtree_node(Type key, Node *parent, Node *left_child, Node* right_child)
{
Node* p;
if ((p = (Node *)malloc(sizeof(Node))) == NULL)
return NULL;
p->key = key;
p->left = left;
p->right = right;
p->left_child = left_child;
p->right_child = right_child;
p->parent = parent;
p->color = BLACK; // 默认为黑色
p->is_red = BLACK; // 默认为黑色
return p;
}
@ -391,7 +296,7 @@ static Node* create_rbtree_node(Type key, Node *parent, Node *left, Node* right)
/*
* (key)
*/
int insert_rbtree(RBRoot *root, Type key)
int RBTreeInsert(RBRoot *root, Type key)
{
Node *node;
if (search(root->node, key) != NULL)
@ -399,7 +304,7 @@ int insert_rbtree(RBRoot *root, Type key)
if ((node=create_rbtree_node(key, NULL, NULL, NULL)) == NULL)
return -1;
rbtree_insert(root, node);
insert_node(root, node);
return 0;
}
@ -407,24 +312,24 @@ int insert_rbtree(RBRoot *root, Type key)
/*
*
*/
static void rbtree_delete_fixup(RBRoot *root, Node *node, Node *parent)
static void DeleteFixup(RBRoot *root, Node *node, Node *parent)
{
Node *other;
while ((!node || rb_is_black(node)) && node != root->node)
{
if (parent->left == node)
if (parent->left_child == node)
{
other = parent->right;
other = parent->right_child;
if (rb_is_red(other))
{
rb_set_black(other);
rb_set_red(parent);
rbtree_left_rotate(root, parent);
other = parent->right;
RBTreeLeftRotate(root, parent);
other = parent->right_child;
}
if ((!other->left || rb_is_black(other->left)) &&
(!other->right || rb_is_black(other->right)))
if ((!other->left_child || rb_is_black(other->left_child)) &&
(!other->right_child || rb_is_black(other->right_child)))
{
rb_set_red(other);
node = parent;
@ -432,33 +337,33 @@ static void rbtree_delete_fixup(RBRoot *root, Node *node, Node *parent)
}
else
{
if (!other->right || rb_is_black(other->right))
if (!other->right_child || rb_is_black(other->right_child))
{
rb_set_black(other->left);
rb_set_black(other->left_child);
rb_set_red(other);
rbtree_right_rotate(root, other);
other = parent->right;
RBTreeRightRotate(root, other);
other = parent->right_child;
}
rb_set_color(other, rb_color(parent));
rb_set_black(parent);
rb_set_black(other->right);
rbtree_left_rotate(root, parent);
rb_set_black(other->right_child);
RBTreeLeftRotate(root, parent);
node = root->node;
break;
}
}
else
{
other = parent->left;
other = parent->left_child;
if (rb_is_red(other))
{
rb_set_black(other);
rb_set_red(parent);
rbtree_right_rotate(root, parent);
other = parent->left;
RBTreeRightRotate(root, parent);
other = parent->left_child;
}
if ((!other->left || rb_is_black(other->left)) &&
(!other->right || rb_is_black(other->right)))
if ((!other->left_child || rb_is_black(other->left_child)) &&
(!other->right_child || rb_is_black(other->right_child)))
{
rb_set_red(other);
node = parent;
@ -466,19 +371,19 @@ static void rbtree_delete_fixup(RBRoot *root, Node *node, Node *parent)
}
else
{
if (!other->left || rb_is_black(other->left))
if (!other->left_child || rb_is_black(other->left_child))
{
// Case 3: x的兄弟w是黑色的并且w的左孩子是红色右孩子为黑色。
rb_set_black(other->right);
rb_set_black(other->right_child);
rb_set_red(other);
rbtree_left_rotate(root, other);
other = parent->left;
RBTreeLeftRotate(root, other);
other = parent->left_child;
}
// Case 4: x的兄弟w是黑色的并且w的右孩子是红色的左孩子任意颜色。
rb_set_color(other, rb_color(parent));
rb_set_black(parent);
rb_set_black(other->left);
rbtree_right_rotate(root, parent);
rb_set_black(other->left_child);
RBTreeRightRotate(root, parent);
node = root->node;
break;
}
@ -494,25 +399,25 @@ static void rbtree_delete_fixup(RBRoot *root, Node *node, Node *parent)
void rbtree_delete(RBRoot *root, Node *node)
{
Node *child, *parent;
int color;
if ( (node->left!=NULL) && (node->right!=NULL) )
int is_red;
if ( (node->left_child!=NULL) && (node->right_child!=NULL) )
{
Node *replace = node;
replace = replace->right;
while (replace->left != NULL)
replace = replace->left;
replace = replace->right_child;
while (replace->left_child != NULL)
replace = replace->left_child;
if (rb_parent(node))
{
if (rb_parent(node)->left == node)
rb_parent(node)->left = replace;
if (rb_parent(node)->left_child == node)
rb_parent(node)->left_child = replace;
else
rb_parent(node)->right = replace;
rb_parent(node)->right_child = replace;
}
else
root->node = replace;
child = replace->right;
child = replace->right_child;
parent = rb_parent(replace);
color = rb_color(replace);
is_red = rb_is_red(replace);
if (parent == node)
{
parent = replace;
@ -521,53 +426,53 @@ void rbtree_delete(RBRoot *root, Node *node)
{
if (child)
rb_set_parent(child, parent);
parent->left = child;
parent->left_child = child;
replace->right = node->right;
rb_set_parent(node->right, replace);
replace->right_child = node->right_child;
rb_set_parent(node->right_child, replace);
}
replace->parent = node->parent;
replace->color = node->color;
replace->left = node->left;
node->left->parent = replace;
replace->is_red = node->is_red;
replace->left_child = node->left_child;
node->left_child->parent = replace;
if (color == BLACK)
rbtree_delete_fixup(root, child, parent);
if (is_red == BLACK)
DeleteFixup(root, child, parent);
free(node);
return ;
}
if (node->left !=NULL)
child = node->left;
if (node->left_child !=NULL)
child = node->left_child;
else
child = node->right;
child = node->right_child;
parent = node->parent;
color = node->color;
is_red = node->is_red;
if (child)
child->parent = parent;
if (parent)
{
if (parent->left == node)
parent->left = child;
if (parent->left_child == node)
parent->left_child = child;
else
parent->right = child;
parent->right_child = child;
}
else
root->node = child;
if (color == BLACK)
rbtree_delete_fixup(root, child, parent);
if (is_red == BLACK)
DeleteFixup(root, child, parent);
free(node);
}
/*
* key的结点
*/
void delete_rbtree(RBRoot *root, Type key)
void RBTreeDelete(RBRoot *root, Type key)
{
Node *z, *node;
@ -583,10 +488,10 @@ static void rbtree_destroy(RBTree tree)
if (tree==NULL)
return ;
if (tree->left != NULL)
rbtree_destroy(tree->left);
if (tree->right != NULL)
rbtree_destroy(tree->right);
if (tree->left_child != NULL)
rbtree_destroy(tree->left_child);
if (tree->right_child != NULL)
rbtree_destroy(tree->right_child);
free(tree);
}
@ -609,10 +514,10 @@ static void rbtree_print(RBTree tree, Type key, int direction)
if(direction==0) // tree是根节点
printf("%2d(B) is root\n", tree->key);
else // tree是分支节点
printf("%2d(%s) is %2d's %6s child\n", tree->key, rb_is_red(tree)?"R":"B", key, direction==1?"right" : "left");
printf("%2d(%s) is %2d's %6s child\n", tree->key, rb_is_red(tree)?"R":"B", key, direction==1?"right_child" : "left_child");
rbtree_print(tree->left, tree->key, -1);
rbtree_print(tree->right,tree->key, 1);
rbtree_print(tree->left_child, tree->key, -1);
rbtree_print(tree->right_child,tree->key, 1);
}
}

View File

@ -7,48 +7,42 @@
typedef int Type;
// 红黑树的节点
typedef struct RBTreeNode{
unsigned char color; // 颜色(RED 或 BLACK)
typedef struct RBNodeType{
unsigned char is_red; // 颜色(RED 或 BLACK)
Type key; // 关键字(键值)
struct RBTreeNode *left; // 左孩子
struct RBTreeNode *right; // 右孩子
struct RBTreeNode *parent; // 父结点
struct RBNodeType *left_child; // 左孩子
struct RBNodeType *right_child; // 右孩子
struct RBNodeType *parent; // 父结点
}Node, *RBTree;
// 红黑树的根
typedef struct rb_root{
typedef struct RBTreeType{
Node *node;
}RBRoot;
// 创建红黑树
// 创建红黑树,返回"红黑树的根"
RBRoot* create_rbtree();
// 销毁红黑树
void destroy_rbtree(RBRoot *root);
// 将结点插入到红黑树中。插入成功返回0失败返回-1。
int insert_rbtree(RBRoot *root, Type key);
int RBTreeInsert(RBRoot *root, Type key);
// 删除结点(key为节点的值)
void delete_rbtree(RBRoot *root, Type key);
void RBTreeDelete(RBRoot *root, Type key);
// 前序遍历"红黑树"
void preorder_rbtree(RBRoot *root);
// 中序遍历"红黑树"
void inorder_rbtree(RBRoot *root);
// 后序遍历"红黑树"
void postorder_rbtree(RBRoot *root);
void RBTreeTraversal(RBRoot *root);
// 递归查找键值为key的节点。找到返回0找不到返回-1
int rbtree_search(RBRoot *root, Type key);
// 非递归查找键值为key的节点。找到返回0找不到返回-1
int iterative_rbtree_search(RBRoot *root, Type key);
// (递归实现)查找"红黑树"中键值为key的节点。找到的话返回0否则返回-1。
int RBTreeSearch(RBRoot *root, Type key);
// 返回最小结点的值存于val找到返回0找不到返回-1
int rbtree_minimum(RBRoot *root, int *val);
// 返回最大结点的值(保存于val)。找到返回0找不到返回-1
int rbtree_maximum(RBRoot *root, int *val);
// 打印红黑树
void print_rbtree(RBRoot *root);

View File

@ -1,64 +1,135 @@
// #include <stdio.h>
// #include "rbtree.h"
// #include "rbtree.c"
// #define IF_INSERT 1 // "插入节点"flag
// #define IF_DELETE 1 // "删除节点"flag
// #define LENGTH(a) ( (sizeof(a)) / (sizeof(a[0])) )
// void main()
// {
// int a[] = {10, 150, 70, 0, 190, 270, 20, 50, 80};
// int i, ilen=LENGTH(a);
// RBRoot *root=NULL;
// root = create_rbtree();
// printf("red black tree 原始数据: ");
// for(i=0; i<ilen; i++)
// printf("%d ", a[i]);
// printf("\n");
// for(i=0; i<ilen; i++)
// {
// RBTreeInsert(root, a[i]);
// #if IF_INSERT
// printf("添加节点: %d\n", a[i]);
// printf("树的结构信息: \n");
// print_rbtree(root);
// printf("\n");
// #endif
// }
// printf("树的结构信息: \n");
// printf("\n== 中序遍历: ");
// RBTreeTraversal(root);
// #if IF_DELETE
// for(i=0; i<ilen; i++)
// {
// RBTreeDelete(root, a[i]);
// printf("== 删除节点: %d\n", a[i]);
// if (root)
// {
// printf("== 树的详细信息: \n");
// print_rbtree(root);
// printf("\n");
// }
// }
// #endif
// destroy_rbtree(root);
// }
#include <stdio.h>
#include "rbtree.h"
#include "rbtree.c"
#define IF_INSERT 1 // "插入节点"flag
#define IF_DELETE 1 // "删除节点"flag
#define LENGTH(a) ( (sizeof(a)) / (sizeof(a[0])) )
// 默认关键字数组
static Type arr[] = {10, 40, 30, 60, 90, 70, 20, 50, 80,
0, 100, 110, 120, 130, 140, 150, 160,
170, 180, 190};
#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
void main()
{
int a[] = {10, 150, 70, 0, 190, 270, 20, 50, 80};
int i, ilen=LENGTH(a);
RBRoot *root=NULL;
int main(void) {
int i, ret;
char cmd;
RBRoot *root = create_rbtree();
root = create_rbtree();
printf("red black tree 原始数据: ");
for(i=0; i<ilen; i++)
printf("%d ", a[i]);
// 构建红黑树
printf("== 原始数据: ");
for (i = 0; i < ARRAY_SIZE(arr); i++)
printf("%d ", arr[i]);
printf("\n");
for(i=0; i<ilen; i++)
{
insert_rbtree(root, a[i]);
#if IF_INSERT
printf("添加节点: %d\n", a[i]);
printf("树的结构信息: \n");
print_rbtree(root);
printf("\n");
#endif
for (i = 0; i < ARRAY_SIZE(arr); i++) {
RBTreeInsert(root, arr[i]);
}
if (rbtree_minimum(root, &i)==0)
printf("最小值: %d\n", i);
if (rbtree_maximum(root, &i)==0)
printf("最大值: %d\n", i);
printf("树的结构信息: \n");
printf("== 前序遍历: ");
preorder_rbtree(root);
printf("\n== 中序遍历: ");
inorder_rbtree(root);
printf("\n== 后序遍历: ");
postorder_rbtree(root);
// 输出红黑树
printf("== 中序遍历: ");
RBTreeTraversal(root);
printf("\n");
#if IF_DELETE
for(i=0; i<ilen; i++)
{
delete_rbtree(root, a[i]);
// 查找并删除
for (i = 0; i < ARRAY_SIZE(arr); i++) {
Node *target = NULL;
if (RBTreeSearch(root, arr[i]) == 0) {
printf("\n== 查找关键字: %d \n", arr[i]);
target = root->node;
if (target->parent)
printf("父节点: %d ", target->parent->key);
else
printf("父节点: NULL ");
printf("== 删除节点: %d\n", a[i]);
if (root)
{
printf("== 树的详细信息: \n");
print_rbtree(root);
printf("\n");
}
if (target->left_child)
printf("左子节点: %d ", target->left_child->key);
else
printf("左子节点: NULL ");
if (target->right_child)
printf("右子节点: %d ", target->right_child->key);
else
printf("右子节点: NULL ");
RBTreeDelete(root, arr[i]);
printf("\n== 关键字: %d 被删除", arr[i]);
}
#endif
destroy_rbtree(root);
}
// 如果红黑树为空, 结束程序
if (root->node == NULL) {
printf("\n红黑树为空,结束程序.\n");
break;
}
printf("\n\n是否显示当前的红黑树 (Y/N): ");
// char inputChar;
// scanf(" %c", &inputChar);
// if (inputChar == 'Y' || inputChar == 'y') {
// printf("\n== 中序遍历: ");
// RBTreeTraversal(root);
// printf("\n");
// }
}
destroy_rbtree(root);
return 0;
}