STL 改造红黑树 模仿封装set和map

打印 上一主题 下一主题

主题 523|帖子 523|积分 1569

改造红黑树


目录

在初次看STL中实现红黑树的源码时有些不理解,然后自己实验对set以RBTree的方式封装红黑树的迭代器;实现过程发现,这样封装复用程度特别低,也特别冗余,因此才能意会到STL源码实现红黑树复用时的英华.下文将模仿STL的实现方式改造红黑树和封装map和set.
参考的STL版本为SGI30
适配STL迭代器的红黑树

红黑树数据操作的代码实现是旧版本的,新版本在上一篇博客中有详细分析.
本篇主要特点在迭代器适配上
基本结构
  1. #pragma once
  2. #include<assert.h>
  3. #include<iostream>
  4. using std::cout;
  5. using std::endl;
  6. using std::cin;
  7. using std::pair;
  8. using std::make_pair;
  9. using std::string;
  10. namespace test
  11. {
  12.         enum Colour { RED, BLACK };
  13.         template<class T> struct RBTreeNode;
  14.         template<class T, class Ref, typename Ptr> struct __RBTree_iterator;
  15.         template<class K, class T, class keyOfT> class RBTree;
  16. }
复制代码
RBTreeNode
  1.         template<class T> // T为key或pair,T是key或者key-value类型
  2.         struct RBTreeNode
  3.         {
  4.                 RBTreeNode* _left;
  5.                 RBTreeNode* _right;
  6.                 RBTreeNode* _parent;
  7.                 T _data; //data是key或pair
  8.                 Colour _col;
  9.                 RBTreeNode(const T& data)
  10.                         : _left(nullptr)
  11.                         , _right(nullptr)
  12.                         , _parent(nullptr)
  13.                         , _data(data)
  14.                         , _col(RED)
  15.                 {}
  16.         };
复制代码
__RBTree_iterator
  1. template<class T, class Ref, typename Ptr>
  2.         struct __RBTree_iterator
  3.         {
  4.                 typedef RBTreeNode<T> node;
  5.                 node* _node;
  6.                 __RBTree_iterator(node* node)
  7.                         :_node(node)
  8.                 {}
  9.                 //首先,<class T, class Ref, class Ptr>这种写法能够提高复用性和灵活性(实现不同类型的迭代器)等,,库里迭代器都这么写
  10.                 //其次,模板参数中,T用于获取类型,Ref用于返回引用,Ptr用于返回指针
  11.                 using Self           = __RBTree_iterator<T,Ref,Ptr>;            //自己--实例化出下面两种迭代器
  12.                 using iterator       = __RBTree_iterator<T,T&,T*>;              //普通迭代器
  13.                 using const_iterator = __RBTree_iterator<T,const T&,const T*>;  //const迭代器
  14.                 __RBTree_iterator(const iterator& it)
  15.                         :_node(it._node)
  16.                 {}
  17.                 //这个构造函数的作用,
  18.             //a.当迭代器被实例化成iterator时,他就是拷贝构造函数.      __RBTree_iterator<T,T&,T*>
  19.                 //b.当迭代器被实例化成const_iterator时,他是支持隐式类型转换的带参构造函数. __RBTree_iterator<T,const T&,const T*>
  20.                 //这样实现的目的
  21.             // 能够复用普通迭代器,可以通过类型转换后直接实现出const迭代器
  22.                 //Ref为 T& 或 const T&, Ptr为 T* 或 const T*
  23.                 //typedef __RBTree_iterator<T, Ref, Ptr> Self;
  24.                 Ref operator*()
  25.                 {
  26.                         return _node->_data;
  27.                 }
  28.                 Ptr operator->()
  29.                 {
  30.                         return &(_node->_data);
  31.                 }
  32.                 bool operator!=(const Self& x)
  33.                 {
  34.                         return _node != x._node;
  35.                 }
  36.     //前置++
  37.     Self& operator++() {
  38.         //此版本实现迭代器不需要判空,为空说明遍历结束,要么是用户错误使用
  39.         Node* cur = _node;
  40.         //1. 有右子树
  41.         if (cur->_right) {
  42.             //找右子树的最小结点
  43.             Node* rightMin = cur->_right;
  44.             while (rightMin->_left) {
  45.                 rightMin = rightMin->_left;
  46.             }
  47.             _node = rightMin;
  48.         }
  49.         //2. 没有右子树
  50.         else {
  51.             ////1.没有父亲,说明是根
  52.             //Node* parent = cur->_parent;
  53.             //if (parent == nullptr) {
  54.             //    _node == nullptr;
  55.             //}
  56.             ////2.且我是父的左子树,说明父亲是下一个正序值
  57.             //else if (parent->_left == cur) {
  58.             //    _node = parent;
  59.             //}
  60.             ////3.或我是父亲的右子树,说明走完了当前最小分支祖先这棵树.迭代往上
  61.             //else if (parent->_right == cur) {
  62.             //    while (parent && cur != parent->_left) {
  63.             //        cur = parent;
  64.             //        parent = parent->_parent;
  65.             //    }
  66.             //    _node = parent;
  67.             //}
  68.             //else {
  69.             //    asssert(false);
  70.             //}
  71.             //上面3种情况可以合并成一种情况:找最近的不是右孩子的祖父
  72.             Node* parent = cur->_parent;
  73.             while (parent && cur != parent->_left) {
  74.                 cur = parent;
  75.                 parent = parent->_parent;
  76.             }
  77.             _node = parent;
  78.         }
  79.         return *this;
  80.     }
  81.     //后置++
  82.     Self operator++(int) {
  83.         Self tmp(_node);
  84.         operator++();
  85.         return tmp;
  86.     }
  87.     Self& operator--() {
  88.         //将++反过来就是--
  89.         Node* cur = _node;
  90.         //左子树存在,就找最大
  91.         if (cur->_left) {
  92.             Node* leftMax = cur->_left;
  93.             while (leftMax->_right) {
  94.                 leftMax = leftMax->_right;
  95.             }
  96.             _node = leftMax;
  97.         }
  98.         //2. 没有左子树
  99.         else {
  100.             Node* parent = cur->_parent;
  101.             while (parent && cur != parent->_right) {
  102.                 cur = parent;
  103.                 parent = parent->_parent;
  104.             }
  105.             _node = parent;
  106.         }
  107.         return *this;
  108.     }
  109.     Self operator--(int) {
  110.         Self tmp(_node);
  111.         operator--();
  112.         return tmp;
  113.     }
  114.         };
