紅黑樹的迭代器
//紅黑樹的迭代器
template<class T>
struct RBTreeIterator
{typedef RBTreeNode<T>Node;typedef RBTreeIterator<T> Self;
public:RBTreeIterator(Node* pNode = nullptr):_pNode(pNode){}//具有指針操作T& operator*(){return _pNode->_data;//找值}T* operator->(){return &(operator*());}//可以移動Self& operator++(){Increament();return *this;}Self operator++(int){Self temp(*this);Increament(); //往后走一步return temp;}Self& operator--(){DeIncreament();return *this;}Self operator--(int){Self temp(*this);DeIncreament();return temp;}bool operator==(const Self& s)const{return _pNode == s._pNode;}bool operator!=(const Self& s)const{return _pNode != s._pNode;}
private:void Increament(){if (_pNode->_pRight) //右子樹存在{_pNode = _pNode->_pRight;while (_pNode->_pLeft)_pNode = _pNode->_pLeft;}else{//比_pNode大的元素可能在它的雙親中Node* pParent = _pNode->_pParent;while (_pNode == pParent->_pRight){_pNode = pParent;pParent = _pNode->_pParent;}//根結點沒有右子樹&&迭代器在根結點的位置if (_pNode->_pRight!=pParent)_pNode = pParent; //把pNode放到雙親的位置}}void DeIncreament(){if (_pNode->_pParent->_pParent == _pNode&& RED == _pNode->_color){_pNode = _pNode->_pRight;}else if (_pNode->_pLeft){//如果左子樹存在,應該在左子樹中找最大的結點_pNode = _pNode->_pLeft;while (_pNode->_pRight)_pNode = _pNode->_pRight;}else{//向上找Node* pParent = _pNode->_pParent;while (_pNode == pParent->_pLeft){_pNode = pParent;pParent = _pNode->_pParent;}//begin不可能再減_pNode = pParent;}}
private:Node* _pNode;
};
紅黑樹的改造
template<class T,class KOFV>
class RBTree
{typedef RBTreeNode<T> Node;
public:typedef RBTreeIterator<T> iterator;public:RBTree():_size(0){_pHead = new Node;_pHead->_pLeft = _pHead;_pHead->_pRight = _pHead;//構造里已經將雙親給成空}iterator begin(){return iterator(_pHead->_pLeft);}iterator end(){return iterator(_pHead);}pair<iterator,bool> Insert(const T&data){Node*& pRoot = GetRoot(); //必須以引用的方式進行接受Node* pNewNode = nullptr;if (nullptr == pRoot) //樹為空,創建根結點{pNewNode = pRoot = new Node(data, BLACK);pRoot->_pParent = _pHead;//只有一個結點,head就是根節點的雙親}else{//說明樹已經不為空了//1.按照二叉搜索樹的性質找到帶插入結點在紅黑樹的位置Node* pCur = pRoot;Node* pParent = nullptr;while (pCur){pParent = pCur;//標記雙親位置if (KOFV()(data) < KOFV()(pCur->_data))pCur = pCur->_pLeft;else if (KOFV()(data) > KOFV()(pCur->_data))pCur = pCur->_pRight;else//相同不插入return make_pair(iterator(pCur),false);}//2. 插入新結點pNewNode = pCur = new Node(data);if (KOFV()(data) < KOFV()(pParent->_data))pParent->_pLeft = pCur;elsepParent->_pRight = pCur;//3. 更新雙親位置pCur->_pParent = pParent;//以上沒錯//4.檢測:是否新結點插入后連在一起的紅色結點while (pParent != _pHead && RED == pParent->_color){Node* granderFather = pParent->_pParent;if (pParent == granderFather->_pLeft){//叔叔結點在右側Node* uncle = granderFather->_pRight;//情況一:叔叔結點存在,且為紅if (uncle && RED == uncle->_color){pParent->_color = BLACK;uncle->_color = BLACK;granderFather->_color = RED;pCur = granderFather;pParent = pCur->_pParent;}//以上沒問題else{//情況三if (pCur == pParent->_pRight) //情況三{//轉變成情況二RotateLeft(pParent);swap(pParent, pCur);}//情況二pParent->_color = BLACK;granderFather->_color = RED;RotateRight(granderFather);}//以上沒問題}else{//叔叔結點在左側Node* uncle = granderFather->_pLeft;//情況一的反情況if (uncle && uncle->_color == RED){pParent->_color = BLACK;uncle->_color = BLACK;granderFather->_color = RED;pCur = granderFather;pParent = pCur->_pParent;}//以上沒問題else{//情況三的反情況if (pCur == pParent->_pLeft) /**/{//情況三的反情況變成情況二的反情況RotateRight(pParent);swap(pParent, pCur);}//情況二反情況處理pParent->_color = BLACK;granderFather->_color = RED;RotateLeft(granderFather);}//以上沒問題}}}//5.調整頭結點的左右指針域//保證根節點是黑色++_size;pRoot->_color = BLACK;_pHead->_pLeft = LeftMost();_pHead->_pRight = RightMost();return make_pair(iterator(pNewNode), true);}iterator Find(const T& data)const{Node* pCur = _pHead->_pParent;while (pCur){if (KOFV()(data) == KOFV()(pCur->_data))return iterator(pCur);else if (KOFV()(data) < KOFV()(pCur->_data))pCur = pCur->_pLeft;elsepCur = pCur->_pRight;}return end();}void InOrder(){_InOrder(GetRoot());}//檢測紅黑樹bool IsValidRBTree(){Node* pRoot = GetRoot();if (nullptr == pRoot)return true;if (pRoot->_color != BLACK){cout << "違反性質2:根結點顏色必須為黑色" << endl;return false;}//獲取一條路徑中結點的個數size_t blackCount = 0; //基準值Node* pCur = pRoot;while (pCur){if (pCur->_color == BLACK)blackCount++;pCur = pCur->_pLeft;}size_t pathBlack = 0; //每條路徑中的黑色結點個數return _IsValidRBTree(pRoot, blackCount, pathBlack);}bool Empty()const{return nullptr = _pHead->_pParent;}size_t Size()const{return _size;}
protected:bool _IsValidRBTree(Node* pRoot, size_t blackCount, size_t pathBlack){if (nullptr == pRoot)return true;if (pRoot->_color == BLACK)pathBlack++;//檢測性質3Node* pParent = pRoot->_pParent;if (pParent != _pHead && pParent->_color == RED&&pRoot->_color == RED){cout << "違反性質3:不能有連在一起的紅色結點" << endl;return false;}if (nullptr == pRoot->_pLeft&&nullptr == pRoot->_pRight){//一條路徑到葉子if (blackCount != pathBlack){cout << "違反了性質4:每條路徑中黑色結點個數必須相同" << endl;return false;}}return _IsValidRBTree(pRoot->_pLeft, blackCount, pathBlack) &&_IsValidRBTree(pRoot->_pRight, blackCount, pathBlack);}Node* LeftMost(){//得到根節點Node* pRoot = GetRoot();if (nullptr == pRoot)return _pHead;Node* pCur = pRoot;//找到最左側結點while (pCur->_pLeft)pCur = pCur->_pLeft;return pCur;}Node* RightMost(){//得到根節點Node* pRoot = GetRoot();if (nullptr == pRoot)return _pHead;Node* pCur = pRoot;//找到最右側結點while (pCur->_pRight)pCur = pCur->_pRight;return pCur;}void RotateLeft(Node* pParent){Node* pSubR = pParent->_pRight;Node* pSubRL = pSubR->_pLeft;pParent->_pRight = pSubRL;if (pSubRL)pSubRL->_pParent = pParent;pSubR->_pLeft = pParent;Node* pPParent = pParent->_pParent;pParent->_pParent = pSubR;pSubR->_pParent = pPParent;if (pPParent == _pHead)GetRoot() = pSubR;else{if (pPParent->_pLeft == pParent)pPParent->_pLeft = pSubR;elsepPParent->_pRight = pSubR;}}void RotateRight(Node* pParent){Node* pSubL = pParent->_pLeft;Node* pSubLR = pSubL->_pRight;pParent->_pLeft = pSubLR;if (pSubLR)pSubLR->_pParent = pParent;pSubL->_pRight = pParent;Node* pPParent = pParent->_pParent;pParent->_pParent = pSubL;pSubL->_pParent = pPParent;//pParent是根結點if (pPParent == _pHead)GetRoot() = pSubL;else{//非根結點if (pPParent->_pLeft == pParent)pPParent->_pLeft = pSubL;elsepPParent->_pRight = pSubL;}}Node*& GetRoot() //head是new出來的,head存在parent一定存在,按引用方式返回沒有問題{//得到根節點,也就是頭結點的下一個結點return _pHead->_pParent;}void _InOrder(Node* pRoot){if (pRoot){_InOrder(pRoot->_pLeft);cout << pRoot->_data << " ";_InOrder(pRoot->_pRight);}}
private:Node* _pHead;size_t _size; //記錄紅黑樹中有效結點的個數
};
struct KeyValue
{int operator()(int data){return data;}
};
模擬實現map和set
map<key,value>----比較方式:鍵值對中的key
set:key ----比較方式:直接用其元素比較
map模擬實現
#pragma once
#include"RBTree.h"namespace bite
{//只需要封裝一個紅黑樹template<class K, class V>class map{typedef pair<K, V> ValueType;struct KeyOfValue{const K& operator()(const ValueType & data){return data.first;}};public://編譯器有可能將iterator當成靜態成員變量來處理typename typedef RBTree<ValueType, KeyOfValue>::iterator iterator;//明確的告訴編譯器iterator 時紅黑樹中的一種類型public:map():_t(){}iterator begin(){return _t.begin();}iterator end(){return _t.end();}pair<iterator, bool> insert(const ValueType&data){return _t.Insert(data);}size_t size()const{return _t.Size();}bool Empty()const{return _t.Empty();}iterator find(const K& key){return _t.Find(make_pair(key,V()));}V& operator[](const K& key){return (*(_t.Insert(ValueType(key, V()))).first).second;}private:RBTree<ValueType,KeyOfValue> _t;};
}#include<string>
#include<iostream>
using namespace std;
void TestMap()
{bite::map<std::string, std::string>m;m.insert(pair<std::string,std::string>("2222","11111"));m.insert(make_pair("1111","1111"));m["0000"] = "0000";cout << m.size() << endl;for (auto e : m)cout << e.first << " " << e.second << endl;cout << endl;
}
set模擬實現
namespace bite
{//只需要封裝一個紅黑樹template<class K>class set{typedef K ValueType;struct KeyOfValue{const K& operator()(const ValueType & data){return data;}};public://編譯器有可能將iterator當成靜態成員變量來處理typename typedef RBTree<ValueType, KeyOfValue>::iterator iterator;//明確的告訴編譯器iterator 時紅黑樹中的一種類型public:set():_t(){}iterator begin(){return _t.begin();}iterator end(){return _t.end();}pair<iterator, bool> insert(const ValueType&data){return _t.Insert(data);}size_t size()const{return _t.Size();}bool Empty()const{return _t.Empty();}iterator find(const K& key){return _t.Find(key);}private:RBTree<ValueType, KeyOfValue> _t;};
}
#include<string>
#include<iostream>
using namespace std;
void TestSet()
{bite::set<std::string>m;m.insert("2222");m.insert("11111");m.insert("11111");cout << m.size() << endl;for (auto e : m)cout << e << endl;cout << endl;
}