编程练习——红黑树(RedBlackTree)

   在网上搜索了很多红黑树的资源,发现自己看代码的能力还是有些欠缺。很多都看不懂,后来找到一篇英文红黑树文献,终于弄明白了红黑树的删除插入是怎么做的。但是那片文献好像还有些漏洞,其中有一种删除情况,他并没有考虑到。如果大家想看的话可以搜索下 "red-black trees" ,由 prof. Lyn Turbak所写。因为时间关系,插入算法只是实现了非CLR算法。删除算法我也不知道叫什么名字(文献里没有讲)。但是删除结果依旧符合红黑树。

  建立红黑树节点的时候,考虑到因为经常要和一个节点的父亲节点打交道,所以采用了在节点里添加父亲节点的指针的方法。这样即方便也提高效率。另外也好调试。如果一味采用递归..估计人会看疯掉的。插入的时候,采用的是先插入删除的时候红色节点,然后在判断是否违反红红条件。违反的话就左旋右旋进行调整。否则就插入成功。删除的时候先删除节点,这个过程和删除二叉查找树节点一致。删除时要看真正要删除的节点是否是根节点,是否是黑色节点。因为红色节点和根节点删除不会调整树形。如果删除的是黑色节点,那么要看替换他的节点(这里的替换一定是左子树树根,因为如果有右子树,那么被删除的也不会是这个节点,记住二叉查找树的删除过程)是否是黑色节点(这里如果左子树为空,那他也是黑色节点)。如果是黑色节点,那么一定要调整树的结构,否则就直接染色。调整树结构时要考虑的情况就非常多了。一共有9种情况需要考虑。

 

  下面是代码部分,删除时如何调整在代码注释中。至于insert操作,看看文献就应该会做了。

 

  1. /*
  2. * create RBNode class for construction RBTree
  3. * create by chico chen
  4. * date: 2008/09/04
  5. * 如需转载注明出处
  6. */
  7. template<class T>
  8. #define RED 0
  9. #define BLACK 1
  10. class RBNode
  11. {
  12. public:
  13.     T data;
  14.     RBNode<T>* left;
  15.     RBNode<T>* right;
  16.     RBNode<T>* parent;
  17.     int color;
  18.     RBNode(T& data)
  19.     {
  20.         this->data = data;
  21.         left = NULL;
  22.         right = NULL;
  23.         parent = NULL;
  24.         color = RED; 
  25.     }
  26.     RBNode(T& data, RBNode<T>* left, RBNode<T>* right, RBNode<T>* parent)
  27.     {
  28.         this->data = data;
  29.         this->left = left;
  30.         this->right = right;
  31.         this->parent = parent;
  32.         color = RED;
  33.     }
  34.     ~RBNode()
  35.     {
  36.         this->left = this->right = this->parent = NULL;
  37.         color = RED;
  38.     }
  39. };

 

 

