• [数据结构]红黑树图解及其代码实现



    一、红黑树的概念

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


    二、红黑树的性质

    1. 每个结点不是红色就是黑色
    2. 根节点是黑色的
    3. 如果一个节点是红色的,则它的两个孩子结点是黑色的(没有连续的红色节点)
    4. 对于每个结点,从该结点到其所有后代叶结点的简单路径上,均包含相同数目的黑色结点(每条路径黑色节点的数量相同)
    5. 每个叶子结点都是黑色的(此处的叶子结点指的是空结点——NIL节点)
      在这里插入图片描述
      假设每条路径黑节点的数量是N,则:
      N <= 任意路径长度 <= 2N

    最短路径:全黑
    最长路径:一黑一红

    最长路径不超过最短路径的2倍

    AVL树:左右两边更均衡,高度更接近logN
    满二多叉树:2^h - 1 = N
    红黑树的两边没有那么均衡,整体高度:2 * logN

    假设红黑树中一条路径黑色节点的数量是X,高度为h,N为节点数量
    则:X <= h <= 2X
    2^X - 1 <= N <= 2^2X - 1
    全黑满二叉树 ~ 一黑一红满二叉树
    
    • 1
    • 2
    • 3
    • 4

    AVL严格平衡,效率logN,红黑树是近似平衡,效率是2 * logN
    而AVL更加平衡是通过更多次旋转到达的


    三、红黑树基本框架

    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>
    struct RBTree
    {
    	typedef RBTreeNode<K, V> Node;
    
    public:
    	RBTree()
    		: _root(nullptr)
    	{}
    
    private:
    	Node* _root;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    四、红黑树插入操作

    前半部分插入按照线索二叉树的插入原则,比他大,插在右边,小就插在左边

    在插入操作,我们是插入红结点还是黑节点?——红节点,因为插入了黑节点可能会影响其他路径了,问题比较严重,所以我们选择插入红色节点,然后再去进行调整
    在这里插入图片描述
    如图若22为新插入节点,并且是黑色,那么绿色路径有3个黑色节点,橙色路径有两个黑色节点,影响会更大一些

    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 (_root->_kv.first > kv.first)
    		{
    			parent = cur;
    			cur = cur->_left;
    		}
    		else if (_root->_kv.first < kv.first)
    		{
    			parent = cur;
    			cur = cur->_right;
    		}
    		else
    		{
    			return false;
    		}
    	}
    	cur = new Node(kv);
    	cur->_col = RED;
    	if (parent->_kv.first > kv.first)
    	{
    		parent->_left = cur;
    	}
    	else
    	{
    		parent->_right = cur;
    	}
    	cur->_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
    • 39
    • 40
    • 41

    在插入新节点后,要看红黑树性质是否被破坏

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

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

    4.1 cur为红,p为红,g为黑,u存在且为红

    在这里插入图片描述

    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 (_root->_kv.first > kv.first)
    		{
    			parent = cur;
    			cur = cur->_left;
    		}
    		else if (_root->_kv.first < kv.first)
    		{
    			parent = cur;
    			cur = cur->_right;
    		}
    		else
    		{
    			return false;
    		}
    	}
    	cur = new Node(kv);
    	cur->_col = RED;
    	if (parent->_kv.first > kv.first)
    	{
    		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 = uncle->_col = BLACK;
    				grandFather->_col = RED;
    
    				cur = grandFather;
    				parent = cur->_parent;
    			}
    		}
    	}
    	_root->_col = BLACK;
    
    	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.2 cur为红,p为红,g为黑,u不存在/u存在且为黑 (单旋情况)

    在这里插入图片描述
    u情况说明:
    如果u节点不存在,则cur一定是新插入节点,因为如果cur不是新插入节点,则cur和p一定有一个节点的颜色是黑色,这样子就不满足性质4(每条路径黑色节点个数要相同)
    当u节点存在且为黑色的时候,cur节点的原先颜色一定是黑色,现在是红色的原因是子树在调整过程中,将cur节点由黑色改为了红色

    处理方法:

    1. 当左边高进行右旋,右边高进行左旋
    2. p和g进行变色,p变黑色、g变红色

    4.2.1 左单旋

    void RotateL(Node* parent)
    {
    	Node* subR = parent->_right;
    	Node* subRL = subR->_left;
    
    	parent->_right = subRL;
    	if (subRL)
    	{
    		subRL->_parent = parent;
    	}
    
    	Node* parentParent = parent->_parent;
    	subR->_left = parent;
    	parent->_parent = subR;
    
    	if (_root == parent)
    	{
    		_root = subR;
    		subR->_parent = nullptr;
    	}
    	else
    	{
    		if (parentParent->_left == parent)
    			parentParent->_left = subR;
    		else
    			parentParent->_right = subR;
    		subR->_parent = parentParent;
    	}
    }
    
    • 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

    4.2.2 右单旋

    void RotateR(Node* parent)
    {
    	Node* subL = parent->_left;
    	Node* subLR = subL->_right;
    
    	parent->_left = subLR;
    	if (subLR)
    		subLR->_parent = parent;
    
    	Node* parentParent = parent->_parent;
    
    	subL->_right = parent;
    	parent->_parent = subL;
    
    	if (parent == _root)
    	{
    		_root = subL;
    		_root->_parent = nullptr;
    	}
    	else
    	{
    		if (parentParent->_left == parent)
    			parentParent->_left = subL;
    		else
    			parentParent->_right = subL;
    
    		subL->_parent = parentParent;
    	}
    }
    
    • 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

    4.3 cur为红,p为红,g为黑,u不存在/u存在且为黑 (双旋情况)

    在这里插入图片描述
    综上所述 我们代码可以进行完善

    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 (_root->_kv.first > kv.first)
    		{
    			parent = cur;
    			cur = cur->_left;
    		}
    		else if (_root->_kv.first < kv.first)
    		{
    			parent = cur;
    			cur = cur->_right;
    		}
    		else
    		{
    			return false;
    		}
    	}
    
    	// 找到该位置后进行插入
    	cur = new Node(kv);
    	cur->_col = RED;
    	if (parent->_kv.first > kv.first)
    	{
    		parent->_left = cur;
    	}
    	else
    	{
    		parent->_right = cur;
    	}
    	cur->_parent = parent;
    
    	// 接下来控制平衡
    	// 只有插入的节点和双亲节点都是红色才需要去进行处理
    	while (parent && parent->_col == RED)
    	{
    		Node* grandFather = parent->_parent;
    		if (parent == grandFather->_left) // parent在左侧的情况
    		{
    			Node* uncle = grandFather->_right;
    
    			// 情况1:叔叔存在且为红
    			if (uncle && uncle->_col == RED)
    			{
    				parent->_col = uncle->_col = BLACK;
    				grandFather->_col = RED;
    
    				cur = grandFather;
    				parent = cur->_parent;
    			}
    			// 情况2:叔叔不存在/叔叔存在且为黑 
    			else
    			{
    				/*
    				单旋
    						 g
    					   p
    					 c
    
    				双旋
    						 g
    					   p
    						 c
    				*/
    				if (cur == parent->_left) // 单旋
    				{
    					RotateR(grandFather);
    					parent->_col = BLACK;
    					grandFather->_col = RED;
    				}
    				else // 双旋
    				{
    					RotateL(parent);
    					RotateR(grandFather);
    					cur->_col = BLACK;
    					grandFather->_col = RED;
    				}
    				break; // 旋转完后跳出循环
    			}
    		}
    		else // if (parent == grandFather->_left) // parent在右侧的情况
    		{
    			Node* uncle = grandFather->_left;
    			if (uncle && uncle->_col == RED)
    			{
    				// 变色+向上处理
    				parent->_col = uncle->_col = BLACK;
    				grandFather->_col = RED;
    
    				cur = grandFather;
    				parent = cur->_parent;
    			}
    			else
    			{
    				/*
    					单旋
    					g
    					  p
    						c
    					双旋
    					g
    					  p
    						c
    				*/
    				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 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
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135

    五、补充

    5.1 求红黑树高度

    int Height()
    {
    	return _Height(_root);
    }
    
    int _Height(Node* root)
    {
    	if (root == NULL)
    	{
    		return 0;
    	}
    	int leftHight = _Height(root->_left);
    	int rightHight = _Height(root->_right);
    	return leftHight > rightHight ? leftHight + 1 : rightHight + 1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    5.2 判断一棵树是否为红黑树

    bool IsBalance()
    {
    	if (_root && _root->_col == RED)
    	{
    		cout << "根节点不是黑色" << endl;
    		return false;
    	}
    	int banchmark = 0; // 基准值—计算一边的黑色节点数目
    	Node* left = _root;
    	while (left)
    	{
    		if (left->_col == BLACK)
    			++banchmark;
    
    		left = left->_left;
    	}
    
    	int blackNum = 0;
    	return _IsBalance(_root, banchmark, blackNum);
    }
    
    bool _IsBalance(Node* root, int banchmark, int blackNum)
    {
    	if (root == nullptr)
    	{
    		if (banchmark != blackNum)
    		{
    			cout << "黑色节点数量不相同" << endl;
    			return false;
    		}
    		return true;
    	}
    
    	if (root->_col == RED && root->_parent->_col == RED)
    	{
    		cout << "存在连续的红色节点" << endl;
    		return false;
    	}
    
    	if (root->_col == BLACK)
    		blackNum++;
    
    	return _IsBalance(root->_left, banchmark, blackNum)
    		&& _IsBalance(root->_right, banchmark, blackNum);
    }
    
    • 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

    六、红黑树与AVL数的比较

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

    红黑树实际中应用有:

    1. map/set 、 multi_map/multi_set
    2. java库
    3. linux内核
    4. 其他库

    七、完整代码

    #pragma once
    
    #include 
    #include 
    #include 
    
    using namespace std;
    
    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)
    		, _col(RED)
    		, _kv(kv)
    	{}
    };
    
    template <class K, class V>
    struct RBTree
    {
    	typedef RBTreeNode<K, V> Node;
    
    public:
    	RBTree()
    		: _root(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 (_root->_kv.first > kv.first)
    			{
    				parent = cur;
    				cur = cur->_left;
    			}
    			else if (_root->_kv.first < kv.first)
    			{
    				parent = cur;
    				cur = cur->_right;
    			}
    			else
    			{
    				return false;
    			}
    		}
    
    		// 找到该位置后进行插入
    		cur = new Node(kv);
    		cur->_col = RED;
    		if (parent->_kv.first > kv.first)
    		{
    			parent->_left = cur;
    		}
    		else
    		{
    			parent->_right = cur;
    		}
    		cur->_parent = parent;
    
    		// 接下来控制平衡
    		// 只有插入的节点和双亲节点都是红色才需要去进行处理
    		while (parent && parent->_col == RED)
    		{
    			Node* grandFather = parent->_parent;
    			if (parent == grandFather->_left) // parent在左侧的情况
    			{
    				Node* uncle = grandFather->_right;
    
    				// 情况1:叔叔存在且为红
    				if (uncle && uncle->_col == RED) 
    				{
    					parent->_col = uncle->_col = BLACK;
    					grandFather->_col = RED;
    
    					cur = grandFather;
    					parent = cur->_parent;
    				}
    				// 情况2:叔叔不存在/叔叔存在且为黑 
    				else
    				{
    					/*
    					单旋
    							 g               
    					       p        
    					     c
    
    					双旋
    						     g
    						   p
    						     c
    					*/
    					if (cur == parent->_left) // 单旋
    					{
    						RotateR(grandFather);
    						parent->_col = BLACK;
    						grandFather->_col = RED;
    					}
    					else // 双旋
    					{
    						RotateL(parent);
    						RotateR(grandFather);
    						cur->_col = BLACK;
    						grandFather->_col = RED;
    					}
    					break; // 旋转完后跳出循环
    				}
    			}
    			else // if (parent == grandFather->_left) // parent在右侧的情况
    			{
    				Node* uncle = grandFather->_left;
    				if (uncle && uncle->_col == RED)
    				{
    					// 变色+向上处理
    					parent->_col = uncle->_col = BLACK;
    					grandFather->_col = RED;
    
    					cur = grandFather;
    					parent = cur->_parent;
    				}
    				else
    				{
    					/*
    						单旋
    						g
    						  p
    						    c
    						双旋
    						g 
    						  p
    							c
    					*/
    					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 true;
    	}
    
    	void RotateL(Node* parent)
    	{
    		Node* subR = parent->_right;
    		Node* subRL = subR->_left;
    
    		parent->_right = subRL;
    		if (subRL)
    		{
    			subRL->_parent = parent;
    		}
    
    		Node* parentParent = parent->_parent;
    		subR->_left = parent;
    		parent->_parent = subR;
    
    		if (_root == parent)
    		{
    			_root = subR;
    			subR->_parent = nullptr;
    		}
    		else
    		{
    			if (parentParent->_left == parent)
    				parentParent->_left = subR;
    			else
    				parentParent->_right = subR;
    			subR->_parent = parentParent;
    		}
    	}
    
    	void RotateR(Node * parent)
    	{
    		Node* subL = parent->_left;
    		Node* subLR = subL->_right;
    
    		parent->_left = subLR;
    		if (subLR)
    			subLR->_parent = parent;
    
    		Node* parentParent = parent->_parent;
    
    		subL->_right = parent;
    		parent->_parent = subL;
    
    		if (parent == _root)
    		{
    			_root = subL;
    			_root->_parent = nullptr;
    		}
    		else
    		{
    			if (parentParent->_left == parent)
    				parentParent->_left = subL;
    			else
    				parentParent->_right = subL;
    
    			subL->_parent = parentParent;
    		}
    	}
    
    	void InOrder()
    	{
    		_InOrder(_root);
    	}
    
    	void _InOrder(Node* root)
    	{
    		if (root == NULL)
    			return;
    		
    		_InOrder(root->_left);
    		cout << root->_kv.first << " -> " << root->_kv.second << endl;
    		_InOrder(root->_right);
    
    	}
    	
    	int Height()
    	{
    		return _Height(_root);
    	}
    
    	int _Height(Node* root)
    	{
    		if (root == NULL)
    		{
    			return 0;
    		}
    		int leftHight = _Height(root->_left);
    		int rightHight = _Height(root->_right);
    		return leftHight > rightHight ? leftHight + 1 : rightHight + 1;
    	}
    	// 判断是否为红黑树(防止只是单纯的二叉搜索树)
    
    	bool IsBalance()
    	{
    		if (_root && _root->_col == RED)
    		{
    			cout << "根节点不是黑色" << endl;
    			return false;
    		}
    		int banchmark = 0; // 基准值—计算一边的黑色节点数目
    		Node* left = _root;
    		while (left)
    		{
    			if (left->_col == BLACK)
    				++banchmark;
    
    			left = left->_left;
    		}
    
    		int blackNum = 0;
    		return _IsBalance(_root, banchmark, blackNum);
    	}
    
    	bool _IsBalance(Node* root, int banchmark, int blackNum)
    	{
    		if (root == nullptr)
    		{ 
    			if (banchmark != blackNum)
    			{
    				cout << "黑色节点数量不相同" << endl;
    				return false;
    			}
    			return true;
    		}
    		
    		if (root->_col == RED && root->_parent->_col == RED)
    		{
    			cout << "存在连续的红色节点" << endl;
    			return false;
    		}
    
    		if (root->_col == BLACK)
    			blackNum++;
    
    		return _IsBalance(root->_left, banchmark, blackNum)
    			&& _IsBalance(root->_right, banchmark, blackNum);
    	}
    private:
    	Node* _root;
    };
    
    void TestRBTree()
    {
    	RBTree<int, int> t;
    	//int a[] = {5,4,3,2,1,0};
    	//int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
    	int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
    	for (auto e : a)
    	{
    		t.Insert(make_pair(e, e));
    		/*if (!t.IsBalance())
    		{
    		cout << "Insert" << e << endl;
    		}*/
    	}
    	t.InOrder();
    	cout << t.IsBalance() << endl;
    	cout << "高度:" << t.Height() << 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
    • 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
  • 相关阅读:
    Bootstrap-媒体类型
    09_CSS3多媒体查询
    用arduino开发梅树派Raspberry Pi Pico
    最小区间覆盖问题
    css前端面试题(三)
    PSO粒子群优化CNN-优化神经网络神经元个数dropout和batch_size等超参数
    如何在mac下使用homebrew安装 mysql?
    视频调整帧率、分辨率+音画同步
    为什么 JavaScript 中的 0.1 + 0.2 不等于 0.3
    Clion-MinGW编译后的exe文件添加ico图标
  • 原文地址:https://blog.csdn.net/weixin_51304981/article/details/126343803