Chinaunix首页 | 论坛 | 博客
  • 博客访问: 268773
  • 博文数量: 84
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 927
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-06 23:00
个人简介

growing

文章分类

全部博文(84)

文章存档

2017年(6)

2016年(61)

2015年(17)

我的朋友

分类: C/C++

2016-04-27 12:36:11

也是像AVL一样分四种情况,不同的是RL,LR时不用想AVL一样调整平衡因子,只需要调色就行了。
插外侧是单旋转,内侧是双旋转。
如下:

  1. #pragma once

  2. enum Color{RED,BLACK};

  3. template<class K,class V>
  4. struct RBtreeNode
  5. {
  6.     RBtreeNode(const K& key,const V& v,Color col = RED)
  7.         :_left(NULL)
  8.         ,_right(NULL)
  9.         ,_parent(NULL)
  10.         ,_key(key)
  11.         ,_vlaue(v)
  12.         ,_col(col)
  13.     {}
  14.     RBtreeNode<K,V>* _left;
  15.     RBtreeNode<K,V>* _right;
  16.     RBtreeNode<K,V>* _parent;
  17.     K _key;
  18.     V _vlaue;
  19.     Color _col;
  20. };
  21. template<class K,class V>
  22. class RBtree
  23. {
  24.     typedef RBtreeNode<K,V> Node;
  25. public:
  26.     RBtree()
  27.         :_root(NULL)
  28.     {}
  29.     bool Insert(const K& key,const V& v)
  30.     {
  31.         if(_root == NULL)
  32.         {
  33.             _root = new Node(key,v,BLACK);
  34.             return true;
  35.         }
  36.         Node* parent = NULL;
  37.         Node* cur = _root;
  38.         while(cur)
  39.         {
  40.             if(key < cur->_key)
  41.             {
  42.                 parent = cur;
  43.                 cur = cur->_left;
  44.             }
  45.             else if(key > cur->_key)
  46.             {
  47.                 parent = cur;
  48.                 cur = cur->_right;
  49.             }
  50.             else
  51.             {
  52.                 return false;
  53.             }
  54.         }
  55.         cur = new Node(key,v,RED);
  56.         if(key < parent->_key)
  57.         {
  58.             parent->_left = cur;
  59.             cur->_parent = parent;
  60.         }
  61.         else
  62.         {
  63.             parent->_right = cur;
  64.             cur->_parent = parent;
  65.         }
  66.         //调色
  67.         while(cur != _root && parent->_col == RED)
  68.         {
  69.             Node* GrandParent = parent->_parent;
  70.             if(parent == GrandParent->_left)//往左子树插
  71.             {
  72.                 Node* uncle = GrandParent->_right;
  73.                 if(uncle && uncle->_col == RED)
  74.                 {
  75.                     uncle->_col = parent->_col = BLACK;
  76.                     GrandParent->_col = RED;

  77.                     cur = GrandParent;
  78.                     parent = cur->_parent;
  79.                 }
  80.                 else
  81.                 {
  82.                     if(cur == parent->_right)
  83.                     {
  84.                         _RotateL(parent);
  85.                         swap(cur,parent);
  86.                     }
  87.                     _RotateR(GrandParent);
  88.                     parent->_col = BLACK;
  89.                     GrandParent->_col = RED;
  90.                 }
  91.             }
  92.             else//往右子树插
  93.             {
  94.                 Node*uncle = GrandParent->_left;
  95.                 if(uncle && uncle->_col == RED)
  96.                 {
  97.                     uncle->_col = parent->_col = BLACK;
  98.                     GrandParent->_col = RED;

  99.                     cur = GrandParent;
  100.                     parent = cur->_parent;
  101.                 }
  102.                 else
  103.                 {
  104.                     if(cur == parent->_left)
  105.                     {
  106.                         _RotateR(parent);
  107.                         swap(cur,parent);
  108.                     }
  109.                     _RotateL(GrandParent);

  110.                     parent->_col = BLACK;
  111.                     GrandParent->_col = RED;
  112.                 }

  113.             }
  114.         }
  115.         _root->_col = BLACK;
  116.         return true;
  117.     }
  118.     bool IsBalanceTree()
  119.     {
  120.         return _IsBalance(_root);
  121.     }
  122.     void InOrder()
  123.     {
  124.         _InOrder(_root);
  125.         cout<<endl;
  126.     }
  127. protected:
  128.     int _Height(Node* root)
  129.     {
  130.         if(root == NULL)
  131.         {
  132.             return 0;
  133.         }
  134.         int left = _Height(root->_left) + 1;
  135.         int right = _Height(root->_right) + 1;

  136.         return (left > right)?left:right;
  137.     }
  138.     bool _IsBalance(Node* root)
  139.     {
  140.         if (root == NULL)
  141.         {
  142.             return true;
  143.         }
  144.         int left = _Height(root->_left);
  145.         int right = _Height(root->_right);

  146.         int bf = abs(left - right);
  147.         if (bf > 1)
  148.         {
  149.             return false;
  150.         }

  151.         return _IsBalance(root->_left) && _IsBalance(root->_right);
  152.     }
  153.     void _RotateL(Node* parent)
  154.     {
  155.         Node *subR = parent->_right;
  156.         Node *subRL = subR->_left;
  157.         parent->_right = subRL;
  158.         if (subRL)
  159.         {
  160.             subRL->_parent = parent;

  161.         }
  162.         subR->_left = parent;
  163.         subR->_parent = parent->_parent;
  164.         parent->_parent = subR;
  165.         parent = subR;
  166.         //连爸爸:)
  167.         if (parent->_parent == NULL)
  168.         {
  169.             _root = parent;
  170.         }
  171.         else
  172.         {
  173.             if (parent->_parent->_key < parent->_key)
  174.             {
  175.                 parent->_parent->_right = parent;
  176.             }
  177.             else
  178.             {
  179.                 parent->_parent->_left = parent;
  180.             }
  181.         }
  182.     }
  183.     void _RotateR(Node* parent)
  184.     {
  185.         Node *subL = parent->_left;
  186.         Node *subLR = subL->_right;
  187.         parent->_left = subLR;
  188.         if (subLR != NULL)
  189.         {
  190.             subLR->_parent = parent;
  191.         }
  192.         subL->_right = parent;
  193.         subL->_parent = parent->_parent;
  194.         parent->_parent = subL;
  195.         parent = subL;
  196.         if (parent->_parent == NULL)
  197.         {
  198.             _root = parent;
  199.         }
  200.         else
  201.         {
  202.             if (parent->_parent->_key < parent->_key)
  203.             {
  204.                 parent->_parent->_right = parent;
  205.             }
  206.             else
  207.             {
  208.                 parent->_parent->_left = parent;
  209.             }
  210.         }
  211.     }
  212.     void _InOrder(Node*& root)
  213.     {
  214.         if(root == NULL)
  215.         {
  216.             return;
  217.         }
  218.         _InOrder(root->_left);
  219.         cout<<root->_key<<" ";
  220.         _InOrder(root->_right);
  221.     }
  222. protected:
  223.     Node* _root;

  224. };
测试:

  1. #include <iostream>
  2. using namespace std;
  3. #include "RBtree.h"

  4. void test()
  5. {
  6.     RBtree<int, int> rb;
  7.     int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
  8.     for (int i = 0; i < sizeof(a)/sizeof(int); ++i)
  9.     {
  10.         rb.Insert(a[i], i);
  11.     }
  12.     rb.InOrder();
  13.     cout<<rb.IsBalanceTree();
  14. }
  15. int main()
  16. {
  17.     test();
  18.     return 0;
  19. }

跑起来发现是平衡的,比AVL调整起来简单很多。

阅读(1393) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~