复制代码
RBTree
  1.         //参数K用在find,erase等,虽然K也可以被T取代了,但没必要,K更快
  2.         template<class K, class T, class keyOfT> //库中还有1个compare,先不写了
  3.         class RBTree
  4.         {
  5.         public:
  6.                 typedef RBTreeNode<T> node; //T是key或pair
  7.         public:
  8.                 typedef __RBTree_iterator<T, T&, T*> iterator;
  9.                 typedef __RBTree_iterator<T, const T&, const T*> const_iterator;
  10.                 iterator begin()
  11.                 {
  12.                         node* cur = _root;
  13.                         while (cur && cur->_left)//不能走到空
  14.                         {
  15.                                 cur = cur->_left;
  16.                         }
  17.                         return iterator(cur);//返回中序的第一个结点,最左结点
  18.                 }
  19.                 iterator end() //end是最一个位置的下一个
  20.                 {
  21.                         return iterator(nullptr);//暂时可以这么写
  22.                 }
  23.                 const_iterator begin()const
  24.                 {
  25.                         node* cur = _root;
  26.                         while (cur && cur->_left)
  27.                         {
  28.                                 cur = cur->_left;
  29.                         }
  30.                         return iterator(cur);
  31.                 }
  32.                 const_iterator end() const
  33.                 {
  34.                         return iterator(nullptr);
  35.                 }
  36.         private:
  37.                 node* _root = nullptr;
  38.         public:
  39.                 node* find(const K& key)
  40.                 {
  41.                         keyOfT kot;//kot是个仿函数,根据不同参数返回不同的参数对象
  42.                         node* cur = _root;
  43.                         while (cur)
  44.                         {
  45.                                 if (key < kot(cur->_data)) // -------------------------------------------- 只需要重载一个 '<' 或 '>' 就可以比较大小
  46.                                 {
  47.                                         cur = cur->_left;
  48.                                 }
  49.                                 else if (kot(cur->_data) < key) // --------------------------------------------只需要重载一个 '<' 或 '>' 就可以比较大小
  50.                                 {
  51.                                         cur = cur->_right;
  52.                                 }
  53.                                 else
  54.                                 {
  55.                                         return cur;
  56.                                 }
  57.                         }
  58.                         return nullptr;
  59.                 }
  60.                 pair<iterator, bool> insert(const T& data)
  61.                 {
  62.                         if (!_root)
  63.                         {
  64.                                 _root = new node(data);
  65.                                 _root->_col = BLACK;
  66.                                 return std::make_pair(iterator(_root), true);
  67.                         }
  68.                        
  69.                         keyOfT kot;
  70.                         node* cur = _root;
  71.                         node* parent = nullptr;
  72.                         while (cur)
  73.                         {
  74.                                 if (kot(cur->_data) < kot(data)  ) // --------------------------------------------只需要重载一个 '<' 或 '>' 就可以比较大小
  75.                                 {
  76.                                         parent = cur;
  77.                                         cur = cur->_right;
  78.                                 }
  79.                                 else if (kot(data) < kot(cur->_data)) // --------------------------------------------只需要重载一个 '<' 或 '>' 就可以比较大小
  80.                                 {
  81.                                         parent = cur;
  82.                                         cur = cur->_left;
  83.                                 }
  84.                                 else
  85.                                 {
  86.                                         return std::make_pair(iterator(cur), false);
  87.                                 }
  88.                         }
  89.                         cur = new node(data);
  90.                         if ( kot(parent->_data) < kot(data)) // --------------------------------------------
  91.                         {
  92.                                 parent->_right = cur;
  93.                         }
  94.                         else
  95.                         {
  96.                                 parent->_left = cur;
  97.                         }
  98.                         cur->_parent = parent;
  99.                         //调整/旋转
  100.                         node* newnode = cur;//调整过程cur会发生变化,将cur结点记住 -- 记住原来key的位置
  101.                         while (parent && parent->_col == RED)
  102.                         {
  103.                                 node* g = parent->_parent;
  104.                                 if (parent == g->_right)
  105.                                 {
  106.                                         node* u = g->_left;
  107.                                         if (u && u->_col == RED)
  108.                                         {
  109.                                                 g->_col = RED;
  110.                                                 parent->_col = BLACK;
  111.                                                 u->_col = BLACK;
  112.                                                 cur = g;
  113.                                                 parent = cur->_parent;
  114.                                         }
  115.                                         else
  116.                                         {
  117.                                                 if (cur == parent->_right)
  118.                                                 {
  119.                                                         RotateL(g);
  120.                                                         parent->_col = BLACK;
  121.                                                         g->_col = RED;
  122.                                                 }
  123.                                                 else
  124.                                                 {
  125.                                                         RotateR(parent);
  126.                                                         RotateL(g);
  127.                                                         g->_col = RED;
  128.                                                         cur->_col = BLACK;
  129.                                                 }
  130.                                                 break;
  131.                                         }
  132.                                 }
  133.                                 else
  134.                                 {
  135.                                         node* u = g->_right;
  136.                                         if (u && u->_col == RED)
  137.                                         {
  138.                                                 g->_col = RED;
  139.                                                 parent->_col = BLACK;
  140.                                                 u->_col = BLACK;
  141.                                                 cur = g;
  142.                                                 parent = cur->_parent;
  143.                                         }
  144.                                         else
  145.                                         {
  146.                                                 if (cur == parent->_left)
  147.                                                 {
  148.                                                         RotateR(g);
  149.                                                         parent->_col = BLACK;
  150.                                                         g->_col = RED;
  151.                                                 }
  152.                                                 else
  153.                                                 {
  154.                                                         RotateL(parent);
  155.                                                         RotateR(g);
  156.                                                         g->_col = RED;
  157.                                                         cur->_col = BLACK;
  158.                                                 }
  159.                                                 break;
  160.                                         }
  161.                                 }
  162.                         }
  163.                         _root->_col = BLACK;
  164.                         return std::make_pair(iterator(newnode), true);
  165.                 }
  166.         public:
  167.                 void InOrderTraversal()
  168.                 {
  169.                         _InOrderTraversal(_root);
  170.                 }
  171.                 bool isBalanceTree()
  172.                 {
  173.                         //需要判断3个规则
  174.                         //1.根为黑
  175.                         if (_root && _root->_col == RED)
  176.                         {
  177.                                 cout << "错误:根是红色" << endl;
  178.                                 return false;
  179.                         }
  180.                         //2.不能有连续得红
  181.                         //3.黑同
  182.                         int benchmark = 0;
  183.                         node* cur = _root;
  184.                         while (cur)
  185.                         {
  186.                                 if (cur->_col == BLACK)
  187.                                 {
  188.                                         ++benchmark;
  189.                                 }
  190.                                 cur = cur->_left;
  191.                         }
  192.                         return _check(_root, 0, benchmark);
  193.                 }
  194.                 int Height()
  195.                 {
  196.                         return _Height(_root);
  197.                 }
  198.                 ~RBTree()
  199.                 {
  200.                         Destroy(_root);
  201.                 }
  202.         private:
  203.                 void Destroy(node*& root)
  204.                 {
  205.                         if (!root)
  206.                         {
  207.                                 return;
  208.                         }
  209.                         Destroy(root->_left);
  210.                         Destroy(root->_right);
  211.                         delete root;
  212.                         root = nullptr;
  213.                 }
  214.                 bool _check(node* root, int blackNum, int benchmark)
  215.                 {
  216.                         keyOfT kot;
  217.                         if (!root) //
  218.                         {
  219.                                 if (blackNum != benchmark)
  220.                                 {
  221.                                         cout << "错误:存在不同路径的黑色结点数量不相同" << endl;
  222.                                         return false;
  223.                                 }
  224.                                 return true;
  225.                         }
  226.                         if (root->_col == BLACK)
  227.                         {
  228.                                 ++blackNum;
  229.                         }
  230.                         if (root->_col == RED && root->_parent->_col == RED)
  231.                         {
  232.                                 cout << kot(root->_data) << " 错误,与父节点同时为红色"; // --------------------------------------------
  233.                                 return false;
  234.                         }
  235.                         return _check(root->_left, blackNum, benchmark) && _check(root->_right, blackNum, benchmark);
  236.                 }
  237.                 int _Height(node* root)
  238.                 {
  239.                         if (!root)
  240.                         {
  241.                                 return 0;
  242.                         }
  243.                         int leftH = _Height(root->_left);
  244.                         int rightH = _Height(root->_right);
  245.                         return leftH > rightH ? leftH + 1 : rightH + 1;
  246.                 }
  247.                 void _InOrderTraversal(node* root)
  248.                 {
  249.                         keyOfT kot;
  250.                         if (root == nullptr)
  251.                         {
  252.                                 return;
  253.                         }
  254.                         _InOrderTraversal(root->_left);
  255.                         cout << kot(root->_data) << " "; // --------------------------------------------
  256.                         _InOrderTraversal(root->_right);
  257.                 }
  258.                 void RotateL(node* parent)
  259.                 {
  260.                         node* subR = parent->_right;
  261.                         node* subRL = subR->_left;
  262.                         parent->_right = subRL;
  263.                         if (subRL)
  264.                         {
  265.                                 subRL->_parent = parent;
  266.                         }
  267.                         node* pparent = parent->_parent;
  268.                         subR->_left = parent;
  269.                         parent->_parent = subR;
  270.                         if (!pparent)
  271.                         {
  272.                                 _root = subR;
  273.                                 _root->_parent = nullptr;
  274.                         }
  275.                         else
  276.                         {
  277.                                 if (pparent->_left == parent)
  278.                                 {
  279.                                         pparent->_left = subR;
  280.                                 }
  281.                                 else
  282.                                 {
  283.                                         pparent->_right = subR;
  284.                                 }
  285.                                 subR->_parent = pparent;
  286.                         }
  287.                 }
  288.                 void RotateR(node* parent)
  289.                 {
  290.                         node* subL = parent->_left;
  291.                         node* subLR = subL->_right;
  292.                         parent->_left = subLR;
  293.                         if (subLR)
  294.                         {
  295.                                 subLR->_parent = parent;
  296.                         }
  297.                         node* pparent = parent->_parent;
  298.                         subL->_right = parent;
  299.                         parent->_parent = subL;
  300.                         if (parent == _root)
  301.                         {
  302.                                 _root = subL;
  303.                                 _root->_parent = nullptr;
  304.                         }
  305.                         else
  306.                         {
  307.                                 if (pparent->_left == parent)
  308.                                 {
  309.                                         pparent->_left = subL;
  310.                                 }
  311.                                 else
  312.                                 {
  313.                                         pparent->_right = subL;
  314.                                 }
  315.                                 subL->_parent = pparent;
  316.                         }
  317.                 }
  318.         };
