• 【C++】set和map的底层结构(AVL树&红黑树)



    一、前言

    前面对map/multimap/set/multiset进行了简单的介绍,在其文档介绍中发现,这几个容器有个共同点是:其底层都是按照二叉搜索树来实现的,但是二叉搜索树有其自身的缺陷,假如往树中插入的元素有序或者接近有序,二叉搜索树就会退化成单支树,时间复杂度会退化成O(N),因此map、set等关联式容器的底层结构是对二叉树进行了平衡处理,即采用平衡树来实现。


    二、AVL 树

    1.AVL树的概念

    二叉搜索树虽可以缩短查找的效率,但如果数据有序或接近有序二叉搜索树将退化为单支树,查找元素相当于在顺序表中搜索元素,效率低下。因此,两位俄罗斯的数学家G.M.Adelson-Velskii和E.M.Landis在1962年发明了一种解决上述问题的方法:当向二叉搜索树中插入新结点后,如果能保证每个结点的左右子树高度之差的绝对值不超过1(需要对树中的结点进行调整),即可降低树的高度,从而减少平均搜索长度

    一棵AVL树或者是空树,或者是具有以下性质的二叉搜索树:

    • 它的左右子树都是AVL树
    • 左右子树高度之差(简称平衡因子)的绝对值不超过1(-1/0/1)

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    如果一棵二叉搜索树是高度平衡的,它就是AVL树。如果它有n个结点,其高度可保持在 O ( l o g 2 n ) O(log_2 n) O(log2n),搜索时间复杂度O( l o g 2 n log_2 n log2n)

    2.AVL树节点的定义

    AVL树节点的定义:

    template<class T>
    struct AVLTreeNode
    {
    	AVLTreeNode(const T& data)
    		: _pLeft(nullptr), _pRight(nullptr), _pParent(nullptr)
    		, _data(data), _bf(0)
    	{}
    	AVLTreeNode<T>* _pLeft;   // 该节点的左孩子
    	AVLTreeNode<T>* _pRight;  // 该节点的右孩子
    	AVLTreeNode<T>* _pParent; // 该节点的双亲
    	T _data;
    	int _bf;                  // 该节点的平衡因子
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3.AVL树的插入

    AVL树就是在二叉搜索树的基础上引入了平衡因子,因此AVL树也可以看成是二叉搜索树。那么AVL树的插入过程可以分为两步:

    1. 按照二叉搜索树的方式插入新节点
    2. 调整节点的平衡因子
    bool Insert(const T& data)
    {
    	// 1. 先按照二叉搜索树的规则将节点插入到AVL树中
    	// ...
    
    	// 2. 新节点插入后,AVL树的平衡性可能会遭到破坏,此时就需要更新平衡因子,并检测是否
    	//破坏了AVL树的平衡性 
    
    	 /*
    	 pCur插入后,pParent的平衡因子一定需要调整,在插入之前,pParent
    	 的平衡因子分为三种情况:-1,0, 1, 分以下两种情况:
    	  1. 如果pCur插入到pParent的左侧,只需给pParent的平衡因子-1即可
    	  2. 如果pCur插入到pParent的右侧,只需给pParent的平衡因子+1即可
    	  
    	 此时:pParent的平衡因子可能有三种情况:0,正负1, 正负2
    	  1. 如果pParent的平衡因子为0,说明插入之前pParent的平衡因子为正负1,插入后被调整
    	成0,此时满足
    	     AVL树的性质,插入成功
    	  2. 如果pParent的平衡因子为正负1,说明插入前pParent的平衡因子一定为0,插入后被更
    	新成正负1,此
    	     时以pParent为根的树的高度增加,需要继续向上更新
    	  3. 如果pParent的平衡因子为正负2,则pParent的平衡因子违反平衡树的性质,需要对其进
    	行旋转处理
    	 */
    		while (pParent)
    		{
    			// 更新双亲的平衡因子
    			if (pCur == pParent->_pLeft)
    				pParent->_bf--;
    			else
    				pParent->_bf++;
    			// 更新后检测双亲的平衡因子
    			if (0 == pParent->_bf)
    			{
    				break;
    			}
    			else if (1 == pParent->_bf || -1 == pParent->_bf)
    			{
    				// 插入前双亲的平衡因子是0,插入后双亲的平衡因为为1 或者 -1 ,说明以双亲
    				为根的二叉树
    					// 的高度增加了一层,因此需要继续向上调整
    					pCur = pParent;
    				pParent = pCur->_pParent;
    			}
    			else
    			{
    				// 双亲的平衡因子为正负2,违反了AVL树的平衡性,需要对以pParent
    				// 为根的树进行旋转处理
    				if (2 == pParent->_bf)
    				{
    					// ...
    				}
    				else
    				{
    					// ...
    				}
    			}
    		}
    	return 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

    4.AVL树的旋转

    如果在一棵原本是平衡的AVL树中插入一个新节点,可能造成不平衡,此时必须调整树的结构,使之平衡化。

    旋转的目的:

    • 让这颗子树的高度不超过1(降低子树高度);
    • 旋转过程中继续保持它是搜索树;
    • 更新孩子节点的平衡因子;

    根据节点插入位置的不同,AVL树的旋转分为四种:

    • 1.新节点插入较高左子树的左侧—左左:右单旋

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    /*
      上图在插入前,AVL树是平衡的,新节点插入到30的左子树(注意:此处不是左孩子)中,30左
    子树增加
      了一层,导致以60为根的二叉树不平衡,要让60平衡,只能将60左子树的高度减少一层,右子
    树增加一层,
      即将左子树往上提,这样60转下来,因为60比30大,只能将其放在30的右子树,而如果30有
    右子树,右子树根的值一定大于30,小于60,只能将其放在60的左子树,旋转完成后,更新节点
    的平衡因子即可。在旋转过程中,有以下几种情况需要考虑:
      1. 30节点的右孩子可能存在,也可能不存在
      2. 60可能是根节点,也可能是子树
         如果是根节点,旋转完成后,要更新根节点
         如果是子树,可能是某个节点的左子树,也可能是右子树
        
    同学们再此处可举一些详细的例子进行画图,考虑各种情况,加深旋转的理解
    */
    
    void _RotateR(PNode pParent)
    {
    	// pSubL: pParent的左孩子
    	// pSubLR: pParent左孩子的右孩子,
    	PNode pSubL = pParent->_pLeft;
    	PNode pSubLR = pSubL->_pRight;
    
    	// 旋转完成之后,30的右孩子作为双亲的左孩子
    	pParent->_pLeft = pSubLR;
        
    	// 如果30的左孩子的右孩子存在,更新亲双亲
    	if (pSubLR)
    		pSubLR->_pParent = pParent;
    
    	// 60 作为 30的右孩子
    	pSubL->_pRight = pParent;
    
    	// 因为60可能是棵子树,因此在更新其双亲前必须先保存60的双亲
    	PNode pPParent = pParent->_pParent;
    
    	// 更新60的双亲
    	pParent->_pParent = pSubL;
    
    	// 更新30的双亲
    	pSubL->_pParent = pPParent;
    
    	// 如果60是根节点,根新指向根节点的指针
    	if (NULL == pPParent)
    	{
    		_pRoot = pSubL;
    		pSubL->_pParent = NULL;
    	}
    	else
    	{
    		// 如果60是子树,可能是其双亲的左子树,也可能是右子树
    		if (pPParent->_pLeft == pParent)
    			pPParent->_pLeft = pSubL;
    		else
    			pPParent->_pRight = pSubL;
    	}
    
    	// 根据调整后的结构更新部分节点的平衡因子
    	pParent->_bf = pSubL->_bf = 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 2.新节点插入较高右子树的右侧—右右:左单旋

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    实现及情况考虑可参考右单旋。

    • 3.新节点插入较高左子树的右侧—左右:先左单旋再右单旋

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    将双旋变成单旋后再旋转,即:先对30进行左单旋,然后再对90进行右单旋,旋转完成后再考虑平衡因子的更新

    // 旋转之前,60的平衡因子可能是-1/0/1,旋转完成之后,根据情况对其他节点的平衡因子进
    // 行调整
    void _RotateLR(PNode pParent)
    {
    	PNode pSubL = pParent->_pLeft;
    	PNode pSubLR = pSubL->_pRight;
    
    	// 旋转之前,保存pSubLR的平衡因子,旋转完成之后,需要根据该平衡因子来调整其他节
    	// 点的平衡因子(pSubLR: pParent左孩子的右孩子)
    		int bf = pSubLR->_bf;
    
    	// 先对30进行左单旋
    	_RotateL(pParent->_pLeft);
    
    	// 再对90进行右单旋
    	_RotateR(pParent);
    	if (1 == bf)
    		pSubL->_bf = -1;
    	else if (-1 == bf)
    		pParent->_bf = 1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 4.新节点插入较高右子树的左侧—右左:先右单旋再左单旋

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    参考右左双旋。

    总结:

    假如以pParent为根的子树不平衡,即pParent的平衡因子为2或者-2,分以下情况考虑

    1. pParent的平衡因子为2,说明pParent的右子树高,设pParent的右子树的根为pSubR

      • 当pSubR的平衡因子为1时,执行左单旋
      • 当pSubR的平衡因子为-1时,执行右左双旋
    2. pParent的平衡因子为-2,说明pParent的左子树高,设pParent的左子树的根为pSubL

      • 当pSubL的平衡因子为-1是,执行右单旋
      • 当pSubL的平衡因子为1时,执行左右双旋

    旋转完成后,原pParent为根的子树个高度降低,已经平衡,不需要再向上更新。

    5.AVL树的验证

    AVL树是在二叉搜索树的基础上加入了平衡性的限制,因此要验证AVL树,可以分两步:

    1. 验证其为二叉搜索树
    • 如果中序遍历可得到一个有序的序列,就说明为二叉搜索树
    1. 验证其为平衡树
    • 每个节点子树高度差的绝对值不超过1(注意节点中如果没有平衡因子)
    • 节点的平衡因子是否计算正确
    1. 验证用例

    6.AVL树的删除、AVL树的性能

    • AVL树的删除(了解)

    因为AVL树也是二叉搜索树,可按照二叉搜索树的方式将节点删除,然后再更新平衡因子,只不错与删除不同的时,删除节点后的平衡因子更新,最差情况下一直要调整到根节点的位置。

    • AVL树的性能

    AVL树是一棵绝对平衡的二叉搜索树,其要求每个节点的左右子树高度差的绝对值都不超过1,这样可以保证查询时高效的时间复杂度,即 l o g 2 ( N ) log_2 (N) log2(N)。但是如果要对AVL树做一些结构修改的操作,性能非常低下,比如:插入时要维护其绝对平衡,旋转的次数比较多,更差的是在删除时,有可能一直要让旋转持续到根的位置。因此:如果需要一种查询高效且有序的数据结构,而且数据的个数为静态的(即不会改变),可以考虑AVL树,但一个结构经常修改,就不太适合。


    三、红黑树

    1.红黑树的概念

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

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    2.红黑树的性质

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

    思考:为什么满足上面的性质,红黑树就能保证:其最长路径中节点个数不会超过最短路径节点个数的两倍?

    答:因为组织结构已经确定(构成树的黑色节点一定,红色节点只能在黑色节点之间),无论如何填充红色节点都不会超过全黑节点路径的两倍。

    3.红黑树节点的定义

    // 节点的颜色
    enum Color { RED, BLACK };
    // 红黑树节点的定义
    template<class ValueType>
    struct RBTreeNode
    {
    	RBTreeNode(const ValueType& data = ValueType(),Color color = RED)
    		: _pLeft(nullptr), _pRight(nullptr), _pParent(nullptr)
    		, _data(data), _color(color)
    	{}
    
    	RBTreeNode<ValueType>* _pLeft;   // 节点的左孩子
    	RBTreeNode<ValueType>* _pRight;  // 节点的右孩子
    	RBTreeNode<ValueType>* _pParent; // 节点的双亲(红黑树需要旋转,为了实现简单给出该字段)
    
    	ValueType _data;            // 节点的值域
    	Color _color;               // 节点的颜色
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    思考:在节点的定义中,为什么要将节点的默认颜色给成红色的?

    答:违背规则3的代价比违背规则4的代价更小(红黑树的性质)。

    4.红黑树结构

    为了后续实现关联式容器简单,红黑树的实现中增加一个头结点,因为跟节点必须为黑色,为了与根节点进行区分,将头结点给成黑色,并且让头结点的 pParent域指向红黑树的根节点,pLeft域指向红黑树中最小的节点,_pRight域指向红黑树中最大的节点,如下:

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    5.红黑树的插入操作

    红黑树是在二叉搜索树的基础上加上其平衡限制条件,因此红黑树的插入可分为两步:

    • 按照二叉搜索的树规则插入新节点
    template<class ValueType>
    class RBTree
    {
    	//……
    	bool Insert(const ValueType& data)
    	{
    		PNode& pRoot = GetRoot();
    		if (nullptr == pRoot)
    		{
    			pRoot = new Node(data, BLACK);
    			// 根的双亲为头节点
    			pRoot->_pParent = _pHead;
    			_pHead->_pParent = pRoot;
    		}
    		else
    		{
    			// 1. 按照二叉搜索的树方式插入新节点
    						// 2. 检测新节点插入后,红黑树的性质是否造到破坏,
    			//   若满足直接退出,否则对红黑树进行旋转着色处理
    		}
    
    		// 根节点的颜色可能被修改,将其改回黑色
    		pRoot->_color = BLACK;
    		_pHead->_pLeft = LeftMost();
    		_pHead->_pRight = RightMost();
    		return true;
    	}
    private:
    	PNode& GetRoot() { return _pHead->_pParent; }
    	// 获取红黑树中最小节点,即最左侧节点
    	PNode LeftMost();
    	// 获取红黑树中最大节点,即最右侧节点
    	PNode RightMost();
    private:
    	PNode _pHead;
    };
    
    • 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
    • 检测新节点插入后,红黑树的性质是否造到破坏

    因为新节点的默认颜色是红色,因此:如果其双亲节点的颜色是黑色,没有违反红黑树任何性质,则不需要调整;但当新插入节点的双亲节点颜色为红色时,就违反了性质三不能有连在一起的红色节点,此时需要对红黑树分情况来讨论:

    • 约定:cur为当前节点,p为父节点,g为祖父节点,u为叔叔节点

    • 情况一: cur为红,p为红,g为黑,u存在且为红

    注:下图右边的树为我们的修改目标

    总结:解决方式:将p,u改为黑,g改为红,然后把g当成cur,继续向上调整。

    • 情况二: cur为红,p为红,g为黑,u不存在/u存在且为黑

    总结:p为g的左孩子,cur为p的左孩子,则进行右单旋转;相反,p为g的右孩子,cur为p的右孩子,则进行左单旋转;p、g变色–p变黑,g变红

    • 情况三: cur为红,p为红,g为黑,u不存在/u存在且为黑

    在这里插入图片描述

    总结:p为g的左孩子,cur为p的右孩子,则针对p做左单旋转;相反,p为g的右孩子,cur为p的左孩子,则针对p做右单旋转;则转换成了情况2

    6.红黑树的验证

    红黑树的检测分为两步:

    1. 检测其是否满足二叉搜索树(中序遍历是否为有序序列)
    2. 检测其是否满足红黑树的性质
    bool IsValidRBTree()
    {
    	PNode pRoot = GetRoot();
    	// 空树也是红黑树
    	if (nullptr == pRoot)
    		return true;
    
    	// 检测根节点是否满足情况
    	if (BLACK != pRoot->_color)
    	{
    		cout << "违反红黑树性质二:根节点必须为黑色" << endl;
    		return false;
    	}
    
    	// 获取任意一条路径中黑色节点的个数
    	size_t blackCount = 0;
    	PNode pCur = pRoot;
    	while (pCur)
    	{
    		if (BLACK == pCur->_color)
    			blackCount++;
    		pCur = pCur->_pLeft;
    	}
    
    	// 检测是否满足红黑树的性质,k用来记录路径中黑色节点的个数
    	size_t k = 0;
    	return _IsValidRBTree(pRoot, k, blackCount);
    }
    bool _IsValidRBTree(PNode pRoot, size_t k, const size_t blackCount)
    {
    	//走到null之后,判断k和black是否相等
    	if (nullptr == pRoot)
    	{
    		if (k != blackCount)
    		{
    			cout << "违反性质四:每条路径中黑色节点的个数必须相同" << endl;
    			return false;
    		}
    		return true;
    	}
    
    	// 统计黑色节点的个数
    	if (BLACK == pRoot->_color)
    		k++;
    
    	// 检测当前节点与其双亲是否都为红色
    	PNode pParent = pRoot->_pParent;
    	if (pParent && RED == pParent->_color && RED == pRoot->_color)
    	{
    		cout << "违反性质三:没有连在一起的红色节点" << endl;
    		return false;
    	}
    
    	return _IsValidRBTree(pRoot->_pLeft, k, blackCount) &&
    		_IsValidRBTree(pRoot->_pRight, k, blackCount);
    }
    
    • 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

    7.红黑树与AVL树比较

    红黑树和AVL树都是高效的平衡二叉树,增删改查的时间复杂度都是O( l o g 2 N log_2 N log2N),红黑树不追求绝对平衡,其只需保证最长路径不超过最短路径的2倍,相对而言,降低了插入和旋转的次数,所以在经常进行增删的结构中性能比AVL树更优,而且红黑树实现比较简单,所以实际运用中红黑树更多。

    由于红黑树优秀的性能,它已经被应用于 C++ STL库 – map/set、mutil_map/mutil_set。

    四、红黑树模拟实现STL中的map与set

    1.红黑树的迭代器

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

    • begin() 与 end()

    STL明确规定,begin()与end()代表的是一段前闭后开的区间,而对红黑树进行中序遍历后,可以得到一个有序的序列,因此:begin()可以放在红黑树中最小节点(即最左侧节点)的位置,end()放在最大节点(最右侧节点)的下一个位置,关键是最大节点的下一个位置在哪块?能否给成nullptr呢?答案是行不通的,因为对end()位置的迭代器进行–操作,必须要能找最后一个元素,此处就不行,因此最好的方式是将end()放在头结点的位置:

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    • operator++() 与 operator–()
    // 找迭代器的下一个节点,下一个节点肯定比其大
    void Increasement()
    {
    	//分两种情况讨论:_pNode的右子树存在和不存在
    	// 右子树存在
    	if (_pNode->_pRight)
    	{
    		// 右子树中最小的节点,即右子树中最左侧节点
    		_pNode = _pNode->_pRight;
    		while (_pNode->_pLeft)
    			_pNode = _pNode->_pLeft;
    	}
    	else
    	{
    		// 右子树不存在,向上查找,直到_pNode != pParent->right
    		PNode pParent = _pNode->_pParent;
    		while (pParent->_pRight == _pNode)
    		{
    			_pNode = pParent;
    			pParent = _pNode->_pParent;
    		}
    
    		// 特殊情况:根节点没有右子树
    		if (_pNode->_pRight != pParent)
    			_pNode = pParent;
    	}
    }
    
    // 获取迭代器指向节点的前一个节点
    void Decreasement()
    {
    	//分三种情况讨论:_pNode 在head的位置,_pNode 左子树存在,_pNode 左子树不
    	存在
    		// 1. _pNode 在head的位置,--应该将_pNode放在红黑树中最大节点的位置
    		if (_pNode->_pParent->_pParent == _pNode && _pNode->_color == RED)
    			_pNode = _pNode->_pRight;
    		else if (_pNode->_pLeft)
    		{
    			// 2. _pNode的左子树存在,在左子树中找最大的节点,即左子树中最右侧节点
    			_pNode = _pNode->_pLeft;
    			while (_pNode->_pRight)
    				_pNode = _pNode->_pRight;
    		}
    		else
    		{
    			// _pNode的左子树不存在,只能向上找
    			PNode pParent = _pNode->_pParent;
    			while (_pNode == pParent->_pLeft)
    			{
    				_pNode = pParent;
    				pParent = _pNode->_pParent;
    			}
    			_pNode = pParent;
    		}
    }
    
    • 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

    2.改造红黑树

    • RBTree.h
    #pragma once
    
    enum Colour
    {
    	RED,
    	BLACK,
    };
    
    template<class T>
    struct RBTreeNode
    {
    	T _data;
    	RBTreeNode<T>* _left;
    	RBTreeNode<T>* _right;
    	RBTreeNode<T>* _parent;
    	Colour _col;
    
    	RBTreeNode(const T& data)
    		:_data(data)
    		, _left(nullptr)
    		, _right(nullptr)
    		, _parent(nullptr)
    		, _col(RED)
    	{}
    };
    
    template<class T, class Ref, class Ptr>
    struct __RBTreeIterator
    {
    	typedef RBTreeNode<T> Node;
    	typedef __RBTreeIterator<T, Ref, Ptr> Self;
    	typedef __RBTreeIterator<T, T&, T*> iterator;
    
    	Node* _node;
    
    	__RBTreeIterator(Node* node)
    		:_node(node)
    	{}
    
    	// 普通迭代器的时候,他是拷贝构造
    	// const迭代器的时候,他是构造,支持用普通迭代器构造const迭代器
    	__RBTreeIterator(const iterator& s)
    		:_node(s._node)
    	{}
    
    	Ref operator*()
    	{
    		return _node->_data;
    	}
    
    	Ptr operator->()
    	{
    		return &_node->_data;
    	}
    
    	Self& operator++()
    	{
    		if (_node->_right)
    		{
    			Node* min = _node->_right;
    			while (min->_left)
    			{
    				min = min->_left;
    			}
    
    			_node = min;
    		}
    		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* max = _node->_left;
    			while (max->_right)
    			{
    				max = max->_right;
    			}
    
    			_node = max;
    		}
    		else
    		{
    			Node* cur = _node;
    			Node* parent = cur->_parent;
    			while (parent && cur == parent->_left)
    			{
    				cur = cur->_parent;
    				parent = parent->_parent;
    			}
    
    			_node = parent;
    		}
    
    		return *this;
    	}
    
    	bool operator!=(const Self& s) const
    	{
    		return _node != s._node;
    	}
    
    	bool operator==(const Self& s) const
    	{
    		return _node == s._node;
    	}
    
    };
    
    
    // map->RBTree, MapKeyOfT> _t;
    // set->RBTree _t;
    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* left = _root;
    		while (left && left->_left)
    		{
    			left = left->_left;
    		}
    
    		return iterator(left);
    	}
    
    	iterator end()
    	{
    		return iterator(nullptr);
    	}
    
    
    	const_iterator begin() const
    	{
    		Node* left = _root;
    		while (left && left->_left)
    		{
    			left = left->_left;
    		}
    
    		return const_iterator(left);
    	}
    
    	const_iterator end() const
    	{
    		return const_iterator(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;
    		cur->_col = RED;
    		if (kot(parent->_data) < kot(data))
    		{
    			parent->_right = cur;
    			cur->_parent = parent;
    		}
    		else
    		{
    			parent->_left = cur;
    			cur->_parent = parent;
    		}
    
    		while (parent && parent->_col == RED)
    		{
    			Node* grandfater = parent->_parent;
    			if (parent == grandfater->_left)
    			{
    				Node* uncle = grandfater->_right;
    				// 情况一  uncle存在且为红
    				if (uncle && uncle->_col == RED)
    				{
    					parent->_col = uncle->_col = BLACK;
    					grandfater->_col = RED;
    
    					cur = grandfater;
    					parent = cur->_parent;
    				}
    				else
    				{
    					if (cur == parent->_left)
    					{
    						// 情况二
    						RotateR(grandfater);
    						parent->_col = BLACK;
    						grandfater->_col = RED;
    					}
    					else
    					{
    						// 情况三
    						RotateL(parent);
    						RotateR(grandfater);
    						cur->_col = BLACK;
    						grandfater->_col = RED;
    					}
    
    					break;
    				}
    			}
    			else // (parent == grandfater->_right)
    			{
    				Node* uncle = grandfater->_left;
    				if (uncle && uncle->_col == RED)
    				{
    					parent->_col = uncle->_col = BLACK;
    					grandfater->_col = RED;
    
    					cur = grandfater;
    					parent = cur->_parent;
    				}
    				else
    				{
    					//   g                
    					//      p
                        //         c
    					if (cur == parent->_right)
    					{
    						RotateL(grandfater);
    						parent->_col = BLACK;
    						grandfater->_col = RED;
    					}
    					else
    					{
    						//   g                
    						//      p
    						//   c
    						RotateR(parent);
    						RotateL(grandfater);
    						cur->_col = BLACK;
    						grandfater->_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;
    
    		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 (_root == parent)
    		if (ppNode == nullptr)
    		{
    			_root = subL;
    			_root->_parent = nullptr;
    		}
    		else
    		{
    			if (ppNode->_left == parent)
    			{
    				ppNode->_left = subL;
    			}
    			else
    			{
    				ppNode->_right = subL;
    			}
    
    			subL->_parent = ppNode;
    		}
    	}
    
    	void Inorder()
    	{
    		_Inorder(_root);
    	}
    
    	void _Inorder(Node* root)
    	{
    		if (root == nullptr)
    			return;
    
    		_Inorder(root->_left);
    		cout << root->_kv.first << ":" << root->_kv.second << endl;
    		_Inorder(root->_right);
    	}
    
    	bool Check(Node* root, int blackNum, const int ref)
    	{
    		if (root == nullptr)
    		{
    			//cout << blackNum << endl;
    			if (blackNum != ref)
    			{
    				cout << "违反规则:本条路径的黑色节点的数量跟最左路径不相等" << endl;
    				return false;
    			}
    
    			return true;
    		}
    
    		if (root->_col == RED && root->_parent->_col == RED)
    		{
    			cout << "违反规则:出现连续红色节点" << endl;
    			return false;
    		}
    
    		if (root->_col == BLACK)
    		{
    			++blackNum;
    		}
    
    		return Check(root->_left, blackNum, ref)
    			&& Check(root->_right, blackNum, ref);
    	}
    
    	bool IsBalance()
    	{
    		if (_root == nullptr)
    		{
    			return true;
    		}
    
    		if (_root->_col != BLACK)
    		{
    			return false;
    		}
    
    		int ref = 0;
    		Node* left = _root;
    		while (left)
    		{
    			if (left->_col == BLACK)
    			{
    				++ref;
    			}
    
    			left = left->_left;
    		}
    
    		return Check(_root, 0, ref);
    	}
    
    private:
    	Node* _root = nullptr;
    };
    
    template<class K>
    struct SetKeyOfT
    {
    	const K& operator()(const K& key)
    	{
    		return key;
    	}
    };
    
    void testRBTree()
    {
    	RBTree<int, int, SetKeyOfT<int>> t;
    	RBTree<int, int, SetKeyOfT<int>>::const_iterator it = t.begin();
    }
    
    • 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

    3.map的模拟实现

    • Map.h
    #pragma once
    
    #include "RBTree.h"
    
    namespace _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>::iterator iterator;
    		typedef typename RBTree<K, pair<const K, V>, 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<const K, V>& kv)
    		{
    			return _t.Insert(kv);
    		}
    
    		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;
    	};
    
    	void test_map()
    	{
    		int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
    		map<int, int> m;
    		for (auto e : a)
    		{
    			m.insert(make_pair(e, e));
    		}
    
    		map<int, int>::iterator it = m.begin();
    		while (it != m.end())
    		{
    			//it->first++;
    			it->second++;
    			cout << it->first << ":" << it->second << endl;
    			++it;
    		}
    		cout << endl;
    
    		map<string, int> countMap;
    		string arr[] = { "ƻ", "", "㽶", "ݮ", "ƻ", "", "ƻ", "ƻ", "", "ƻ", "㽶", "ƻ", "㽶" };
    		for (auto& e : arr)
    		{
    			countMap[e]++;
    		}
    
    		for (auto& kv : countMap)
    		{
    			cout << kv.first << ":" << kv.second << endl;
    		}
    	}
    }
    
    
    • 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

    4.set的模拟实现

    • Set.h
    #pragma once
    
    #include "RBTree.h"
    
    namespace _set
    {
    	template<class K>
    	class set
    	{
    		struct SetKeyOfT
    		{
    			const K& operator()(const K& key)
    			{
    				return key;
    			}
    		};
    	public:
    		typedef typename RBTree<K, K, SetKeyOfT>::const_iterator iterator;
    		typedef typename RBTree<K, K, SetKeyOfT>::const_iterator const_iterator;
    
    		iterator begin() const
    		{
    			return _t.begin();
    		}
    
    		iterator end() const
    		{
    			return _t.end();
    		}
    
    		// 20:21
    		pair<iterator, bool> insert(const K& key)
    		{
    			pair<typename RBTree<K, K, SetKeyOfT>::iterator, bool> ret = _t.Insert(key);
    			return pair<iterator, bool>(ret.first, ret.second);
    		}
    	private:
    		RBTree<K, K, SetKeyOfT> _t;
    	};
    
    	void test_set()
    	{
    		int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
    		set<int> s;
    		for (auto e : a)
    		{
    			s.insert(e);
    		}
    
    		set<int>::iterator it = s.begin();
    		while (it != s.end())
    		{
    			//*it += 10;
    			cout << *it << " ";
    			++it;
    		}
    		cout << endl;
    
    		for (auto e : s)
    		{
    			cout << e << " ";
    		}
    		cout << endl;
    	}
    }
    
    
    • 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
    • Test.cpp
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    #include "RBTree.h"
    
    #include "Map.h"
    #include "Set.h"
    
    int main()
    {
    	_map::test_map();
    	_set::test_set();
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    🌹🌹 map和set的底层原理 的知识大概就讲到这里啦,博主后续会继续更新更多C++ 和 Linux的相关知识,干货满满,如果觉得博主写的还不错的话,希望各位小伙伴不要吝啬手中的三连哦!你们的支持是博主坚持创作的动力!💪💪

  • 相关阅读:
    WebRTC 源码 编译 iOS端
    QT实现简易闹钟功能
    Bigemap是如何在生态林业科技行业去应用的
    分类之混淆矩阵(Confusion Matrix)
    Markdown 数学公式详解
    分布式文件系统HDFS实践及原理详解part3
    404 - File or directory not found.
    躬身入局,干货分享,2023年春招后端技术岗(Python)面试实战教程,Offer今始为君发
    uniapp使用pinia状态管理永久缓存方法
    3.4 设置环境变量MAKEFILES
  • 原文地址:https://blog.csdn.net/Captain_ldx/article/details/134542443