C++学习记录——이십이 红黑树以及map、set的封装


1、了解概念

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

性质:

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

对于第四条,也就是说(从根节点走到空)每个路径的黑结点数量都相同。

根据以上性质,最短路径是全黑,最长路径是红黑相间。

在这里插入图片描述

一颗红黑树可能不是最长和最短路径都有。假设有N个黑节点,那么最短路径长度就是logN,整棵树的节点数量区间是[N, 2N],最长路径长度是2logN。

单纯比较增删查改效率的话,红黑树比不过AVL树。在插入10亿个结点的情况下,AVL树最多查找30次,红黑树则是60次,不过这个次数没有差别。但实际生活中用红黑树多,红黑树性能不弱于AVL树,虽然没有超过,但因为它是近似平衡,不像AVL树那样严格的平衡,旋转次数少的原因,所以红黑树用得多,AVL树用得很少。

2、模拟实现

enum Colour
{
    
    
	RED,
	BLACK,
};

template<class K, class V>
struct RBTreeNode
{
    
    
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;
	pair<K, V> _kv;
	Colour _col;

	RBTreeNode(const pair<K, V>& kv)
		:_left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _kv(kv)
		, _col(RED)
	{
    
    }
};

template<class K, class V>
class RBTree
{
    
    
	typedef RBTreeNode<K, V> Node;
public:
	bool Insert(const pair<K, V>& kv)
	{
    
    
		if (_root == nullptr)
		{
    
    
			_root = new Node(kv);
			_root->_col = BLACK;
			return true;
		}
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
    
    
			if (cur->_kv.first < kv.first)
			{
    
    
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
    
    
				parent = cur;
				cur = cur->_left;
			}
			else
			{
    
    
				return false;
			}
		}
		cur = new Node(kv);
		if (parent->_kv.first > kv.first)
		{
    
    
			parent->_left = cur;
		}
		else
		{
    
    
			parent->_right = cur;
		}
		cur->_parent = parent;
		return true;
	}
private:
	Node* _root;
};

1、插入

第1种情况

创建新节点的时候,应该让这个结点是什么颜色的?如果是黑色,那么就不能保证每条路径黑节点数都相同了,所以要红色。新增后,如果父节点是黑节点,那就不做什么,如果是红色,这就有一些情况。

在这里插入图片描述

现在只看右边新插入节点的情况,假设新增结点是cur。cur颜色不能动,那么parent一定要变黑才符合性质;爷节点是黑的;那么还有一个改变就是叔叔结点要变为黑结点,也就是和父结点同一层的那个。这样改变后为了保证每个路径黑节点数量一样,那么爷节点就得变红。

在这里插入图片描述

这样之后还需要判断,爷节点的父节点如果是黑,那就不做什么,如果是红,就把爷节点当作cur,重复刚才的动作。

相当于parent和uncle变黑,grandfather变红,然后g变为cur,继续向上调整。

当整理完一个子树后,向上调整,还会遇到上方的其他节点的子点的子树。

第2种情况

cur有可能是新增节点为红,也有可能是下面变色而来的红色。

在这里插入图片描述

上图中的部分也就是下图中cur之下的那一部分。

在这里插入图片描述

为了所有路径都有同样数量的黑节点,cde的情况有四种,都是包含一个黑节点的红黑子树

在这里插入图片描述

现在的代码

		while (parent&& parent->_col == RED)
		{
    
    
			Node* gf = parent->_parent;
			if (gf->_left == parent)
			{
    
    
				Node* uncle = gf->_right;
				if (uncle && uncle->_col == RED)
				{
    
    
					parent->_col = BLACK;
					uncle->_col = BLACK;
					gf->_col = RED;
					cur = gf;
					parent = cur->_parent;
				}
			}
		}
		_root->_col = BLACK;//因为上面的循环有可能把根节点变成红色的,所以出来循环就直接变黑

第3种情况

在这里插入图片描述

第三个插入节点的情况,也就是1->6->节点,这时候没有叔叔结点,此时1是黑,6是红,连红肯定不行,那么6要变为黑。因为这个红节点的插入,这个路径相关的性质以及整个树的高度已经变化了,可能最长路径已经是最短路径的二倍多了,所以这里需要旋转,结果就是6成为根,1和红节点都是子节点,并且把1变为红,那么路径性质就符合了。如果插在6的右边,那就左旋。

相当于旋转+变色,parent变黑,grandfather变红,parent做新的根,其余两个为子节点。

在这里插入图片描述

叔叔节点存在且为黑的话,我们还是看cur节点,它为红,cur节点可能以前为黑,之后由下面变色而来,cur就是红色了。为了保证整体路径上黑节点数量的相同,那么c应当是xyzm某一种,然后de可能是空或者红节点,这时候因为左边的子树已经高度超了,所以得旋转

在这里插入图片描述
让c成为g的子节点,并对它们进行变色,如果父节点在爷节点左边,那就右旋,右边则左旋。父节点变黑,爷节点变红。

更复杂的情况是这样的。cur为红,是parent的右子节点,parent为红,grandfather为黑,叔叔结点存在且为黑或不存在,不存在就说明cur是新增节点,存在为黑那么cur就是由下面变色而来的。这时候以cur和parent的形状来看,单旋并不能解决问题,所以需要双旋。

