红黑树模拟实现map和set
一、map和set模板
set用value标识元素(value就是key,类型为T),并且每个value必须唯一 。
在map中,键值key通常用于排序和惟一地标识元素,而值value中存储与此键值key关联的内容。键值key和值value的类型可能不同,并且在map的内部,key与value通过成员类型value_type绑定在一起,为其取别名称为pair:
1 2
   | typedef pair<const Key, T> value_type; template < class Key, class T>
   | 
 
 用红黑树同时封装出set和map时,set传给value的是一个value,map传给value的是一个pair,set和map传给红黑树的value决定了这棵树里面存的节点值类型。上层容器不同,底层红黑树的Key和T也不同。

在上层容器set中,K和T都代表Key,底层红黑树节点当中存储K和T都是一样的;map中,K代表键值Key,T代表由Key和Value构成的键值对,底层红黑树中只能存储T。所以红黑树为了满足同时支持set和map,节点当中存储T
这就要对红黑树进行改动。
二、红黑树节点定义
1.红黑树节点定义由类模板
1
   | template<class K,class V>
   | 
 
修改为
那么节点定义修改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
   |  template<class T> struct RBTreeNode { 	RBTreeNode<T>* _left; 	RBTreeNode<T>* _right; 	RBTreeNode<T>* _parent;   	T _data; 	Colour _col;   	RBTreeNode(const T& x) 		:_left(nullptr) 		, _right(nullptr) 		, _parent(nullptr) 		, _data(x) 		, _col(RED) 	{} };
 
  | 
 
