红黑树



  红黑树>

    在之前实现了AVL树,其实红黑树和AVL树都是高效且平衡的二叉搜索树,增删查改的时间复杂度都是O(lg N).但是在实际应用中红黑树的应用最多,比如C++STL库--map/set,Java库,Linux内核以及其他的一些库里都用到了红黑树,这是因为红黑树是不追求完全平衡的是一种近似平衡的二叉树保证最长路径不超过最短路径的二倍,相对于AVL树来说降低了旋转的要求,所以性能会优于AVL树(这篇文章主要介绍红黑树的插入).

    在上面提到了红黑树的特性是保证最长路径不超过最短路径的二倍,那仫为什仫满足这样的要求就是红黑树了呢?让我先来介绍一下红黑树的性质>

    1).每个结点不是红色就是黑色.

    2).根结点是黑色的.

    3).如果一个结点是红色的,则它的两个子节点是黑色的(不存在连续的红结点).

    4).对每个节点,从该节点到其所有后代叶节点的简单路径上,均包含相同数目的黑色节点(每条路径上黑色结点的数量相等)

    5).每个叶子结点都是黑色的(这里的叶子结点指的是空结点).

    假设有这样的一棵树它的所有节点都是黑色的,而且满足红黑树的特性,此时再次插入新的结点要重新满足红黑树的特性4,我们只能插入红色结点,而且插入新的红色结点为了满足特性3,只能在黑色结点之间插入新的红色结点,此时最长路径上的结点数量恰好满足不超过最短路径的两倍.

   

 红黑树的插入>

      为了满足红黑树的特性4,所以新插入的结点的颜色必须为红色.

      当前结点cur为红色,parent为红色,则必然存在祖父结点.因为不能存在连续的红色结点,所以祖父结点的颜色一定是黑色的.根据uncle是否存在以及uncle结点的颜色来进行不同的处理.

      (1).uncle存在且uncle为红.

         

      (2).uncle不存在或者uncle为黑色.

         

    通过上图观察可得情况3和情况2类似,可以将情况3先进行左单旋变成和情况2类似的情况再进行右单旋,最后达到处理的效果.

    

           

    在进行双旋的时候,因为双旋是分成单旋来完成的所以双旋完成第一次单旋后要修改parent和cur指针否则就会导致丢失结点的现象.

 

     (3).当parent->_right == cur时和上述情况类似,在这里我就不多加叙述了.

 代码实现区>

     

  1. #pragma once  
  2.   
  3. enum Colour  
  4. {  
  5.     RED,  
  6.     BLACK  
  7. };  
  8.   
  9. template<class K,class V>  
  10. struct RBTreeNode  
  11. {  
  12.     K _key;  
  13.     V _value;  
  14.     RBTreeNode<K,V>* _left;  
  15.     RBTreeNode<K,V>* _right;  
  16.     RBTreeNode<K,V>* _parent;  
  17.     Colour _color;  
  18.     RBTreeNode(const K& key,const V& value)  
  19.         :_key(key)  
  20.         ,_value(value)  
  21.         ,_left(NULL)  
  22.         ,_right(NULL)  
  23.         ,_parent(NULL)  
  24.         ,_color(RED)  
  25.     {}  
  26. };  
  27.   
  28. template<class K,class V>  
  29. class RBTree  
  30. {  
  31.     typedef RBTreeNode<K,V> Node;  
  32. public:  
  33.     RBTree()  
  34.         :_root(NULL)  
  35.     {}  
  36.     bool Insert(const K& key,const V& value)  
  37.     {  
  38.         if(_root == NULL)  
  39.         {  
  40.             _root=new Node(key,value);  
  41.             _root->_color=BLACK;  
  42.             return true;  
  43.         }  
  44.         Node *parent=NULL;  
  45.         Node *cur=_root;  
  46.         while (cur)  
  47.         {  
  48.             if (cur->_key < key)  
  49.             {  
  50.                 parent=cur;  
  51.                 cur=cur->_right;  
  52.             }  
  53.             else if (cur->_key > key)  
  54.             {  
  55.                 parent=cur;  
  56.                 cur=cur->_left;  
  57.             }  
  58.             else  
  59.                 return false;  
  60.         }  
  61.         cur=new Node(key,value);  
  62.         cur->_parent=parent;  
  63.         if(parent->_key < key)  
  64.             parent->_right=cur;  
  65.         else  
  66.             parent->_left=cur;  
  67.         while (cur != _root && parent->_color == RED)   //保证有父亲和祖父  
  68.         {  
  69.             Node *grandfather=parent->_parent;  
  70.             if (grandfather->_left == parent)  
  71.             {  
  72.                 Node *uncle=grandfather->_right;  
  73.                 if (uncle && uncle->_color == RED)      //情况1  
  74.                 {  
  75.                     parent->_color=uncle->_color=BLACK;  
  76.                     grandfather->_color=RED;  
  77.                     //继续向上调整  
  78.                     cur=grandfather;  
  79.                     parent=cur->_parent;  
  80.                 }  
  81.                 else     //uncle不存在或者uncle为黑色  
  82.                 {  
  83.                     if (parent->_right == cur)          //情况3  
  84.                     {  
  85.                         _RotateL(parent);  
  86.                         //第一次旋转完成后更新parent和cur  
  87.                         swap(parent,cur);  
  88.                     }  
  89.                     _RotateR(grandfather);              //情况2  
  90.                     grandfather->_color=RED;  
  91.                     parent->_color=BLACK;  
  92.                     break;  
  93.                 }  
  94.             }  
  95.             else      //grandfather->_right == parent  
  96.             {  
  97.                 Node *uncle=grandfather->_left;  
  98.                 if(uncle && uncle->_color == RED)  
  99.                 {  
  100.                     parent->_color=uncle->_color=BLACK;  
  101.                     grandfather->_color=RED;  
  102.                     cur=grandfather;  
  103.                     parent=cur->_parent;  
  104.                 }  
  105.                 else     //uncle不存在或者uncle为黑色  
  106.                 {  
  107.                     if (parent->_left == cur)  
  108.                     {  
  109.                         _RotateR(parent);  
  110.                         swap(parent,cur);  
  111.                     }  
  112.                     _RotateL(grandfather);  
  113.                     grandfather->_color=RED;  
  114.                     parent->_color=BLACK;  
  115.                     break;  
  116.                 }  
  117.             }  
  118.         }  
  119.         _root->_color=BLACK;  
  120.         return true;  
  121.     }  
  122.     bool Find(const K& key)  
  123.     {  
  124.         if(_root == NULL)  
  125.             return false;  
  126.         Node *cur=_root;  
  127.         while (cur)  
  128.         {  
  129.             if(cur->_key < key)  
  130.                 cur=cur->_right;  
  131.             else if(cur->_key > key)  
  132.                 cur=cur->_left;  
  133.             else  
  134.                 return true;  
  135.         }  
  136.         return false;  
  137.     }  
  138.     void InOrder()  
  139.     {  
  140.         _InOrder(_root);  
  141.         cout<<endl;  
  142.     }  
  143.     bool IsRBTree()  
  144.     {  
  145.         if(_root == NULL)  
  146.             return true;  
  147.         if(_root->_color == RED)       //判断根结点是否是黑色  
  148.             return false;  
  149.         int count=0;  
  150.         Node *cur=_root;  
  151.         while (cur)  
  152.         {  
  153.             if(cur->_color == BLACK)  
  154.                 count++;  
  155.             cur=cur->_left;  
  156.         }  
  157.         int k=0;  
  158.         return _IsRBTree(_root,count,k);  
  159.     }  
  160.     ~RBTree()  
  161.     {  
  162.         _Destroy(_root);  
  163.     }  
  164. protected:  
  165.     bool _IsRBTree(Node *root,const int count,int k)  
  166.     {  
  167.         if(root == NULL)  
  168.             return true;  
  169.         if (root->_color == RED && root->_parent->_color == RED)  
  170.         {  
  171.             cout<<"存在连续的红结点:"<<root->_key<<endl;  
  172.             return false;  
  173.         }  
  174.         if(root->_color == BLACK)  
  175.             ++k;  
  176.         if (root->_left == NULL && root->_right == NULL)  
  177.         {  
  178.             if(count != k)  
  179.             {  
  180.                 cout<<"黑色结点的数量不等"<<root->_key<<endl;  
  181.                 return false;  
  182.             }  
  183.         }  
  184.         return _IsRBTree(root->_left,count,k) && _IsRBTree(root->_right,count,k);  
  185.     }  
  186.     void _RotateL(Node *parent)  
  187.     {  
  188.         Node *subR=parent->_right;  
  189.         Node *subRL=subR->_left;  
  190.         parent->_right=subRL;  
  191.         if(subRL)  
  192.             subRL->_parent=parent;  
  193.         subR->_left=parent;  
  194.         Node *ppNode=parent->_parent;  
  195.         parent->_parent=subR;  
  196.         if (ppNode == NULL)  
  197.         {  
  198.             _root=subR;  
  199.             subR->_parent=NULL;  
  200.         }  
  201.         else  
  202.         {  
  203.             if(ppNode->_left == parent)  
  204.                 ppNode->_left=subR;  
  205.             else  
  206.                 ppNode->_right=subR;  
  207.             subR->_parent=ppNode;  
  208.         }  
  209.     }  
  210.     void _RotateR(Node *parent)  
  211.     {  
  212.         Node *subL=parent->_left;  
  213.         Node *subLR=subL->_right;  
  214.         parent->_left=subLR;  
  215.         if(subLR)  
  216.             subLR->_parent=parent;  
  217.         subL->_right=parent;  
  218.         Node *ppNode=parent->_parent;  
  219.         parent->_parent=subL;  
  220.         if (ppNode == NULL)  
  221.         {  
  222.             _root=subL;  
  223.             subL->_parent=NULL;  
  224.         }  
  225.         else  
  226.         {  
  227.             if(ppNode->_left == parent)  
  228.                 ppNode->_left=subL;  
  229.             else  
  230.                 ppNode->_right=subL;  
  231.             subL->_parent=ppNode;  
  232.         }  
  233.     }  
  234.     void _InOrder(Node *root)  
  235.     {  
  236.         if(root == NULL)  
  237.             return ;  
  238.         _InOrder(root->_left);  
  239.         cout<<root->_key<<" ";  
  240.         _InOrder(root->_right);  
  241.     }  
  242.     void _Destroy(Node *&root)  
  243.     {  
  244.         if(root == NULL)  
  245.             return ;  
  246.         Node *cur=root;  
  247.         if (cur)  
  248.         {  
  249.             _Destroy(cur->_left);  
  250.             _Destroy(cur->_right);  
  251.             delete cur;  
  252.             cur=NULL;  
  253.         }  
  254.     }  
  255. protected:  
  256.     Node *_root;  
  257. };  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值