在这里插入图片描述

以p为轴点进行单旋

在这里插入图片描述
cur的左节点给parent,cur做parent的父节点。

然后对g进行单旋。

在这里插入图片描述

cur剩余的节点给到grandfather的左边,然后cur作为新根。

也就是说叔叔节点不存在或存在且为黑时,需要旋转,如果爷,父,子节点是一条直线的,那就是单旋,如果是折线,那就是双旋。

2、插入代码

bool Insert(const pair<K, V>& data)
	{
    
    
		if (_root == nullptr)
		{
    
    
			_root = new Node(data);
			_root->_col = BLACK;
			return true;
		}
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
    
    
			if (cur->_kv.first < data.first)
			{
    
    
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > data.first)
			{
    
    
				parent = cur;
				cur = cur->_left;
			}
			else
			{
    
    
				return false;
			}
		}
		cur = new Node(data);
		if (parent->_kv.first > data.first)
		{
    
    
			parent->_left = cur;
		}
		else
		{
    
    
			parent->_right = cur;
		}
		cur->_parent = parent;
		
		while (parent && parent->_col == RED)
		{
    
    
			Node* gf = parent->_parent;
			if (gf->_left == parent)
			{
    
    
				Node* uncle = gf->_right;
				//情况1:u存在且为红,变色处理,并继续往上处理
				if (uncle && uncle->_col == RED)
				{
    
    
					parent->_col = BLACK;
					uncle->_col = BLACK;
					gf->_col = RED;
					cur = gf;
					parent = cur->_parent;
				}
				else//情况2+3:u不存在或者u存在且为黑,旋转+变色
				{
    
    
					//      g   
					//   p     u
					// c
					if (cur == parent->_left)
					{
    
    
						RotateR(gf);
						parent->_col = BLACK;
						gf->_col = RED;
					}
					else
					{
    
    
						//      g
						//   p     u
						//      c
						RotateL(parent);
						RotateR(gf);
						cur->_col = BLACK;
						//parent->_col = RED;
						gf->_col = RED;
					}
					break;
				}
			}
			else if(gf->_right == parent)
			{
    
    
				//      g
				//   u     p
				//            c
				Node* uncle = gf->_right;
				//u存在且为红,变色处理,并继续往上处理
				if (uncle && uncle->_col == RED)
				{
    
    
					parent->_col = BLACK;
					uncle->_col = BLACK;
					gf->_col = RED;
					cur = gf;
					parent = cur->_parent;
				}
				else//u不存在或者u存在且为黑,旋转+变色
				{
    
    
					//     g
					//   u   p
					//         c
					if (cur == parent->_right)
					{
    
    
						RotateL(gf);
						gf->_col = RED;
						parent->_col = BLACK;
					}
					else
					{
    
    
						//     g
						//   u   p
						//      c
						RotateR(parent);
						RotateL(gf);
						cur->_col = BLACK;
						gf->_col = RED;
					}
					break;
				}
			}
		}
		_root->_col = BLACK;
		return true;
	}
	
 	void RotateL(Node* parent)
	{
    
    
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		Node* ppnode = parent->_parent;

		subR->_left = parent;
		parent->_parent = subR;

		if (ppnode == nullptr)
		{
    
    
			_root = subR;
			_root->_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;

		Node* ppnode = parent->_parent;

		subL->_right = parent;
		parent->_parent = subL;

		if (parent == _root)
		{
    
    
			_root = subL;
			_root->_parent = nullptr;
		}
		else
		{
    
    
			if (ppnode->_left == parent)
			{
    
    
				ppnode->_left = subL;
			}
			else
			{
    
    
				ppnode->_right = subL;
			}
			subL->_parent = ppnode;
		}
	}

3、测试是否是红黑树

判断路径2倍这个点不可取,不是充要条件。我们要判断的是根节点颜色,是否有连红,以及黑节点的数量。我们可以添加一个blackNum以及定义一个benchmark变量。根据性质来判断即可。

	void IsBalance()
	{
    
    
		if (_root && _root->_col == RED)
		{
    
    
			cout << "根节点是红色" << endl;
			return false;
		}
		int benchmark = 0;//黑节点参考值,选择一条路径上的黑节点数量做参考值即可
		Node* cur = _root;
		while (cur)
		{
    
    
			if (cur->_col == BLACK) ++benchmark;
			cur = cur->_left;
		}
		return _Check(_root, 0, benchmark);
	}
	
	bool _Check(Node* root, int blackNum, int benchmark)
	{
    
    
		if (root == nullptr)
		{
    
    
			if (benchmark != blackNum)
			{
    
    
				cout << "某条路径黑节点的数量不相等" << endl;
				return false;
			}
			return true;
		}
		if (root->_col == BLACK) ++blackNum;
		if (root->_col == RED
			&& root->_parent
			&& root->_parent->_col == RED)
		{
    
    
			cout << "存在连续的红色节点" << endl;
			return false;
		}
		return _Check(root->_left, blackNum, benchmark)
			&& _Check(root->_right, blackNum, benchmark);
	}
	
	int _High(Node* root)//测量高度
	{
    
    
		if (root == NULL) return 0;
		int leftH = _High(root->_left);
		int rightH = _High(root->_lright);
		return leftH > rightH ? leftH + 1 : rightH + 1;
	}

4、完整代码

enum Colour
{
    
    
	RED,
	BLACK,
};

template<class K, class V>
struct RBTreeNode
{
    
    
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;
	pair<K, V> _kv;
	Colour _col;

	RBTreeNode(const pair<K, V>& kv)
		:_left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _kv(kv)
		, _col(RED)
	{
    
    }
};

template<class K, class V>
class RBTree
{
    
    
	typedef RBTreeNode<K, V> Node;
public:
	~RBTree()
	{
    
    
		_Destroy(_root);
		_root = nullptr;
	}

	Node* Find(const K& key)
	{
    
    
		Node* cur = _root;
		while (cur)
		{
    
    
			if (cur->_kv.first < key)
			{
    
    
				cur = cur->_right;
			}
			else if (cur->_kv.first > key)
			{
    
    
				cur = cur->_left;
			}
			else
			{
    
    
				return cur;
			}
		}

		return nullptr;
	}

	bool Insert(const pair<K, V>& kv)
	{
    
    
		if (_root == nullptr)
		{
    
    
			_root = new Node(kv);
			_root->_col = BLACK;
			return true;
		}
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
    
    
			if (cur->_kv.first < kv.first)
			{
    
    
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
    
    
				parent = cur;
				cur = cur->_left;
			}
			else
			{
    
    
				return false;
			}
		}
		cur = new Node(kv);
		if (parent->_kv.first > kv.first)
		{
    
    
			parent->_left = cur;
		}
		else
		{
    
    
			parent->_right = cur;
		}
		cur->_parent = parent;

		while (parent && parent->_col == RED)
		{
    
    
			Node* gf = parent->_parent;
			if (gf->_left == parent)
			{
    
    
				Node* uncle = gf->_right;
				// u存在且为红,变色处理,并继续往上处理
				if (uncle && uncle->_col == RED)
				{
    
    
					parent->_col = BLACK;
					uncle->_col = BLACK;
					grandfather->_col = RED;

					// 继续往上调整
					cur = gf;
					parent = cur->_parent;
				}
				else // u不存在/u存在且为黑,旋转+变色
				{
    
    
					//     g
					//   p   u
					// c 
					if (cur == parent->_left)
					{
    
    
						RotateR(gf);
						parent->_col = BLACK;
						gf->_col = RED;
					}
					else
					{
    
    
						//     g
						//   p   u
						//     c
						RotateL(parent);
						RotateR(gf);
						cur->_col = BLACK;
						//parent->_col = RED;
						gf->_col = RED;
					}
					break;
				}
			}
			else if(gf->_right == parent)
			{
    
    
				//    g
				//  u   p
				//        c
				Node* uncle = gf->_left;
				// u存在且为红,变色处理,并继续往上处理
				if (uncle && uncle->_col == RED)
				{
    
    
					parent->_col = BLACK;
					uncle->_col = BLACK;
					gf->_col = RED;

					// 继续往上调整
					cur = gf;
					parent = cur->_parent;
				}
				else // 情况2+3:u不存在/u存在且为黑,旋转+变色
				{
    
    
					//    g
					//  u   p
					//        c
					if (cur == parent->_right)
					{
    
    
						RotateL(gf);
						gf->_col = RED;
						parent->_col = BLACK;
					}
					else
					{
    
    
						//    g
						//  u   p
						//    c
						RotateR(parent);
						RotateL(gf);
						cur->_col = BLACK;
						gf->_col = RED;
					}
					break;
				}
			}
		}
		_root->_col = BLACK;
		return true;
	}

	void InOrder()
	{
    
    
		_InOrder(_root);
	}

	bool IsBalance()
	{
    
    
		if (_root && _root->_col == RED)
		{
    
    
			cout << "根节点颜色是红色" << endl;
			return false;
		}
		int benchmark = 0;//黑节点参考值,选择一条路径上的黑节点数量做参考值即可
		Node* cur = _root;
		while (cur)
		{
    
    
			if (cur->_col == BLACK)
				++benchmark;
			cur = cur->_left;
		}
		// 连续红色节点
		return _Check(_root, 0, benchmark);
	}

	int Height()
	{
    
    
		return _Height(_root);
	}
private:
	void _Destroy(Node* root)
	{
    
    
		if (root == nullptr)
		{
    
    
			return;
		}

		_Destroy(root->_left);
		_Destroy(root->_right);
		delete root;
	}

	int _Height(Node* root)
	{
    
    
		if (root == NULL)
			return 0;
		int leftH = _Height(root->_left);
		int rightH = _Height(root->_right);
		return leftH > rightH ? leftH + 1 : rightH + 1;
	}

	bool _Check(Node* root, int blackNum, int benchmark)
	{
    
    
		if (root == nullptr)
		{
    
    
			if (benchmark != blackNum)
			{
    
    
				cout << "某条路径黑色节点的数量不相等" << endl;
				return false;
			}
			return true;
		}
		if (root->_col == BLACK)
		{
    
    
			++blackNum;
		}
		if (root->_col == RED
			&& root->_parent
			&& root->_parent->_col == RED)
		{
    
    
			cout << "存在连续的红色节点" << endl;
			return false;
		}
		return _Check(root->_left, blackNum, benchmark)
			&& _Check(root->_right, blackNum, benchmark);
	}

	void _InOrder(Node* root)
	{
    
    
		if (root == nullptr)
		{
    
    
			return;
		}

		_InOrder(root->_left);
		cout << root->_kv.first << " ";
		_InOrder(root->_right);
	}

	void RotateL(Node* parent)
	{
    
    
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		Node* ppnode = parent->_parent;

		subR->_left = parent;
		parent->_parent = subR;

		if (ppnode == nullptr)
		{
    
    
			_root = subR;
			_root->_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;

		Node* ppnode = parent->_parent;

		subL->_right = parent;
		parent->_parent = subL;

		if (parent == _root)
		{
    
    
			_root = subL;
			_root->_parent = nullptr;
		}
		else
		{
    
    
			if (ppnode->_left == parent)
			{
    
    
				ppnode->_left = subL;
			}
			else
			{
    
    
				ppnode->_right = subL;
			}
			subL->_parent = ppnode;
		}
	}
private:
	Node* _root;
};

3、封装map、set

先把析构函数和find都写上

	~RBTree()
	{
    
    
		_Destroy(_root);
		_root = nullptr;
	}
    
    void _Destroy(Node* root)
	{
    
    
		if (root == nullptr)
		{
    
    
			return;
		}

		_Destroy(root->_left);
		_Destroy(root->_right);
		delete root;
	}
	
	Node* Find(const K& key)
	{
    
    
		Node* cur = _root;
		while (cur)
		{
    
    
			if (cur->_kv.first < key)
			{
    
    
				cur = cur->_right;
			}
			else if (cur->_kv.first > key)
			{
    
    
				cur = cur->_left;
			}
			else
			{
    
    
				return cur;
			}
		}
		return nullptr;
	}

1、解释说明

我们让map和set都是两个模板参数,并且写仿函数来实现对其中变量的提取。也用到了模板复用。

剩下的部分也仔细琢磨代码,其中有注释说明。

KeyOfT是set和map的比较方法,这个放在下面解释。

Set.h

	template<class K>
	class set
	{
    
    
		struct SetKeyOfT
		{
    
    
			const K& operator()(const K& key)
			{
    
    
				return key;
			}
		};
	private:
		RBTree<K, K, SetKeyOfT> _t;//为什么这里要用两个k?为了匹配map的kv,所以set就用两个k,那么模板参数就可以固定下来为3个了。下面有解释。
	};

Map.h

	template<class K, class V>
	class map
	{
    
    
		struct MapKeyOfT
		{
    
    
			const K& operator()(const pair<const K, V>& kv)
			{
    
    
				return kv.first;
			}
		};
	private:
		RBTree<K, pair<const K, V>, MapKeyOfT> _t;
	};

我们再看RBTree的实现,set传K,map传pair<const K, V>。

RBTree.h

注释里写的都是原来的部分,与现在为了封装而做的改动做对比。

template<class T>//class K, class V
struct RBTreeNode//T也就是map传的pair,set传的K
{
    
    
	RBTreeNode<T>* _left;//<K, V>
	RBTreeNode<T>* _right;
	RBTreeNode<T>* _parent;
	T _data;//pair<K, V>
	Colour _col;

	RBTreeNode(const T& data)
		:_left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _data(data)
		, _col(RED)
	{
    
    }
};

template<class K, class T, class KeyOfT>//class K, class V。这里就体现了一个很好的写法,引入这个KeyOfT是用来做比较的,map和set都有自己的比较方法,除此之外,map是kv,set是k,加上这个模板参数,那么我们难道要写两个类,模板参数分别是K、KeyOfT和K、V、KeyOfT?这样太麻烦了,所以前面就写到set多带一个k,不会影响k,而且RBTree的实现可以只有集成起来
class RBTree
{
    
    
	typedef RBTreeNode<T> Node;//<K, V>,这里的T,map传过来的就是pair<const K, V>,set就是K,然后再传给RBTreeNode
public:
    bool Insert(const T& data)const pair<K, V>& kv
	{
    
    
		if (_root == nullptr)
		{
    
    
			_root = new Node(data);
			_root->_col = BLACK;
			return true;
		}
		Node* parent = nullptr;
		Node* cur = _root;
		KeyOfT kot;//既然set和map的排序方法都写好了,那么就用模板来使用
		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);
	    if (kot(parent->_data) > kot(data))
		{
    
    
			parent->_left = cur;
		}
		else
		{
    
    
			parent->_right = cur;
		}
		cur->_parent = parent;
		//下面部分没有更改,所以就没写

Find不能用第二个模板参数,因为如果是map,它就是pair了。

继续完善一下set和map的文件,插入就调用底层红黑树的插入。

Set.h

template<class K>
class set
{
    
    
	struct SetKeyOfT
	{
    
    
		const K& operator()(const K& key)
		{
    
    
			return key;
		}
	};
public:
	bool insert(const K& key)
	{
    
    
		return _t.Insert(key);
	}
private:
	RBTree<K, K, SetKeyOfT> _t;
};

Map.h

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)
	{
    
    
		return _t.Insert(kv);
	}
private:
	RBTree<K, pair<const K, V>, MapKeyOfT> _t;
};

