透彻理解红黑树之六

  1. //file RBTree.h   
  2. //written by saturnman,20101008。   
  3. //updated by July,20110329。   
  4. /*----------------------------------------------- 
  5. 版权声明: 
  6. July和saturnman对此份红黑树的c++实现代码享有全部的版权, 
  7. 谢绝转载,侵权必究。 
  8. ------------------------------------------------*/  
  9. #ifndef _RB_TREE_H_   
  10. #define _RB_TREE_H_   
  11. #include<iostream>   
  12. #include<string>   
  13. #include<sstream>   
  14. #include<fstream>   
  15. using namespace std;  
  16.   
  17. template<class KEY,class U>  
  18. class RB_Tree  
  19. {  
  20.     private:  
  21.         RB_Tree(const RB_Tree& input){}  
  22.         const RB_Tree& operator=(const RB_Tree& input){}  
  23.     private:  
  24.         enum COLOR{RED,BLACK};  
  25.         class RB_Node  
  26.         {  
  27.         public:  
  28.             RB_Node()  
  29.             {  
  30.                 //RB_COLOR = BLACK;   
  31.                 right = NULL;  
  32.                 left = NULL;  
  33.                 parent = NULL;  
  34.             }  
  35.             COLOR RB_COLOR;  
  36.             RB_Node* right;  
  37.             RB_Node* left;  
  38.             RB_Node* parent;  
  39.             KEY key;  
  40.             U data;  
  41.         };  
  42.         public:  
  43.             RB_Tree()  
  44.             {  
  45.                 this->m_nullNode = new RB_Node();  
  46.                 this->m_root = m_nullNode;  
  47.                 this->m_nullNode->right = this->m_root;  
  48.                 this->m_nullNode->left = this->m_root;  
  49.                 this->m_nullNode->parent = this->m_root;  
  50.                 this->m_nullNode->RB_COLOR = BLACK;  
  51.             }  
  52.               
  53.             bool Empty()  
  54.             {  
  55.                 if(this->m_root == this->m_nullNode)  
  56.                 {  
  57.                     return true;  
  58.                 }  
  59.                 else  
  60.                 {  
  61.                     return false;  
  62.                 }  
  63.             }  
  64.               
  65.             //查找key   
  66.             RB_Node* find(KEY key)  
  67.             {  
  68.                 RB_Node* index = m_root;  
  69.                 while(index != m_nullNode)  
  70.                 {  
  71.                     if(key<index->key)     
  72.                     {  
  73.                         index  = index->left;  //比当前的小,往左   
  74.                     }  
  75.                     else if(key>index->key)  
  76.                     {  
  77.                         index = index->right;  //比当前的大,往右   
  78.                     }  
  79.                     else  
  80.                     {  
  81.                         break;  
  82.                     }  
  83.                 }  
  84.                 return index;  
  85.             }  
  86.               
  87.             //--------------------------插入结点总操作----------------------------------   
  88.             //全部的工作,都在下述伪代码中:   
  89.             /*RB-INSERT(T, z)    
  90.             1  y ← nil[T]                 // y 始终指向 x 的父结点。 
  91.             2  x ← root[T]              // x 指向当前树的根结点, 
  92.             3  while x ≠ nil[T] 
  93.             4      do y ← x 
  94.             5         if key[z] < key[x]           //向左,向右.. 
  95.             6            then x ← left[x] 
  96.             7            else x ← right[x]   //为了找到合适的插入点,x探路跟踪路径,直到x成为NIL 为止。 
  97.             8  p[z] ← y         //y置为 插入结点z 的父结点。 
  98.             9  if y = nil[T] 
  99.             10     then root[T] ← z 
  100.             11     else if key[z] < key[y] 
  101.             12             then left[y] ← z 
  102.             13             else right[y] ← z     //此 8-13行,置z 相关的指针。 
  103.             14  left[z] ← nil[T] 
  104.             15  right[z] ← nil[T]            //设为空, 
  105.             16  color[z] ← RED             //将新插入的结点z作为红色 
  106.             17  RB-INSERT-FIXUP(T, z)    
  107.             */  
  108.             //因为将z着为红色,可能会违反某一红黑性质,   
  109.             //所以需要调用下面的RB-INSERT-FIXUP(T, z)来保持红黑性质。   
  110.             bool Insert(KEY key,U data)  
  111.             {  
  112.                 RB_Node* insert_point = m_nullNode;  
  113.                 RB_Node* index = m_root;  
  114.                 while(index!=m_nullNode)  
  115.                 {  
  116.                     insert_point = index;  
  117.                     if(key<index->key)  
  118.                     {  
  119.                         index = index->left;  
  120.                     }  
  121.                     else if(key>index->key)  
  122.                     {  
  123.                         index = index->right;  
  124.                     }  
  125.                     else  
  126.                     {  
  127.                         return false;  
  128.                     }  
  129.                 }  
  130.                 RB_Node* insert_node = new RB_Node();  
  131.                 insert_node->key = key;  
  132.                 insert_node->data = data;  
  133.                 insert_node->RB_COLOR = RED;  
  134.                 insert_node->right = m_nullNode;  
  135.                 insert_node->left = m_nullNode;  
  136.                 if(insert_point==m_nullNode) //如果插入的是一颗空树   
  137.                 {  
  138.                     m_root = insert_node;  
  139.                     m_root->parent = m_nullNode;  
  140.                     m_nullNode->left = m_root;  
  141.                     m_nullNode->right = m_root;  
  142.                     m_nullNode->parent = m_root;  
  143.                 }  
  144.                 else  
  145.                 {  
  146.                     if(key<insert_point->key)  
  147.                     {  
  148.                         insert_point->left = insert_node;  
  149.                     }  
  150.                     else  
  151.                     {  
  152.                         insert_point->right = insert_node;  
  153.                     }  
  154.                     insert_node->parent = insert_point;  
  155.                 }  
  156.                 InsertFixUp(insert_node);    //调用InsertFixUp修复红黑树性质。   
  157.             }  
  158.               
  159.             //---------------------插入结点性质修复--------------------------------   
  160.             //3种插入情况,都在下面的伪代码中(未涉及到所有全部的插入情况)。   
  161.             /* 
  162.             RB-INSERT-FIXUP(T, z) 
  163.             1 while color[p[z]] = RED 
  164.             2     do if p[z] = left[p[p[z]]] 
  165.             3           then y ← right[p[p[z]]] 
  166.             4                if color[y] = RED 
  167.             5                   then color[p[z]] ← BLACK                    ? Case 1 
  168.             6                        color[y] ← BLACK                       ? Case 1 
  169.             7                        color[p[p[z]]] ← RED                   ? Case 1 
  170.             8                        z ← p[p[z]]                            ? Case 1 
  171.             9                   else if z = right[p[z]] 
  172.             10                           then z ← p[z]                       ? Case 2 
  173.             11                                LEFT-ROTATE(T, z)              ? Case 2 
  174.             12                           color[p[z]] ← BLACK                 ? Case 3 
  175.             13                           color[p[p[z]]] ← RED                ? Case 3 
  176.             14                           RIGHT-ROTATE(T, p[p[z]])            ? Case 3 
  177.             15           else (same as then clause with "right" and "left" exchanged) 
  178.             16 color[root[T]] ← BLACK 
  179.             */  
  180.             //然后的工作,就非常简单了,即把上述伪代码改写为下述的c++代码:   
  181.             void InsertFixUp(RB_Node* node)  
  182.             {  
  183.                 while(node->parent->RB_COLOR==RED)  
  184.                 {  
  185.                     if(node->parent==node->parent->parent->left)   //   
  186.                     {  
  187.                         RB_Node* uncle = node->parent->parent->right;  
  188.                         if(uncle->RB_COLOR == RED)   //插入情况1,z的叔叔y是红色的。   
  189.                         {  
  190.                             node->parent->RB_COLOR = BLACK;  
  191.                             uncle->RB_COLOR = BLACK;  
  192.                             node->parent->parent->RB_COLOR = RED;  
  193.                             node = node->parent->parent;  
  194.                         }  
  195.                         else if(uncle->RB_COLOR == BLACK )  //插入情况2:z的叔叔y是黑色的,。   
  196.                         {  
  197.                             if(node == node->parent->right) //且z是右孩子   
  198.                             {  
  199.                                 node = node->parent;  
  200.                                 RotateLeft(node);  
  201.                             }  
  202.                             else                 //插入情况3:z的叔叔y是黑色的,但z是左孩子。   
  203.                             {  
  204.                                 node->parent->RB_COLOR = BLACK;  
  205.                                 node->parent->parent->RB_COLOR = RED;  
  206.                                 RotateRight(node->parent->parent);  
  207.                             }  
  208.                         }  
  209.                     }  
  210.                     else //这部分是针对为插入情况1中,z的父亲现在作为祖父的右孩子了的情况,而写的。   
  211.                         //15 else (same as then clause with "right" and "left" exchanged)   
  212.                     {  
  213.                         RB_Node* uncle = node->parent->parent->left;  
  214.                         if(uncle->RB_COLOR == RED)  
  215.                         {  
  216.                             node->parent->RB_COLOR = BLACK;  
  217.                             uncle->RB_COLOR = BLACK;  
  218.                             uncle->parent->RB_COLOR = RED;  
  219.                             node = node->parent->parent;  
  220.                         }  
  221.                         else if(uncle->RB_COLOR == BLACK)  
  222.                         {  
  223.                             if(node == node->parent->left)  
  224.                             {  
  225.                                 node = node->parent;  
  226.                                 RotateRight(node);     //与上述代码相比,左旋改为右旋   
  227.                             }  
  228.                             else  
  229.                             {  
  230.                                 node->parent->RB_COLOR = BLACK;  
  231.                                 node->parent->parent->RB_COLOR = RED;  
  232.                                 RotateLeft(node->parent->parent);   //右旋改为左旋,即可。   
  233.                             }  
  234.                         }  
  235.                     }  
  236.                 }  
  237.                 m_root->RB_COLOR = BLACK;  
  238.             }  
  239.               
  240.             //左旋代码实现   
  241.             bool RotateLeft(RB_Node* node)  
  242.             {  
  243.                 if(node==m_nullNode || node->right==m_nullNode)  
  244.                 {  
  245.                     return false;//can't rotate   
  246.                 }  
  247.                 RB_Node* lower_right = node->right;  
  248.                 lower_right->parent =  node->parent;  
  249.                 node->right=lower_right->left;  
  250.                 if(lower_right->left!=m_nullNode)  
  251.                 {  
  252.                     lower_right->left->parent = node;  
  253.                 }  
  254.                 if(node->parent==m_nullNode) //rotate node is root   
  255.                 {  
  256.                     m_root = lower_right;  
  257.                     m_nullNode->left = m_root;  
  258.                     m_nullNode->right= m_root;  
  259.                     //m_nullNode->parent = m_root;   
  260.                 }  
  261.                 else  
  262.                 {  
  263.                     if(node == node->parent->left)  
  264.                     {  
  265.                         node->parent->left = lower_right;  
  266.                     }  
  267.                     else  
  268.                     {  
  269.                         node->parent->right = lower_right;  
  270.                     }  
  271.                 }  
  272.                 node->parent = lower_right;  
  273.                 lower_right->left = node;  
  274.             }  
  275.               
  276.             //右旋代码实现   
  277.             bool RotateRight(RB_Node* node)  
  278.             {  
  279.                 if(node==m_nullNode || node->left==m_nullNode)  
  280.                 {  
  281.                     return false;//can't rotate   
  282.                 }  
  283.                 RB_Node* lower_left = node->left;  
  284.                 node->left = lower_left->right;  
  285.                 lower_left->parent = node->parent;  
  286.                 if(lower_left->right!=m_nullNode)  
  287.                 {  
  288.                     lower_left->right->parent = node;  
  289.                 }  
  290.                 if(node->parent == m_nullNode) //node is root   
  291.                 {  
  292.                     m_root = lower_left;  
  293.                     m_nullNode->left = m_root;  
  294.                     m_nullNode->right = m_root;  
  295.                     //m_nullNode->parent = m_root;   
  296.                 }  
  297.                 else  
  298.                 {  
  299.                     if(node==node->parent->right)  
  300.                     {  
  301.                         node->parent->right = lower_left;  
  302.                     }  
  303.                     else  
  304.                     {  
  305.                         node->parent->left = lower_left;  
  306.                     }  
  307.                 }  
  308.                 node->parent = lower_left;  
  309.                 lower_left->right = node;  
  310.             }  
  311.               
  312.             //--------------------------删除结点总操作----------------------------------   
  313.             //伪代码,不再贴出,详情,请参考此红黑树系列第二篇文章:   
  314.             //经典算法研究系列:五、红黑树算法的实现与剖析:   
  315.             //http://blog.csdn.net/v_JULY_v/archive/2010/12/31/6109153.aspx。   
  316.             bool Delete(KEY key)  
  317.             {  
  318.                 RB_Node* delete_point = find(key);  
  319.                 if(delete_point == m_nullNode)  
  320.                 {  
  321.                     return false;  
  322.                 }  
  323.                 if(delete_point->left!=m_nullNode && delete_point->right!=m_nullNode)  
  324.                 {  
  325.                     RB_Node* successor = InOrderSuccessor(delete_point);  
  326.                     delete_point->data = successor->data;  
  327.                     delete_point->key = successor->key;  
  328.                     delete_point = successor;  
  329.                 }  
  330.                 RB_Node* delete_point_child;  
  331.                 if(delete_point->right!=m_nullNode)  
  332.                 {  
  333.                     delete_point_child = delete_point->right;  
  334.                 }  
  335.                 else if(delete_point->left!=m_nullNode)  
  336.                 {  
  337.                     delete_point_child = delete_point->left;  
  338.                 }  
  339.                 else  
  340.                 {  
  341.                     delete_point_child = m_nullNode;  
  342.                 }  
  343.                 delete_point_child->parent = delete_point->parent;  
  344.                 if(delete_point->parent==m_nullNode)//delete root node   
  345.                 {  
  346.                     m_root = delete_point_child;  
  347.                     m_nullNode->parent = m_root;  
  348.                     m_nullNode->left = m_root;  
  349.                     m_nullNode->right = m_root;  
  350.                 }  
  351.                 else if(delete_point == delete_point->parent->right)  
  352.                 {  
  353.                     delete_point->parent->right = delete_point_child;  
  354.                 }  
  355.                 else  
  356.                 {  
  357.                     delete_point->parent->left = delete_point_child;  
  358.                 }  
  359.                 if(delete_point->RB_COLOR==BLACK && !(delete_point_child==m_nullNode && delete_point_child->parent==m_nullNode))  
  360.                 {  
  361.                     DeleteFixUp(delete_point_child);  
  362.                 }  
  363.                 delete delete_point;  
  364.                 return true;  
  365.             }  
  366.               
  367.             //---------------------删除结点性质修复-----------------------------------   
  368.             //所有的工作,都在下述23行伪代码中:   
  369.             /* 
  370.             RB-DELETE-FIXUP(T, x) 
  371.             1 while x ≠ root[T] and color[x] = BLACK 
  372.             2     do if x = left[p[x]] 
  373.             3           then w ← right[p[x]] 
  374.             4                if color[w] = RED 
  375.             5                   then color[w] ← BLACK                        ?  Case 1 
  376.             6                        color[p[x]] ← RED                       ?  Case 1 
  377.             7                        LEFT-ROTATE(T, p[x])                    ?  Case 1 
  378.             8                        w ← right[p[x]]                         ?  Case 1 
  379.             9                if color[left[w]] = BLACK and color[right[w]] = BLACK 
  380.             10                   then color[w] ← RED                          ?  Case 2 
  381.             11                        x p[x]                                  ?  Case 2 
  382.             12                   else if color[right[w]] = BLACK 
  383.             13                           then color[left[w]] ← BLACK          ?  Case 3 
  384.             14                                color[w] ← RED                  ?  Case 3 
  385.             15                                RIGHT-ROTATE(T, w)              ?  Case 3 
  386.             16                                w ← right[p[x]]                 ?  Case 3 
  387.             17                         color[w] ← color[p[x]]                 ?  Case 4 
  388.             18                         color[p[x]] ← BLACK                    ?  Case 4 
  389.             19                         color[right[w]] ← BLACK                ?  Case 4 
  390.             20                         LEFT-ROTATE(T, p[x])                   ?  Case 4 
  391.             21                         x ← root[T]                            ?  Case 4 
  392.             22        else (same as then clause with "right" and "left" exchanged) 
  393.             23 color[x] ← BLACK  
  394.             */  
  395.             //接下来的工作,很简单,即把上述伪代码改写成c++代码即可。   
  396.             void DeleteFixUp(RB_Node* node)  
  397.             {  
  398.                 while(node!=m_root && node->RB_COLOR==BLACK)  
  399.                 {  
  400.                     if(node == node->parent->left)  
  401.                     {  
  402.                         RB_Node* brother = node->parent->right;  
  403.                         if(brother->RB_COLOR==RED)   //情况1:x的兄弟w是红色的。   
  404.                         {  
  405.                             brother->RB_COLOR = BLACK;  
  406.                             node->parent->RB_COLOR = RED;  
  407.                             RotateLeft(node->parent);  
  408.                         }  
  409.                         else     //情况2:x的兄弟w是黑色的,   
  410.                         {  
  411.                             if(brother->left->RB_COLOR == BLACK && brother->right->RB_COLOR == BLACK)  
  412.                                 //且w的俩个孩子都是黑色的。   
  413.                             {  
  414.                                 brother->RB_COLOR = RED;  
  415.                                 node = node->parent;  
  416.                             }  
  417.                             else if(brother->right->RB_COLOR == BLACK)  
  418.                                 //情况3:x的兄弟w是黑色的,w的右孩子是黑色(w的左孩子是红色)。   
  419.                             {  
  420.                                 brother->RB_COLOR = RED;  
  421.                                 brother->left->RB_COLOR = BLACK;  
  422.                                 RotateRight(brother);  
  423.                             }  
  424.                             else if(brother->right->RB_COLOR == RED)  
  425.                                 //情况4:x的兄弟w是黑色的,且w的右孩子时红色的。   
  426.                             {  
  427.                                 brother->RB_COLOR = node->parent->RB_COLOR;  
  428.                                 node->parent->RB_COLOR = BLACK;  
  429.                                 brother->right->RB_COLOR = BLACK;  
  430.                                 RotateLeft(node->parent);  
  431.                                 node = m_root;  
  432.                             }  
  433.                         }  
  434.                     }  
  435.                     else  //下述情况针对上面的情况1中,node作为右孩子而阐述的。   
  436.                         //22        else (same as then clause with "right" and "left" exchanged)   
  437.                         //同样,原理一致,只是遇到左旋改为右旋,遇到右旋改为左旋,即可。其它代码不变。   
  438.                     {  
  439.                         RB_Node* brother = node->parent->left;  
  440.                         if(brother->RB_COLOR == RED)  
  441.                         {  
  442.                             brother->RB_COLOR = BLACK;  
  443.                             node->parent->RB_COLOR = RED;  
  444.                             RotateRight(node->parent);  
  445.                         }  
  446.                         else  
  447.                         {  
  448.                             if(brother->left->RB_COLOR==BLACK && brother->right->RB_COLOR == BLACK)  
  449.                             {  
  450.                                 brother->RB_COLOR = RED;  
  451.                                 node = node->parent;  
  452.                             }  
  453.                             else if(brother->left->RB_COLOR==BLACK)  
  454.                             {  
  455.                                 brother->RB_COLOR = RED;  
  456.                                 brother->right->RB_COLOR = BLACK;  
  457.                                 RotateLeft(brother);  
  458.                             }  
  459.                             else if(brother->left->RB_COLOR==RED)  
  460.                             {  
  461.                                 brother->RB_COLOR = node->parent->RB_COLOR;  
  462.                                 node->parent->RB_COLOR = BLACK;  
  463.                                 brother->left->RB_COLOR = BLACK;  
  464.                                 RotateRight(node->parent);  
  465.                                 node = m_root;  
  466.                             }  
  467.                         }  
  468.                     }  
  469.                 }  
  470.                 m_nullNode->parent = m_root;   //最后将node置为根结点,   
  471.                 node->RB_COLOR = BLACK;    //并改为黑色。   
  472.             }  
  473.               
  474.             //   
  475.             inline RB_Node* InOrderPredecessor(RB_Node* node)  
  476.             {  
  477.                 if(node==m_nullNode)       //null node has no predecessor   
  478.                 {  
  479.                     return m_nullNode;  
  480.                 }  
  481.                 RB_Node* result = node->left;     //get node's left child   
  482.                 while(result!=m_nullNode)         //try to find node's left subtree's right most node   
  483.                 {  
  484.                     if(result->right!=m_nullNode)       
  485.                     {  
  486.                         result = result->right;  
  487.                     }  
  488.                     else  
  489.                     {  
  490.                         break;  
  491.                     }  
  492.                 }            //after while loop result==null or result's right child is null   
  493.                 if(result==m_nullNode)  
  494.                 {  
  495.                     RB_Node* index = node->parent;  
  496.                     result = node;  
  497.                     while(index!=m_nullNode && result == index->left)  
  498.                     {  
  499.                         result = index;  
  500.                         index = index->parent;  
  501.                     }  
  502.                     result = index;         // first right parent or null   
  503.                 }  
  504.                 return result;  
  505.             }  
  506.               
  507.             //   
  508.             inline RB_Node* InOrderSuccessor(RB_Node* node)  
  509.             {  
  510.                 if(node==m_nullNode)       //null node has no successor   
  511.                 {  
  512.                     return m_nullNode;  
  513.                 }  
  514.                 RB_Node* result = node->right;   //get node's right node   
  515.                 while(result!=m_nullNode)        //try to find node's right subtree's left most node   
  516.                 {  
  517.                     if(result->left!=m_nullNode)       
  518.                     {  
  519.                         result = result->left;  
  520.                     }  
  521.                     else  
  522.                     {  
  523.                         break;  
  524.                     }  
  525.                 }                              //after while loop result==null or result's left child is null   
  526.                 if(result == m_nullNode)  
  527.                 {  
  528.                     RB_Node* index = node->parent;  
  529.                     result = node;  
  530.                     while(index!=m_nullNode && result == index->right)  
  531.                     {  
  532.                         result = index;  
  533.                         index = index->parent;  
  534.                     }  
  535.                     result = index;         //first parent's left or null   
  536.                 }  
  537.                 return result;  
  538.             }  
  539.               
  540.             //debug   
  541.             void InOrderTraverse()  
  542.             {  
  543.                 InOrderTraverse(m_root);  
  544.             }  
  545.             void CreateGraph(string filename)  
  546.             {  
  547.                 //delete   
  548.             }  
  549.             void InOrderCreate(ofstream& file,RB_Node* node)  
  550.             {  
  551.                 //delete   
  552.             }  
  553.             void InOrderTraverse(RB_Node* node)  
  554.             {  
  555.                 if(node==m_nullNode)  
  556.                 {  
  557.                     return;  
  558.                 }  
  559.                 else  
  560.                 {  
  561.                     InOrderTraverse(node->left);  
  562.                     cout<<node->key<<endl;  
  563.                     InOrderTraverse(node->right);  
  564.                 }  
  565.             }  
  566.             ~RB_Tree()  
  567.             {  
  568.                 clear(m_root);  
  569.                 delete m_nullNode;  
  570.             }  
  571.     private:  
  572.         // utility function for destructor to destruct object;   
  573.         void clear(RB_Node* node)  
  574.         {  
  575.             if(node==m_nullNode)  
  576.             {  
  577.                 return ;  
  578.             }  
  579.             else  
  580.             {  
  581.                 clear(node->left);  
  582.                 clear(node->right);  
  583.                 delete node;  
  584.             }  
  585.         }  
  586.     private:  
  587.         RB_Node *m_nullNode;  
  588.         RB_Node *m_root;  
  589. };  
  590. #endif /*_RB_TREE_H_*/  
~

你可能感兴趣的:(透彻理解红黑树之六)