复制代码
完备代码
  1. #pragma once#include#includeusing std::cout;using std::endl;using std::cin;using std::pair;using std::make_pair;using std::string;namespace test{        //与库中的RBT差异        /**         *         * 库中另有结点数量 count         *         * 库中RBT是带头结点哨兵卫的,头结点的左是中序第一个(最小结点),右节点是中序的末了一个(最大结点),         * 哨兵卫的parent指向根节点,根节点的parent指向哨兵卫         *         * 库中的begin直接取head的left -- 函数:leftmost() //最左结点         * 库中的end 是head的right -- 不是rightmost,rightmost是最右结点,end是最右结点的下一个         * 库中的end 需要判断一下,防止只有左子树的歪脖子树时,end == head->right,死循环         *         * 和库的区别就是end,库的end能走回到head,我的不能,只能走到空就没了d         *         */        enum Colour { RED, BLACK };        template //T是什么,为什么只传T? T为key或pair,T是key或者key-value范例        struct RBTreeNode        {                RBTreeNode* _left;                RBTreeNode* _right;                RBTreeNode* _parent;                T _data; //data是key或pair                Colour _col;                RBTreeNode(const T& data)                        : _left(nullptr)                        , _right(nullptr)                        , _parent(nullptr)                        , _data(data)                        , _col(RED)                {}        };        //const_iterator        //iterator        template<class T, class Ref, typename Ptr>
  2.         struct __RBTree_iterator
  3.         {
  4.                 typedef RBTreeNode<T> node;
  5.                 node* _node;
  6.                 __RBTree_iterator(node* node)
  7.                         :_node(node)
  8.                 {}
  9.                 //首先,<class T, class Ref, class Ptr>这种写法能够提高复用性和灵活性(实现不同类型的迭代器)等,,库里迭代器都这么写
  10.                 //其次,模板参数中,T用于获取类型,Ref用于返回引用,Ptr用于返回指针
  11.                 using Self           = __RBTree_iterator<T,Ref,Ptr>;            //自己--实例化出下面两种迭代器
  12.                 using iterator       = __RBTree_iterator<T,T&,T*>;              //普通迭代器
  13.                 using const_iterator = __RBTree_iterator<T,const T&,const T*>;  //const迭代器
  14.                 __RBTree_iterator(const iterator& it)
  15.                         :_node(it._node)
  16.                 {}
  17.                 //这个构造函数的作用,
  18.             //a.当迭代器被实例化成iterator时,他就是拷贝构造函数.      __RBTree_iterator<T,T&,T*>
  19.                 //b.当迭代器被实例化成const_iterator时,他是支持隐式类型转换的带参构造函数. __RBTree_iterator<T,const T&,const T*>
  20.                 //这样实现的目的
  21.             // 能够复用普通迭代器,可以通过类型转换后直接实现出const迭代器
  22.                 //Ref为 T& 或 const T&, Ptr为 T* 或 const T*
  23.                 //typedef __RBTree_iterator<T, Ref, Ptr> Self;
  24.                 Ref operator*()
  25.                 {
  26.                         return _node->_data;
  27.                 }
  28.                 Ptr operator->()
  29.                 {
  30.                         return &(_node->_data);
  31.                 }
  32.                 bool operator!=(const Self& x)
  33.                 {
  34.                         return _node != x._node;
  35.                 }
  36.     //前置++
  37.     Self& operator++() {
  38.         //此版本实现迭代器不需要判空,为空说明遍历结束,要么是用户错误使用
  39.         Node* cur = _node;
  40.         //1. 有右子树
  41.         if (cur->_right) {
  42.             //找右子树的最小结点
  43.             Node* rightMin = cur->_right;
  44.             while (rightMin->_left) {
  45.                 rightMin = rightMin->_left;
  46.             }
  47.             _node = rightMin;
  48.         }
  49.         //2. 没有右子树
  50.         else {
  51.             ////1.没有父亲,说明是根
  52.             //Node* parent = cur->_parent;
  53.             //if (parent == nullptr) {
  54.             //    _node == nullptr;
  55.             //}
  56.             ////2.且我是父的左子树,说明父亲是下一个正序值
  57.             //else if (parent->_left == cur) {
  58.             //    _node = parent;
  59.             //}
  60.             ////3.或我是父亲的右子树,说明走完了当前最小分支祖先这棵树.迭代往上
  61.             //else if (parent->_right == cur) {
  62.             //    while (parent && cur != parent->_left) {
  63.             //        cur = parent;
  64.             //        parent = parent->_parent;
  65.             //    }
  66.             //    _node = parent;
  67.             //}
  68.             //else {
  69.             //    asssert(false);
  70.             //}
  71.             //上面3种情况可以合并成一种情况:找最近的不是右孩子的祖父
  72.             Node* parent = cur->_parent;
  73.             while (parent && cur != parent->_left) {
  74.                 cur = parent;
  75.                 parent = parent->_parent;
  76.             }
  77.             _node = parent;
  78.         }
  79.         return *this;
  80.     }
  81.     //后置++
  82.     Self operator++(int) {
  83.         Self tmp(_node);
  84.         operator++();
  85.         return tmp;
  86.     }
  87.     Self& operator--() {
  88.         //将++反过来就是--
  89.         Node* cur = _node;
  90.         //左子树存在,就找最大
  91.         if (cur->_left) {
  92.             Node* leftMax = cur->_left;
  93.             while (leftMax->_right) {
  94.                 leftMax = leftMax->_right;
  95.             }
  96.             _node = leftMax;
  97.         }
  98.         //2. 没有左子树
  99.         else {
  100.             Node* parent = cur->_parent;
  101.             while (parent && cur != parent->_right) {
  102.                 cur = parent;
  103.                 parent = parent->_parent;
  104.             }
  105.             _node = parent;
  106.         }
  107.         return *this;
  108.     }
  109.     Self operator--(int) {
  110.         Self tmp(_node);
  111.         operator--();
  112.         return tmp;
  113.     }
  114.         };        //参数K用在find,erase等,虽然K也可以被T取代了,但没必要,K更快
  115.         template<class K, class T, class keyOfT> //库中还有1个compare,先不写了
  116.         class RBTree
  117.         {
  118.         public:
  119.                 typedef RBTreeNode<T> node; //T是key或pair
  120.         public:
  121.                 typedef __RBTree_iterator<T, T&, T*> iterator;
  122.                 typedef __RBTree_iterator<T, const T&, const T*> const_iterator;
  123.                 iterator begin()
  124.                 {
  125.                         node* cur = _root;
  126.                         while (cur && cur->_left)//不能走到空
  127.                         {
  128.                                 cur = cur->_left;
  129.                         }
  130.                         return iterator(cur);//返回中序的第一个结点,最左结点
  131.                 }
  132.                 iterator end() //end是最一个位置的下一个
  133.                 {
  134.                         return iterator(nullptr);//暂时可以这么写
  135.                 }
  136.                 const_iterator begin()const
  137.                 {
  138.                         node* cur = _root;
  139.                         while (cur && cur->_left)
  140.                         {
  141.                                 cur = cur->_left;
  142.                         }
  143.                         return iterator(cur);
  144.                 }
  145.                 const_iterator end() const
  146.                 {
  147.                         return iterator(nullptr);
  148.                 }
  149.         private:
  150.                 node* _root = nullptr;
  151.         public:
  152.                 node* find(const K& key)
  153.                 {
  154.                         keyOfT kot;//kot是个仿函数,根据不同参数返回不同的参数对象
  155.                         node* cur = _root;
  156.                         while (cur)
  157.                         {
  158.                                 if (key < kot(cur->_data)) // -------------------------------------------- 只需要重载一个 '<' 或 '>' 就可以比较大小
  159.                                 {
  160.                                         cur = cur->_left;
  161.                                 }
  162.                                 else if (kot(cur->_data) < key) // --------------------------------------------只需要重载一个 '<' 或 '>' 就可以比较大小
  163.                                 {
  164.                                         cur = cur->_right;
  165.                                 }
  166.                                 else
  167.                                 {
  168.                                         return cur;
  169.                                 }
  170.                         }
  171.                         return nullptr;
  172.                 }
  173.                 pair<iterator, bool> insert(const T& data)
  174.                 {
  175.                         if (!_root)
  176.                         {
  177.                                 _root = new node(data);
  178.                                 _root->_col = BLACK;
  179.                                 return std::make_pair(iterator(_root), true);
  180.                         }
  181.                        
  182.                         keyOfT kot;
  183.                         node* cur = _root;
  184.                         node* parent = nullptr;
  185.                         while (cur)
  186.                         {
  187.                                 if (kot(cur->_data) < kot(data)  ) // --------------------------------------------只需要重载一个 '<' 或 '>' 就可以比较大小
  188.                                 {
  189.                                         parent = cur;
  190.                                         cur = cur->_right;
  191.                                 }
  192.                                 else if (kot(data) < kot(cur->_data)) // --------------------------------------------只需要重载一个 '<' 或 '>' 就可以比较大小
  193.                                 {
  194.                                         parent = cur;
  195.                                         cur = cur->_left;
  196.                                 }
  197.                                 else
  198.                                 {
  199.                                         return std::make_pair(iterator(cur), false);
  200.                                 }
  201.                         }
  202.                         cur = new node(data);
  203.                         if ( kot(parent->_data) < kot(data)) // --------------------------------------------
  204.                         {
  205.                                 parent->_right = cur;
  206.                         }
  207.                         else
  208.                         {
  209.                                 parent->_left = cur;
  210.                         }
  211.                         cur->_parent = parent;
  212.                         //调整/旋转
  213.                         node* newnode = cur;//调整过程cur会发生变化,将cur结点记住 -- 记住原来key的位置
  214.                         while (parent && parent->_col == RED)
  215.                         {
  216.                                 node* g = parent->_parent;
  217.                                 if (parent == g->_right)
  218.                                 {
  219.                                         node* u = g->_left;
  220.                                         if (u && u->_col == RED)
  221.                                         {
  222.                                                 g->_col = RED;
  223.                                                 parent->_col = BLACK;
  224.                                                 u->_col = BLACK;
  225.                                                 cur = g;
  226.                                                 parent = cur->_parent;
  227.                                         }
  228.                                         else
  229.                                         {
  230.                                                 if (cur == parent->_right)
  231.                                                 {
  232.                                                         RotateL(g);
  233.                                                         parent->_col = BLACK;
  234.                                                         g->_col = RED;
  235.                                                 }
  236.                                                 else
  237.                                                 {
  238.                                                         RotateR(parent);
  239.                                                         RotateL(g);
  240.                                                         g->_col = RED;
  241.                                                         cur->_col = BLACK;
  242.                                                 }
  243.                                                 break;
  244.                                         }
  245.                                 }
  246.                                 else
  247.                                 {
  248.                                         node* u = g->_right;
  249.                                         if (u && u->_col == RED)
  250.                                         {
  251.                                                 g->_col = RED;
  252.                                                 parent->_col = BLACK;
  253.                                                 u->_col = BLACK;
  254.                                                 cur = g;
  255.                                                 parent = cur->_parent;
  256.                                         }
  257.                                         else
  258.                                         {
  259.                                                 if (cur == parent->_left)
  260.                                                 {
  261.                                                         RotateR(g);
  262.                                                         parent->_col = BLACK;
  263.                                                         g->_col = RED;
  264.                                                 }
  265.                                                 else
  266.                                                 {
  267.                                                         RotateL(parent);
  268.                                                         RotateR(g);
  269.                                                         g->_col = RED;
  270.                                                         cur->_col = BLACK;
  271.                                                 }
  272.                                                 break;
  273.                                         }
  274.                                 }
  275.                         }
  276.                         _root->_col = BLACK;
  277.                         return std::make_pair(iterator(newnode), true);
  278.                 }
  279.         public:
  280.                 void InOrderTraversal()
  281.                 {
  282.                         _InOrderTraversal(_root);
  283.                 }
  284.                 bool isBalanceTree()
  285.                 {
  286.                         //需要判断3个规则
  287.                         //1.根为黑
  288.                         if (_root && _root->_col == RED)
  289.                         {
  290.                                 cout << "错误:根是红色" << endl;
  291.                                 return false;
  292.                         }
  293.                         //2.不能有连续得红
  294.                         //3.黑同
  295.                         int benchmark = 0;
  296.                         node* cur = _root;
  297.                         while (cur)
  298.                         {
  299.                                 if (cur->_col == BLACK)
  300.                                 {
  301.                                         ++benchmark;
  302.                                 }
  303.                                 cur = cur->_left;
  304.                         }
  305.                         return _check(_root, 0, benchmark);
  306.                 }
  307.                 int Height()
  308.                 {
  309.                         return _Height(_root);
  310.                 }
  311.                 ~RBTree()
  312.                 {
  313.                         Destroy(_root);
  314.                 }
  315.         private:
  316.                 void Destroy(node*& root)
  317.                 {
  318.                         if (!root)
  319.                         {
  320.                                 return;
  321.                         }
  322.                         Destroy(root->_left);
  323.                         Destroy(root->_right);
  324.                         delete root;
  325.                         root = nullptr;
  326.                 }
  327.                 bool _check(node* root, int blackNum, int benchmark)
  328.                 {
  329.                         keyOfT kot;
  330.                         if (!root) //
  331.                         {
  332.                                 if (blackNum != benchmark)
  333.                                 {
  334.                                         cout << "错误:存在不同路径的黑色结点数量不相同" << endl;
  335.                                         return false;
  336.                                 }
  337.                                 return true;
  338.                         }
  339.                         if (root->_col == BLACK)
  340.                         {
  341.                                 ++blackNum;
  342.                         }
  343.                         if (root->_col == RED && root->_parent->_col == RED)
  344.                         {
  345.                                 cout << kot(root->_data) << " 错误,与父节点同时为红色"; // --------------------------------------------
  346.                                 return false;
  347.                         }
  348.                         return _check(root->_left, blackNum, benchmark) && _check(root->_right, blackNum, benchmark);
  349.                 }
  350.                 int _Height(node* root)
  351.                 {
  352.                         if (!root)
  353.                         {
  354.                                 return 0;
  355.                         }
  356.                         int leftH = _Height(root->_left);
  357.                         int rightH = _Height(root->_right);
  358.                         return leftH > rightH ? leftH + 1 : rightH + 1;
  359.                 }
  360.                 void _InOrderTraversal(node* root)
  361.                 {
  362.                         keyOfT kot;
  363.                         if (root == nullptr)
  364.                         {
  365.                                 return;
  366.                         }
  367.                         _InOrderTraversal(root->_left);
  368.                         cout << kot(root->_data) << " "; // --------------------------------------------
  369.                         _InOrderTraversal(root->_right);
  370.                 }
  371.                 void RotateL(node* parent)
  372.                 {
  373.                         node* subR = parent->_right;
  374.                         node* subRL = subR->_left;
  375.                         parent->_right = subRL;
  376.                         if (subRL)
  377.                         {
  378.                                 subRL->_parent = parent;
  379.                         }
  380.                         node* pparent = parent->_parent;
  381.                         subR->_left = parent;
  382.                         parent->_parent = subR;
  383.                         if (!pparent)
  384.                         {
  385.                                 _root = subR;
  386.                                 _root->_parent = nullptr;
  387.                         }
  388.                         else
  389.                         {
  390.                                 if (pparent->_left == parent)
  391.                                 {
  392.                                         pparent->_left = subR;
  393.                                 }
  394.                                 else
  395.                                 {
  396.                                         pparent->_right = subR;
  397.                                 }
  398.                                 subR->_parent = pparent;
  399.                         }
  400.                 }
  401.                 void RotateR(node* parent)
  402.                 {
  403.                         node* subL = parent->_left;
  404.                         node* subLR = subL->_right;
  405.                         parent->_left = subLR;
  406.                         if (subLR)
  407.                         {
  408.                                 subLR->_parent = parent;
  409.                         }
  410.                         node* pparent = parent->_parent;
  411.                         subL->_right = parent;
  412.                         parent->_parent = subL;
  413.                         if (parent == _root)
  414.                         {
  415.                                 _root = subL;
  416.                                 _root->_parent = nullptr;
  417.                         }
  418.                         else
  419.                         {
  420.                                 if (pparent->_left == parent)
  421.                                 {
  422.                                         pparent->_left = subL;
  423.                                 }
  424.                                 else
  425.                                 {
  426.                                         pparent->_right = subL;
  427.                                 }
  428.                                 subL->_parent = pparent;
  429.                         }
  430.                 }
  431.         };}