map的insert写上自己的办法,因为我们调用的是map里面的方法,map再传模板参数,使用RBTree类来完成程序。在插入的时候,我们还需要比较元素,map的比较是pair,红黑树底层不知道传过来的T是什么类型,但是map知道,所以红黑树要去调用map里的比较方法。set可以直接返回,但是为了和map形成复用,它也要写一个对应的方法。红黑树里就可以用KeyOfT这个模板参数来实例化一个对象,然后通过对象调用对应的方法,也就是上面代码中的kot。

2、迭代器

下面有完整代码和链接

map和set的迭代器用的都是红黑树的迭代器。如果用红黑树的迭代器,像RBTree::iterator,那么放在set和map里typedef的时候,在后面还得写上typename,因为编译器不知道要取的是类型还是静态变量,所以要这样写

typedef typename RBTree::iterator iterator

set和map里调用的都是底层红黑树迭代器里的方法,源码中是用const迭代器来实现普通和const版本的。

template <class T, class Ref, class Ptr>
struct __RBTreeIterator
{
    
    
	typedef RBTreeNode<T> Node;
	typedef __RBTreeIterator<T, Ref, Ptr> Self;
	Node* _node;
	__RBTreeIterator(Node* node)
		:_node(node)
	{
    
    }

	Ref operator*()
	{
    
    
		return _node->_data;
	}

