您的位置:首页 > 其它

红黑树实现(二叉搜索树)

2015-04-25 00:00 134 查看
自己实现了了一个红黑树 分享一下

#include<iostream>
#include<stdlib.h>
using namespace std;
typedef enum{RED,BLACK} Color;
char COLOR[2][6]={"RED","BlACK"};
typedef struct BRNode
{
Color clr;
struct BRNode* left;
struct BRNode* right;
struct BRNode* parent;
int key;

}BRNode,*LPBRNode;
bool LRotate(LPBRNode head,int value);
bool RRotate(LPBRNode head,int value);
//insert value to the binary search tree
void RB_Insert_Fixup(LPBRNode nil,LPBRNode newN)
{
LPBRNode ps=newN;
while(ps->parent->clr==RED)
{
//节点是左孩子
if(ps->parent==ps->parent->parent->left)
{
//叔叔节点
LPBRNode uncle=ps->parent->parent->right;
if(uncle->clr==RED)
{
uncle->clr=BLACK;
ps->parent->clr=BLACK;
ps->parent->parent->clr=RED;
ps=ps->parent->parent;

}
else if(ps==ps->parent->right)
{
ps=ps->parent;
LRotate(nil,ps->key);
ps->parent->clr=BLACK;
ps->parent->parent->clr=RED;
RRotate(nil,ps->parent->parent->key);
}
else if(ps==ps->parent->right)
{
ps->parent->clr=BLACK;
ps->parent->parent->clr=RED;
RRotate(nil,ps->parent->parent->key);
}

}
//节点是左孩子
else if(ps->parent==ps->parent->parent->right)
{
LPBRNode uncle=ps->parent->parent->left;
if(uncle->clr==RED)
{
uncle->clr=BLACK;
ps->parent->clr=BLACK;
ps->parent->parent->clr=RED;
ps=ps->parent->parent;

}
else if(ps==ps->parent->left)
{
ps=ps->parent;
RRotate(nil,ps->key);
ps->parent->clr=BLACK;
ps->parent->parent->clr=RED;
LRotate(nil,ps->parent->parent->key);
}
else if(ps==ps->parent->right)
{
ps->parent->clr=BLACK;
ps->parent->parent->clr=RED;
LRotate(nil,ps->parent->parent->key);
}
}
}
nil->left->clr=BLACK;
}
LPBRNode Nil=(LPBRNode)malloc(sizeof(BRNode));
//插入
void Insert(LPBRNode nil,int value)
{
LPBRNode newN=(LPBRNode)malloc(sizeof(BRNode));
newN->key=value;
newN->clr=RED;
newN->left=newN->right=nil;
//root node
if(nil->left==NULL)
{
nil->left=newN;
newN->parent=nil;
newN->clr=BLACK;
return ;
}
LPBRNode pre=nil->left;
while(true)
{
if(value<pre->key)
{
if(pre->left!=nil)
pre=pre->left;
else
{
pre->left=newN;
newN->parent=pre;
break;
}
}
else
{
if(pre->right!=nil)
pre=pre->right;
else
{
pre->right=newN;
newN->parent=pre;
break;
}
}

}
RB_Insert_Fixup(nil,newN);
}
//print the value
void printNode(LPBRNode LpNode)
{
printf("%d\t%s\n",LpNode->key,COLOR[LpNode->clr]);
}
//print the whole tree
void printTree(LPBRNode Lphead)
{
printNode(Lphead);
if(Lphead->left!=Nil)
printTree(Lphead->left);
if(Lphead->right!=Nil)
printTree(Lphead->right);

}
//find value position
LPBRNode findvalue(LPBRNode LpNode,const int value)
{
/// printNode(LpNode);
if(value==LpNode->key)
{
//  cout<<"\nfind the value"<<endl;
return LpNode;
}
if(value<LpNode->key)
{
if(LpNode->left==Nil)
{
cout<<"\n failure";
return NULL;
}

else
{
return findvalue(LpNode->left,value);
}
}
//right
if(value>LpNode->key)
{
if(LpNode->right==Nil)
{
cout<<"\n failure";
return NULL;
}

else
{
return findvalue(LpNode->right,value);

}
}
return NULL;
}
//void Left Rotate
bool LRotate(LPBRNode head,int value)
{
LPBRNode LpNode=findvalue(head,value);
if(LpNode==NULL)
return false;
LPBRNode child=LpNode->right;
LpNode->right=child->left;
if(child->left!=Nil)
child->left->parent=LpNode;
child->parent=LpNode->parent;
if(LpNode->parent==Nil)
{
Nil->left=child;
}
else if(LpNode==LpNode->parent->left)
{
LpNode->parent->left=child;

}
else
{
LpNode->parent->right=child;
}
child->left=LpNode;
LpNode->parent=child;
return true;
/*LPBRNode p=LpNode->parent;
if(LpNode->right==Nil)
return false;
LPBRNode rchild=LpNode->right;
LPBRNode rlchild=rchild->left;
if(p->left==LpNode)
{

p->left=rchild;
}
else if(p->right==LpNode)
{
p->right=rchild;
}
rchild->parent=p;
rchild->left=LpNode;
LpNode->parent=rchild;
LpNode->right=rlchild;
if(rlchild!=Nil)
rlchild->parent=LpNode;
return true;*/
}

//right Rotate
bool RRotate(LPBRNode head,int value)
{
LPBRNode LpNode=findvalue(head,value);
//printNode(LpNode);
if(LpNode==NULL)
return false;
LPBRNode child=LpNode->left;
LpNode->left=child->right;
if(child->right!=Nil)
{
child->right->parent=LpNode;
}
child->parent=LpNode->parent;
if(LpNode->parent==Nil)
{
Nil->left=child;
}
else if(LpNode==LpNode->parent->left)
{
LpNode->parent->left=child;

}
else
{
LpNode->parent->right=child;
}
child->right=LpNode;
LpNode->parent=child;
return true;
}
int main(int argc,char **argv)
{

Nil->clr=BLACK;
Nil->left=NULL;
for(int i=1;i<15;i++)
{
Insert(Nil,i);

cout<<endl<<"after insert "<<i<<endl;

printTree(Nil->left);
}

}

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: