• 【map、set的封装】


    前言

    本文的代码是基于前一篇文章的红黑树的代码来封装map、set。

    一、map、set的框架搭建

    在这里插入图片描述
    代码:
    map:

    namespace Ting
    {
    	template<class K,class V>
    	class map
    	{
    	public:
    		struct KeyOfT
    		{
    			K& operator()(const pair<K,V>& p)
    			{
    				return p.first;
    			}
    		};
    	private:
    		RBTree<K, pair<K,V>, KeyOfT> _t;
    	};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    set:

    namespace Ting
    {
    	template<class K>
    	class set
    	{
    	public:
    		struct KeyOfT
    		{
    			K& operator()(const K& key)
    			{
    				return key;
    			}
    		};
    	private:
    		RBTree<K, K,KeyOfT> _t;
    	};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    二、map、set的迭代器的封装

    2.1、map、set的迭代器的初步封装

    首先先对树的迭代器进行代码编写

    template<class T>
    class __TreeIterator
    {
    	typedef __TreeIterator<T> self;
    public:
    	typedef RBTreeNode<T> Node;
    
    	__TreeIterator(Node* node)
    		:_node(node)
    	{}
    
    
    	self& operator++()
    	{
    		if (_node->_right)
    		{
    			//找node右子树的最左结点
    			Node* cur = _node->_right;
    			while (cur->_left)
    			{
    				cur = cur->_left;
    			}
    			_node = cur;
    		}
    		else
    		{
    			Node* cur = _node;
    			Node* parent = _node->_parent;
    			while (parent)
    			{
    				if (cur == parent->_left)
    				{
    					break;
    				}
    				else
    				{
    					cur = parent;
    					parent = parent->_parent;
    				}
    			}
    			_node = parent;
    		}
    		return *this;
    	}
    
    	T* operator->()
    	{
    		return &_node->_date;
    	}
    
    	T& operator*()
    	{
    		return _node->_date;
    	}
    
    	bool operator!=(const self& s)
    	{
    		return _node != s._node;
    	}
    private:
    	Node* _node;
    };
    
    • 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
    template<class K,class T,class KeyOfT>
    class RBTree
    {
    	typedef RBTreeNode<T> Node;
    public:
    	typedef  __TreeIterator<T> iterator;
    
    	RBTree()
    		:_root(nullptr)
    	{}
    
    	iterator begin()
    	{
    		Node* cur = _root;
    		while (cur&&cur->_left)
    		{
    			cur = cur->_left;
    		}
    		return iterator(cur);
    	}
    
    	iterator end()
    	{
    		return 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

    map

    #pragma once
    #include"RBTree.h"
    namespace Ting
    {
    	template<class K,class V>
    	class map
    	{
    	public:
    		struct KeyOfT
    		{
    			K operator()(const pair<K,V>& p)
    			{
    				return p.first;
    			}
    		};
    
    		typedef typename RBTree<K, pair<K, V>, KeyOfT>::iterator iterator;
    
    		iterator begin()
    		{
    			return _t.begin();
    		}
    
    		iterator end()
    		{
    			return _t.end();
    		}
    
    		bool insert(const pair<K, V>& p)
    		{
    			return _t.insert(p);
    		}
    
    	private:
    		RBTree<K, pair<K,V>, KeyOfT> _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

    set

    #pragma once
    #include"RBTree.h"
    namespace Ting
    {
    	template<class K>
    	class set
    	{
    
    	public:
    		struct KeyOfT
    		{
    			K operator()(const K& key)
    			{
    				return key;
    			}
    		};
    		typedef typename RBTree<K, K, KeyOfT>::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,KeyOfT> _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

    2.2、map、set的const迭代器的封装

    2.2.1、set的const迭代器的封装

    目标:set的key不能被修改
    第一步:先利用模板参数设计出树的const的迭代器

    template<class T,class Ptr,class Ref>
    class __TreeIterator
    {
    	typedef __TreeIterator<T,Ptr,Ref> self;
    public:
    	typedef RBTreeNode<T> Node;
    
    	__TreeIterator(Node* node)
    		:_node(node)
    	{}
    
    
    	self& operator++()
    	{
    		if (_node->_right)
    		{
    			//找node右子树的最左结点
    			Node* cur = _node->_right;
    			while (cur->_left)
    			{
    				cur = cur->_left;
    			}
    			_node = cur;
    		}
    		else
    		{
    			Node* cur = _node;
    			Node* parent = _node->_parent;
    			while (parent)
    			{
    				if (cur == parent->_left)
    				{
    					break;
    				}
    				else
    				{
    					cur = parent;
    					parent = parent->_parent;
    				}
    			}
    			_node = parent;
    		}
    		return *this;
    	}
    
    	Ptr operator->()
    	{
    		return &_node->_date;
    	}
    
    	Ref operator*()
    	{
    		return _node->_date;
    	}
    
    	bool operator!=(const self& s)
    	{
    		return _node != s._node;
    	}
    private:
    	Node* _node;
    };
    
    • 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

    在这里插入图片描述
    第二步: begin()、end()

    	typedef  __TreeIterator<T, T*, T&> iterator;
    	typedef  __TreeIterator<T, const T*, const T&> const_iterator;
    
    
    	RBTree()
    		:_root(nullptr)
    	{}
    
    	iterator begin()
    	{
    		Node* cur = _root;
    		while (cur&&cur->_left)
    		{
    			cur = cur->_left;
    		}
    		return iterator(cur);
    	}
    
    	iterator end()
    	{
    		return iterator(nullptr);
    	}
    
    	const_iterator begin() const
    	{
    		Node* cur = _root;
    		while (cur && cur->_left)
    		{
    			cur = cur->_left;
    		}
    		return const_iterator(cur);
    	}
    
    	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
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    第三步: 很关键也很巧妙
    在这里插入图片描述

    2.2.2、map的const迭代器的封装

    #pragma once
    #include"RBTree.h"
    namespace Ting
    {
    	template<class K,class V>
    	class map
    	{
    	public:
    		struct KeyOfT
    		{
    			K operator()(const pair<K,V>& p)
    			{
    				return p.first;
    			}
    		};
    
    		typedef typename RBTree<K, pair<const K, V>, KeyOfT>::iterator iterator;
    		typedef typename RBTree<K, pair<const K, V>, KeyOfT>::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();
    		}
    
    		bool insert(const pair<K, V>& p)
    		{
    			return _t.insert(p);
    		}
    
    	private:
    		RBTree<K, pair<const K,V>, KeyOfT> _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

    在这里插入图片描述

    三、operator[ ]

    **第一步:**对RBTree中插入的返回值类型进行修改

    pair<iterator,bool> insert(const T& date)
    	{
    		if (_root == nullptr)
    		{
    			_root = new Node(date);
    			_root->_col = BLACK;
    			return make_pair(_root,true);
    		}
    		KeyOfT kt;
    
    		Node* parent = _root;
    		Node* cur = _root;
    		while (cur)
    		{
    			if (kt(cur->_date)> kt(date))
    			{
    				parent = cur;
    				cur = cur->_left;
    			}
    			else if ((kt(cur->_date) < kt(date)))
    			{
    				parent = cur;
    				cur = cur->_right;
    			}
    			else
    			{
    				return make_pair(cur, false);
    			}
    		}
    
    		cur = new Node(date);
    		Node* newnode = cur;
    		if (kt(parent->_date) > kt(date))
    		{
    			parent->_left = cur;
    		}
    		else
    		{
    			parent->_right = 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 = BLACK;
    					uncle->_col = BLACK;
    					grandfather->_col = RED;
    
    					cur = grandfather;
    					parent = cur->_parent;
    				}
    				//叔叔不存在或者叔叔是黑色
    				else
    				{
    					if (cur == parent->_left)
    					{
    						RotateR(grandfather);
    						grandfather->_col = RED;
    						parent->_col = BLACK;
    					}
    					else
    					{
    						RotateL(parent);
    						RotateR(grandfather);
    
    						cur->_col = BLACK;
    						grandfather->_col = RED;
    
    					}
    					break;
    				}
    			}
    			else
    			{
    				Node* uncle = grandfather->_left;
    				if (uncle && uncle->_col == RED)
    				{
    					parent->_col = BLACK;
    					uncle->_col = BLACK;
    					grandfather->_col = RED;
    
    					cur = grandfather;
    					parent = cur->_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);
    
    	}
    
    • 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

    然后该完后会发现编译报错。如图:
    在这里插入图片描述
    原因是:
    在这里插入图片描述
    **解决办法:**给迭代器写个构造函数让迭代器支持普通迭代器构造const迭代器
    在这里插入图片描述

    V& operator[](const K& key)
    {
    	pair < iterator, bool > ret= _t.insert(make_pair(key, V()));
    	return ret.first->second;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    四、代码实现

    MySet.h

    #pragma once
    #include"RBTree.h"
    namespace Ting
    {
    	template<class K>
    	class set
    	{
    
    	public:
    		struct KeyOfT
    		{
    			K operator()(const K& key)
    			{
    				return key;
    			}
    		};
    		typedef typename RBTree<K, K, KeyOfT>::const_iterator iterator;
    		typedef typename RBTree<K, K, KeyOfT>::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, KeyOfT>::iterator, bool> ret = _t.insert(key);
    			return make_pair(ret.first, ret.second);
    		}
    
    	private:
    		RBTree<K, K,KeyOfT> _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

    MyMap.h

    #pragma once
    #include"RBTree.h"
    namespace Ting
    {
    	template<class K,class V>
    	class map
    	{
    	public:
    		struct KeyOfT
    		{
    			K operator()(const pair<K,V>& p)
    			{
    				return p.first;
    			}
    		};
    
    		typedef typename RBTree<K, pair<const K, V>, KeyOfT>::iterator iterator;
    		typedef typename RBTree<K, pair<const K, V>, KeyOfT>::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>& p)
    		{
    			return _t.insert(p);
    		}
    
    		V& operator[](const K& key)
    		{
    			pair < iterator, bool > ret= _t.insert(make_pair(key, V()));
    			return ret.first->second;
    
    		}
    
    	private:
    		RBTree<K, pair<const K,V>, KeyOfT> _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

    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 _date;
    	Colour _col;
    
    	RBTreeNode(const T& date)
    		:_left(nullptr)
    		, _right(nullptr)
    		, _parent(nullptr)
    		, _date(date)
    		, _col(RED)
    	{}
    
    };
    
    template<class T,class Ptr,class Ref>
    struct __TreeIterator
    {
    	typedef __TreeIterator<T, Ptr, Ref> self;
    	typedef __TreeIterator<T, T*, T&> Iterator;
    	typedef RBTreeNode<T> Node;
    
    	__TreeIterator(const Iterator& it)
    		:_node(it._node)
    	{}
    
    	__TreeIterator(Node* node)
    		:_node(node)
    	{}
    
    
    	self& operator++()
    	{
    		if (_node->_right)
    		{
    			//找node右子树的最左结点
    			Node* cur = _node->_right;
    			while (cur->_left)
    			{
    				cur = cur->_left;
    			}
    			_node = cur;
    		}
    		else
    		{
    			Node* cur = _node;
    			Node* parent = _node->_parent;
    			while (parent)
    			{
    				if (cur == parent->_left)
    				{
    					break;
    				}
    				else
    				{
    					cur = parent;
    					parent = parent->_parent;
    				}
    			}
    			_node = parent;
    		}
    		return *this;
    	}
    
    	Ptr operator->()
    	{
    		return &_node->_date;
    	}
    
    	Ref operator*()
    	{
    		return _node->_date;
    	}
    
    	bool operator!=(const self& s)
    	{
    		return _node != s._node;
    	}
    
    	Node* _node;
    };
    
    template<class K,class T,class KeyOfT>
    class RBTree
    {
    	typedef RBTreeNode<T> Node;
    public:
    	typedef  __TreeIterator<T, T*, T&> iterator;
    	typedef  __TreeIterator<T, const T*, const T&> const_iterator;
    
    	
    	RBTree()
    		:_root(nullptr)
    	{}
    
    	iterator begin()
    	{
    		Node* cur = _root;
    		while (cur&&cur->_left)
    		{
    			cur = cur->_left;
    		}
    		return iterator(cur);
    	}
    
    	iterator end()
    	{
    		return iterator(nullptr);
    	}
    
    	const_iterator begin() const
    	{
    		Node* cur = _root;
    		while (cur && cur->_left)
    		{
    			cur = cur->_left;
    		}
    		return const_iterator(cur);
    	}
    
    	const_iterator end() const
    	{
    		return const_iterator(nullptr);
    	}
    
    	pair<iterator,bool> insert(const T& date)
    	{
    		if (_root == nullptr)
    		{
    			_root = new Node(date);
    			_root->_col = BLACK;
    			return make_pair(_root,true);
    		}
    		KeyOfT kt;
    
    		Node* parent = _root;
    		Node* cur = _root;
    		while (cur)
    		{
    			if (kt(cur->_date)> kt(date))
    			{
    				parent = cur;
    				cur = cur->_left;
    			}
    			else if ((kt(cur->_date) < kt(date)))
    			{
    				parent = cur;
    				cur = cur->_right;
    			}
    			else
    			{
    				return make_pair(cur, false);
    			}
    		}
    
    		cur = new Node(date);
    		Node* newnode = cur;
    		if (kt(parent->_date) > kt(date))
    		{
    			parent->_left = cur;
    		}
    		else
    		{
    			parent->_right = 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 = BLACK;
    					uncle->_col = BLACK;
    					grandfather->_col = RED;
    
    					cur = grandfather;
    					parent = cur->_parent;
    				}
    				//叔叔不存在或者叔叔是黑色
    				else
    				{
    					if (cur == parent->_left)
    					{
    						RotateR(grandfather);
    						grandfather->_col = RED;
    						parent->_col = BLACK;
    					}
    					else
    					{
    						RotateL(parent);
    						RotateR(grandfather);
    
    						cur->_col = BLACK;
    						grandfather->_col = RED;
    
    					}
    					break;
    				}
    			}
    			else
    			{
    				Node* uncle = grandfather->_left;
    				if (uncle && uncle->_col == RED)
    				{
    					parent->_col = BLACK;
    					uncle->_col = BLACK;
    					grandfather->_col = RED;
    
    					cur = grandfather;
    					parent = cur->_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);
    
    	}
    
    	bool isBalance()
    	{
    		return isBalance(_root);
    	}
    
    	bool checkcolour(Node* root, int basevalue,int blacknum)
    	{
    		if (root == nullptr)
    		{
    			if (basevalue != blacknum)
    			{
    				return false;
    			}
    			return true;
    		}
    		if (root->_col == RED && root->_parent && root->_parent->_col == RED)
    		{
    			return false;
    		}
    		if (root->_col == BLACK)
    		{
    			blacknum++;
    		}
    		return checkcolour(root->_left, basevalue, blacknum) &&
    			   checkcolour(root->_right, basevalue, blacknum);
    	}
    
    	bool isBalance(Node* root)
    	{
    		if (root == nullptr)
    		{
    			return true;
    		}
    		if (root->_col != BLACK)
    		{
    			return false;
    		}
    		//计算最左路基中黑色结点数量
    		int basevalue = 0;
    		Node* cur = root;
    		while (cur)
    		{
    			if (cur->_col == BLACK)
    			{
    				basevalue++;
    			}
    			cur = cur->_left;
    		}
    
    		return checkcolour(root, basevalue,0);
    	}
    
    	void inorder()
    	{
    		inorder(_root);
    	}
    
    	void inorder(Node* root)
    	{
    		if (root == nullptr)
    		{
    			return;
    		}
    		inorder(root->_left);
    		cout << root->_p.first << endl;
    		inorder(root->_right);
    	}
    
    
    private:
    	Node* _root;
    	void RotateL(Node* parent)
    	{
    	//右边高  左旋
    	Node* cur = parent->_right;
    	Node* curleft = cur->_left;
    	Node* ppnode = parent->_parent;
    
    	parent->_right = curleft;
    	if (curleft)
    	{
    		curleft->_parent = parent;
    	}
    	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* ppnode = parent->_parent;
    
    		parent->_left = curRight;
    		if (curRight)
    		{
    			curRight->_parent = parent;
    		}
    
    		cur->_right = 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;
    		}
    	}
    };
    
    • 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
  • 相关阅读:
    uni-app之android离线打包
    对elementui中一个el-form-item中包含多个输入框或下拉框等的表单验证处理问题
    佛山科学技术学院考研介绍
    教你如何玩转海外版抖音TikTok跨境电商独立站
    [NISACTF 2022]popchains - 反序列化+伪协议
    Linux串口编程进阶
    备战蓝桥杯Day20 - 堆排序的实现
    计算机毕业设计ssm高校就业管理系统157v3系统+程序+源码+lw+远程部署
    【Java 进阶篇】Java会话技术之Cookie的存活时间
    JAVA中自定义扩展Swagger的能力,自动生成参数取值含义说明,提升开发效率
  • 原文地址:https://blog.csdn.net/qq_69544758/article/details/133031054