	Ptr operator->()
	{
    
    
		return &_node->_data;
	}

	bool operator!=(const Self& s)
	{
    
    
		return _node != s._node;
	}
};

在RBTree的类里就可以写上

	typedef __RBTreeIterator<T, T&, T*> itertaor;
	typedef __RBTreeIterator<T, const T&, const T*> const_itertaor;

以及begin和end。红黑树是走中序遍历,所以begin应当从最左节点开始

	itertaor begin()
	{
    
    
		Node* cur = _root;
		while (cur && cur->_left)
		{
    
    
			cur = cur->_left;
		}

		return itertaor(cur);//用迭代器类来构造的一个节点,传过去后用模板参数T来构造一个node,赋值给_node
	}

	itertaor end()
	{
    
    
		return itertaor(nullptr);
	}

然后给set里写上对应的begin和end

template<class K>
class set
{
    
    
	struct SetKeyOfT
	{
    
    
		const K& operator()(const K& key)
		{
    
    
			return key;
		}
	};
public:
	typedef typename RBTree<K, K, SetKeyOfT>::itertaor iterator;//不写typename会编译不过

	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;
};

重点是迭代器的++。之前对于一棵二叉树,有递归(前中后序)和非递归(层序)的办法来遍历,现在用第三种方法来遍历。

