标签:style blog class code c ext
#include <stdio.h>
typedef int ElementType;
typedef struct RedBlackNode *Position;
typedef Position RedBlackTree;
typedef enum ColorType {Red, Black} ColorType;
struct RedBlackNode {
ElementType Element;
RedBlackTree Left;
RedBlackTree Right;
ColorType Color;
};
Position NullNode = NULL;
// 初始化两个特殊节点:根标记和NULL节点
RedBlackTree Initialize(void)
{
RedBlackTree T;
if (NullNode == NULL)
{
NullNode = malloc(sizeof (struct RedBlackNode));
if (NullNode == NULL)
return -1;
NullNode->Left = NullNode->Right = NullNode;
NullNode->Element = 65535;
NullNode->Color = Black; // NULL节点为黑色
}
T = malloc(sizeof (struct RedBlackNode));
if (T == NULL)
return -1;
T->Left = T->Right = NullNode;
T->Element = -65535;
T->Color = Black;
return T;
}
Position SingleRotate_Left(Position T)
{
Position NewRoot;
NewRoot = T->Left;
T->Left = NewRoot->Right;
NewRoot->Right = T;
return NewRoot;
}
Position SingleRotate_Right(Position T)
{
Position NewRoot;
NewRoot = T->Right;
T->Right = NewRoot->Left;
NewRoot->Left = T;
return NewRoot;
}
static Position Rotate(Position Parent, ElementType Item)
{
if (Item < Parent->Element)
{
if (Item < Parent->Left->Element)
return Parent->Left = SingleRotate_Left(Parent->Left); // 左-左
else
return Parent->Left = SingleRotate_Right(Parent->Left); // 左-右
}
else
{
if (Item > Parent->Right->Element)
return Parent->Right = SingleRotate_Right(Parent->Right); // 右-右
else
return Parent->Right = SingleRotate_Left(Parent->Right); // 右-左
}
}
static Position x, p, gp, ggp;
static void HandleReorient(RedBlackTree T, ElementType Item)
{
x->Color = Red;
x->Left->Color = Black;
x->Right->Color = Black;
if (p->Color == Red)
{
gp->Color = Red;
if ((Item < gp->Element) != (Item < p->Element))
p = Rotate(gp, Item); // 之字形,双旋转
x = Rotate(ggp, Item);
x->Color = Black;
}
T->Right->Color = Black;
}
// 每进行一次插入都要从上至下的遍历
RedBlackTree Insert(RedBlackTree T, ElementType Item)
{
gp = p = x = T;
NullNode->Element = Item;
while (x->Element != Item)
{
ggp = gp;
gp = p;
p = x;
if (Item < x->Element)
x = x->Left;
else
x = x->Right;
if (x->Left->Color == Red && x->Right->Color == Red)
HandleReorient(T, Item);
}
if (x != NullNode)
return T;
x = malloc(sizeof (struct RedBlackNode));
if (x == NULL)
return -1;
x->Element = Item;
x->Left = x->Right = NullNode;
if (Item < p->Element)
p->Left = x;
else
p->Right = x;
HandleReorient(T, Item);
return T;
}
static void MidPrint(RedBlackTree T)
{
if (T != NullNode)
{
MidPrint(T->Left);
printf("%d ", T->Right->Element);
MidPrint(T->Right);
}
}
int main()
{
RedBlackTree tree;
tree = Initialize();
tree = Insert(tree, 10);
tree = Insert(tree, 85);
tree = Insert(tree, 15);
tree = Insert(tree, 70);
tree = Insert(tree, 20);
tree = Insert(tree, 60);
tree = Insert(tree, 30);
tree = Insert(tree, 50);
tree = Insert(tree, 65);
tree = Insert(tree, 80);
tree = Insert(tree, 90);
tree = Insert(tree, 40);
tree = Insert(tree, 5);
tree = Insert(tree, 55);
MidPrint(tree);
return 0;
}
标签:style blog class code c ext
原文地址:http://blog.csdn.net/nestler/article/details/25738139