手机版
你好,游客 登录 注册
背景:
阅读新闻

红黑树 C++实现

[日期:2012-01-28] 来源:Linux社区  作者:kay_sprint [字体:大 中 小]
rbtree.cpp

[cpp]
  1. #include "rb_tree.h"   
  2. #include <iostream>   
  3. #include <queue>   
  4. #include <stack>   
  5. using namespace std;  
  6.   
  7. //以结点x为中心进行左旋转   
  8. void RB_Tree::Leaf_Rotate(Tree_Node *x)  
  9. {  
  10.     Tree_Node *y=x->r_child;  
  11.     x->r_child=y->l_child;  
  12.       
  13.     if(y->l_child != Nil)  
  14.         y->l_child->parent=x;  
  15.     y->parent=x->parent;  
  16.       
  17.     if(x->parent == Nil)  
  18.         root=y;  
  19.     else if( x->parent->l_child == x )  
  20.         x->parent->l_child=y;  
  21.     else  
  22.         x->parent->r_child=y;  
  23.   
  24.     y->l_child=x;  
  25.     x->parent=y;  
  26. }  
  27.   
  28. //以x为中心进行右旋转   
  29. void RB_Tree::Right_Rotate(Tree_Node *x)  
  30. {  
  31.     Tree_Node *y=x->l_child;  
  32.     x->l_child=y->r_child;  
  33.   
  34.     if(y->r_child != Nil)  
  35.         y->r_child->parent=x;  
  36.     y->parent=x->parent;  
  37.   
  38.     if(x->parent == Nil)  
  39.         root=y;  
  40.     else if( x->parent->l_child == x)  
  41.         x->parent->l_child=y;  
  42.     else   
  43.         x->parent->r_child=y;  
  44.   
  45.     y->r_child=x;  
  46.     x->parent=y;  
  47. }  
  48.   
  49. //在红黑树中搜索关键字值为k的结点   
  50. bool RB_Tree::RB_Search(int k,Tree_Node *&x)  
  51. {  
  52.     //红黑树是在二叉排序树的基础上建立的,所以也是有序的   
  53.     x=root;  
  54.     while(x != Nil)  
  55.     {  
  56.         if(x->key == k)  
  57.             return true;  
  58.         else if(x->key < k)  
  59.             x=x->r_child;  
  60.         else  
  61.             x=x->l_child;  
  62.     }  
  63.     return false;  
  64. }  
  65.   
  66. //在红黑树中插入关键字值为k的结点   
  67. bool RB_Tree::RB_Insert(Tree_Node *k)  
  68. {  
  69.     Tree_Node *z;  
  70.     if(RB_Search(k->key,z))  
  71.     {  
  72.         cout<<"含有关键字值为 "<<k->key<<" 的结点已存在,不重复插入"<<endl;  
  73.         return false;  
  74.     }  
  75.     else  
  76.     {  
  77.         Tree_Node *x=root;  
  78.         Tree_Node *y=Nil;  
  79.         while(x != Nil)  
  80.         {  
  81.             y=x;  
  82.             if(k->key < x->key)  
  83.                 x=x->l_child;  
  84.             else  
  85.                 x=x->r_child;  
  86.         }  
  87.         k->parent=y;  
  88.         if(y == Nil)  
  89.             root=k;  
  90.         else if( k->key <y->key)  
  91.             y->l_child=k;  
  92.         else  
  93.             y->r_child=k;  
  94.   
  95.         k->l_child=Nil;  
  96.         k->r_child=Nil;  
  97.         k->color=RED;  
  98.         if(k == root)  
  99.             k->parent=Nil;  
  100.         RB_Insert_Fixup(k);  
  101.     }  
  102.     return true;  
  103. }  
  104.   
  105. //插入后调整红黑树结构,使插入新元素后的树满足红黑树的性质要求   
  106. void RB_Tree::RB_Insert_Fixup(Tree_Node *z)  
  107. {  
  108.     Tree_Node *y;  
  109.     while( z->parent->color == RED)  
  110.     {  
  111.         if( z->parent == z->parent->parent->l_child )  
  112.         {  
  113.             y=z->parent->parent->r_child;  
  114.             if(y->color == RED)  
  115.             {  
  116.                 //case 1   
  117.                 z->parent->color=BLACK;  
  118.                 y->color=BLACK;  
  119.                 z->parent->parent->color=RED;  
  120.                 z=z->parent->parent;  
  121.             }  
  122.             else   
  123.             {  
  124.                 // y->color=BLACK   
  125.                 if(z == z->parent->r_child)  
  126.                 {  
  127.                     //case 2    
  128.                     z=z->parent;  
  129.                     Leaf_Rotate(z);  
  130.                 }  
  131.                 //case 3   
  132.                 z->parent->color=BLACK;  
  133.                 z->parent->parent->color=RED;  
  134.                 Right_Rotate(z->parent->parent);  
  135.             }  
  136.   
  137.         }  
  138.         else  
  139.         {  
  140.             //z->parent == z->parent->parent->r_child   
  141.             y=z->parent->parent->l_child;  
  142.             if(y->color == RED)  
  143.             {  
  144.                 z->parent->color=BLACK;  
  145.                 y->color=BLACK;  
  146.                 z->parent->parent->color=RED;  
  147.                 z=z->parent->parent;  
  148.             }  
  149.             else  
  150.             {  
  151.                 if(z == z->parent->l_child)  
  152.                 {  
  153.                     z=z->parent;  
  154.                     Right_Rotate(z);  
  155.                 }  
  156.                 z->parent->color=BLACK;  
  157.                 z->parent->parent->color=RED;  
  158.                 Leaf_Rotate(z->parent->parent);  
  159.             }  
  160.         }  
  161.     }  
  162.     root->color=BLACK;  
  163. }  
  164.   
  165. //利用数组A[]中的数据创建红黑树   
  166. void RB_Tree::RB_Create(int A[],int n)  
  167. {  
  168.     for(int i=0;i<n;i++)  
  169.     {  
  170.         Tree_Node *k=new Tree_Node(A[i]);  
  171.         if(RB_Insert(k))  
  172.             cout<<"插入 "<<A[i]<<" 成功"<<endl;  
  173.     }  
  174.     cout<<"红黑树创建完成"<<endl;  
  175. }  
  176.   
  177. //返回结点x的直接后继   
  178. Tree_Node *RB_Tree::RB_Successor(Tree_Node *x)  
  179. {  
  180.     Tree_Node *y;  
  181.     if(x->r_child != Nil)  
  182.     {  
  183.         y=x->r_child;  
  184.         while(y->l_child != Nil)  
  185.             y=y->l_child;  
  186.     }  
  187.     else  
  188.     {  
  189.         y=x->parent;  
  190.         while( (y != Nil) && (x == y->r_child) )  
  191.         {  
  192.             x=y;  
  193.             y=y->parent;  
  194.         }  
  195.     }  
  196.     return y;  
  197. }  
  198.   
  199. //删除关键字为k的结点   
  200. bool RB_Tree::RB_Delete(int k)  
  201. {  
  202.     Tree_Node *x,*y,*z;  
  203.     if(!RB_Search(k,z))  
  204.     {  
  205.         cout<<"关键字为 "<<k<<" 的结点不存在红黑树中"<<endl;  
  206.         return false;  
  207.     }  
  208.     else  
  209.     {  
  210.         if( (z->l_child == Nil) || (z->r_child == Nil) )  
  211.             y=z;  
  212.         else  
  213.             y=RB_Successor(z);  
  214.           
  215.         if(y->l_child != Nil) //说明z的左右子树只存在一个   
  216.             x=y->l_child;  
  217.         else  
  218.             x=y->r_child;  //说明z的左右子树均存在,y指向的是z的直接后继结点   
  219.           
  220.         x->parent=y->parent;  
  221.         if(y->parent == Nil)  
  222.             root=x;  
  223.         else if(y == y->parent->l_child)  
  224.             y->parent->l_child=x;  
  225.         else  
  226.             y->parent->r_child=x;  
  227.   
  228.         if(y!=z)  //y指向的是z的直接后继的时候   
  229.             z->key=y->key;  
  230.           
  231.         if(y->color == BLACK)  //被删结点是黑色的时候,红黑树性质会被影响,所以需要重新调整   
  232.             RB_Delete_Fixup(x);  
  233.           
  234.         delete y;  
  235.         return true;  
  236.     }  
  237. }  
  238.   
  239. //删除后红黑树的结构出现不符合要求的情况时,调整树的结构   
  240. void RB_Tree::RB_Delete_Fixup(Tree_Node *x)  
  241. {  
  242.     Tree_Node *w;  
  243.     while( (x != root) && (x->color == BLACK) )  
  244.     {  
  245.         if(x == x->parent->l_child)  
  246.         {  
  247.             w=x->parent->r_child;  
  248.             if(w->color == RED)  
  249.             {  
  250.                 //case 1   
  251.                 w->color=BLACK;  
  252.                 x->parent->color=RED;  
  253.                 Leaf_Rotate(x->parent);  
  254.                 w=x->parent->r_child;  
  255.             }  
  256.   
  257.             if( (w->l_child->color == BLACK) && (w->r_child->color == BLACK) )  
  258.             {  
  259.                 //case 2   
  260.                 w->color=RED;  
  261.                 x=x->parent;  
  262.             }  
  263.             else   
  264.             {  
  265.                 if (w->r_child->color == BLACK)  
  266.                 {  
  267.                     //case 3   
  268.                     w->l_child->color=BLACK;  
  269.                     w->color=RED;  
  270.                     Right_Rotate(w);  
  271.                     w=x->parent->r_child;  
  272.                 }  
  273.                 //case 4   
  274.                 w->color=x->parent->color;  
  275.                 x->parent->color=BLACK;  
  276.                 w->r_child->color=BLACK;  
  277.                 Leaf_Rotate(x->parent);  
  278.                 x=root;  
  279.             }  
  280.         }  
  281.         else  
  282.         {  
  283.             //x=x->parent->r_child   
  284.             w=x->parent->l_child;  
  285.             if(w->color == RED)  
  286.             {  
  287.                 //case 1   
  288.                 w->color=BLACK;  
  289.                 x->parent->color=RED;  
  290.                 Right_Rotate(x->parent);  
  291.                 w=x->parent->l_child;  
  292.             }  
  293.               
  294.             if( (w->l_child->color == BLACK) && (w->r_child->color == BLACK) )  
  295.             {  
  296.                 //case 2   
  297.                 w->color=RED;  
  298.                 x=x->parent;  
  299.             }  
  300.             else   
  301.             {  
  302.                 if (w->l_child->color == BLACK)  
  303.                 {  
  304.                     //case 3   
  305.                     w->r_child->color=BLACK;  
  306.                     w->color=RED;  
  307.                     Leaf_Rotate(w);  
  308.                     w=x->parent->l_child;  
  309.                 }  
  310.                 //case 4   
  311.                 w->color=x->parent->color;  
  312.                 x->parent->color=BLACK;  
  313.                 w->l_child->color=BLACK;  
  314.                 Right_Rotate(x->parent);  
  315.                 x=root;  
  316.             }  
  317.         }  
  318.     }  
  319.     x->color=BLACK;  
  320. }  
  321.   
  322. //层次遍历红黑树,并且输出各个结点的颜色   
  323. void RB_Tree::LevelOrder_Traverse()  
  324. {  
  325.     Tree_Node *y=root;  
  326.     queue<Tree_Node *> qu;  
  327.     qu.push(y);  
  328.     while(!qu.empty())  
  329.     {  
  330.         y=qu.front();  
  331.         qu.pop();  
  332.         cout<<"key= "<<y->key<<" ,color= ";  
  333.         if(y->color == BLACK)  
  334.             cout<<"BLACK"<<endl;  
  335.         else  
  336.             cout<<"RED"<<endl;  
  337.         if(y->l_child != Nil)  
  338.             qu.push(y->l_child);  
  339.         if(y->r_child != Nil)  
  340.             qu.push(y->r_child);  
  341.     }  
  342. }  
  343.   
  344. //中序遍历红黑树各结点的关键字值   
  345. void RB_Tree::InOrder_Traverse()  
  346. {  
  347.     Tree_Node *y;  
  348.     stack<Tree_Node *> s;  
  349.     y=root;  
  350.     while( (y != Nil) || (!s.empty()) )  
  351.     {  
  352.         if(y != Nil)  
  353.         {  
  354.             s.push(y);  
  355.             y=y->l_child;  
  356.         }  
  357.         else  
  358.         {  
  359.             y=s.top();  
  360.             s.pop();  
  361.             cout<<y->key<<"  ";  
  362.             y=y->r_child;  
  363.         }  
  364.     }  
  365. }  
linux
相关资讯       C++  红黑树 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款