给个实际使用

it = begin();
while (it != end())
{
    
    
	cout << *it << " ";
	++it;
}
cout << endl;

在这里插入图片描述
begin从最左节点开始,所以it先找到1的左节点,为空,然后就指向1,那么it怎么走下一步?从图中看it的下一个位置应当是6,但如果6的左节点不为空,那么it的下一个位置应当是6的左节点,因为红黑树按中序走。如果右树不为空,下一个就是右树的最左节点。

如果右树为空,或者已经走完了,那么下一个是谁?如果从图中看,下一个肯定就是8。也就是说我们要往上找。右为空,就沿着路径,找孩子是父亲的左节点的那个父亲。因为中序遍历,顺序是左子树,根,右子树。如果全部都走完了,也就是走到了27的右,那么整棵树就结束了,按照第二个情况的方法,我们往上找节点,直到找到根的父节点,也就是空,这时候it应当被赋值为空,然后遍历结束。

	Self operator++()
	{
    
    
		if (_node->_right)
		{
    
    
			//1、右不为空,下一个就是右子树的最左节点
			Node* subLeft = _node->_right;
			while (subLeft->_left)
			{
    
    
				subLeft = subLeft->_left;
			}
			_node = subLeft;//让这个迭代器节点被赋值这个最左节点,就把它移动过来了
		}
		else
		{
    
    
			//2、右为空,沿着到根的路径,找孩子是父亲左的那个父亲
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_right)//是右,继续往上走
			{
    
    
				cur = parent;
				parent = parent->_parent;
			}
			_node = parent;
		}
		return *this;
	}

按照上面的图一点点移动it来理解。测试

void test_set1()
{
    
    
	int a[] = {
    
     16, 3, 7, 11, 9, 26, 18, 14, 15 };
	set<int> s;
	for (auto e : a)
	{
    
    
		s.insert(e);
	}
	set<int>::iterator it = s.begin();
	while (it != s.end())
	{
    
    
		cout << *it << " ";
		++it;
	}
	cout << endl;
	for (auto e : s)
	{
    
    
		cout << e << " ";
	}
	cout << endl;
}