复制代码
封装的set

红黑树改造完成后,封装set和map就比较简单了,基本上都是套用红黑树的功能,特别方便,这也体会到了实现STL的大佬们的厉害.
[code]#pragma once#include"RBT.hpp"namespace test{        template        class set        {        private:                struct setKeyOfT//取出RBT的范例T中的key                {                        const K& operator()(const K& key)                         {                                return key;                         }                };        private:                RBTree< K, K, setKeyOfT>_t;        public:                //利用类域的要加typename,以防和静态变量辩论                typedef typename RBTree::const_iterator iterator; //寻常迭代器                typedef typename RBTree::const_iterator const_iterator; //const迭代器                iterator begin()                {                        return _t.begin(); //begin是寻常迭代器,返回值是const,发生隐式范例转换(单参数)                        //如果有相应的构造函数,则支持隐式范例转换 ,但此时迭代器没有参数为迭代器的构造函数,需要添加                }                iterator end()                {                        return _t.end();                }                const_iterator begin()const                {                        return _t.begin();                }                const_iterator end()const                {                        return _t.end();                }                        public:                pair insert(const K& key)                {                        return _t.insert(key);                }        };        void test_mySet1()        {                test::set s;                s.insert(2);                s.insert(1);                s.insert(3);                set::iterator it = s.begin();                //while (it!=s.end())                //{                //        cout
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

九天猎人

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表