下面是红黑树代码

 

  1. /*
  2. * this is red-black-tree code
  3. * this code is not efficent in inserting
  4. *
  5. * create by chico chen
  6. * date: 2008/09/04
  7. * 如需转载注明出处
  8. */
  9. #include "RBNode.h"
  10. template<class T>
  11. class RBTree
  12. {
  13. private:
  14.     RBNode<T>* root;
  15. public:
  16.     RBTree()
  17.     {
  18.         this->root = NULL;
  19.     }
  20.     ~RBTree()
  21.     {
  22.         clearTree(this->root);
  23.     }
  24.     void print()
  25.     {
  26.         print(this->root,0);
  27.     }
  28.     
  29.     void insert(T& data)
  30.     {
  31.         RBNode<T>* node = new RBNode<T>(data);
  32.         insertNode(node);
  33.     }
  34.     void deleteKey(T& data)
  35.     {
  36.         deleteNode(data);
  37.     }
  38.     bool RBTreeTest()
  39.     {
  40.         try
  41.         {
  42.             getBlackNum(this->root);
  43.         }
  44.         catch(...)
  45.         {
  46.             return false;
  47.         }
  48.         return true;
  49.     }
  50. private:
  51.     void print(RBNode<T>* subRoot, int num)
  52.     {
  53.         if(subRoot != NULL)
  54.         {
  55.             print(subRoot->right,num+3);
  56.             for(int i = 0; i < num; i++)
  57.             {
  58.                 cout << " ";
  59.             }
  60.             cout << subRoot->data<<"("<<subRoot->color<<")"<<endl;
  61.             print(subRoot->left,num+3);
  62.         }
  63.     }
  64.     void clearTree(RBNode<T>* subRoot)
  65.     {
  66.         if(subRoot == NULL)
  67.             return;
  68.         clearTree(subRoot->left);
  69.         clearTree(subRoot->right);
  70.         delete subRoot;
  71.     }
  72.     RBNode<T>* deleteMin(RBNode<T>* subRoot,RBNode<T>** parent)
  73.     {
  74.         
  75.         while(subRoot != NULL)
  76.         {
  77.             parent = &subRoot;
  78.             if(subRoot->right != NULL)
  79.             {
  80.                 subRoot = subRoot->right;
  81.             }
  82.             else
  83.             {
  84.                 break;
  85.             }
  86.         }
  87.         return subRoot;
  88.     }
  89.     void deleteNode(T& data)
  90.     {
  91.         if(root == NULL)
  92.         {
  93.             throw "Empty tree";
  94.         }
  95.         RBNode<T>* subRoot = root;
  96.         while(subRoot != NULL)
  97.         {
  98.             if(subRoot->data > data)
  99.             {
  100.                 subRoot = subRoot->left;
  101.             }
  102.             else if(subRoot->data < data)
  103.             {
  104.                 subRoot = subRoot->right;
  105.             }
  106.             else
  107.             {
  108.                 // find the data
  109.                 // use the midOrder last one which is before the data node to replace
  110.                 RBNode<T>* inParent = subRoot;
  111.                 RBNode<T>* deleteNode = deleteMin(subRoot->left,&inParent);
  112.                 RBNode<T>* realDelete = NULL;
  113.                 if(deleteNode != NULL)
  114.                 {
  115.                     subRoot->data = deleteNode->data; // copy
  116.                     realDelete = deleteNode;
  117.                     //real delete
  118.                 }
  119.                 else
  120.                 {
  121.                     realDelete = subRoot;
  122.                     //real delete
  123.                 }
  124.                 if(realDelete->parent == NULL)
  125.                 {
  126.                     // delete root
  127.                     delete realDelete;
  128.                 }
  129.                 else
  130.                 {
  131.                     // nothing at realDelete->right
  132.                     RBNode<T>* parent = realDelete->parent;
  133.                     RBNode<T>* subATree = NULL;
  134.                     if(parent->left == realDelete)
  135.                     {
  136.                         parent->left = realDelete->left;
  137.                     }
  138.                     else
  139.                     {
  140.                         parent->right = realDelete->left;
  141.                     }
  142.                     subATree = realDelete->left;
  143.                     if(realDelete->left != NULL)
  144.                     {
  145.                         realDelete->left->parent = parent;
  146.                     }
  147.                     int color = realDelete->color;
  148.                     delete realDelete;
  149.                     if(color == BLACK && 
  150.                         (subATree == NULL || subATree->color == BLACK))
  151.                     {
  152.                         
  153.                         
  154.                         deleteRebuild(subATree,parent);
  155.                     }
  156.                     else if(color == BLACK && subATree->color == RED)
  157.                     {
  158.                         subATree->color = BLACK;
  159.                     }
  160.                     else
  161.                     {
  162.                         // do nothing
  163.                     }
  164.                     break;
  165.                 }
  166.             }
  167.         }
  168.     }
  169.     /* delete them if you want
  170.     *
  171.     //     x(r)                c(r)
  172.     //     /  /                /  /
  173.     //   `a(b) b(b) -->     x(b)   b(b)
  174.     //         /            /
  175.     //        c(r)         a(b)
  176.     // LRL means L-> double black is at left, and rotate is RL
  177.     RBNode<T>* LRLCaseA(RBNode<T>* x)
  178.     {
  179.         RLRotate(x,x->right,x->right->left);
  180.         x->color = BLACK;
  181.         return x->parent;
  182.         
  183.     }
  184.     //     x(r)                 b(r)
  185.     //     /  /                 /  /
  186.     //   `a(b) b(b)   -->    x(b)   c(b)
  187.     //          /             /
  188.     //           c(r)       a(b)
  189.     RBNode<T>* LLLCaseA(RBNode<T>* x)
  190.     {
  191.         RRRotate(x,x->right);
  192.         x->color = BLACK;
  193.         x->parent->color = RED;
  194.         x->parent->right->color = BLACK;
  195.         return x->parent;
  196.     }
  197.     //     x(r)                b(r)
  198.     //     /  /                /  /
  199.     //   b(b) `a(b)-->       c(b) x(b)
  200.     //    /                         /
  201.     //  c(r)                        a(b)
  202.     RBNode<T>* RLLCaseA(RBNode<T>* x)
  203.     {
  204.         LLRotate(x,x->left);
  205.         x->color = BLACK;
  206.         x->parent->color = RED;
  207.         x->parent->left->color = BLACK;
  208.         return x->parent;
  209.     }
  210.     //     x(r)                c(r)
  211.     //     /  /                /  /
  212.     //   b(b) `a(b)-->       b(b) x(b)
  213.     //    /                         /
  214.     //    c(r)                        a(b)
  215.     RBNode<T>* RLRCaseA(RBNode<T>* x)
  216.     {
  217.         LRRotate(x,x->left,x->left->right);
  218.         x->color = BLACK;
  219.         
  220.         return x->parent;
  221.     }
  222.     
  223.     //     x(r)                     x(b)
  224.     //     /  /                     /  /
  225.     //   `a(b) c(b)        ->     a(b) c(r)
  226.     //         /   /                  /   /
  227.     //        d(b)  e(b)            d(b)  e(b)
  228.     RBNode<T>* LCaseB(RBNode<T>* x)
  229.     {
  230.         x->color = BLACK;
  231.         x->right->color = RED;
  232.     }
  233.     //     x(r)                     x(b)
  234.     //     /  /                     /  /
  235.     //    c(b) `a(b)       ->     c(r) a(b)
  236.     //   /   /                   /   /
  237.     // d(b)  e(b)              d(b)  e(b)
  238.     RBNode<T>* RCaseB(RBNode<T>* x)
  239.     {
  240.         x->color = BLACK;
  241.         x->left->color = RED;
  242.     }
  243.     //     x(b)                    c(b)
  244.     //     /  /                    /  /
  245.     //  `a(b)  c(r)      ->     x(r)  e(b)
  246.     //        /   /             /  / 
  247.     //       d(b)  e(b)      `a(b)  d(b)
  248.     RBNode<T>* LCaseC(RBNode<T>* x)
  249.     {
  250.         RRRotate(x,x->right);
  251.         x->color = RED;
  252.         x->parent->color = BLACK;
  253.         return  x->parent;
  254.     }
  255.     //     x(b)                    c(b)
  256.     //     /  /                    /  /
  257.     //   c(r) `a(b)     ->       d(b) x(r)
  258.     //   /   /                        /  / 
  259.     //  d(b)  e(b)                  e(b) `a(b) 
  260.     RBNode<T>* RCaseC(RBNode<T>* x)
  261.     {
  262.         LLRotate(x,x->left);
  263.         x->color = RED;
  264.         x->parent->color = BLACK;
  265.         return  x->parent;
  266.     }
  267.     *
  268.     */
  269.     bool isBlack(RBNode<T>* node)
  270.     {
  271.         if(node == NULL || node->color == BLACK)
  272.             return true;
  273.         return false;
  274.     }
  275.     void rebuild(RBNode<T>* node)
  276.     {
  277.         if(node->parent->data > node->data)
  278.         {
  279.             node->parent->left = node;
  280.         }
  281.         else
  282.         {
  283.             node->parent->right = node;
  284.         }
  285.     }
  286.     /*
  287.     * There are 9 cases we will meet. The cases of the double black node at the right is ignored.
  288.     * (b) means black node, (db) means double black node, (r) means red node
  289.     * 1.    (b)              2   (b)           3 (b)
  290.     *      /   /                 /  /            /  /
  291.     *    (db)  (b)             (db)  (b)       (db)  (b)
  292.     *          /  /                  / /             / /
  293.     *         (b)  (b)             (r)  (b)        (b)  (r)
  294.     *
  295.     * 4.    (b)              5   (b)           6 (r)
  296.     *      /   /                 /  /            /  /
  297.     *    (db)  (b)             (db)  (r)       (db)  (b)
  298.     *          /  /                  / /             / /
  299.     *         (r)  (r)             (b)  (b)        (b)  (b)
  300.     *
  301.     * 7.    (r)              8   (r)           9 (r)
  302.     *      /   /                 /  /            /  /
  303.     *    (db)  (b)             (db)  (b)       (db)  (b)
  304.     *          /  /                  / /             / /
  305.     *         (r)  (b)             (b)  (r)        (r)  (r)
  306.     *
  307.     * case 1,6: up the black, if the parent is black then call the function again until the 
  308.     * double black node is root, else blacken the parent.
  309.     * case 2,4,7,9: call the RLRotate, if the parent of the double
  310.     * node is black, up the black and call the function again, else 
  311.     * blacken the parent.
  312.     * case 3,8: call the LLRotate, the same as case 2.
  313.     * case 5: call LLRotate, change as    (b)   then end.
  314.     *                                    /   / 
  315.     *                                  (b)    (b)
  316.     *                                  / /
  317.     *                                (b)  (r)
  318.     * 
  319.     */
  320.     void deleteRebuild(RBNode<T>* node,RBNode<T>* parent)
  321.     {
  322.         if(parent == NULL)
  323.         {
  324.             // root, delete the black
  325.             return;
  326.         }
  327.         if(parent->left == node)
  328.         {
  329.             RBNode<T>* brother = parent->right;
  330.             RBNode<T>* nephewA = brother->left;
  331.             RBNode<T>* nephewB = brother->right;
  332.             
  333.             if(isBlack(nephewA) && isBlack(nephewB) && isBlack(brother))
  334.             {
  335.                 // case 1,6
  336.                 brother->color = RED;
  337.                 if(parent->color == BLACK)
  338.                 {
  339.                     deleteRebuild(parent,parent->parent);
  340.                 }
  341.                 else
  342.                 {
  343.                     parent->color = BLACK;
  344.                     return;
  345.                 }
  346.                 
  347.             }
  348.         
  349.             else if(!isBlack(nephewA))
  350.             {
  351.                 // case 2,4,7,9
  352.                 RBNode<T>* tempRoot = RLRotate(parent,brother,nephewA);
  353.                 // rebuild
  354.                 rebuild(tempRoot);
  355.             }
  356.             else if(!isBlack(nephewB))
  357.             {
  358.                 // case 3,8
  359.                 RBNode<T>* tempRoot = RRRotate(parent,brother);
  360.                 rebuild(tempRoot);
  361.                 nephewB->color = BLACK;
  362.                 brother->color = RED;
  363.             }
  364.             else if(!isBlack(brother))
  365.             {
  366.                 // case 5
  367.                 RBNode<T>* tempRoot = RRRotate(parent,brother);
  368.                 rebuild(tempRoot);
  369.                 brother->color = BLACK;
  370.                 nephewA->color = RED;
  371.                 return;
  372.             }
  373.             else
  374.             {
  375.                 // unknown 
  376.                 throw "none excption, about there is no red or black";
  377.             }
  378.             if(parent->color == BLACK)
  379.             {
  380.                 // case 2,3,4
  381.                 deleteRebuild(parent,parent->parent);
  382.             }
  383.             else
  384.             {
  385.                 // case 7,8,9
  386.                 parent->color = BLACK;
  387.             }
  388.         }
  389.         else
  390.         {
  391.             RBNode<T>* brother = parent->left;
  392.             RBNode<T>* nephewA = brother->right;
  393.             RBNode<T>* nephewB = brother->left;
  394.             if(isBlack(nephewA) && isBlack(nephewB) && isBlack(brother))
  395.             {
  396.                 brother->color = RED;
  397.                 if(parent->color == BLACK)
  398.                 {
  399.                     deleteRebuild(parent,parent->parent);
  400.                 }
  401.                 else
  402.                 {
  403.                     parent->color = BLACK;
  404.                     return;
  405.                 }
  406.             }
  407.             else if(!isBlack(nephewA))
  408.             {
  409.                 RBNode<T>* tempRoot = LRRotate(parent,brother,nephewA);
  410.                 // rebuild
  411.                 rebuild(tempRoot);
  412.             }
  413.             else if(!isBlack(nephewB))
  414.             {
  415.                 RBNode<T>* tempRoot = LLRotate(parent,brother);
  416.                 rebuild(tempRoot);
  417.                 nephewB->color = BLACK;
  418.                 brother->color = RED;
  419.             }
  420.             else if(!isBlack(brother))
  421.             {
  422.                 RBNode<T>* tempRoot = LLRotate(parent,brother);
  423.                 rebuild(tempRoot);
  424.                 nephewA->color = RED;
  425.                 brother->color = BLACK;
  426.                 return;
  427.             }
  428.             else
  429.             {
  430.                 throw "none excption, about there is no red or black";
  431.             }
  432.             if(parent->color == BLACK)
  433.             {
  434.                 deleteRebuild(parent,parent->parent);
  435.             }
  436.             else
  437.             {
  438.                 parent->color = BLACK;
  439.             }
  440.         }
  441.         
  442.     }
  443.     
  444.     
  445.     void insertNode(RBNode<T>* node)
  446.     {
  447.         if(root == NULL)
  448.         {
  449.             root = node;
  450.             node->color = BLACK;
  451.             return;
  452.         }
  453.         RBNode<T>* subRoot = root;
  454.         RBNode<T>* insertPoint = NULL;
  455.         while(subRoot!=NULL)
  456.         {
  457.             insertPoint = subRoot;
  458.             if(subRoot->data > node->data)
  459.             {
  460.                 // insert left
  461.                 subRoot = subRoot->left;
  462.             }
  463.             else if(subRoot->data < node->data)
  464.             {
  465.                 subRoot = subRoot->right;
  466.             }
  467.             else
  468.             {
  469.                 throw "same key";
  470.             }   
  471.         }
  472.         
  473.         if(insertPoint->data > node->data)
  474.         {
  475.             insertPoint->left = node;
  476.         }
  477.         else
  478.         {
  479.             insertPoint->right = node;
  480.         }
  481.         node->parent = insertPoint;
  482.         
  483.         
  484.         insertRebuild(node);
  485.         
  486.     }
  487.     // return the subRoot
  488.     //        a                   b
  489.     //       /                  /   /
  490.     //      b        ->        c     a
  491.     //     /
  492.     //    c
  493.     RBNode<T>* LLRotate(RBNode<T>* a, RBNode<T>* b)
  494.     {
  495.         if(b->right != NULL)
  496.         {
  497.             b->right->parent = a;
  498.             
  499.         }
  500.         a->left = b->right;
  501.         b->right = a;
  502.         b->parent = a->parent;
  503.         a->parent = b;
  504.         return b;
  505.     }
  506.     // return the subRoot
  507.     //        a                      b
  508.     //          /                  /   /
  509.     //            b        ->     a     c
  510.     //              /
  511.     //                c
  512.     RBNode<T>* RRRotate(RBNode<T>* a, RBNode<T>* b)
  513.     {
  514.         if(b->left != NULL)
  515.         {
  516.             b->left->parent = a;
  517.             
  518.         }
  519.         a->right = b->left;
  520.         b->left = a;
  521.         b->parent = a->parent;
  522.         a->parent = b;
  523.         return b;
  524.     }
  525.     // return the subRoot
  526.     //        a                      c
  527.     //          /                  /   /
  528.     //            b        ->     a     b
  529.     //           /                 /      
  530.     //          c                   d  
  531.     //         /
  532.     //        d
  533.     RBNode<T>* RLRotate(RBNode<T>* a, RBNode<T>* b, RBNode<T>* c)
  534.     {
  535.         
  536.         if(c->right != NULL)
  537.         {
  538.             c->right->parent = b;       
  539.             
  540.         }
  541.         b->left = c->right;
  542.         c->right = b;
  543.         b->parent = c;
  544.         if(c->left != NULL)
  545.         {
  546.             c->left->parent = a;        
  547.             
  548.         }
  549.         a->right = c->left;
  550.         c->left = a;
  551.         c->parent = a->parent;
  552.         a->parent = c;
  553.         
  554.         return c;
  555.     }
  556.     // return the subRoot
  557.     //        a                      c
  558.     //       /                     /   /
  559.     //      b              ->     b     a
  560.     //       /                         /       
  561.     //        c                       d   
  562.     //         /
  563.     //          d
  564.     RBNode<T>* LRRotate(RBNode<T>* a, RBNode<T>* b, RBNode<T>* c)
  565.     {
  566.         if(c->left != NULL)
  567.         {
  568.             c->left->parent = b;
  569.             
  570.             
  571.         }
  572.         b->right = c->left;
  573.         c->left = b;
  574.         b->parent = c;
  575.         if(c->right!= NULL)
  576.         {
  577.             c->right->parent = a;
  578.             
  579.         }
  580.         a->left = c->right;
  581.         c->right = a;
  582.         c->parent = a->parent;
  583.         a->parent = c;
  584.         
  585.         return c;
  586.     }
  587.     // node is not the root
  588.     void insertRebuild(RBNode<T>* node)
  589.     {
  590.         RBNode<T>* parent = NULL;
  591.         RBNode<T>* grandParent = NULL;
  592.         while(node->parent != NULL)
  593.         {
  594.             parent = node->parent;
  595.             
  596.             if(parent->color == RED)// here means there must be a grandparent
  597.             {
  598.                 grandParent = parent->parent;
  599.                 grandParent->color = BLACK;
  600.                 
  601.                 if(grandParent->left == parent)
  602.                 {
  603.                     if(parent->left == node)
  604.                     {
  605.                         //LLRotate
  606.                         node->color = BLACK;
  607.                         node = LLRotate(grandParent,parent);
  608.                         
  609.                     }
  610.                     else
  611.                     {
  612.                         //LRRotate
  613.                         parent->color = BLACK;
  614.                         node = LRRotate(grandParent,parent,node);
  615.                         
  616.                     }
  617.                     
  618.                 }
  619.                 else
  620.                 {
  621.                     if(parent->left == node)
  622.                     {
  623.                         //RLRotate          
  624.                         parent->color = BLACK;
  625.                         node = RLRotate(grandParent,parent,node);
  626.                     }
  627.                     else
  628.                     {
  629.                         //RRRotate
  630.                         node->color = BLACK;
  631.                         node = RRRotate(grandParent,parent);
  632.                     }
  633.                 }
  634.             }
  635.             else
  636.             {
  637.                 break;
  638.             }
  639.         }
  640.         if(node->parent == NULL)
  641.         {
  642.             node->color = BLACK;
  643.             this->root = node;
  644.         }
  645.         else
  646.         {
  647.             rebuild(node);
  648.             /*if(node->parent->data > node->data)
  649.             {
  650.                 node->parent->left = node;
  651.             }
  652.             else
  653.             {
  654.                 node->parent->right = node;
  655.             }*/
  656.         }
  657.     }
  658.     int getBlackNum(RBNode<T>* subRoot)
  659.     {
  660.         if(subRoot == NULL)
  661.         {
  662.             return 1;
  663.         }
  664.         int left = getBlackNum(subRoot->left);
  665.         int right = getBlackNum(subRoot->right);
  666.         if(left != right)
  667.         {
  668.             throw "wrong";
  669.         }
  670.         if(subRoot->color == BLACK)
  671.         {
  672.             
  673.             return 1+left;
  674.         }
  675.         else
  676.         {
  677.             return left;
  678.         }
  679.     }
  680. };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值