–则是反过来。右子树 根 左子树。右子树结束了,根还没有结束;左子树结束了,那就往上找孩子是父亲右节点的那个父亲。

	Self& operator--()
	{
    
    
		if (_node->_right)
		{
    
    
			//1、左不为空,找左子树的最右节点
			Node* subRight = _node->_left;
			while (subRight->_right)
			{
    
    
				subRight = subRight->_right;
			}
			_node = subRight;//让这个迭代器节点被赋值这个最右节点,就把它移动过来了
		}
		else
		{
    
    
			//2、左为空,沿着到根的路径,找孩子是父亲右的那个父亲
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_left)//是左,继续往上走
			{
    
    
				cur = parent;
				parent = parent->_parent;
			}
			_node = parent;
		}
		return *this;
	}

可以用之前map的测试用例来

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, pair<const K, V>, MapKeyOfT>::itertaor iterator;

	iterator begin()
	{
    
    
		return _t.begin();
	}

	iterator end()
	{
    
    
		return _t.end();
	}

	bool insert(const pair<const K, V>& kv)
	{
    
    
		return _t.Insert(kv);
	}
private:
	RBTree<K, pair<const K, V>, MapKeyOfT> _t;
};

void test_map1()
{
    
    
	map<string, string> dict;
	dict.insert(make_pair("sort", "排序"));
	dict.insert(make_pair("string", "字符串"));
	dict.insert(make_pair("count", "计数"));
	dict.insert(make_pair("count", "数值"));
	map<string, string>::iterator it = dict.begin();
	while (it != dict.end())
	{
    
    
		cout << it->first << ":" << it->second << endl;
		++it;
	}
	cout << endl;
}

3、map的方括号[]和迭代器的完善

要实现迭代器的方括号功能,得修改一下insert,把bool类型换成pair<iterator, bool>,相应地插入函数中的return的true和false也需要改变,Map.h和Set.h中把bool改成pair<iterator, bool>。这样map和set就可以就[]了。插入函数中,插入成功就返回插入节点的迭代器,不成功就是返回false。如果可以插入,原先代码是写cur = new Node(data),不过因为得配合pair,以及cur在下面的调整中如果没有备份就会有问题,所以再写上一个Node* newnode = cur。在最后的return true时就用newnode。

	pair<iterator, bool> Insert(const T& data)
	{
    
    
		if (_root == nullptr)
		{
    
    
			_root = new Node(data);
			_root->_col = BLACK;
			return make_pair(iterator(_root), true);//return 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;
        //......
        return make_pair(iterator(newnode), true);//newnode
    }

Map中

	V& operator[](const K& key)
	{
    
    
		pair<iterator, bool> ret = _t.Insert(make_pair(key, V()));
		return ret.first->second;
	}

不过这里有个bug,就是不同的编译器可能会出现迭代器中可以修改元素的操作,实际上我们都知道迭代器不能修改,如果单纯地加const,要修改的地方很多,还会出现其他问题。库里的源代码是这样做的,普通和const迭代器都是const迭代器。

在这里插入图片描述

但是都用const迭代器就可以做到不修改了吗?在RBTreeNode结构体中,我们可没有写const修饰的成员。先看看源代码在处理这方面做了什么

在这里插入图片描述

图中是迭代器类。最后一行这里,迭代器的构造函数,const iterator& it,用的是普通迭代器,上面有iterator的typedef,是__rb_tree_iterator<Vlaue, Value&, Value*>。

看一下我们自己的代码

在这里插入图片描述

set的迭代器会调用RBTree里的const iterator,那么set的模板参数K,传给RBTree,就会来到RBTree的这一行typedef __RBTreeIterator<T, const T&, const T*> const_itertaor,T也就被换为K

在这里插入图片描述

再去找迭代器__RBTreeIterator<T, Ref, Ptr>,这时候这三个模板参数就是T,const T&,const T*,走两个typedef,Node和Self会被创建。我们外面写的时候,会写set< int > s,set< int >::iterator it = s.begin(),s调用begin时,如上图所示,_t.begin(),_t就是RBTree类型的变量,传过去了两个K和SetKeyOfT,由于set里面的begin函数是iterator类型,也就是RBTree里的const_iterator,那么就会调用RBTree类里的const版本的begin函数,begin会将_node的data给it。

在这里插入图片描述

如果迭代器类里只有一个构造函数,也就是括号里是Node* node的那个,那么获取到的当前节点的值是可以修改,因为_node就没有什么限制,在构造这个节点的时候,数据传到RBTreeNode的结构体中

在这里插入图片描述

用T来声明一个_data变量,这里就没有限制,所以_node的data就可以更改,所以这样子不行;而第二个构造,对it进行const修饰,然后通过it访问_node,那么这时候因为it是const的,所以它里面的数据就没法更改了,所以使用const迭代器也就真的可以不修改数据了。写到这里,如果在RBTreeNode里写成T _data是不是也能起到不能修改的作用?这确实可以,但是set只是迭代器时不能修改,其它时候还是可以修改key的。

set的迭代器只有const迭代器,只不过set里给它起了两个别名iterator和const iterator,但都是const的。

当然还有其他函数,不过这篇就只写了重要的函数。

4、整体代码

Map.h

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, pair<const K, V>, MapKeyOfT>::itertaor iterator;

	iterator begin()
	{
    
    
		return _t.begin();
	}

	iterator end()
	{
    
    
		return _t.end();
	}

	V& operator[](const K& key)
	{
    
    
		pair<iterator, bool> ret = _t.Insert(make_pair(key, V()));
		return ret.first->second;
	}

	pair<iterator, bool> insert(const pair<const K, V>& kv)
	{
    
    
		return _t.Insert(kv);
	}
private:
	RBTree<K, pair<const K, V>, MapKeyOfT> _t;
};

