红黑树实现(二叉搜索树)
2015-04-25 00:00
134 查看
自己实现了了一个红黑树 分享一下
[/code]
#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]
相关文章推荐
- 红黑树、二叉搜索树的实现和性能比较(c++实现红黑树)
- PHP实现绘制二叉树图形显示功能详解【包括二叉搜索树、平衡树及红黑树】
- 二叉搜索树和红黑树概述以及模板实现(1)
- 二叉搜索树和红黑树概述以及模板实现(2)--红黑树
- 红黑树插入算法的实现
- 红黑树代码实现
- 使用C++实现二叉搜索树的数据结构
- java 实现红黑树
- JS实现红黑树
- 二叉搜索树(BST树)的简单实现
- 红黑树的实现源码
- 【Java】二叉搜索树的实现操作及应用
- 红黑树的实现
- 《C++实现数据结构》:二叉搜索树
- 【算法】【python实现】二叉搜索树插入、删除、查找
- 二叉搜索树就地转双向链表二叉搜索树实现文件C++
- 浅谈红黑树(C语言代码实现)
- 红黑树java实现
- 红黑树实现代码
- 二叉搜索树的基本java实现