由于红黑树不知道上层传的是K还是pair,这是由上层传递的模板参数T决定的,上层是封装我的map和set
2.仿函数
(1)节点比较大小时存在的问题
红黑树插入节点时,需要比较节点的大小,kv需要改成_data:
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
   |  pair<Node*, bool> Insert(const T& data) { 	if (_root == nullptr) 	{ 		_root = new Node(data); 		_root->_col = BLACK; 		return make_pair(_root, true); 	}
  	 	Node* parent = nullptr; 	Node* cur = _root; 	while (cur) 	{ 		if (cur->_data < data) 		{ 			 			parent = cur; 			cur = cur->_right; 		} 		else if (cur->_data > data) 		{ 			 			parent = cur; 			cur = cur->_left; 		} 		else 		{ 			 			return make_pair(cur, false); 		} 	}
  	 	Node* newNode = new Node(kv); 	newNode->_col = RED; 	if (parent->_data < data) 	{ 		 		parent->_right = newNode; 		newNode->_parent = parent; 	} 	else 	{ 		 		parent->_left = newNode; 		newNode->_parent = parent; 	} 	cur = newNode; 	         	while (parent && parent->_col == RED) 	{ 		 		Node* grandfather = parent->_parent; 		if (parent == grandfather->_left) 		{ 			Node* uncle = grandfather->_right; 			 			if (uncle->_col == RED) 			{ 				parent->_col = uncle->_col = BLACK; 				grandfather->_col = RED;
  				 				cur = grandfather; 				parent = cur->_parent; 			} 			else 			{ 				 				if (cur == parent->_left) 				{ 					RotateR(grandfather); 					parent->_col = BLACK; 					grandfather->_col = RED; 				} 				else 				{ 					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 = grandfather->_parent; 			} 			else 			{ 				 				if (cur == parent->_right) 				{ 					RotateL(grandfather); 					parent->_col = BLACK; 					grandfather->_col = RED; 				} 				else 				{ 					RotateR(parent); 					RotateL(grandfather); 					cur->_col = BLACK; 					grandfather->_col = RED; 				} 				break; 			} 		}
  	} 	_root->_col = BLACK;
  	return make_pair(newNode, true); }
 
  | 
 
但是以上代码在插入新节和查找节点时,当和当前节点比较大小时,Key可以比较,但是pair比较不了,也就是set可以比较,但是map比较不了。这就需要写一个仿函数,如果是map就取_data里面的first也就是Key进行比较,通过泛型解决红黑树里面存的是什么。所以上层容器map需要向底层的红黑树提供仿函数来获取T里面的Key,这样无论上层容器是set还是map,都可以用统一的方式进行比较了。
(2) 仿函数
仿函数让一个类的使用看上去像个函数。仿函数是在类中实现了一个operator( ),是一个类的对象,这个类就有了类似函数的行为,所以这个类就是一个仿函数类,目的是为了让函数拥有类的性质。
这个类的对象即仿函数,可以当作一般函数去用,只不过仿函数的功能是在一个类中的运算符operator()中实现的,使用的时候把函数作为参进行传递即可。
set有set的仿函数,map有map的仿函数,尽管set的仿函数看起来没有什么作用,但是,必须要把它传给底层红黑树,这样红黑树就能根据仿函数分别获取set的key和map的first。
①:set的仿函数
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
   | namespace delia { 	template<class K> 	class set 	{ 		 		struct SetKeyOfT 		{ 			const K& operator()(const K& key) 			{ 				return key; 			} 		};          public: 		bool insert(const K& k) 		{ 			_t.Insert(k); 			return true; 		}   	private: 		RBTree<K, K,SetKeyOfT> _t; 	}; }
   | 
 
②map的仿函数
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
   | namespace delia { 	template<class K,class V> 	class map 	{ 		 		struct MapKeyOfT 		{ 			const K& operator()(const pair<const K, V>& kv) 			{ 				return kv.first; 			} 		};       public:          		bool insert(const pair<const K, V>& kv) 		{ 			_t.Insert(kv); 			return true; 		} 	private: 		RBTree<K, pair<const K, V>, MapKeyOfT> _t; 	}; }
   | 
 
有了仿函数红黑树的类在实现时,就要在模板参数中增加KeyOfT仿函数。
(3)修改红黑树定义
1 2 3 4 5 6 7 8
   | template<class K, class T, class KeyOfT> class RBTree { 	typedef RBTreeNode<T> Node; 	 private: 	Node* _root; };
   | 
 
(4)修改红黑树插入
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
   |  pair<Node*, bool> Insert(const pair<K, V>& kv) { 	if (_root == nullptr) 	{ 		_root = new Node(kv); 		_root->_col = BLACK; 		return make_pair(_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(cur, false); 		} 	}
  	 	Node* newNode = new Node(kv); 	newNode->_col = RED; 	if (kot(parent->_data) < kot(data)) 	{ 		 		parent->_right = newNode; 		newNode->_parent = parent; 	} 	else 	{ 		 		parent->_left = newNode; 		newNode->_parent = parent; 	} 	cur = newNode;
  	 	while (parent && parent->_col == RED) 	{ 		 		Node* grandfather = parent->_parent; 		if (parent == grandfather->_left) 		{ 			Node* uncle = grandfather->_right; 			 			if (uncle->_col == RED) 			{ 				parent->_col = uncle->_col = BLACK; 				grandfather->_col = RED;
  				 				cur = grandfather; 				parent = cur->_parent; 			} 			else 			{ 				 				if (cur == parent->_left) 				{ 					RotateR(grandfather); 					parent->_col = BLACK; 					grandfather->_col = RED; 				} 				else 				{ 					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 = grandfather->_parent; 			} 			else 			{ 				 				if (cur == parent->_right) 				{ 					RotateL(grandfather); 					parent->_col = BLACK; 					grandfather->_col = RED; 				} 				else 				{ 					RotateR(parent); 					RotateL(grandfather); 					cur->_col = BLACK; 					grandfather->_col = RED; 				} 				break; 			} 		}
  	} 	_root->_col = BLACK;
  	return make_pair(newNode, true); }
  void RotateR(Node* parent) { 	Node* subL = parent->_left; 	Node* subLR = nullptr;
  	if (subL) 	{ 		subLR = subL->_right; 	} 	 	parent->_left = subLR;
  	if (subLR) 	{ 		subLR->_parent = parent; 	}
  	 	subL->_right = parent; 	Node* parentParent = parent->_parent; 	parent->_parent = subL;
 
  	if (parent == _root) 	{ 		_root = subL; 		_root->_parent = nullptr; 	} 	else 	{ 		if (parentParent->_left == parent) 		{ 			 			parentParent->_left = subL; 		} 		else 		{ 			 			parentParent->_right = subL; 		}
  		 		subL->_parent = parentParent; 	} }
  void RotateL(Node* parent) { 	Node* subR = parent->_right; 	Node* subRL = nullptr;
  	if (subR) 	{ 		subRL = subR->_left; 	}
  	 	parent->_right = subRL;
  	if (subRL) 	{ 		subRL->_parent = parent; 	}
  	 	subR->_left = parent; 	Node* parentParent = parent->_parent; 	parent->_parent = subR;
  	if (parent == _root) 	{ 		_root = parent; 		_root->_parent = nullptr; 	} 	else 	{ 		if (parentParent->_left == parent) 		{ 			 			parentParent->_left = subR; 		} 		else 		{ 			 			parentParent->_right = subR; 		}
  		 		subR->_parent = parentParent; 	} }
 
  | 
 
(5)修改红黑树查找
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   |  Node* 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 cur; 		} 	} 	return nullptr; }
 
  | 
 
