目录
后记:●由于作者水平有限,文章难免存在谬误之处,敬请读者斧正,俚语成篇,恳望指教!
<红黑树模拟实现STL中的map与set>
GIF:
1.红黑树的迭代器
迭代器的好处是可以方便遍历,是数据结构的底层实现与用户透明。如果想要给红黑树增加迭代器,需要考虑以前问题: begin()与end()STL明确规定,begin()与end()代表的是一段前闭后开的区间,而对红黑树进行中序遍历后,可以得到一个有序的序列,因此:begin()可以放在红黑树中最小节点(即最左侧节点)的位置,end()放在最大节点(最右侧节点)的下一个位置,关键是最大节点的下一个位置在哪块?能否给成nullptr呢?答案是行不通的,因为对end()位置的迭代器进行--操作,必须要能找最后一个元素,此处就不行,因此最好的方式是将end()放在头结点的位置:图片源于网络:(这里仅做学习用途,如有侵权,请联系删除!)![]()
- operator++()与operator--()
// 找迭代器的下一个节点,下一个节点肯定比其大 void Increasement() { //分两种情况讨论:_pNode的右子树存在和不存在 // 右子树存在 if (_pNode->_pRight) { // 右子树中最小的节点,即右子树中最左侧节点 _pNode = _pNode->_pRight; while (_pNode->_pLeft) _pNode = _pNode->_pLeft; } else { // 右子树不存在,向上查找,直到_pNode != pParent->right PNode pParent = _pNode->_pParent; while (pParent->_pRight == _pNode) { _pNode = pParent; pParent = _pNode->_pParent; } // 特殊情况:根节点没有右子树 if (_pNode->_pRight != pParent) _pNode = pParent; } } // 获取迭代器指向节点的前一个节点 void Decreasement() { //分三种情况讨论:_pNode 在head的位置,_pNode 左子树存在,_pNode 左子树不存在 // 1. _pNode 在head的位置,--应该将_pNode放在红黑树中最大节点的位置 if (_pNode->_pParent->_pParent == _pNode && _pNode->_color == RED) _pNode = _pNode->_pRight; else if (_pNode->_pLeft) { // 2. _pNode的左子树存在,在左子树中找最大的节点,即左子树中最右侧节点 _pNode = _pNode->_pLeft; while (_pNode->_pRight) _pNode = _pNode->_pRight; } else { // _pNode的左子树不存在,只能向上找 PNode pParent = _pNode->_pParent; while (_pNode == pParent->_pLeft) { _pNode = pParent; pParent = _pNode->_pParent; } _pNode = pParent; } }
2. 改造红黑树
// 因为关联式容器中存储的是<key, value>的键值对,因此 // k为key的类型, // ValueType: 如果是map,则为pair<K, V>; 如果是set,则为k // KeyOfValue: 通过value来获取key的一个仿函数类 template<class K, class ValueType, class KeyOfValue> class RBTree { typedef RBTreeNode<ValueType> Node; typedef Node* PNode; public: typedef RBTreeIterator<ValueType, ValueType*, ValueType&> Iterator; public: RBTree(); ~RBTree() / // Iterator Iterator Begin() { return Iterator(_pHead->_pLeft); } Iterator End() { return Iterator(_pHead); } // // Modify pair<Iterator, bool> Insert(const ValueType& data) { // 插入节点并进行调整 // 参考上文... return make_pair(Iterator(pNewNode), true); } // 将红黑树中的节点清空 void Clear(); Iterator Find(const K& key); // // capacity size_t Size()const; bool Empty()const; // …… private: PNode _pHead; size_t _size; // 红黑树中有效节点的个数 };
3.map的模拟实现
map的底层结构就是红黑树,因此在map中直接封装一棵红黑树,然后将其接口包装下即可namespace bite { template<class K, class V> class map { typedef pair<K, V> ValueType; // 作用:将value中的key提取出来 struct KeyOfValue { const K& operator()(const ValueType& v) { return v.first; } }; typedef RBTree<K, ValueType, KeyOfValue> RBTree; public: typedef typename RBTree::Iterator iterator; public: map() {} / // Iterator iterator begin() { return _t.Begin(); } iterator end() { return _t.End(); } / // Capacity size_t size()const { return _t.Size(); } bool empty()const { return _t.Empty(); } / // Acess V& operator[](const K& key) { return (*(_t.Insert(ValueType(key, V()))).first).second; } const V& operator[](const K& key)const; // modify pair<iterator, bool> insert(const ValueType& data) { return _t.Insert(data); } void clear() { _t.Clear(); } iterator find(const K& key) { return _t.Find(key); } private: RBTree _t; }; }
附用红黑树:
(1)map.h:
#pragma once #include"RBTree.h" namespace my { template<class K, class V> class map { struct MapKeyOfT { const K& operator()(const pair<K, V>& kv) { return kv.first; } }; public: typedef typename RBTree<K, pair<K, V>, MapKeyOfT>::iterator iterator; typedef typename RBTree<K, pair<K, V>, MapKeyOfT>::const_iterator const_iterator; iterator begin() { return _t.Begin(); } iterator end() { return _t.End(); } pair<iterator, bool> insert(const pair<K, V>& kv) { return _t.Insert(kv); } iterator find(const K& key) { return _t.Find(key); } V& operator[](const K& key) { pair<iterator, bool> ret = insert(make_pair(key, V())); return ret.first->second; } private: RBTree<K, pair<K, V>, MapKeyOfT> _t; }; void test_map1() { map<string, int> m; m.insert(make_pair("111", 1)); m.insert(make_pair("555", 5)); m.insert(make_pair("333", 3)); m.insert(make_pair("222", 2)); //迭代器遍历 map<string, int>::iterator it = m.begin(); while (it != m.end()) { cout << it->first << ":" << it->second << endl; ++it; } cout << endl; //范围for遍历 for (auto& kv : m) { cout << kv.first << ":" << kv.second << endl; } cout << endl; } void test_map2() { string arr[] = { "ƻ", "", "ƻ", "", "ƻ", "ƻ", "", "ƻ", "㽶", "ƻ", "㽶" }; //迭代器遍历 map<string, int> countMap; for (auto& str : arr) { countMap[str]++; } //范围for遍历 for (const auto& kv : countMap) { cout << kv.first << ":" << kv.second << endl; } } void test_map3() { map<string, string> dict; dict["insert"]; dict["insert"] = ""; dict["left"] = ""; } }
(2)RBTree.h:
#pragma once #include<iostream> #include<vector> #include<queue> #include<cassert> using namespace std; enum Colour { RED, BLACK, }; template<class T> struct RBTreeNode { RBTreeNode<T>* _left; RBTreeNode<T>* _right; RBTreeNode<T>* _parent; T _data; Colour _col; RBTreeNode(const T& data) :_data(data) , _left(nullptr) , _right(nullptr) , _parent(nullptr) , _col(RED) {} }; //T决定红黑树存什么数据 //set RBTree<K,K> //map RBTree<K,pair<K,V>> //KeyOfT -> 支持取出T对象中key的仿函数 template<class T, class Ref, class Ptr> struct __RBTreeIterator { typedef RBTreeNode<T> Node; typedef __RBTreeIterator<T, Ref, Ptr> Self; Node* _node; __RBTreeIterator(Node* node) :_node(node) {} //运算符*重载 Ref operator*() { return _node->_data; } //运算符->重载 Ptr operator->() { return &_node->_data; } //重载 前置++ Self& operator++() { if (_node->_right == nullptr) { // 找祖先里面,孩子是父亲左的那个 Node* cur = _node; Node* parent = cur->_parent; while (parent && parent->_right == cur) { cur = cur->_parent; parent = parent->_parent; } _node = parent; } else { // 右子树的最左节点 Node* subLeft = _node->_right; while (subLeft->_left) { subLeft = subLeft->_left; } _node = subLeft; } return *this; } //重载 后置++ Self operator++(int) { Self tmp(*this); ++(*this); return tmp; } //重载 前置-- Self& operator--() { if (_node->_left == nullptr) { // 找祖先里面,孩子是父亲 Node* cur = _node; Node* parent = cur->_parent; while (parent && cur == parent->_left) { cur = cur->_parent; parent = parent->_parent; } _node = parent; } else { // 左子树的最右节点 Node* subRight = _node->_left; while (subRight->_right) { subRight = subRight->_right; } _node = subRight; } return *this; } //重载 后置-- Self operator--(int) { Self tmp(*this); --(*this); return tmp; } //重载 != bool operator!=(const Self& s) const { return _node != s._node; } //重载 == bool operator==(const Self& s) const { return _node == s->_node; } }; template<class K, class T, class KeyOfT> class RBTree { typedef RBTreeNode<T> Node; //默认为私有 public: typedef __RBTreeIterator<T, T&, T*> iterator; typedef __RBTreeIterator<T, const T&, const T*> const_iterator; // 构造 拷贝构造 赋值 和析构 跟搜索树实现方式是一样的 iterator Begin() { Node* subLeft = _root; while (subLeft && subLeft->_left) { subLeft = subLeft->_left; } return iterator(subLeft); } iterator End() { return iterator(nullptr); } const_iterator Begin() const { Node* subLeft = _root; while (subLeft && subLeft->_left) { subLeft = subLeft->_left; } return const_iterator(subLeft); } const_iterator End() const { return const_iterator(nullptr); } //insert数据 pair<iterator, bool> Insert(const T& data) { // 1、搜索树的规则插入 // 2、看是否违反平衡规则,如果违反就需要处理:旋转 if (_root == nullptr) { _root = new Node(data); _root->_col = BLACK; return make_pair(iterator(_root), true); } KeyOfT kot; Node* parent = nullptr; Node* cur = _root; while (cur) { if (kot(cur->_data) < kot(data)) { parent = cur; cur = cur->_right; } else if (kot(cur->_data) > kot(data)) { parent = cur; cur = cur->_left; } else { return make_pair(iterator(cur), true); } } cur = new Node(data); Node* newnode = cur; cur->_col = RED; if (kot(parent->_data) < kot(data)) { parent->_right = cur; } else { parent->_left = cur; } cur->_parent = parent; // 存在连续红色节点 while (parent && parent->_col == RED) { Node* grandfater = parent->_parent; assert(grandfater); if (grandfater->_left == parent) { Node* uncle = grandfater->_right; // 情况一: if (uncle && uncle->_col == RED) // 叔叔存在且为红 { // 变色 parent->_col = uncle->_col = BLACK; grandfater->_col = RED; // 继续往上处理 cur = grandfater; parent = cur->_parent; } else // 叔叔不存在 或者 叔叔存在且为黑 { if (cur == parent->_left) // 单旋 { // g // p // c RotateR(grandfater); parent->_col = BLACK; grandfater->_col = RED; } else // 双旋 { // g // p // c RotateL(parent); RotateR(grandfater); cur->_col = BLACK; grandfater->_col = RED; } break; } } else //(grandfater->_right == parent) { Node* uncle = grandfater->_left; // 情况一: if (uncle && uncle->_col == RED) { // 变色 parent->_col = uncle->_col = BLACK; grandfater->_col = RED; // 继续往上处理 cur = grandfater; parent = cur->_parent; } else { if (cur == parent->_right) { // g // p // c RotateL(grandfater); parent->_col = BLACK; grandfater->_col = RED; } else // 双旋 { // g // p // c RotateR(parent); RotateL(grandfater); cur->_col = BLACK; grandfater->_col = RED; } break; } } } _root->_col = BLACK; return make_pair(iterator(newnode), true); } //左旋 void RotateL(Node* parent) { Node* subR = parent->_right; Node* subRL = subR->_left; parent->_right = subRL; if (subRL) subRL->_parent = parent; Node* ppNode = parent->_parent; subR->_left = parent; parent->_parent = subR; if (parent == _root) { _root = subR; _root->_parent = nullptr; } else { if (parent == ppNode->_left) { ppNode->_left = subR; } else { ppNode->_right = subR; } subR->_parent = ppNode; } } //右旋 void RotateR(Node* parent) { Node* subL = parent->_left; Node* subLR = subL->_right; parent->_left = subLR; if (subLR) subLR->_parent = parent; Node* ppNode = parent->_parent; subL->_right = parent; parent->_parent = subL; if (parent == _root) { _root = subL; _root->_parent = nullptr; } else { if (ppNode->_left == parent) { ppNode->_left = subL; } else { ppNode->_right = subL; } subL->_parent = ppNode; } } iterator Find(const K& key) { Node* cur = _root; KeyOfT kot; while (cur) { if (kot(cur->_data) < key) { cur = cur->_right; } else if (kot(cur->_data) > key) { cur = cur->_left; } else { return iterator(cur); } } return End(); } private: Node* _root = nullptr; }; 层序遍历 //vector<vector<int>> levelOrder() //{ // cout << "层序遍历:" << endl; // vector<vector<int>> vv; // if (_root == nullptr) // return vv; // // queue<Node*> q; // int levelSize = 1; // q.push(_root); // // while (!q.empty()) // { // // // levelSize控制一层一层出 // vector<int> levelV; // while (levelSize--) // { // Node* front = q.front(); // q.pop(); // levelV.push_back(front->_kv.first); // if (front->_left) // q.push(front->_left); // // if (front->_right) // q.push(front->_right); // } // vv.push_back(levelV); // for (auto e : levelV) // { // cout << e << " "; // } // cout << endl; // // // 上一层出完,下一层就都进队列 // levelSize = q.size(); // } // // return vv; //} // // 求最长路径 //int _maxHeight(Node* root) //{ // if (root == nullptr) // return 0; // // int lh = _maxHeight(root->_left); // int rh = _maxHeight(root->_right); // // return lh > rh ? lh + 1 : rh + 1; //} 求最短路径 //int _minHeight(Node* root) //{ // if (root == nullptr) // return 0; // // int lh = _minHeight(root->_left); // int rh = _minHeight(root->_right); // // return lh < rh ? lh + 1 : rh + 1; //} 1.中序遍历(递归版) //void _InOrder(Node* root) //{ // if (root == nullptr) // return; // // _InOrder(root->_left); // cout << root->_kv.first << " "; // _InOrder(root->_right); //} 2.前序遍历(递归版) //void _PrevOrder(Node* root) //{ // if (root == nullptr) // return; // // cout << root->_kv.first << " "; // _PrevOrder(root->_left); // _PrevOrder(root->_right); //} 3.后序遍历(递归版) //void _PostOrder(Node* root) //{ // if (root == nullptr) // return; // // _PostOrder(root->_left); // _PostOrder(root->_right); // cout << root->_kv.first << " "; //} 判断是否为有效红黑树 //bool _IsValidRBTree(Node* pRoot, size_t k, const size_t blackCount) //{ // //走到null之后,判断k和black是否相等 // if (nullptr == pRoot) // { // if (k != blackCount) // { // cout << "违反性质四:每条路径中黑色节点的个数必须相同" << endl; // return false; // } // return true; // } // // // 统计黑色节点的个数 // if (BLACK == pRoot->_col) // k++; // // // 检测当前节点与其双亲是否都为红色 // if (RED == pRoot->_col && pRoot->_parent && pRoot->_parent->_col == RED) // { // cout << "违反性质三:存在连在一起的红色节点" << endl; // return false; // } // // return _IsValidRBTree(pRoot->_left, k, blackCount) && // _IsValidRBTree(pRoot->_right, k, blackCount); //} // //public: // //遍历 // void InOrder() // { // cout << "中序遍历:"; // _InOrder(_root); // cout << endl; // } // void PrevOrder() // { // cout << "前序遍历:"; // _PrevOrder(_root); // cout << endl; // } // void PostOrder() // { // cout << "后序遍历:"; // _PostOrder(_root); // cout << endl; // } // // void Height() // { // cout << "最长路径:" << _maxHeight(_root) << endl; // cout << "最短路径:" << _minHeight(_root) << endl; // } // // bool IsBalanceTree() // { // // 检查红黑树几条规则 // // Node* pRoot = _root; // // 空树也是红黑树 // if (nullptr == pRoot) // return true; // // // 检测根节点是否满足情况 // if (BLACK != pRoot->_col) // { // cout << "违反红黑树性质二:根节点必须为黑色" << endl; // return false; // } // // // 获取任意一条路径中黑色节点的个数 -- 比较基准值 // size_t blackCount = 0; // Node* pCur = pRoot; // while (pCur) // { // if (BLACK == pCur->_col) // blackCount++; // // pCur = pCur->_left; // } // // // 检测是否满足红黑树的性质,k用来记录路径中黑色节点的个数 // size_t k = 0; // return _IsValidRBTree(pRoot, k, blackCount); // }
(3)test.cpp:
#include"map.h" int main() { //my::test_map1(); my::test_map2(); //my::test_map3(); return 0; }
4.set的模拟实现
set的底层为红黑树,因此只需在set内部封装一棵红黑树,即可将该容器实现出来(具体实现可参 考map)。namespace bite { template<class K> class set { typedef K ValueType; // 作用是:将value中的key提取出来 struct KeyOfValue { const K& operator()(const ValueType& key) { return key; } }; // 红黑树类型重命名 typedef RBTree<K, ValueType, KeyOfValue> RBTree; public: typedef typename RBTree::Iterator iterator; public: Set() {} / // Iterator iterator Begin(); iterator End(); / // Capacity size_t size()const; bool empty()const; // modify pair<iterator, bool> insert(const ValueType& data) { return _t.Insert(data); } void clear(); iterator find(const K& key); private: RBTree _t; }; }
附用红黑树:
(1)set.h:
#pragma once #include "RBTree.h" namespace my { template<class K> class set { struct SetKeyOfT { const K& operator()(const K& key) { return key; } }; public: typedef typename RBTree<K, K, SetKeyOfT>::const_iterator iterator; typedef typename RBTree<K, K, SetKeyOfT>::const_iterator const_iterator; iterator begin() const { return _t.Begin(); } iterator end() const { return _t.End(); } pair<iterator, bool> insert(const K& key) { //pair<typename RBTree<K, K, SetKeyOfT>::iterator, bool> ret = _t.Insert(key); auto ret = _t.Insert(key); return pair<iterator, bool>(iterator(ret.first._node), ret.second); } iterator find(const K& key) { return _t.Find(key); } private: RBTree<K, K, SetKeyOfT> _t; }; void test_set1() { set<int> s; s.insert(8); s.insert(6); s.insert(11); s.insert(5); s.insert(6); s.insert(7); s.insert(10); s.insert(13); s.insert(12); s.insert(15); //迭代器遍历 set<int>::iterator it = s.begin(); while (it != s.end()) { cout << *it << " "; ++it; } cout << endl; //范围for遍历 for (auto e : s) { cout << e << " "; } cout << endl; } }
(2)RBTree.h:
#pragma once #include<iostream> #include<vector> #include<queue> #include<cassert> using namespace std; enum Colour { RED, BLACK, }; template<class T> struct RBTreeNode { RBTreeNode<T>* _left; RBTreeNode<T>* _right; RBTreeNode<T>* _parent; T _data; Colour _col; RBTreeNode(const T& data) :_data(data) , _left(nullptr) , _right(nullptr) , _parent(nullptr) , _col(RED) {} }; //T决定红黑树存什么数据 //set RBTree<K,K> //map RBTree<K,pair<K,V>> //KeyOfT -> 支持取出T对象中key的仿函数 template<class T, class Ref, class Ptr> struct __RBTreeIterator { typedef RBTreeNode<T> Node; typedef __RBTreeIterator<T, Ref, Ptr> Self; Node* _node; __RBTreeIterator(Node* node) :_node(node) {} //运算符*重载 Ref operator*() { return _node->_data; } //运算符->重载 Ptr operator->() { return &_node->_data; } //重载 前置++ Self& operator++() { if (_node->_right == nullptr) { // 找祖先里面,孩子是父亲左的那个 Node* cur = _node; Node* parent = cur->_parent; while (parent && parent->_right == cur) { cur = cur->_parent; parent = parent->_parent; } _node = parent; } else { // 右子树的最左节点 Node* subLeft = _node->_right; while (subLeft->_left) { subLeft = subLeft->_left; } _node = subLeft; } return *this; } //重载 后置++ Self operator++(int) { Self tmp(*this); ++(*this); return tmp; } //重载 前置-- Self& operator--() { if (_node->_left == nullptr) { // 找祖先里面,孩子是父亲 Node* cur = _node; Node* parent = cur->_parent; while (parent && cur == parent->_left) { cur = cur->_parent; parent = parent->_parent; } _node = parent; } else { // 左子树的最右节点 Node* subRight = _node->_left; while (subRight->_right) { subRight = subRight->_right; } _node = subRight; } return *this; } //重载 后置-- Self operator--(int) { Self tmp(*this); --(*this); return tmp; } //重载 != bool operator!=(const Self& s) const { return _node != s._node; } //重载 == bool operator==(const Self& s) const { return _node == s->_node; } }; template<class K, class T, class KeyOfT> class RBTree { typedef RBTreeNode<T> Node; //默认为私有 public: typedef __RBTreeIterator<T, T&, T*> iterator; typedef __RBTreeIterator<T, const T&, const T*> const_iterator; // 构造 拷贝构造 赋值 和析构 跟搜索树实现方式是一样的 iterator Begin() { Node* subLeft = _root; while (subLeft && subLeft->_left) { subLeft = subLeft->_left; } return iterator(subLeft); } iterator End() { return iterator(nullptr); } const_iterator Begin() const { Node* subLeft = _root; while (subLeft && subLeft->_left) { subLeft = subLeft->_left; } return const_iterator(subLeft); } const_iterator End() const { return const_iterator(nullptr); } //insert数据 pair<iterator, bool> Insert(const T& data) { // 1、搜索树的规则插入 // 2、看是否违反平衡规则,如果违反就需要处理:旋转 if (_root == nullptr) { _root = new Node(data); _root->_col = BLACK; return make_pair(iterator(_root), true); } KeyOfT kot; Node* parent = nullptr; Node* cur = _root; while (cur) { if (kot(cur->_data) < kot(data)) { parent = cur; cur = cur->_right; } else if (kot(cur->_data) > kot(data)) { parent = cur; cur = cur->_left; } else { return make_pair(iterator(cur), true); } } cur = new Node(data); Node* newnode = cur; cur->_col = RED; if (kot(parent->_data) < kot(data)) { parent->_right = cur; } else { parent->_left = cur; } cur->_parent = parent; // 存在连续红色节点 while (parent && parent->_col == RED) { Node* grandfater = parent->_parent; assert(grandfater); if (grandfater->_left == parent) { Node* uncle = grandfater->_right; // 情况一: if (uncle && uncle->_col == RED) // 叔叔存在且为红 { // 变色 parent->_col = uncle->_col = BLACK; grandfater->_col = RED; // 继续往上处理 cur = grandfater; parent = cur->_parent; } else // 叔叔不存在 或者 叔叔存在且为黑 { if (cur == parent->_left) // 单旋 { // g // p // c RotateR(grandfater); parent->_col = BLACK; grandfater->_col = RED; } else // 双旋 { // g // p // c RotateL(parent); RotateR(grandfater); cur->_col = BLACK; grandfater->_col = RED; } break; } } else //(grandfater->_right == parent) { Node* uncle = grandfater->_left; // 情况一: if (uncle && uncle->_col == RED) { // 变色 parent->_col = uncle->_col = BLACK; grandfater->_col = RED; // 继续往上处理 cur = grandfater; parent = cur->_parent; } else { if (cur == parent->_right) { // g // p // c RotateL(grandfater); parent->_col = BLACK; grandfater->_col = RED; } else // 双旋 { // g // p // c RotateR(parent); RotateL(grandfater); cur->_col = BLACK; grandfater->_col = RED; } break; } } } _root->_col = BLACK; return make_pair(iterator(newnode), true); } //左旋 void RotateL(Node* parent) { Node* subR = parent->_right; Node* subRL = subR->_left; parent->_right = subRL; if (subRL) subRL->_parent = parent; Node* ppNode = parent->_parent; subR->_left = parent; parent->_parent = subR; if (parent == _root) { _root = subR; _root->_parent = nullptr; } else { if (parent == ppNode->_left) { ppNode->_left = subR; } else { ppNode->_right = subR; } subR->_parent = ppNode; } } //右旋 void RotateR(Node* parent) { Node* subL = parent->_left; Node* subLR = subL->_right; parent->_left = subLR; if (subLR) subLR->_parent = parent; Node* ppNode = parent->_parent; subL->_right = parent; parent->_parent = subL; if (parent == _root) { _root = subL; _root->_parent = nullptr; } else { if (ppNode->_left == parent) { ppNode->_left = subL; } else { ppNode->_right = subL; } subL->_parent = ppNode; } } iterator Find(const K& key) { Node* cur = _root; KeyOfT kot; while (cur) { if (kot(cur->_data) < key) { cur = cur->_right; } else if (kot(cur->_data) > key) { cur = cur->_left; } else { return iterator(cur); } } return End(); } private: Node* _root = nullptr; }; 层序遍历 //vector<vector<int>> levelOrder() //{ // cout << "层序遍历:" << endl; // vector<vector<int>> vv; // if (_root == nullptr) // return vv; // // queue<Node*> q; // int levelSize = 1; // q.push(_root); // // while (!q.empty()) // { // // // levelSize控制一层一层出 // vector<int> levelV; // while (levelSize--) // { // Node* front = q.front(); // q.pop(); // levelV.push_back(front->_kv.first); // if (front->_left) // q.push(front->_left); // // if (front->_right) // q.push(front->_right); // } // vv.push_back(levelV); // for (auto e : levelV) // { // cout << e << " "; // } // cout << endl; // // // 上一层出完,下一层就都进队列 // levelSize = q.size(); // } // // return vv; //} // // 求最长路径 //int _maxHeight(Node* root) //{ // if (root == nullptr) // return 0; // // int lh = _maxHeight(root->_left); // int rh = _maxHeight(root->_right); // // return lh > rh ? lh + 1 : rh + 1; //} 求最短路径 //int _minHeight(Node* root) //{ // if (root == nullptr) // return 0; // // int lh = _minHeight(root->_left); // int rh = _minHeight(root->_right); // // return lh < rh ? lh + 1 : rh + 1; //} 1.中序遍历(递归版) //void _InOrder(Node* root) //{ // if (root == nullptr) // return; // // _InOrder(root->_left); // cout << root->_kv.first << " "; // _InOrder(root->_right); //} 2.前序遍历(递归版) //void _PrevOrder(Node* root) //{ // if (root == nullptr) // return; // // cout << root->_kv.first << " "; // _PrevOrder(root->_left); // _PrevOrder(root->_right); //} 3.后序遍历(递归版) //void _PostOrder(Node* root) //{ // if (root == nullptr) // return; // // _PostOrder(root->_left); // _PostOrder(root->_right); // cout << root->_kv.first << " "; //} 判断是否为有效红黑树 //bool _IsValidRBTree(Node* pRoot, size_t k, const size_t blackCount) //{ // //走到null之后,判断k和black是否相等 // if (nullptr == pRoot) // { // if (k != blackCount) // { // cout << "违反性质四:每条路径中黑色节点的个数必须相同" << endl; // return false; // } // return true; // } // // // 统计黑色节点的个数 // if (BLACK == pRoot->_col) // k++; // // // 检测当前节点与其双亲是否都为红色 // if (RED == pRoot->_col && pRoot->_parent && pRoot->_parent->_col == RED) // { // cout << "违反性质三:存在连在一起的红色节点" << endl; // return false; // } // // return _IsValidRBTree(pRoot->_left, k, blackCount) && // _IsValidRBTree(pRoot->_right, k, blackCount); //} // //public: // //遍历 // void InOrder() // { // cout << "中序遍历:"; // _InOrder(_root); // cout << endl; // } // void PrevOrder() // { // cout << "前序遍历:"; // _PrevOrder(_root); // cout << endl; // } // void PostOrder() // { // cout << "后序遍历:"; // _PostOrder(_root); // cout << endl; // } // // void Height() // { // cout << "最长路径:" << _maxHeight(_root) << endl; // cout << "最短路径:" << _minHeight(_root) << endl; // } // // bool IsBalanceTree() // { // // 检查红黑树几条规则 // // Node* pRoot = _root; // // 空树也是红黑树 // if (nullptr == pRoot) // return true; // // // 检测根节点是否满足情况 // if (BLACK != pRoot->_col) // { // cout << "违反红黑树性质二:根节点必须为黑色" << endl; // return false; // } // // // 获取任意一条路径中黑色节点的个数 -- 比较基准值 // size_t blackCount = 0; // Node* pCur = pRoot; // while (pCur) // { // if (BLACK == pCur->_col) // blackCount++; // // pCur = pCur->_left; // } // // // 检测是否满足红黑树的性质,k用来记录路径中黑色节点的个数 // size_t k = 0; // return _IsValidRBTree(pRoot, k, blackCount); // }
(3)test.cpp:
#include"set.h" int main() { my::test_set1(); return 0; }