Set.h

template<class K>
class set
{
    
    
	struct SetKeyOfT
	{
    
    
		const K& operator()(const K& key)
		{
    
    
			return key;
		}
	};
public:
	typedef typename RBTree<K, K, SetKeyOfT>::const_itertaor iterator;
	typedef typename RBTree<K, K, SetKeyOfT>::const_itertaor const_iterator;
	 
	iterator begin()
	{
    
    
		return _t.begin();
	}

	iterator end()
	{
    
    
		return _t.end();
	}

	pair<iterator, bool> insert(const K& key)
	{
    
    
		return _t.Insert(key);
	}
private:
	RBTree<K, K, SetKeyOfT> _t;
};

RBTree.h

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, class Ref, class Ptr>
struct __RBTreeIterator
{
    
    
	typedef RBTreeNode<T> Node;
	typedef __RBTreeIterator<T, Ref, Ptr> Self;
	Node* _node;
	__RBTreeIterator(Node* node)
		:_node(node)
	{
    
    }
	
	__RBTreeIterator(const __RBTreeIterator<T, T*, T&> it)
		:_node(it._node)
	{
    
    }//用普通迭代器实例化,是拷贝构造,是const的话,是支持普通转换成构造const迭代器的构造函数

	Ref operator*()
	{
    
    
		return _node->_data;
	}

	Ptr operator->()
	{
    
    
		return &_node->_data;
	}

	bool operator!=(const Self& s)
	{
    
    
		return _node != s._node;
	}

	Self operator++()
	{
    
    
		if (_node->_right)
		{
    
    
			//1、右不为空,下一个就是右子树的最左节点
			Node* subLeft = _node->_right;
			while (subLeft->_left)
			{
    
    
				subLeft = subLeft->_left;
			}
			_node = subLeft;//让这个迭代器节点被赋值这个最左节点,就把它移动过来了
		}
		else
		{
    
    
			//2、右为空,沿着到根的路径,找孩子是父亲左的那个父亲
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_right)//是右,继续往上走
			{
    
    
				cur = parent;
				parent = parent->_parent;
			}
			_node = parent;
		}
		return *this;
	}

	Self& operator--()
	{
    
    
		if (_node->_right)  
		{
    
    
			//1、左不为空,找左子树的最右节点
			Node* subRight = _node->_left;
			while (subRight->_right)
			{
    
    
				subRight = subRight->_right;
			}
			_node = subRight;//让这个迭代器节点被赋值这个最右节点,就把它移动过来了
		}
		else
		{
    
    
			//2、左为空,沿着到根的路径,找孩子是父亲右的那个父亲
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_left)//是左,继续往上走
			{
    
    
				cur = parent;
				parent = parent->_parent;
			}
			_node = parent;
		}
		return *this;
	}
};

template<class K, class T, class KeyOfT>
class RBTree
{
    
    
	typedef RBTreeNode<T> Node;
public:
	~RBTree()
	{
    
    
		_Destroy(_root);
		_root = nullptr;
	}
public:
	typedef __RBTreeIterator<T, T&, T*> itertaor;
	typedef __RBTreeIterator<T, const T&, const T*> const_itertaor;

	itertaor begin()
	{
    
    
		Node* cur = _root;
		while (cur && cur->_left)
		{
    
    
			cur = cur->_left;
		}
		return itertaor(cur);
	}

	const_itertaor end() const
	{
    
    
		return itertaor(nullptr);
	}

	const_itertaor begin() const
	{
    
    
		Node* cur = _root;
		while (cur && cur->_left)
		{
    
    
			cur = cur->_left;
		}
		return itertaor(cur);
	}

	itertaor end()
	{
    
    
		return itertaor(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;
	}

	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->_left = cur;
		}
		else
		{
    
    
			parent->_right = cur;
		}
		cur->_parent = parent;

