• 红黑树的模拟实现


    什么是红黑树

    红黑树,是一种二叉搜索树(近似平衡),但在每个结点上增加一个存储位表示结点的颜色,可以是Red或
    Black。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制,红黑树确保没有一条路
    径会比其他路径长出俩倍
    ,因而是接近平衡的。
    特性(约束):
    1, 每个节点只能是红色或者是黑色。
    2,每一条路径的黑色节点数量相同。
    3,不能出现连续的红色节点。(红色节点的孩子必须是黑色)
    4,根节点是黑色。

    为什么最常路径是最短路径的两倍呢?
    因为每条路径的黑色节点数量相等,也就是最短的路径全是黑色节点,这时候最长的路径是一黑一红。

    红黑树的节点

    enum Color
    {
    	RED,
    	BLACK
    };
    
    template < class K , class V>
    struct RBTreeNode
    {
    	RBTreeNode<K, V>* _left;
    	RBTreeNode<K, V>* _right;
    	RBTreeNode<K, V>* _parent;
    
    	Color _color;        //枚举变量来控制颜色
    	pair<K, V> _kv;
    
    	RBTreeNode(const pair<K,V>& kv)
    		:_left(nullptr)
    		,_right(nullptr)
    		,_parent(nullptr)
    		,_color(RED)        
    		,_kv(kv)
    	{}
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    这里为什么要将节点的默认颜色给成红色呢?
    因为红色节点只影响当前路径,黑色节点会影响所有的路径。

    在这里插入图片描述

    红黑树的插入操作

    1,先按照二叉搜索树的方式进行插入操作
    2,判断是否要进行颜色更改或者旋转。

             if (_root == nullptr)
    		{
    			_root = new Node(kv);
    			_root->_color = BLACK;
    			return pair<Node*, bool>(_root, true);
    		}
    
    		Node* cur = _root, * parent = _root;
    		while (cur)
    		{
    			if (kv.first > cur->_kv.first)
    			{
    				parent = cur;
    				cur = cur->_right;
    			}
    			else if (kv.first < cur->_kv.first)
    			{
    				parent = cur;
    				cur = cur->_left;
    			}
    			else
    			{
    				return pair<Node*, bool>(cur,false);
    			}
    		}
    
    		Node* newnode = new Node(kv);
    		if (kv.first > parent->_kv.first)
    		{
    			parent->_right = newnode;
    			newnode->_parent = parent;
    		}
    		else
    		{
    			parent->_left = newnode;
    			newnode->_parent = parent;
    		}
    
    
    • 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

    因为新插入的节点默认是红色节点,所以这里要判断它的父亲。
    第一种情况就是新增节点的父亲是黑色(这里就不需要更改)。
    第二种情况就是新增节点的父亲是红色(需要更改)
    这里的第二种情况又分3种情况(这里主要看叔叔)
    a,cur(新增)为红,parent(父亲)为红,uncle(叔叔)为红,grandpanrent(祖父)是黑色
    这里只需要parent和uncle改为黑,然后grandparent改为红,继续向上调整。 cur变为grandparent
    在这里插入图片描述

    b,如果uncle为黑色或者不存在(这里要旋转处理)
    1,cur为红,parent为红,uncle不存在或者为黑色,grandparent,panrent和cur在一条直线上单旋处理
    旋转完成以后,grandparent变为红,panrent便为黑(因为parent作为子树的根,变为黑色,所以不需要向上调整)
    在这里插入图片描述

    2,cur为红,parent为红,uncle不存在或者为黑色,grandparent,panrent和cur在一条折线上双旋处理
    1,以panrent’为旋转点
    2,以grandparent为旋转点。
    3,改变颜色(只需要记住,旋转后的子树根节点的颜色是黑色,并且一黑一红)。

    在这里插入图片描述

    	pair<Node*, bool> Insert(const pair<K, V>& kv)
    	{
    		if (_root == nullptr)
    		{
    			_root = new Node(kv);
    			_root->_color = BLACK;
    			return pair<Node*, bool>(_root, true);
    		}
    
    		Node* cur = _root, * parent = _root;
    		while (cur)
    		{
    			if (kv.first > cur->_kv.first)
    			{
    				parent = cur;
    				cur = cur->_right;
    			}
    			else if (kv.first < cur->_kv.first)
    			{
    				parent = cur;
    				cur = cur->_left;
    			}
    			else
    			{
    				return pair<Node*, bool>(cur,false);
    			}
    		}
    
    		Node* newnode = new Node(kv);
    		if (kv.first > parent->_kv.first)
    		{
    			parent->_right = newnode;
    			newnode->_parent = parent;
    		}
    		else
    		{
    			parent->_left = newnode;
    			newnode->_parent = parent;
    		}
    
    		cur = newnode;
    		while (parent && parent->_color == RED)
    		{
    			Node* grandparent = parent->_parent; //parent为红色,那么肯定有父亲
    			if (grandparent->_left == parent)  
    			{
    				Node* uncle = grandparent->_right;
    				//第一种情况,cur为红,grandparent为黑,parent和uncle为红
    				if (uncle && uncle->_color == RED)
    				{
    					parent->_color = uncle->_color = BLACK;
    					grandparent->_color = RED;
    					cur = grandparent;
    					parent = cur->_parent;
    				}
    				else  // uncle不存在 或者uncle为黑
    				{
    					if (parent->_left == cur) //右单旋
    					{
    						RotateR(grandparent);
    						
    						grandparent->_color = RED;
    						parent->_color = BLACK;
    					}
    					else        //左右单旋
    					{
    						RotateL(parent);
    						RotateR(grandparent);
    						
    						grandparent->_color = RED;
    						cur->_color = BLACK;
    					}
    					break;
    	      		}
    			}
    			else   //grandparent->_right == parent
    			{
    				Node* uncle = grandparent->_left;
    				if (uncle && uncle->_color == RED)
    				{
    					parent->_color = uncle->_color = BLACK;
    					grandparent->_color = RED;
    
    					cur = grandparent;
    					parent = cur->_parent;
    				}
    				else //叔叔不存在或者叔叔为黑色
    				{
    					if (cur == parent->_right)
    					{
    						//左单旋
    						RotateL(grandparent);
    						grandparent->_color = RED;
    						parent->_color = BLACK;
    					}
    					else
    					{
    						//右左双旋
    						RotateR(parent);
    						RotateL(grandparent);
    						grandparent->_color = RED;
    						cur->_color = BLACK;
    					}
    
    				}
    			}
    		}
    		_root->_color = BLACK;
    		return  pair<Node*, bool>(cur, 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

    如何去判断是否是红黑树

    大致思路是:1,判断每条路径的黑色节点数量是否相同
    2,红色节点的父亲不能是红色

    bool CheckBalance()
    	{
    		if (_root == nullptr)
    		{
    			return true;
    		}
    
    		if (_root->_color == RED)
    		{
    			cout << " _root color is RED" << endl;
    			return false;
    		}
    
    		int BlackNum = 0;
    		Node* cur = _root;  //统计最左侧黑色节点的数量,用来对比
    		while (cur)
    		{
    			if (cur->_color == BLACK)
    			{
    			    BlackNum++;
    			}
    
    			cur = cur->_left;
    		}
    
    		int count = 0;
    		return _CheckBalance(_root, BlackNum, count);
    	}
    
    • 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
    bool _CheckBalance(Node* root, int BlackNum, int count)
    	{
    		if (root == nullptr)
    		{
    			if (count != BlackNum)
    			{
    				cout << "路径上的黑色节点数量不相等" << endl;
    				return false;
    			}
    
    			return true;
    		}
    
    		if (root->_color == RED && root->_parent->_color == RED) //红色节点必然有父亲
    		{
    			cout << " 出现了连续的红色节点" << endl;
    			return false;
    		}
    
    		if (root->_color == BLACK)
    		{
    			count++;
    		}
    
    		return _CheckBalance(root->_left, BlackNum, count)
    			&& _CheckBalance(root->_right, BlackNum, count);
    	}
    
    • 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
  • 相关阅读:
    外汇天眼:经济调控之术!升息如何化解通货膨胀的困扰?
    简单学习解析Python的命令行参数
    JavaScript如何打开和使用JavaScript控制台
    Linux内存管理(二十 六):slub分配器之kmalloc详解
    道路千万条,为什么这家创新存储公司会选这条?
    NeurIPS 2022 | 涨点神器!利用图像辅助三维点云分析的训练新范式
    PyTorch学习笔记(二)
    【ES6】阮一峰ES6学习(一) let、const、解构赋值
    java计算机毕业设计在线考试管理系统源程序+mysql+系统+lw文档+远程调试
    剑指 Offer 04. 二维数组中的查找
  • 原文地址:https://blog.csdn.net/CL2426/article/details/126719439