数据结构b类红黑二叉树正文终稿.docx
- 文档编号:23998950
- 上传时间:2023-05-23
- 格式:DOCX
- 页数:33
- 大小:18.80KB
数据结构b类红黑二叉树正文终稿.docx
《数据结构b类红黑二叉树正文终稿.docx》由会员分享,可在线阅读,更多相关《数据结构b类红黑二叉树正文终稿.docx(33页珍藏版)》请在冰豆网上搜索。
数据结构b类红黑二叉树正文终稿
#include
#include
#include
#include
#include
#defineTRUE1
#defineBOOLint
#defineFALSE0
#defineStatusint
enumcolor_t
{
RED,BlACK
};
typedefstructRedBlackNode//红黑二叉树结构体
{
intdata;
charphone[12];
charname[12];//数据域
color_tcolor;//颜色
RedBlackNode*left;//左孩子
RedBlackNode*right;//右孩子
RedBlackNode*parent;//父亲节点
}RedBlackNode,*RBTree;
typedefstructLinkStack
{
RedBlackNode*rbtree;
structLinkStack*next;
}LinkStack;
LinkStack*InitStack();
StatusStackEmpty(LinkStack*L);
StatusDestroyStack(LinkStack*L);
StatusStackLength(LinkStack*L);
StatusPushStack(LinkStack*L,RedBlackNode*r);
RedBlackNode*PopStack(LinkStack*L);
RedBlackNode*RBserach(RedBlackNode*rbtree,intkey);
RedBlackNode*RBMinimum(RBTree*T);
RedBlackNode*RBMaximum(RBTree*T);
RedBlackNode*RBpioneer(RedBlackNode*T);
RedBlackNode*RBsucceed(RedBlackNode*T);
voidleft_rotate(RBTree*rbtree,RedBlackNode*T);
voidright_rotate(RBTree*retree,RedBlackNode*T);
BOOLRBInsertNode(RBTree*T,intdata);
intRBDeleteNode(RBTree*T,intdata);
voidRbTreeInsertAdjust(RBTree*T,RedBlackNode*p);
voidRbTreeDeleteAdjust(RBTree*T,RedBlackNode*parent,RedBlackNode*x);
voidOutput(RedBlackNode*p);
voidPreorderTraverse(RedBlackNode*T);
voidInorderTraverse(RedBlackNode*T);
voidPostorderTraverse(RedBlackNode*T);
intprerecursion(RedBlackNode*T);
intinrecursion(RedBlackNode*T);
intpostrecursion(RedBlackNode*T);
voidmenu1();
voidmenu2();
voidlogon();
LinkStack*InitStack()
{
LinkStack*L;
L=(LinkStack*)malloc(sizeof(LinkStack));
L->next=NULL;
returnL;
}
StatusStackEmpty(LinkStack*L)
{
if(L->next)
{
returnFALSE;
}
else
{
returnTRUE;
}
}
StatusDestroyStack(LinkStack*L)
{
LinkStack*p,*r,*q;
p=L->next;
r=L;
if(p==NULL)
{
returnFALSE;
}
while(p!
=NULL)
{
r->next=p->next;
q=p;
p=p->next;
free(q);
}
free(L);
returnTRUE;
}
StatusStackLength(LinkStack*L)
{
inti=0;
LinkStack*p;
p=L->next;
if(L==NULL)
{
returnFALSE;
}
while(p!
=NULL)
{
i++;
p=p->next;
}
returni;
}
RedBlackNode*PopStack(LinkStack*L)
{
LinkStack*p;
RedBlackNode*q;
p=L;
while(p->next&&p->next->next!
=NULL)
{
p=p->next;
}
q=p->next->rbtree;
p->next=NULL;
returnq;
}
StatusPushStack(LinkStack*L,RedBlackNode*r)
{
LinkStack*p,*stacknode=(LinkStack*)malloc(sizeof(LinkStack));
p=L;
while(p->next!
=NULL)
{
p=p->next;
}
stacknode->rbtree=r;
stacknode->next=NULL;
p->next=stacknode;
returnTRUE;
}
RedBlackNode*RBserach(RBTree*rbtree,intkey)//查找值为key的节点
{
RedBlackNode*T;
T=*rbtree;
while(T!
=NULL&&T->data!
=key)
{
if(key
{
T=T->left;
}
else
{
T=T->right;
}
}
Output(T);
returnT;
}
RedBlackNode*RBMinimum(RBTree*T)//返回红黑树局部最小值
{
RedBlackNode*curNode,*targetNode;
curNode=*T;
targetNode=NULL;
if(curNode!
=NULL)
{
targetNode=curNode;
curNode=curNode->left;
}
returntargetNode;
}
RedBlackNode*RBMaximum(RBTree*T)//返回红黑树局部最大值
{
RedBlackNode*curNode,*targetNode;
curNode=*T;
targetNode=NULL;
if(curNode!
=NULL)
{
targetNode=curNode;
curNode=curNode->right;
}
returntargetNode;
}
RedBlackNode*RBpioneer(RedBlackNode*T)//返回T的前驱
{
RedBlackNode*targetNode;
RedBlackNode*P;
P=NULL;
if(T==NULL)
{
returnP;
}
if(T->left!
=NULL)
{
targetNode=RBMaximum(&(T->left));
}
else
{
while(T->parent!
=NULL&&T->parent->right!
=T)
{
T=T->parent;
}
targetNode=T->parent;
}
returntargetNode;
}
RedBlackNode*RBsucceed(RedBlackNode*T)//后继节点
{
RedBlackNode*targetNode;
RedBlackNode*P;
P=NULL;
if(T==NULL)
{
returnP;
}
if(T->right!
=NULL)
{
targetNode=RBMinimum(&(T->right));
}
else
{
while(T->parent!
=NULL&&T->parent->left!
=T)
{
T=T->parent;
}
targetNode=T->parent;
}
returntargetNode;
}
voidleft_rotate(RBTree*rbtree,RedBlackNode*T)//左旋
{
RedBlackNode*p;
p=T->right;
T->right=p->left;
if(p->left!
=NULL)
{
p->left->parent=T;
}
p->parent=T->parent;
if(T->parent==NULL)
{
*rbtree=p;
}
else
{
if(T->parent->left==T)
{
T->parent->left=p;
}
else
{
T->parent->right=p;
}
}
p->left=T;
T->parent=p;
}
voidright_rotate(RBTree*retree,RedBlackNode*T)
{
RedBlackNode*p;
p=T->left;
T->left=p->right;
if(p->right!
=NULL)
{
p->right->parent=T;
}
p->parent=T->parent;
if(T->parent==NULL)
{
*retree=p;
}
else
{
if(T->parent->left==T)
{
T->parent->left=p;
}
else
{
T->parent->right=p;
}
}
p->right=T;
T->parent=p;
}
BOOLRBInsertNode(RBTree*T,intdata,char*name,char*phone)
{
RedBlackNode*node,*p,*curNode;
node=(RedBlackNode*)malloc(sizeof(RedBlackNode));
if(node==NULL)
returnFALSE;
node->data=data;
strcpy(node->phone,phone);
strcpy(node->name,name);
node->color=RED;
node->left=NULL;
node->right=NULL;
node->parent=NULL;
curNode=*T;
p=NULL;
while(curNode!
=NULL)
{
p=curNode;
if(data
{
curNode=curNode->left;
}
else
{
curNode=curNode->right;
}
}
if(p==NULL)
{
*T=node;
}
else
{
if(data
{
p->left=node;
}
else
{
p->right=node;
}
}
node->parent=p;
RbTreeInsertAdjust(T,node);
returnTRUE;
}
intRBDeleteNode(RBTree*T,intdata,char*name,char*phone)
{
RedBlackNode*child,*target,*realdel;
target=RBserach(T,data);
if(target!
=NULL)
{
if(target->left==NULL||target->right==NULL)
{
realdel=target;
}
else
{
realdel=RBsucceed(target);
}
if(realdel->left!
=NULL)
{
child=realdel->left;
}
else
{
child=realdel->right;
}
if(child!
=NULL)
{
child->parent=realdel->parent;
}
if(realdel->parent==NULL)
{
*T=child;
}
else
{
if(realdel->parent->left==realdel)
{
realdel->parent->left=child;
}
else
{
realdel->parent->right=child;
}
}
if(target!
=realdel)
{
target->data=realdel->data;
strcpy(target->phone,phone);
strcpy(target->name,name);
}
if(realdel->color==BlACK)
{
RbTreeDeleteAdjust(T,realdel->parent,child);
}
free(realdel);
returnTRUE;
}
else
{
returnFALSE;
}
}
voidRbTreeInsertAdjust(RBTree*T,RedBlackNode*p)
{
RedBlackNode*q,*uncle,*grandparent;
while((q=p->parent)!
=NULL&&q->color==RED)
{
grandparent=q->parent;
if(q==grandparent->left)
{
uncle=grandparent->right;
if(uncle!
=NULL&&uncle->color==RED)
{
grandparent->color=RED;
q->color=BlACK;
uncle->color=BlACK;
p=grandparent;
}
else
{
if(p==q->right)
{
p=q;
left_rotate(T,p);
q=p->parent;
}
else
{
q->color=BlACK;
grandparent->color=RED;
right_rotate(T,grandparent);
}
}
}
else
{
uncle=grandparent->left;
if(uncle!
=NULL&&uncle->color==RED)
{
grandparent->color=RED;
q->color=BlACK;
uncle->color=BlACK;
p=grandparent;
}
else
{
if(p==q->left)
{
p=q;
right_rotate(T,p);
q=p->parent;
}
else
{
q->color=BlACK;
grandparent->color=RED;
left_rotate(T,grandparent);
}
}
}
}
(*T)->color=BlACK;
}
voidRbTreeDeleteAdjust(RBTree*T,RedBlackNode*parent,RedBlackNode*x)
{
RedBlackNode*brother;
while((x==NULL||x->color==BlACK)&&x!
=*T)
{
if(x==parent->left)
{
brother=parent->right;
if(brother->color==RED)
{
brother->color=BlACK;
parent->color=RED;
left_rotate(T,parent);
brother=parent->right;
}
if((brother->left==NULL||brother->left->color==BlACK)&&
(brother->right==NULL||brother->right->color==BlACK))
{
brother->color=RED;
x=parent;
parent=parent->parent;
}
else
{
if(brother->right==NULL||brother->color==BlACK)
{
brother->left->color=BlACK;
brother->color=RED;
right_rotate(T,brother);
brother=parent->right;
}
brother->color=parent->color;
parent->color=BlACK;
brother->right->color=BlACK;
left_rotate(T,parent);
x=*T;
}
}
else
{
brother=parent->left;
if(brother->color==RED)
{
brother->color=BlACK;
parent->color=RED;
right_rotate(T,parent);
brother=parent->left;
}
if((brother->right==NULL||brother->right->color==BlACK)&&
(brother->left==NULL||brother->left->color==BlACK))
{
brother->color=RED;
x=parent;
parent=parent->parent;
}
else
{
if(brother->left==NULL||brother->left->color==BlACK)
{
brother->right->color=BlACK;
brother->color=RED;
left_rotate(T,brother);
brother=parent->left;
}
brother->color=parent->color;
parent->color=BlACK;
brother->left->color=BlACK;
right_rotate(T,parent);
x=*T;
}
}
}
if(x!
=NULL)
{
x->color=BlACK;
}
}
voidOutput(RedBlackNode*p)
{
printf("data:
%d,color:
%s,parent:
%d,name:
%s,phone:
%s\n",
p->data,(p->color==RED?
"RED":
"BlACK"),
(p->parent!
=NULL)?
p->parent->data:
-1,p->name,p->phone);
}
voidPreorderTraverse(RedBlackNode*T)
{
LinkStack*L=InitStack();
RedBlackNode*p;
p=T;
while(p||!
StackEmpty(L))
{
while(p)//遍历左子树
{
Output(p);
PushStack(L,p);
p=p->left;
}
if(!
StackEmpty(L))//通过下一次循环中的内嵌while实现右子树遍历
{
p=PopStack(L);
p=p->right;
}
}
}
voidInorderTraverse(RedBlackNode*T)
{
LinkStack*L;
L=InitStack();
RedBlackNode*p;
p=T;
while(p!
=NULL||!
StackEmpty(L))
{
while(p!
=NULL)//遍历左子树
{
PushStack(L,p);
p=p->left;
}
if(!
StackEmpty(L))
{
p=PopStack(L);
Output(p);//访问根结点
p=p->right;//通过下一次循环实现右子树遍历
}
}
DestroyStack(L);
}
voidPostorderTraverse
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 类红黑 二叉 正文