		while (parent && parent->_col == RED)
		{
    
    
			Node* gf = parent->_parent;
			if (gf->_left == parent)
			{
    
    
				Node* uncle = gf->_right;
				// u存在且为红,变色处理,并继续往上处理
				if (uncle && uncle->_col == RED)
				{
    
    
					parent->_col = BLACK;
					uncle->_col = BLACK;
					grandfather->_col = RED;

					// 继续往上调整
					cur = gf;
					parent = cur->_parent;
				}
				else // u不存在/u存在且为黑,旋转+变色
				{
    
    
					//     g
					//   p   u
					// c 
					if (cur == parent->_left)
					{
    
    
						RotateR(gf);//以gf为轴点
						parent->_col = BLACK;
						gf->_col = RED;
					}
					else
					{
    
    
						//     g
						//   p   u
						//     c
						RotateL(parent);
						RotateR(gf);
						cur->_col = BLACK;
						//parent->_col = RED;
						gf->_col = RED;
					}
					break;
				}
			}
			else if(gf->_right == parent)
			{
    
    
				//    g
				//  u   p
				//        c
				Node* uncle = gf->_left;
				// u存在且为红,变色处理,并继续往上处理
				if (uncle && uncle->_col == RED)
				{
    
    
					parent->_col = BLACK;
					uncle->_col = BLACK;
					gf->_col = RED;

					// 继续往上调整
					cur = gf;
					parent = cur->_parent;
				}
				else // 情况2+3:u不存在/u存在且为黑,旋转+变色
				{
    
    
					//    g
					//  u   p
					//        c
					if (cur == parent->_right)
					{
    
    
						RotateL(gf);
						gf->_col = RED;
						parent->_col = BLACK;
					}
					else
					{
    
    
						//    g
						//  u   p
						//    c
						RotateR(parent);
						RotateL(gf);
						cur->_col = BLACK;
						gf->_col = RED;
					}
					break;
				}
			}
		}
		_root->_col = BLACK;
		return make_pair(iterator(newnode), true);
	}

	void InOrder()
	{
    
    
		_InOrder(_root);
	}

	bool IsBalance()
	{
    
    
		if (_root && _root->_col == RED)
		{
    
    
			cout << "根节点颜色是红色" << endl;
			return false;
		}
		int benchmark = 0;//黑节点参考值,选择一条路径上的黑节点数量做参考值即可
		Node* cur = _root;
		while (cur)
		{
    
    
			if (cur->_col == BLACK)
				++benchmark;
			cur = cur->_left;
		}
		// 连续红色节点
		return _Check(_root, 0, benchmark);
	}

	int Height()
	{
    
    
		return _Height(_root);
	}
private:
	void _Destroy(Node* root)
	{
    
    
		if (root == nullptr)
		{
    
    
			return;
		}

		_Destroy(root->_left);
		_Destroy(root->_right);
		delete root;
	}

	int _Height(Node* root)
	{
    
    
		if (root == NULL)
			return 0;
		int leftH = _Height(root->_left);
		int rightH = _Height(root->_right);
		return leftH > rightH ? leftH + 1 : rightH + 1;
	}

	bool _Check(Node* root, int blackNum, int benchmark)
	{
    
    
		if (root == nullptr)
		{
    
    
			if (benchmark != blackNum)
			{
    
    
				cout << "某条路径黑色节点的数量不相等" << endl;
				return false;
			}
			return true;
		}
		if (root->_col == BLACK)
		{
    
    
			++blackNum;
		}
		if (root->_col == RED
			&& root->_parent
			&& root->_parent->_col == RED)
		{
    
    
			cout << "存在连续的红色节点" << endl;
			return false;
		}
		return _Check(root->_left, blackNum, benchmark)
			&& _Check(root->_right, blackNum, benchmark);
	}

	void _InOrder(Node* root)
	{
    
    
		if (root == nullptr)
		{
    
    
			return;
		}

		_InOrder(root->_left);
		cout << root->_kv.first << " ";
		_InOrder(root->_right);
	}

	void RotateL(Node* parent)
	{
    
    
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		Node* ppnode = parent->_parent;

		subR->_left = parent;
		parent->_parent = subR;

		if (ppnode == nullptr)
		{
    
    
			_root = subR;
			_root->_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;

		Node* ppnode = parent->_parent;

		subL->_right = parent;
		parent->_parent = subL;

		if (parent == _root)
		{
    
    
			_root = subL;
			_root->_parent = nullptr;
		}
		else
		{
    
    
			if (ppnode->_left == parent)
			{
    
    
				ppnode->_left = subL;
			}
			else
			{
    
    
				ppnode->_right = subL;
			}
			subL->_parent = ppnode;
		}
	}
private:
	Node* _root;
};

测试用例

void test_set1()
{
    
    
	int a[] = {
    
     16, 3, 7, 11, 9, 26, 18, 14, 15 };
	set<int> s;
	for (auto e : a)
	{
    
    
		s.insert(e);
	}
	set<int>::iterator it = s.begin();
	while (it != s.end())
	{
    
    
		cout << *it << " ";
		++it;
	}
	cout << endl;
	for (auto e : s)
	{
    
    
		cout << e << " ";
	}
	cout << endl;
}

void test_map1()
{
    
    
	map<string, string> dict;
	dict.insert(make_pair("sort", ""));
	dict.insert(make_pair("string", "ַ"));
	dict.insert(make_pair("count", ""));
	dict.insert(make_pair("string", "(ַ)"));

	map<string, string>::iterator it = dict.begin();
	while (it != dict.end())
	{
    
    
		cout << it->first << ":" << it->second << endl;
		/*it->first = "1111";
		it->second = "111";*/

		++it;
	}
	cout << endl;

	for (auto& kv : dict)
	{
    
    
		cout << kv.first << ":" << kv.second << endl;
	}
	cout << endl;
}

void test_map2()
{
    
    
	string arr[] = {
    
     "西瓜", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉", "梨" };
	map<string, int> countMap;
	for (auto& e : arr)
	{
    
    
		countMap[e]++;
	}

	for (auto& kv : countMap)
	{
    
    
		cout << kv.first << ":" << kv.second << endl;
	}
}
}

完整代码:红黑树

结束。

猜你喜欢

转载自blog.csdn.net/kongqizyd146/article/details/130543838