modify2
This commit is contained in:
parent
4aceaff0c0
commit
3e2208cbec
|
@ -3,13 +3,13 @@
|
||||||
#include "rbtree.h"
|
#include "rbtree.h"
|
||||||
|
|
||||||
#define rb_parent(r) ((r)->parent)
|
#define rb_parent(r) ((r)->parent)
|
||||||
#define rb_color(r) ((r)->color)
|
#define rb_color(r) ((r)->is_red)
|
||||||
#define rb_is_red(r) ((r)->color==RED)
|
#define rb_is_red(r) ((r)->is_red==RED)
|
||||||
#define rb_is_black(r) ((r)->color==BLACK)
|
#define rb_is_black(r) ((r)->is_red==BLACK)
|
||||||
#define rb_set_black(r) do { (r)->color = BLACK; } while (0)
|
#define rb_set_black(r) do { (r)->is_red = BLACK; } while (0)
|
||||||
#define rb_set_red(r) do { (r)->color = RED; } 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_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;
|
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)
|
if(tree != NULL)
|
||||||
{
|
{
|
||||||
inorder(tree->left);
|
inorder(tree->left_child);
|
||||||
printf("%d ", tree->key);
|
printf("%d ", tree->key);
|
||||||
inorder(tree->right);
|
inorder(tree->right_child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void inorder_rbtree(RBRoot *root)
|
void RBTreeTraversal(RBRoot *root)
|
||||||
{
|
{
|
||||||
if (root)
|
if (root)
|
||||||
inorder(root->node);
|
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的节点
|
* 递归查找键值为key的节点
|
||||||
|
@ -87,36 +52,17 @@ static Node* search(RBTree x, Type key)
|
||||||
return x;
|
return x;
|
||||||
|
|
||||||
if (key < x->key)
|
if (key < x->key)
|
||||||
return search(x->left, key);
|
return search(x->left_child, key);
|
||||||
else
|
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)
|
if (root)
|
||||||
return search(root->node, key)? 0 : -1;
|
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为根结点的红黑树的最小结点。
|
* 返回tree为根结点的红黑树的最小结点。
|
||||||
|
@ -126,8 +72,8 @@ static Node* minimum(RBTree tree)
|
||||||
if (tree == NULL)
|
if (tree == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
while(tree->left != NULL)
|
while(tree->left_child != NULL)
|
||||||
tree = tree->left;
|
tree = tree->left_child;
|
||||||
return tree;
|
return tree;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,42 +91,17 @@ int rbtree_minimum(RBRoot *root, int *val)
|
||||||
return 0;
|
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)的后继结点
|
* 找结点(x)的后继结点
|
||||||
*/
|
*/
|
||||||
static Node* rbtree_successor(RBTree x)
|
static Node* FindSuccessor(RBTree x)
|
||||||
{
|
{
|
||||||
if (x->right != NULL)
|
if (x->right_child != NULL)
|
||||||
return minimum(x->right);
|
return minimum(x->right_child);
|
||||||
Node* y = x->parent;
|
Node* y = x->parent;
|
||||||
while ((y!=NULL) && (x==y->right))
|
while ((y!=NULL) && (x==y->right_child))
|
||||||
{
|
{
|
||||||
x = y;
|
x = y;
|
||||||
y = y->parent;
|
y = y->parent;
|
||||||
|
@ -189,32 +110,16 @@ static Node* rbtree_successor(RBTree x)
|
||||||
return y;
|
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)进行左旋转
|
* 对红黑树的节点(x)进行左旋转
|
||||||
*/
|
*/
|
||||||
static void rbtree_left_rotate(RBRoot *root, Node *x)
|
static void RBTreeLeftRotate(RBRoot *root, Node *x)
|
||||||
{
|
{
|
||||||
Node *y = x->right;
|
Node *y = x->right_child;
|
||||||
x->right = y->left;
|
x->right_child = y->left_child;
|
||||||
if (y->left != NULL)
|
if (y->left_child != NULL)
|
||||||
y->left->parent = x;
|
y->left_child->parent = x;
|
||||||
y->parent = x->parent;
|
y->parent = x->parent;
|
||||||
|
|
||||||
if (x->parent == NULL)
|
if (x->parent == NULL)
|
||||||
|
@ -223,25 +128,25 @@ static void rbtree_left_rotate(RBRoot *root, Node *x)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (x->parent->left == x)
|
if (x->parent->left_child == x)
|
||||||
x->parent->left = y;
|
x->parent->left_child = y;
|
||||||
else
|
else
|
||||||
x->parent->right = y;
|
x->parent->right_child = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
y->left = x;
|
y->left_child = x;
|
||||||
x->parent = y;
|
x->parent = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 对红黑树的节点(y)进行右旋转
|
* 对红黑树的节点(y)进行右旋转
|
||||||
*/
|
*/
|
||||||
static void rbtree_right_rotate(RBRoot *root, Node *y)
|
static void RBTreeRightRotate(RBRoot *root, Node *y)
|
||||||
{
|
{
|
||||||
Node *x = y->left;
|
Node *x = y->left_child;
|
||||||
y->left = x->right;
|
y->left_child = x->right_child;
|
||||||
if (x->right != NULL)
|
if (x->right_child != NULL)
|
||||||
x->right->parent = y;
|
x->right_child->parent = y;
|
||||||
x->parent = y->parent;
|
x->parent = y->parent;
|
||||||
|
|
||||||
if (y->parent == NULL)
|
if (y->parent == NULL)
|
||||||
|
@ -250,19 +155,19 @@ static void rbtree_right_rotate(RBRoot *root, Node *y)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (y == y->parent->right)
|
if (y == y->parent->right_child)
|
||||||
y->parent->right = x;
|
y->parent->right_child = x;
|
||||||
else
|
else
|
||||||
y->parent->left = x;
|
y->parent->left_child = x;
|
||||||
}
|
}
|
||||||
x->right = y;
|
x->right_child = y;
|
||||||
y->parent = x;
|
y->parent = x;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 红黑树插入修正函数
|
* 红黑树插入修正函数
|
||||||
*/
|
*/
|
||||||
static void rbtree_insert_fixup(RBRoot *root, Node *node)
|
static void InsertFixup(RBRoot *root, Node *node)
|
||||||
{
|
{
|
||||||
Node *parent, *gparent;
|
Node *parent, *gparent;
|
||||||
|
|
||||||
|
@ -272,11 +177,11 @@ static void rbtree_insert_fixup(RBRoot *root, Node *node)
|
||||||
gparent = rb_parent(parent);
|
gparent = rb_parent(parent);
|
||||||
|
|
||||||
//若“父节点”是“祖父节点的左孩子”
|
//若“父节点”是“祖父节点的左孩子”
|
||||||
if (parent == gparent->left)
|
if (parent == gparent->left_child)
|
||||||
{
|
{
|
||||||
// Case 1条件:叔叔节点是红色
|
// Case 1条件:叔叔节点是红色
|
||||||
{
|
{
|
||||||
Node *uncle = gparent->right;
|
Node *uncle = gparent->right_child;
|
||||||
if (uncle && rb_is_red(uncle))
|
if (uncle && rb_is_red(uncle))
|
||||||
{
|
{
|
||||||
rb_set_black(uncle);
|
rb_set_black(uncle);
|
||||||
|
@ -288,10 +193,10 @@ static void rbtree_insert_fixup(RBRoot *root, Node *node)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Case 2条件:叔叔是黑色,且当前节点是右孩子
|
// Case 2条件:叔叔是黑色,且当前节点是右孩子
|
||||||
if (parent->right == node)
|
if (parent->right_child == node)
|
||||||
{
|
{
|
||||||
Node *tmp;
|
Node *tmp;
|
||||||
rbtree_left_rotate(root, parent);
|
RBTreeLeftRotate(root, parent);
|
||||||
tmp = parent;
|
tmp = parent;
|
||||||
parent = node;
|
parent = node;
|
||||||
node = tmp;
|
node = tmp;
|
||||||
|
@ -300,13 +205,13 @@ static void rbtree_insert_fixup(RBRoot *root, Node *node)
|
||||||
// Case 3条件:叔叔是黑色,且当前节点是左孩子。
|
// Case 3条件:叔叔是黑色,且当前节点是左孩子。
|
||||||
rb_set_black(parent);
|
rb_set_black(parent);
|
||||||
rb_set_red(gparent);
|
rb_set_red(gparent);
|
||||||
rbtree_right_rotate(root, gparent);
|
RBTreeRightRotate(root, gparent);
|
||||||
}
|
}
|
||||||
else//若“z的父节点”是“z的祖父节点的右孩子”
|
else//若“z的父节点”是“z的祖父节点的右孩子”
|
||||||
{
|
{
|
||||||
// Case 1条件:叔叔节点是红色
|
// Case 1条件:叔叔节点是红色
|
||||||
{
|
{
|
||||||
Node *uncle = gparent->left;
|
Node *uncle = gparent->left_child;
|
||||||
if (uncle && rb_is_red(uncle))
|
if (uncle && rb_is_red(uncle))
|
||||||
{
|
{
|
||||||
rb_set_black(uncle);
|
rb_set_black(uncle);
|
||||||
|
@ -318,10 +223,10 @@ static void rbtree_insert_fixup(RBRoot *root, Node *node)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Case 2条件:叔叔是黑色,且当前节点是左孩子
|
// Case 2条件:叔叔是黑色,且当前节点是左孩子
|
||||||
if (parent->left == node)
|
if (parent->left_child == node)
|
||||||
{
|
{
|
||||||
Node *tmp;
|
Node *tmp;
|
||||||
rbtree_right_rotate(root, parent);
|
RBTreeRightRotate(root, parent);
|
||||||
tmp = parent;
|
tmp = parent;
|
||||||
parent = node;
|
parent = node;
|
||||||
node = tmp;
|
node = tmp;
|
||||||
|
@ -330,7 +235,7 @@ static void rbtree_insert_fixup(RBRoot *root, Node *node)
|
||||||
// Case 3条件:叔叔是黑色,且当前节点是右孩子。
|
// Case 3条件:叔叔是黑色,且当前节点是右孩子。
|
||||||
rb_set_black(parent);
|
rb_set_black(parent);
|
||||||
rb_set_red(gparent);
|
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 *y = NULL;
|
||||||
Node *x = root->node;
|
Node *x = root->node;
|
||||||
|
@ -349,41 +254,41 @@ static void rbtree_insert(RBRoot *root, Node *node)
|
||||||
{
|
{
|
||||||
y = x;
|
y = x;
|
||||||
if (node->key < x->key)
|
if (node->key < x->key)
|
||||||
x = x->left;
|
x = x->left_child;
|
||||||
else
|
else
|
||||||
x = x->right;
|
x = x->right_child;
|
||||||
}
|
}
|
||||||
rb_parent(node) = y;
|
rb_parent(node) = y;
|
||||||
|
|
||||||
if (y != NULL)
|
if (y != NULL)
|
||||||
{
|
{
|
||||||
if (node->key < y->key)
|
if (node->key < y->key)
|
||||||
y->left = node;
|
y->left_child = node;
|
||||||
else
|
else
|
||||||
y->right = node;
|
y->right_child = node;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
root->node = node;
|
root->node = node;
|
||||||
}
|
}
|
||||||
node->color = RED;
|
node->is_red = RED;
|
||||||
rbtree_insert_fixup(root, node);
|
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;
|
Node* p;
|
||||||
|
|
||||||
if ((p = (Node *)malloc(sizeof(Node))) == NULL)
|
if ((p = (Node *)malloc(sizeof(Node))) == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
p->key = key;
|
p->key = key;
|
||||||
p->left = left;
|
p->left_child = left_child;
|
||||||
p->right = right;
|
p->right_child = right_child;
|
||||||
p->parent = parent;
|
p->parent = parent;
|
||||||
p->color = BLACK; // 默认为黑色
|
p->is_red = BLACK; // 默认为黑色
|
||||||
|
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
@ -391,7 +296,7 @@ static Node* create_rbtree_node(Type key, Node *parent, Node *left, Node* right)
|
||||||
/*
|
/*
|
||||||
* 新建结点(节点键值为key),并将其插入到红黑树中
|
* 新建结点(节点键值为key),并将其插入到红黑树中
|
||||||
*/
|
*/
|
||||||
int insert_rbtree(RBRoot *root, Type key)
|
int RBTreeInsert(RBRoot *root, Type key)
|
||||||
{
|
{
|
||||||
Node *node;
|
Node *node;
|
||||||
if (search(root->node, key) != NULL)
|
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)
|
if ((node=create_rbtree_node(key, NULL, NULL, NULL)) == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
rbtree_insert(root, node);
|
insert_node(root, node);
|
||||||
|
|
||||||
return 0;
|
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;
|
Node *other;
|
||||||
|
|
||||||
while ((!node || rb_is_black(node)) && node != root->node)
|
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))
|
if (rb_is_red(other))
|
||||||
{
|
{
|
||||||
rb_set_black(other);
|
rb_set_black(other);
|
||||||
rb_set_red(parent);
|
rb_set_red(parent);
|
||||||
rbtree_left_rotate(root, parent);
|
RBTreeLeftRotate(root, parent);
|
||||||
other = parent->right;
|
other = parent->right_child;
|
||||||
}
|
}
|
||||||
if ((!other->left || rb_is_black(other->left)) &&
|
if ((!other->left_child || rb_is_black(other->left_child)) &&
|
||||||
(!other->right || rb_is_black(other->right)))
|
(!other->right_child || rb_is_black(other->right_child)))
|
||||||
{
|
{
|
||||||
rb_set_red(other);
|
rb_set_red(other);
|
||||||
node = parent;
|
node = parent;
|
||||||
|
@ -432,33 +337,33 @@ static void rbtree_delete_fixup(RBRoot *root, Node *node, Node *parent)
|
||||||
}
|
}
|
||||||
else
|
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);
|
rb_set_red(other);
|
||||||
rbtree_right_rotate(root, other);
|
RBTreeRightRotate(root, other);
|
||||||
other = parent->right;
|
other = parent->right_child;
|
||||||
}
|
}
|
||||||
rb_set_color(other, rb_color(parent));
|
rb_set_color(other, rb_color(parent));
|
||||||
rb_set_black(parent);
|
rb_set_black(parent);
|
||||||
rb_set_black(other->right);
|
rb_set_black(other->right_child);
|
||||||
rbtree_left_rotate(root, parent);
|
RBTreeLeftRotate(root, parent);
|
||||||
node = root->node;
|
node = root->node;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
other = parent->left;
|
other = parent->left_child;
|
||||||
if (rb_is_red(other))
|
if (rb_is_red(other))
|
||||||
{
|
{
|
||||||
rb_set_black(other);
|
rb_set_black(other);
|
||||||
rb_set_red(parent);
|
rb_set_red(parent);
|
||||||
rbtree_right_rotate(root, parent);
|
RBTreeRightRotate(root, parent);
|
||||||
other = parent->left;
|
other = parent->left_child;
|
||||||
}
|
}
|
||||||
if ((!other->left || rb_is_black(other->left)) &&
|
if ((!other->left_child || rb_is_black(other->left_child)) &&
|
||||||
(!other->right || rb_is_black(other->right)))
|
(!other->right_child || rb_is_black(other->right_child)))
|
||||||
{
|
{
|
||||||
rb_set_red(other);
|
rb_set_red(other);
|
||||||
node = parent;
|
node = parent;
|
||||||
|
@ -466,19 +371,19 @@ static void rbtree_delete_fixup(RBRoot *root, Node *node, Node *parent)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!other->left || rb_is_black(other->left))
|
if (!other->left_child || rb_is_black(other->left_child))
|
||||||
{
|
{
|
||||||
// Case 3: x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。
|
// Case 3: x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。
|
||||||
rb_set_black(other->right);
|
rb_set_black(other->right_child);
|
||||||
rb_set_red(other);
|
rb_set_red(other);
|
||||||
rbtree_left_rotate(root, other);
|
RBTreeLeftRotate(root, other);
|
||||||
other = parent->left;
|
other = parent->left_child;
|
||||||
}
|
}
|
||||||
// Case 4: x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。
|
// Case 4: x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。
|
||||||
rb_set_color(other, rb_color(parent));
|
rb_set_color(other, rb_color(parent));
|
||||||
rb_set_black(parent);
|
rb_set_black(parent);
|
||||||
rb_set_black(other->left);
|
rb_set_black(other->left_child);
|
||||||
rbtree_right_rotate(root, parent);
|
RBTreeRightRotate(root, parent);
|
||||||
node = root->node;
|
node = root->node;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -494,25 +399,25 @@ static void rbtree_delete_fixup(RBRoot *root, Node *node, Node *parent)
|
||||||
void rbtree_delete(RBRoot *root, Node *node)
|
void rbtree_delete(RBRoot *root, Node *node)
|
||||||
{
|
{
|
||||||
Node *child, *parent;
|
Node *child, *parent;
|
||||||
int color;
|
int is_red;
|
||||||
if ( (node->left!=NULL) && (node->right!=NULL) )
|
if ( (node->left_child!=NULL) && (node->right_child!=NULL) )
|
||||||
{
|
{
|
||||||
Node *replace = node;
|
Node *replace = node;
|
||||||
replace = replace->right;
|
replace = replace->right_child;
|
||||||
while (replace->left != NULL)
|
while (replace->left_child != NULL)
|
||||||
replace = replace->left;
|
replace = replace->left_child;
|
||||||
if (rb_parent(node))
|
if (rb_parent(node))
|
||||||
{
|
{
|
||||||
if (rb_parent(node)->left == node)
|
if (rb_parent(node)->left_child == node)
|
||||||
rb_parent(node)->left = replace;
|
rb_parent(node)->left_child = replace;
|
||||||
else
|
else
|
||||||
rb_parent(node)->right = replace;
|
rb_parent(node)->right_child = replace;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
root->node = replace;
|
root->node = replace;
|
||||||
child = replace->right;
|
child = replace->right_child;
|
||||||
parent = rb_parent(replace);
|
parent = rb_parent(replace);
|
||||||
color = rb_color(replace);
|
is_red = rb_is_red(replace);
|
||||||
if (parent == node)
|
if (parent == node)
|
||||||
{
|
{
|
||||||
parent = replace;
|
parent = replace;
|
||||||
|
@ -521,53 +426,53 @@ void rbtree_delete(RBRoot *root, Node *node)
|
||||||
{
|
{
|
||||||
if (child)
|
if (child)
|
||||||
rb_set_parent(child, parent);
|
rb_set_parent(child, parent);
|
||||||
parent->left = child;
|
parent->left_child = child;
|
||||||
|
|
||||||
replace->right = node->right;
|
replace->right_child = node->right_child;
|
||||||
rb_set_parent(node->right, replace);
|
rb_set_parent(node->right_child, replace);
|
||||||
}
|
}
|
||||||
|
|
||||||
replace->parent = node->parent;
|
replace->parent = node->parent;
|
||||||
replace->color = node->color;
|
replace->is_red = node->is_red;
|
||||||
replace->left = node->left;
|
replace->left_child = node->left_child;
|
||||||
node->left->parent = replace;
|
node->left_child->parent = replace;
|
||||||
|
|
||||||
if (color == BLACK)
|
if (is_red == BLACK)
|
||||||
rbtree_delete_fixup(root, child, parent);
|
DeleteFixup(root, child, parent);
|
||||||
free(node);
|
free(node);
|
||||||
|
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node->left !=NULL)
|
if (node->left_child !=NULL)
|
||||||
child = node->left;
|
child = node->left_child;
|
||||||
else
|
else
|
||||||
child = node->right;
|
child = node->right_child;
|
||||||
|
|
||||||
parent = node->parent;
|
parent = node->parent;
|
||||||
color = node->color;
|
is_red = node->is_red;
|
||||||
|
|
||||||
if (child)
|
if (child)
|
||||||
child->parent = parent;
|
child->parent = parent;
|
||||||
if (parent)
|
if (parent)
|
||||||
{
|
{
|
||||||
if (parent->left == node)
|
if (parent->left_child == node)
|
||||||
parent->left = child;
|
parent->left_child = child;
|
||||||
else
|
else
|
||||||
parent->right = child;
|
parent->right_child = child;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
root->node = child;
|
root->node = child;
|
||||||
|
|
||||||
if (color == BLACK)
|
if (is_red == BLACK)
|
||||||
rbtree_delete_fixup(root, child, parent);
|
DeleteFixup(root, child, parent);
|
||||||
free(node);
|
free(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 删除键值为key的结点
|
* 删除键值为key的结点
|
||||||
*/
|
*/
|
||||||
void delete_rbtree(RBRoot *root, Type key)
|
void RBTreeDelete(RBRoot *root, Type key)
|
||||||
{
|
{
|
||||||
Node *z, *node;
|
Node *z, *node;
|
||||||
|
|
||||||
|
@ -583,10 +488,10 @@ static void rbtree_destroy(RBTree tree)
|
||||||
if (tree==NULL)
|
if (tree==NULL)
|
||||||
return ;
|
return ;
|
||||||
|
|
||||||
if (tree->left != NULL)
|
if (tree->left_child != NULL)
|
||||||
rbtree_destroy(tree->left);
|
rbtree_destroy(tree->left_child);
|
||||||
if (tree->right != NULL)
|
if (tree->right_child != NULL)
|
||||||
rbtree_destroy(tree->right);
|
rbtree_destroy(tree->right_child);
|
||||||
|
|
||||||
free(tree);
|
free(tree);
|
||||||
}
|
}
|
||||||
|
@ -609,10 +514,10 @@ static void rbtree_print(RBTree tree, Type key, int direction)
|
||||||
if(direction==0) // tree是根节点
|
if(direction==0) // tree是根节点
|
||||||
printf("%2d(B) is root\n", tree->key);
|
printf("%2d(B) is root\n", tree->key);
|
||||||
else // tree是分支节点
|
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->left_child, tree->key, -1);
|
||||||
rbtree_print(tree->right,tree->key, 1);
|
rbtree_print(tree->right_child,tree->key, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,48 +7,42 @@
|
||||||
typedef int Type;
|
typedef int Type;
|
||||||
|
|
||||||
// 红黑树的节点
|
// 红黑树的节点
|
||||||
typedef struct RBTreeNode{
|
typedef struct RBNodeType{
|
||||||
unsigned char color; // 颜色(RED 或 BLACK)
|
unsigned char is_red; // 颜色(RED 或 BLACK)
|
||||||
Type key; // 关键字(键值)
|
Type key; // 关键字(键值)
|
||||||
struct RBTreeNode *left; // 左孩子
|
struct RBNodeType *left_child; // 左孩子
|
||||||
struct RBTreeNode *right; // 右孩子
|
struct RBNodeType *right_child; // 右孩子
|
||||||
struct RBTreeNode *parent; // 父结点
|
struct RBNodeType *parent; // 父结点
|
||||||
}Node, *RBTree;
|
}Node, *RBTree;
|
||||||
|
|
||||||
// 红黑树的根
|
// 红黑树的根
|
||||||
typedef struct rb_root{
|
typedef struct RBTreeType{
|
||||||
Node *node;
|
Node *node;
|
||||||
}RBRoot;
|
}RBRoot;
|
||||||
|
|
||||||
// 创建红黑树
|
// 创建红黑树,返回"红黑树的根"!
|
||||||
RBRoot* create_rbtree();
|
RBRoot* create_rbtree();
|
||||||
|
|
||||||
// 销毁红黑树
|
// 销毁红黑树
|
||||||
void destroy_rbtree(RBRoot *root);
|
void destroy_rbtree(RBRoot *root);
|
||||||
|
|
||||||
// 将结点插入到红黑树中。插入成功,返回0;失败返回-1。
|
// 将结点插入到红黑树中。插入成功,返回0;失败返回-1。
|
||||||
int insert_rbtree(RBRoot *root, Type key);
|
int RBTreeInsert(RBRoot *root, Type key);
|
||||||
|
|
||||||
// 删除结点(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 RBTreeTraversal(RBRoot *root);
|
||||||
// 后序遍历"红黑树"
|
|
||||||
void postorder_rbtree(RBRoot *root);
|
|
||||||
|
|
||||||
// 递归查找键值为key的节点。(找到返回0,找不到返回-1)
|
|
||||||
int rbtree_search(RBRoot *root, Type key);
|
// (递归实现)查找"红黑树"中键值为key的节点。找到的话,返回0;否则,返回-1。
|
||||||
// 非递归查找键值为key的节点。(找到返回0,找不到返回-1)
|
int RBTreeSearch(RBRoot *root, Type key);
|
||||||
int iterative_rbtree_search(RBRoot *root, Type key);
|
|
||||||
|
|
||||||
// 返回最小结点的值(存于val)(找到返回0,找不到返回-1)
|
// 返回最小结点的值(存于val)(找到返回0,找不到返回-1)
|
||||||
int rbtree_minimum(RBRoot *root, int *val);
|
int rbtree_minimum(RBRoot *root, int *val);
|
||||||
// 返回最大结点的值(保存于val)。(找到返回0,找不到返回-1)
|
|
||||||
int rbtree_maximum(RBRoot *root, int *val);
|
|
||||||
|
|
||||||
// 打印红黑树
|
// 打印红黑树
|
||||||
void print_rbtree(RBRoot *root);
|
void print_rbtree(RBRoot *root);
|
||||||
|
|
|
@ -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 <stdio.h>
|
||||||
#include "rbtree.h"
|
#include "rbtree.h"
|
||||||
#include "rbtree.c"
|
#include "rbtree.c"
|
||||||
|
|
||||||
#define IF_INSERT 1 // "插入节点"flag
|
// 默认关键字数组
|
||||||
#define IF_DELETE 1 // "删除节点"flag
|
static Type arr[] = {10, 40, 30, 60, 90, 70, 20, 50, 80,
|
||||||
#define LENGTH(a) ( (sizeof(a)) / (sizeof(a[0])) )
|
0, 100, 110, 120, 130, 140, 150, 160,
|
||||||
|
170, 180, 190};
|
||||||
|
#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
|
||||||
|
|
||||||
void main()
|
int main(void) {
|
||||||
{
|
int i, ret;
|
||||||
int a[] = {10, 150, 70, 0, 190, 270, 20, 50, 80};
|
char cmd;
|
||||||
int i, ilen=LENGTH(a);
|
RBRoot *root = create_rbtree();
|
||||||
RBRoot *root=NULL;
|
|
||||||
|
|
||||||
root = create_rbtree();
|
// 构建红黑树
|
||||||
printf("red black tree 原始数据: ");
|
printf("== 原始数据: ");
|
||||||
for(i=0; i<ilen; i++)
|
for (i = 0; i < ARRAY_SIZE(arr); i++)
|
||||||
printf("%d ", a[i]);
|
printf("%d ", arr[i]);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
for(i=0; i<ilen; i++)
|
for (i = 0; i < ARRAY_SIZE(arr); i++) {
|
||||||
{
|
RBTreeInsert(root, arr[i]);
|
||||||
insert_rbtree(root, a[i]);
|
|
||||||
#if IF_INSERT
|
|
||||||
printf("添加节点: %d\n", a[i]);
|
|
||||||
printf("树的结构信息: \n");
|
|
||||||
print_rbtree(root);
|
|
||||||
printf("\n");
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rbtree_minimum(root, &i)==0)
|
// 输出红黑树
|
||||||
printf("最小值: %d\n", i);
|
printf("== 中序遍历: ");
|
||||||
if (rbtree_maximum(root, &i)==0)
|
RBTreeTraversal(root);
|
||||||
printf("最大值: %d\n", i);
|
|
||||||
printf("树的结构信息: \n");
|
|
||||||
|
|
||||||
printf("== 前序遍历: ");
|
|
||||||
preorder_rbtree(root);
|
|
||||||
|
|
||||||
printf("\n== 中序遍历: ");
|
|
||||||
inorder_rbtree(root);
|
|
||||||
|
|
||||||
printf("\n== 后序遍历: ");
|
|
||||||
postorder_rbtree(root);
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
#if IF_DELETE
|
// 查找并删除
|
||||||
for(i=0; i<ilen; i++)
|
for (i = 0; i < ARRAY_SIZE(arr); i++) {
|
||||||
{
|
Node *target = NULL;
|
||||||
delete_rbtree(root, a[i]);
|
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 (target->left_child)
|
||||||
if (root)
|
printf("左子节点: %d ", target->left_child->key);
|
||||||
{
|
else
|
||||||
printf("== 树的详细信息: \n");
|
printf("左子节点: NULL ");
|
||||||
print_rbtree(root);
|
|
||||||
printf("\n");
|
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;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue