• 【C++】map & set 底层刨析



    在这里插入图片描述

    在 C++ STL 库中,map 与 set 的底层为红黑树,那么在不写冗余代码的情况下使用红黑树同时实现 map 与 set 便是本文的重点。

    1. 红黑树的迭代器

    迭代器的好处是可以方便遍历,是数据结构的底层实现与用户透明。如果想要给红黑树增加迭代器,需要考虑以下问题:

    • begin()end()

      STL 明确规定,begin() 与 end() 代表的是一段前闭后开的区间,而对红黑树进行中序遍历后,可以得到一个有序的序列,因此:begin() 可以放在红黑树中最小节点(即最左侧节点)的位置,end() 放在最大节点(最右侧节点)的下一个位置

      iterator begin()
      {
      	Node* subLeft = _root;
      	while (subLeft && subLeft->_left)
      	{
      		subLeft = subLeft->_left;
      	}
      	return iterator(subLeft);
      }
      
      const_iterator begin() const
      {
      	Node* subLeft = _root;
      	while (subLeft && subLeft->_left)
      	{
      		subLeft = subLeft->_left;
      	}
      	return const_iterator(subLeft);
      }
      
      iterator end()
      {
      	return iterator(nullptr);
      }
      
      const_iterator end() const
      {
      	return const_iterator(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
    • operator++()operator--()

      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 && cur == parent->_right)
      		{
      			cur = parent;
      			parent = cur->_parent;
      		}
      		_node = parent;
      	}
      	return *this;
      }
      
      Self& operator--()
      {
      	// 跟++逻辑相反
      	return *this;
      }
      
      • 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

    2. 改造红黑树

    #pragma once
    
    enum Color
    {
    	RED,
    	BLACK
    };
    
    template<class T>
    struct RBTreeNode
    {
    	RBTreeNode<T>* _left;
    	RBTreeNode<T>* _right;
    	RBTreeNode<T>* _parent;
    	Color _col;
    	T _data;
    
    	RBTreeNode(const T& data)
    		: _left(nullptr)
    		, _right(nullptr)
    		, _parent(nullptr)
    		, _data(data)
    		, _col(RED)
    	{}
    };
    
    template<class T, class Ptr, class Ref>
    struct RBTreeIterator
    {
    	typedef RBTreeNode<T> Node;
    	typedef RBTreeIterator<T, Ptr, Ref> Self;
    
    	Node* _node;
    
    	RBTreeIterator(Node* node)
    		: _node(node)
    	{}
    
    	T& operator*()
    	{
    		return _node->_data;
    	}
    
    	T* operator->()
    	{
    		return &_node->_data;
    	}
    
    	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 && cur == parent->_right)
    			{
    				cur = parent;
    				parent = cur->_parent;
    			}
    			_node = parent;
    		}
    		return *this;
    	}
    
    	Self& operator--()
    	{
    		// 跟++逻辑相反
    		return *this;
    	}
    
    	bool operator!=(const Self& s)
    	{
    		return _node != s._node;
    	}
    
    	bool operator==(const Self& s)
    	{
    		return _node == s._node;
    	}
    };
    
    // set->RBTree
    // map->RBTree, MapKeyOfT>
    
    // 因为关联式容器中存储的是的键值对,因此
    // K为key的类型
    // T:如果是map,则为pair;如果是set,则为K
    // KeyOfT仿函数,取出T对象中的key
    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);
    	}
    
    	const_iterator begin() const
    	{
    		Node* subLeft = _root;
    		while (subLeft && subLeft->_left)
    		{
    			subLeft = subLeft->_left;
    		}
    		return const_iterator(subLeft);
    	}
    
    	iterator end()
    	{
    		return iterator(nullptr);
    	}
    
    	const_iterator end() const
    	{
    		return const_iterator(nullptr);
    	}
    
    	iterator Find(const K& key)
    	{
    		KeyOfT kot;
    		Node* cur = _root;
    		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();
    	}
    
    	pair<iterator, bool> Insert(const T& data)
    	{
    		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), false);
    			}
    		}
    
    		cur = new Node(data);	// 红色的
    		Node* newnode = cur;
    		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;
    				// 情况一:叔叔存在且为红
    				if (uncle && uncle->_col == RED)
    				{
    					// 变色
    					parent->_col = uncle->_col = BLACK;
    					grandfather->_col = RED;
    
    					// 继续往上处理
    					cur = grandfather;
    					parent = cur->_parent;
    				}
    				// 情况二:叔叔不存在或者存在且为黑
    				else
    				{
    					// 旋转+变色
    					if (cur == parent->_left)
    					{
    						//     g
    						//   p   u
    						// c
    						RotateR(grandfather);
    						parent->_col = BLACK;
    						grandfather->_col = RED;
    					}
    					else
    					{
    						//    g
    						// p     u
    						//   c
    						RotateL(parent);
    						RotateR(grandfather);
    						cur->_col = BLACK;
    						grandfather->_col = RED;
    					}
    					break;
    				}
    			}
    			else
    			{
    				Node* uncle = grandfather->_left;
    				// 情况一:叔叔存在且为红
    				if (uncle && uncle->_col == RED)
    				{
    					// 变色
    					parent->_col = uncle->_col = BLACK;
    					grandfather->_col = RED;
    
    					// 继续往上处理
    					cur = grandfather;
    					parent = cur->_parent;
    				}
    				// 情况二:叔叔不存在或者存在且为黑
    				else
    				{
    					// 旋转+变色
    					//   g
    					// u   p
    					//       c
    					if (cur == parent->_right)
    					{
    						RotateL(grandfather);
    						parent->_col = BLACK;
    						grandfather->_col = RED;
    					}
    					else
    					{
    						//    g
    						// u     p
    						//     c
    						RotateR(parent);
    						RotateL(grandfather);
    						cur->_col = BLACK;
    						grandfather->_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;
    
    		subR->_left = parent;
    		Node* ppnode = parent->_parent;
    		parent->_parent = subR;
    
    		if (parent == _root)
    		{
    			_root = subR;
    			subR->_parent = nullptr;
    		}
    		else
    		{
    			if (ppnode->_left == parent)
    			{
    				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;
    
    		subL->_right = parent;
    		Node* ppnode = parent->_parent;
    		parent->_parent = subL;
    
    		if (parent == _root)
    		{
    			_root = subL;
    			subL->_parent = nullptr;
    		}
    		else
    		{
    			if (ppnode->_left == parent)
    			{
    				ppnode->_left = subL;
    			}
    			else
    			{
    				ppnode->_right = subL;
    			}
    			subL->_parent = ppnode;
    		}
    	}
    
    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
    • 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

    3. map 的模拟实现

    map 的底层结构就是红黑树,因此在 map 中直接封装一棵红黑树,然后将其接口包装下即可。

    #pragma once
    
    #include "RBTree.h"
    
    namespace tjq
    {
    	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<const K, V>, MapKeyOfT>::iterator iterator;
    		typedef typename RBTree<K, const K, MapKeyOfT>::const_iterator const_iterator;
    		
    		iterator begin()
    		{
    			return _t.begin();
    		}
    
    		iterator end()
    		{
    			return _t.end();
    		}
    
    		const_iterator begin() const
    		{
    			return _t.begin();
    		}
    
    		const_iterator end() const
    		{
    			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<const 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
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    4. set 的模拟实现

    set 的底层为红黑树,因此只需在 set 内部封装一棵红黑树,即可将该容器实现出来。

    #pragma once
    
    #include "RBTree.h"
    
    namespace tjq
    {
    	template<class K>
    	class set
    	{
    		struct SetKeyOfT
    		{
    			const K& operator()(const K& key)
    			{
    				return key;
    			}
    		};
    
    	public:
    		typedef typename RBTree<K, const K, SetKeyOfT>::iterator iterator;
    		typedef typename RBTree<K, const K, SetKeyOfT>::const_iterator const_iterator;
    
    		iterator begin()
    		{
    			return _t.begin();
    		}
    
    		iterator end()
    		{
    			return _t.end();
    		}
    
    		pair<iterator, bool> insert(const K& key)
    		{
    			return _t.Insert(key);
    		}
    
    		iterator find(const K& key)
    		{
    			return _t.Find(key);
    		}
    
    	private:
    		RBTree<K, const 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
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    END
  • 相关阅读:
    基于Python PHP+mysql的校园电脑外设的电商平台
    你是不是足够了解Lambda表达式?
    职业了解|03师范生的编制教师之路
    华清 Qt day2 9月18
    050:vue+openlayers使用Popup组件显示经纬度坐标(代码示例)
    (二十五)大数据实战——kafka集群及Kafka-Eagle控制台安装与部署
    java计算机毕业设计鞋店销售管理源程序+mysql+系统+lw文档+远程调试
    运维如何学习、自我提升价值?
    22张图带你深入剖析前缀、中缀、后缀表达式以及表达式求值
    漏洞深度分析|Apache Airflow example_bash_operator DAG 远程代码执行漏洞
  • 原文地址:https://blog.csdn.net/m0_73156359/article/details/137405703