• 【C++进阶】:红黑树



    本篇的前置条件是AVL树的旋转和搜索树,如果不了解可以看看我的AVL树博客

    一.红黑树简单实现

    1.性质

    红黑树,是一种二叉搜索树,但在每个结点上增加一个存储位表示结点的颜色,可以是Red或Black。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制,红黑树确保没有一条路径会比其他路径长出俩倍,因而是接近平衡的。

    1. 每个结点不是红色就是黑色。
    2. 根节点是黑色的。
    3. 如果一个节点是红色的,则它的两个孩子结点是黑色的。
    4. 对于每个结点,从该结点到其所有后代叶结点的简单路径上,均包含相同数目的黑色结点。
    5. 每个叶子结点都是黑色的(此处的叶子结点指的是空结点)。

    创建节点

    在这里插入图片描述

    在这里插入图片描述

    二.更新颜色

    1.情况一

    插入一个节点,它的父亲是红色的并且它有叔叔且叔叔也是红色的。

    在这里插入图片描述

    2.情况二

    如果叔叔不存在

    在这里插入图片描述
    此时单纯的变色是无法解决问题的,需要进行旋转,在此情况是右旋。

    在这里插入图片描述

    3.情况三

    叔叔存在且为黑,注意此时插入的点是在最下面,cur经过上面的转变后到达图示的位置。

    在这里插入图片描述

    在这里插入图片描述

    3.完整代码(代码有注释,稍微画图很容易理解,旋转部分可以看我的AVL树博客)

    测试

    #include"RBTree.h"
    #include
    
    int main()
    {
    	RBTree<int, int> t;
    	srand(time(0));
    	vector<int>a;
    	for (int i = 0; i < 100; i++)
    	{
    		int x = rand();
    		a.push_back(x);
    	}
    	
    	for (auto x : a)
    		t.Insert(make_pair(x, x));
    	cout << t.IsBalance() << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    #include
    #include
    using namespace std;
    
    
    
    enum Color
    {
    	RED,
    	BLACK
    };
    
    template<class K,class V>
    struct RBTreeNode
    {
    	pair<K, V> _kv;
    	RBTreeNode<K, V>* _left;
    	RBTreeNode<K, V>* _right;
    	RBTreeNode<K, V>* _parent;
    
    	RBTreeNode(const pair<K,V>& kv)//初始化节点
    		:_kv(kv)
    		,_left(nullptr)
    		,_right(nullptr)
    		,_parent(nullptr)
    		,_col(RED)
    	{}
    
    	Color _col;
    };
    
    
    template<class K,class V>
    class RBTree
    {
    public:
    	typedef RBTreeNode<K, V> Node;
    
    	bool Insert(const pair<K, V>& kv)
    	{
    		if (_root == nullptr)
    		{
    			_root = new Node(kv);
    			_root->_col = BLACK;//根为黑色
    			return true;
    		}
    
    		Node* parent = _root;
    		Node* cur = _root;
    		//一般的搜索二叉树插入
    		while (cur)
    		{
    			if (kv.first > cur->_kv.first)
    			{
    				parent = cur;
    				cur = cur->_right;
    			}
    			else if (kv.first < cur->_kv.first)
    			{
    				parent = cur;
    				cur = cur->_left;
    			}
    			else
    				return false;
    		}
    		cur = new Node(kv);
    		cur->_parent = parent;
    		
    		if (parent->_kv > kv) parent->_left = cur;
    		else parent->_right = cur;
    
    		//进行颜色更新
    		while (parent && parent->_col == RED)
    		{
    			Node* grandparent = parent->_parent;
    			Node* uncle;
    			//找到叔叔
    			if (grandparent->_left == parent)
    				uncle = grandparent->_right;
    			else
    				uncle = grandparent->_left;
    
    			//如果叔叔存在且为红色
    			if (uncle && uncle->_col == RED)
    			{
    				//变颜色
    				parent->_col = uncle->_col = BLACK;
    				grandparent->_col = RED;
    
    				//继续向上
    				cur = grandparent;
    				parent = cur->_parent;
    			}
    			//如果叔叔不存在或者是黑色
    			else
    			{
    				//parent是左,cur是左,进行右单旋
    				if (grandparent->_left == parent && parent->_left == cur)
    				{
    					RotateR(grandparent);
    					grandparent->_col = RED;
    					parent->_col = BLACK;
    				}
    				//parent是左,cur是右,进行左右旋
    				else if (grandparent->_left == parent && parent->_right == cur)
    				{
    					RotateL(parent);
    					RotateR(grandparent);
    
    					cur->_col = BLACK;
    					grandparent->_col = RED;
    				}
    				//parent是右,cur是右,进行左单旋
    				else if (grandparent->_right == parent && parent->_right == cur)
    				{
    					RotateL(grandparent);
    
    					grandparent->_col = RED;
    					parent->_col = BLACK;
    				}
    				//parent是右,cur是左,进行右左旋
    				else
    				{
    					RotateR(parent);
    					RotateL(grandparent);
    
    					cur->_col = BLACK;
    					grandparent->_col = RED;
    				}
    			}
    		}
    		_root->_col = BLACK;
    	}
    	//旋转函数
    	void RotateL(Node* parent)
    	{
    		Node* cur = parent->_right;
    		Node* curleft = cur->_left;
    		Node* ppnode = parent->_parent;//记录父节点的父节点
    
    		//父节点的右孩子变成curleft
    		parent->_right = curleft;
    		if (curleft)//细节注意curleft为空时不能操作
    			curleft->_parent = parent;
    		//父节点变为cur的左孩子
    		cur->_left = parent;
    		parent->_parent = cur;
    
    		//如果原来父节点是根节点
    		if (parent == _root)
    		{
    			_root = cur;
    			cur->_parent = nullptr;
    		}
    		else//如果不是根节点判断它应该是左儿子还是右儿子
    		{
    			if (ppnode->_left == parent)
    			{
    				ppnode->_left = cur;
    			}
    			else
    			{
    				ppnode->_right = cur;
    
    			}
    			cur->_parent = ppnode;
    		}
    	}
    	void RotateR(Node* parent)
    	{
    		Node* cur = parent->_left;
    		Node* curright = cur->_right;
    		Node* pphead = parent->_parent;
    
    		//父节点到cur右边
    		cur->_right = parent;
    		parent->_parent = cur;
    		//父节点的左孩子变成curright
    		parent->_left = curright;
    		if (curright)
    			curright->_parent = parent;
    		//cur的父节点变为原来父节点的父节点
    		if (pphead)//如果不是根节点
    		{
    			if (pphead->_left == parent)
    				pphead->_left = cur;
    			else
    				pphead->_right = cur;
    			cur->_parent = pphead;
    		}
    		else
    		{
    			_root = cur;
    			cur->_parent = nullptr;
    		}
    	}
    	void RotateRL(Node* parent)
    	{
    		Node* cur = parent->_right;
    		Node* curleft = cur->_left;
    
    		RotateR(parent->_right);
    		RotateL(parent);
    	}
    	void RotateLR(Node* parent)
    	{
    		Node* cur = parent->_left;
    		Node* curright = cur->_right;
    
    		RotateL(parent->_left);
    		RotateR(parent);
    	}
    
    
    	//测试是否出问题
    	bool IsBalance()
    	{
    		return IsBalance(_root);
    	}
    	bool IsBalance(Node* root)
    	{
    		//根是否为黑色
    		if (_root->_col != BLACK)
    		{
    			cout << "根不是红色"<<endl;
    			return false;
    		}
    
    		int blackcheck = 0;
    		Node* cur = _root;
    		while (cur)
    		{
    			if (cur->_col == BLACK)
    				blackcheck++;
    			cur = cur->_left;
    		}
    
    		//判断是否有两个连续的红色和每条路径的黑色是否相同
    		return CheckColor(_root,blackcheck,0);
    	}
    
    	bool CheckColor(Node*root,int blackcheck,int blacknum)
    	{
    		if (root == nullptr)
    		{
    			//检查是否每条路径的黑色相同
    			if (blackcheck != blacknum)
    			{
    				cout << "路径上黑色个数不同" << ' ';
    				return false;
    			}
    			return true;
    		}
    
    		//判断是否有两个连续的红色
    		Node* parent = root->_parent;
    		if (root->_col == RED)
    		{
    			if (parent && parent->_col == RED)
    			{
    				cout << "有连续的红色" << ' ';
    				return false;
    			}
    		}
    		if (root->_col == BLACK) blacknum++;
    
    		return CheckColor(root->_left, blackcheck, blacknum)
    			&& CheckColor(root->_right, blackcheck, blacknum);
    	}
    private:
    	Node* _root = nullptr;
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273

    二.map和set

    1.基本实现

    map和set虽然功能不同,但在库里都是使用红黑树实现的,接下来对红黑树的代码进行改造。在库里,set和map走的是泛型,也就是map和set可以使用同一份红黑树代码。

    对红黑树进行泛化处理,方便之后传参

    在这里插入图片描述

    这里有一个问题:对于data,如果是set那么我们可以直接比较,但如果是map呢?map的T是一个pair,我们是不能直接比较的,为了解决这个问题,我们可以使用仿函数(如果不了解可以看看我的仿函数这篇博客)。

    map里返回pair的firist
    在这里插入图片描述

    set为了保持一致,直接返回key
    在这里插入图片描述

    在树里创建一个对象,用该对象的规则进行比较
    在这里插入图片描述

    分别对map和set加入插入操作

    在这里插入图片描述

    在这里插入图片描述

    2.迭代器

    首先需要明确迭代器的功能,就是能够将整棵树进行中序遍历。

    在这里插入图片描述

    我们需要++,*,–,!=等操作。对于++操作,我们需要进行中序遍历。

    在这里插入图片描述

    在这里插入图片描述

    其他一些小功能就不细说,下面是完整代码

    RBTree.h

    #pragma once
    #include
    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)
    		:_left(nullptr)
    		,_right(nullptr)
    		,_parent(nullptr)
    		,_data(data)
    		,_col(RED)
    	{}
    };
    
    template<class T>
    struct __TreeIterator
    {
    	typedef RBTreeNode<T> Node;
    	typedef __TreeIterator<T> Self;
    	Node* _node;
    
    	__TreeIterator(Node* node)
    		:_node(node)
    	{}
    
    	T& operator*()
    	{
    		return _node->_data;
    	}
    
    	T* operator->()
    	{
    		return &_node->_data;
    	}
    
    	bool operator!=(const Self& s)
    	{
    		return _node != s._node;
    	}
    	Self& operator++()
    	{
    		if (_node->_right)
    		{
    			// 右树的最左节点(最小节点)
    			Node* subLeft = _node->_right;
    			while (subLeft->_left)
    			{
    				subLeft = subLeft->_left;
    			}
    
    			_node = subLeft;
    		}
    		else
    		{
    			Node* cur = _node;
    			Node* parent = cur->_parent;
    			// 找孩子是父亲左的那个祖先节点,就是下一个要访问的节点
    			while (parent)
    			{
    				if (cur == parent->_left)
    				{
    					break;
    				}
    				else
    				{
    					cur = cur->_parent;
    					parent = parent->_parent;
    				}
    			}
    
    			_node = parent;
    		}
    
    		return *this;
    	}
    };
    
    // set->RBTree _t;
    // map->RBTree, MapKeyOfT> _t;
    
    template<class K, class T, class KeyOfT>
    struct RBTree
    {
    	typedef RBTreeNode<T> Node;
    public:
    	typedef __TreeIterator<T> iterator;
    	// const_iterator
    
    	iterator begin()
    	{
    		Node* leftMin = _root;
    		while (leftMin && leftMin->_left)
    		{
    			leftMin = leftMin->_left;
    		}
    
    		return iterator(leftMin);
    	}
    
    	iterator end()
    	{
    		return iterator(nullptr);
    	}
    
    
    	Node* 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 cur;
    			}
    		}
    
    		return nullptr;
    	}
    
    	bool Insert(const T& data)
    	{
    		if (_root == nullptr)
    		{
    			_root = new Node(data);
    			_root->_col = BLACK;
    			return true;
    		}
    
    		Node* parent = nullptr;
    		Node* cur = _root;
    
    		KeyOfT kot;
    		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 false;
    			}
    		}
    
    		cur = new Node(data);
    		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* grandfather = parent->_parent;
    			if (parent == grandfather->_left)
    			{
    				Node* uncle = grandfather->_right;
    				// u存在且为红
    				if (uncle && uncle->_col == RED)
    				{
    					// 变色
    					parent->_col = uncle->_col = BLACK;
    					grandfather->_col = RED;
    
    					// 继续向上处理
    					cur = grandfather;
    					parent = cur->_parent;
    				}
    				else // u不存在 或 存在且为黑
    				{
    					if (cur == parent->_left)
    					{
    						//     g
    						//   p
    						// c
    						RotateR(grandfather);
    						parent->_col = BLACK;
    						grandfather->_col = RED;
    					}
    					else
    					{
    						//     g
    						//   p
    						//		c
    						RotateL(parent);
    						RotateR(grandfather);
    
    						cur->_col = BLACK;
    						grandfather->_col = RED;
    					}
    
    					break;
    				}
    			}
    			else // parent == grandfather->_right
    			{
    				Node* uncle = grandfather->_left;
    				// u存在且为红
    				if (uncle && uncle->_col == RED)
    				{
    					// 变色
    					parent->_col = uncle->_col = BLACK;
    					grandfather->_col = RED;
    
    					// 继续向上处理
    					cur = grandfather;
    					parent = cur->_parent;
    				}
    				else
    				{
    					if (cur == parent->_right)
    					{
    						// g
    						//	  p
    						//       c
    						RotateL(grandfather);
    						grandfather->_col = RED;
    						parent->_col = BLACK;
    					}
    					else
    					{
    						// g
    						//	  p
    						// c
    						RotateR(parent);
    						RotateL(grandfather);
    						cur->_col = BLACK;
    						grandfather->_col = RED;
    					}
    
    					break;
    				}
    			}
    		}
    
    		_root->_col = BLACK;
    
    		return true;
    	}
    
    	void RotateL(Node* parent)
    	{
    		++_rotateCount;
    
    		Node* cur = parent->_right;
    		Node* curleft = cur->_left;
    
    		parent->_right = curleft;
    		if (curleft)
    		{
    			curleft->_parent = parent;
    		}
    
    		cur->_left = parent;
    
    		Node* ppnode = parent->_parent;
    
    		parent->_parent = cur;
    
    
    		if (parent == _root)
    		{
    			_root = cur;
    			cur->_parent = nullptr;
    		}
    		else
    		{
    			if (ppnode->_left == parent)
    			{
    				ppnode->_left = cur;
    			}
    			else
    			{
    				ppnode->_right = cur;
    
    			}
    
    			cur->_parent = ppnode;
    		}
    	}
    
    
    	void RotateR(Node* parent)
    	{
    		++_rotateCount;
    
    		Node* cur = parent->_left;
    		Node* curright = cur->_right;
    
    		parent->_left = curright;
    		if (curright)
    			curright->_parent = parent;
    
    		Node* ppnode = parent->_parent;
    		cur->_right = parent;
    		parent->_parent = cur;
    
    		if (ppnode == nullptr)
    		{
    			_root = cur;
    			cur->_parent = nullptr;
    		}
    		else
    		{
    			if (ppnode->_left == parent)
    			{
    				ppnode->_left = cur;
    			}
    			else
    			{
    				ppnode->_right = cur;
    			}
    
    			cur->_parent = ppnode;
    		}
    	}
    	
    
    	bool CheckColour(Node* root, int blacknum, int benchmark)
    	{
    		if (root == nullptr)
    		{
    			if (blacknum != benchmark)
    				return false;
    
    			return true;
    		}
    
    		if (root->_col == BLACK)
    		{
    			++blacknum;
    		}
    
    		if (root->_col == RED && root->_parent && root->_parent->_col == RED)
    		{
    			cout << root->_kv.first << "出现连续红色节点" << endl;
    			return false;
    		}
    
    		return CheckColour(root->_left, blacknum, benchmark)
    			&& CheckColour(root->_right, blacknum, benchmark);
    	}
    
    	bool IsBalance()
    	{
    		return IsBalance(_root);
    	}
    
    	bool IsBalance(Node* root)
    	{
    		if (root == nullptr)
    			return true;
    
    		if (root->_col != BLACK)
    		{
    			return false;
    		}
    
    		// 基准值
    		int benchmark = 0;
    		Node* cur = _root;
    		while (cur)
    		{
    			if (cur->_col == BLACK)
    				++benchmark;
    
    			cur = cur->_left;
    		}
    
    		return CheckColour(root, 0, benchmark);
    	}
    
    	int Height()
    	{
    		return Height(_root);
    	}
    
    	int Height(Node* root)
    	{
    		if (root == nullptr)
    			return 0;
    
    		int leftHeight = Height(root->_left);
    		int rightHeight = Height(root->_right);
    
    		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
    	}
    
    private:
    	Node* _root = nullptr;
    
    public:
    	int _rotateCount = 0;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431

    Map.h

    #include"RBTree.h"
    
    namespace Mine
    {
    	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;
    		iterator begin()
    		{
    			return _t.begin();
    		}
    
    		iterator end()
    		{
    			return _t.end();
    		}
    
    		V& operator[](const K& key);
    
    		bool insert(const pair<K, V>& kv)
    		{
    			return _t.Insert(kv);
    		}
    
    	private:
    		RBTree<K, pair<K, V>, MapKeyOfT> _t;
    	};
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    Set.h

    #include"RBTree.h"
    
    namespace Mine
    {
    	template<class K>
    	class set
    	{
    		struct SetKeyOfT
    		{
    			const K& operator()(const K& key)
    			{
    				return key;
    			}
    		};
    	public:
    		typedef typename RBTree<K, K, SetKeyOfT>::iterator iterator;
    
    		iterator begin()
    		{
    			return _t.begin();
    		}
    
    		iterator end()
    		{
    			return _t.end();
    		}
    
    		bool insert(const K& key)
    		{
    			return _t.Insert(key);
    		}
    	private:
    		RBTree<K, K, SetKeyOfT> _t;
    	};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
  • 相关阅读:
    单节点伪分布式Hadoop部署笔记
    Linux下排除死锁详细教程(基于C++11、GDB)
    11.Pandas的axis参数
    哪些人需要考PMP®证书呢?考了PMP®证书有什么用呢?
    网络安全(黑客)—自学
    安全架构设计理论与实践
    zookeeper:服务器有几种状态?
    我,机器学习工程师,决定跑路了
    浅析DNS劫持及应对方案
    【Hack The Box】linux练习-- Nibbles
  • 原文地址:https://blog.csdn.net/m0_73790767/article/details/132781500