三、红黑树迭代器
map和set的迭代器的实现其实本质上是红黑树迭代器的实现,迭代器的实现需要定义模板类型、模板类型引用、模板类型指针。 
1.红黑树中迭代器重命名
 在红黑树中重命名模板类型、模板类型引用、模板类型指针,定义为public,外部就能使用iterator了:
1 2 3 4 5 6 7 8 9 10 11 12 13
   | template<class K, class T, class KeyOfT> class RBTree { 	typedef RBTreeNode<T> Node;   public: 	typedef __TreeIterator<T, T&, T*> iterator;                private: 	Node* _root; };
   | 
 
2.正向迭代器定义
红黑树的迭代器的本质是对节点指针进行封装,所以迭代器中只有封装红黑树节点指针这一个成员变量 。正向迭代器:
1 2 3 4 5 6 7 8 9
   | template<class T,class Ref,class ptr> struct __TreeIterator { 	typedef RBTreeNode<T> Node; 	typedef __TreeIterator<T, Ref, ptr> Self;        	Node* _node; 	 };
   | 
 
3.迭代器构造
用节点指针构造正向迭代器:
1 2 3 4
   | //构造函数 __TreeIterator(Node* node) 	:_node(node) {}
   | 
 
4.正向迭代器重载*
Ref对正向迭代器解引用,返回节点数据引用
1 2 3 4 5
   |  Ref Operator*() { 	return _node->_data; }
 
  | 
 
5.正向迭代器重载->
Ptr对正向迭代器使用->,返回节点数据指针:
1 2 3 4 5
   |  Ptr Operator->() { 	return &_node->_data; }
 
  | 
 
6.正向迭代器重载==
判断节点是否相同
1 2 3 4 5
   |  bool operator==(const Self& s) { 	return _node == s._node; }
 
  | 
 
7.正向迭代器重载!=
判断节点是否不同
1 2 3 4 5
   |  bool operator!=(const Self& s) { 	return _node != s._node; }
 
  | 
 
8.正向迭代器++
①当节点的右子树不为空时,++就要走到右子树的最左节点
 ②当节点的右子树为空时,++就要走到节点的父亲
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
   | 	 	Self operator++() 	{ 		 		if (_node->_right) 		{ 			 			Node* left = _node->_right;   			 			while (left->_left) 			{ 				left = left->_left; 			} 			_node = left; 		} 		else 		{ 			Node* cur = _node; 			Node* parent = cur->_parent; 			while (parent && cur == parent->_right) 			{ 				cur = cur->_parent; 				parent = parent->_parent; 			} 			_node = parent; 		}   		return *this; 	} };
   | 
 
9.正向迭代器–
 ①当节点的左子树不为空时,++就要走到左子树的最右节点
 ②当节点的左子树为空时,++就要走到节点的父亲
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
   |  Self operator--() { 	 	if (_node->_left) 	{ 		 		Node* right = _node->_left;
  		 		while (right->_right) 		{ 			right = right->_right; 		} 		_node = right; 	} 	else 	{ 		Node* cur = _node; 		Node* parent = cur->_parent; 		while (parent && cur == parent->_left) 		{ 			cur = cur->_parent; 			parent = parent->_parent; 		} 		_node = parent; 	}
  	return *this; }
 
  | 
 
10.红黑树中实现迭代器
实现begin( )找最左节点,end( )最后一个节点的下一个位置
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
   | template<class K, class T, class KeyOfT> class RBTree { 	typedef RBTreeNode<T> Node;   public: 	typedef __TreeIterator<T, T&, T*> iterator;           	iterator begin() 	{ 		Node* left = _root; 		while (left && left->_left) 		{ 			left = left->_left; 		}   		return iterator(left) 	}   	 	iterator end() 	{ 		return iterator(nullptr); 	}      private: 	Node* _root; };
   | 
 
四、set模拟实现
调用红黑树对应接口实现set,插入和查找函数返回值当中的节点指针改为迭代器:
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
   | #pragma once #include "RBTree.h" namespace delia { 	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(); 		}   		 		pair<iterator,bool> insert(const K& key) 		{ 			 			return _t.Insert(key); 		}   		 		iterator find(const K& key) 		{ 			return _t.find(key); 		} 	private: 		RBTree<K, K, SetKeyOfT> _t; 	}; }
   | 
 
五、map模拟实现
调用红黑树对应接口实现map,插入和查找函数返回值当中的节点指针改为迭代器,增加operator[ ]的重载:
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
   | #pragma once #include "RBTree.h" namespace delia { 	template<class K, class V> 	class map 	{ 		 		struct MapKeyOfT 		{ 			const K& operator()(const pair<const K, V>& kv) 			{ 				return kv.first; 			} 		}; 	public: 		typedef typename RBTree<K, K, MapKeyOfT>::iterator iterator;   		 		iterator begin() 		{ 			return _t.begin(); 		}   		 		iterator end() 		{ 			return _t.end(); 		}   		 		pair<iterator, bool> insert(const pair<const K, V>& kv) 		{ 			return _t.Insert(kv); 		}   		 		V& operator[](const K& key) 		{ 			pair<iterator, bool> ret = insert(make_pair(key, V())); 			iterator it = ret.first; 			return it->second; 		}   		 		iterator find(const K& key) 		{ 			return _t.find(key); 		}   	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 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 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
   | #pragma once #include<iostream> 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& x) 		:_left(nullptr) 		, _right(nullptr) 		, _parent(nullptr) 		, _data(x) 		, _col(RED) 	{} };     template<class T,class Ref,class ptr> struct __TreeIterator { 	typedef RBTreeNode<T> Node; 	typedef __TreeIterator<T, Ref, ptr> Self;   	Node* _node;   	 	__TreeIterator(Node* node) 		:_node(node) 	{} 	 	 	Ref operator*() 	{ 		return _node->_data; 	}   	 	 	 	 	   	 	bool operator==(const Self& s) 	{ 		return _node == s._node; 	}   	 	bool operator!=(const Self& s) 	{ 		return _node != s._node; 	}   	 	Self operator++() 	{ 		 		if (_node->_right) 		{ 			 			Node* left = _node->_right;   			 			while (left->_left) 			{ 				left = left->_left; 			} 			_node = left; 		} 		else 		{ 			Node* cur = _node; 			Node* parent = cur->_parent; 			while (parent && cur == parent->_right) 			{ 				cur = cur->_parent; 				parent = parent->_parent; 			} 			_node = parent; 		}   		return *this; 	}   	 	Self operator--() 	{ 		 		if (_node->_left) 		{ 			 			Node* right = _node->_left;   			 			while (right->_right) 			{ 				right = right->_right; 			} 			_node = right; 		} 		else 		{ 			Node* cur = _node; 			Node* parent = cur->_parent; 			while (parent && cur == parent->_left) 			{ 				cur = cur->_parent; 				parent = parent->_parent; 			} 			_node = parent; 		}   		return *this; 	}     };  
 
 
    template<class K, class T, class KeyOfT> class RBTree { 	typedef RBTreeNode<T> Node; public: 	typedef __TreeIterator<T, T&, T*> iterator;   	 	RBTree() 		:_root(nullpte) 	{}   	 	~RBTree() 	{ 		_Destroy(_root); 		_root = nullptr; 	}   	void _Destroy(Node* root) 	{ 		if (root == nullptr) 		{ 			return; 		} 		_Destroy(root->_left); 		_Destroy(root->_right); 		delete root; 	}   	 	iterator begin() 	{ 		Node* left = _root; 		while (left && left->_left) 		{ 			left = left->_left; 		}   		return iterator(left); 	}   	 	iterator end() 	{ 		return iterator(nullptr); 	}   	 	RBTree() 		:_root(nullptr) 	{}   	void Destroy(Node* root) 	{ 		if (root == nullptr) 		{ 			return; 		}   		Destroy(root->_left); 		Destroy(root->_right); 	} 	~RBTree() 	{ 		Destroy(_root); 		_root = nullptr; 	}   	 	pair<Node*, bool> Insert(const T& data) 	{ 		if (_root == nullptr) 		{ 			_root = new Node(data); 			_root->_col = BLACK; 			return make_pair(_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(cur, false); 			} 		}   		 		Node* newNode = new Node(data); 		newNode->_col = RED; 		if (kot(parent->_data) < kot(data)) 		{ 			 			parent->_right = newNode; 			newNode->_parent = parent; 		} 		else 		{ 			 			parent->_left = newNode; 			newNode->_parent = parent; 		} 		cur = newNode;   		 		while (parent && parent->_col == RED) 		{ 			 			Node* grandfather = parent->_parent; 			if (parent == grandfather->_left) 			{ 				Node* uncle = grandfather->_right; 				 				if (uncle->_col == RED) 				{ 					parent->_col = uncle->_col = BLACK; 					grandfather->_col = RED;   					 					cur = grandfather; 					parent = cur->_parent; 				} 				else 				{ 					 					if (cur == parent->_left) 					{ 						RotateR(grandfather); 						parent->_col = BLACK; 						grandfather->_col = RED; 					} 					else 					{ 						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 = grandfather->_parent; 				} 				else 				{ 					 					if (cur == parent->_right) 					{ 						RotateL(grandfather); 						parent->_col = BLACK; 						grandfather->_col = RED; 					} 					else 					{ 						RotateR(parent); 						RotateL(grandfather); 						cur->_col = BLACK; 						grandfather->_col = RED; 					} 					break; 				} 			}   		} 		_root->_col = BLACK;   		return make_pair(newNode, true); 	}   	void RotateR(Node* parent) 	{ 		Node* subL = parent->_left; 		Node* subLR = nullptr;   		if (subL) 		{ 			subLR = subL->_right; 		} 		 		parent->_left = subLR;   		if (subLR) 		{ 			subLR->_parent = parent; 		}   		 		subL->_right = parent; 		Node* parentParent = parent->_parent; 		parent->_parent = subL;     		if (parent == _root) 		{ 			_root = subL; 			_root->_parent = nullptr; 		} 		else 		{ 			if (parentParent->_left == parent) 			{ 				 				parentParent->_left = subL; 			} 			else 			{ 				 				parentParent->_right = subL; 			}   			 			subL->_parent = parentParent; 		} 	}   	void RotateL(Node* parent) 	{ 		Node* subR = parent->_right; 		Node* subRL = nullptr;   		if (subR) 		{ 			subRL = subR->_left; 		}   		 		parent->_right = subRL;   		if (subRL) 		{ 			subRL->_parent = parent; 		}   		 		subR->_left = parent; 		Node* parentParent = parent->_parent; 		parent->_parent = subR;   		if (parent == _root) 		{ 			_root = parent; 			_root->_parent = nullptr; 		} 		else 		{ 			if (parentParent->_left == parent) 			{ 				 				parentParent->_left = subR; 			} 			else 			{ 				 				parentParent->_right = subR; 			}   			 			subR->_parent = parentParent; 		} 	}   	 	Node* 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 cur; 			} 		} 		return nullptr; 	}   	bool _CheckBalance(Node* root, int blackNum, int count) 	{ 		if (root == nullptr) 		{ 			if (count != blackNum) 			{ 				cout << "黑色节点数量不相等" << endl; 				return false; 			} 			return true; 		}   		if (root->_col == RED && root->_parent->_col == RED) 		{ 			cout << "存在连续红色节点" << endl; 			return false; 		}   		if (root->_col == BLACK) 		{ 			count++; 		}   		return _CheckBalance(root->_left, blackNum, count) 			&& _CheckBalance(root->_right, blackNum, count); 	}   	 	bool CheckBalance() 	{ 		if (_root == nullptr) 		{ 			return true; 		}   		if (_root->_col == RED) 		{ 			cout << "根节点为红色" << endl; 			return false; 		}   		 		int blackNum = 0; 		Node* left = _root; 		while (left) 		{ 			if (left->_col == BLACK) 			{ 				blackNum++; 			} 			left = left->_left; 		}   		int count = 0; 		return _CheckBalance(_root, blackNum, count); 	}     	 	void _InOrder(Node* root) 	{ 		if (root == nullptr) 		{ 			return; 		}   		_InOrder(root->_left); 		cout << root->_kv.first << ":" << root->_kv.second << endl; 		_InOrder(root->_right); 	}   	void InOrder() 	{ 		_InOrder(_root); 		cout << endl; 	} private: 	Node* _root; };
   | 
 
七、验证代码
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
   | #pragma once #include "RBTree.h" #include <vector> #include <stdlib.h> #include <time.h> #include "Map.h" #include "Set.h"   int main() { 	delia::map<int, int> m; 	m.insert(make_pair(1, 1)); 	m.insert(make_pair(3, 3)); 	m.insert(make_pair(0, 0)); 	m.insert(make_pair(9, 9));     	delia::set<int> s; 	s.insert(1); 	s.insert(5); 	s.insert(2); 	s.insert(1); 	s.insert(13); 	s.insert(0); 	s.insert(15); 	s.insert(18);     	delia::set<int>::iterator sit = s.begin(); 	while (sit != s.end()) 	{ 		cout << *sit << " "; 		++sit; 	} 	cout << endl;     	return 0; }
   |