• _cpp AVL树(map、set等关联式容器的底层结构)


    0. 前言

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

    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)。(也就是这个树的高度h)

    2. AVL树节点的定义

    • AVL树节点的定义:
    template<class K, class V>
    struct AVLTreeNode
    {
    	AVLTreeNode<K, V>* _left;// 该节点的左孩子
    	AVLTreeNode<K, V>* _right;// 该节点的右孩子
    	AVLTreeNode<K, V>* _parent;// 该节点的双亲
    
    	pair<K, V> _kv;
    	int _bf; //该节点的平衡因子
    
    	AVLTreeNode(const pair<K, V>& kv)//初始化列表初始化
    		:_left(nullptr)
    		, _right(nullptr)
    		, _parent(nullptr)
    		, _kv(kv)
    		, _bf(0)
    	{}
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3. AVL树的插入

    • AVL树就是在二叉搜索树的基础上引入了平衡因子,因此AVL树也可以看成是二叉搜索树。

    AVL树的插入过程分为两步:

    1. 按照二叉搜索树的方式插入新节点
    2. 调整节点的平衡因子
    • 实现思路:
    1. 先按照二叉搜索树的规则将节点插入到AVL树中。
    2. 新节点插入后,AVL树的平衡性可能会遭到破坏,此时就需要更新平衡因子,并检测是否破坏了AVL树的平衡性pCur插入后,pParent的平衡因子一定需要调整,在插入之前,pParent的平衡因子分为三种情况:-1,0, 1, 分以下两种情况:
      1. 如果pCur插入到pParent的左侧,只需给pParent的平衡因子-1即可。
      1. 如果pCur插入到pParent的右侧,只需给pParent的平衡因子+1即可此时:pParent的平衡因子可能有三种情况:0,正负1, 正负2。
    1. 此时:pParent的平衡因子可能有三种情况:0,正负1, 正负2。
      1. 如果pParent的平衡因子为0,说明插入之前pParent的平衡因子为正负1,插入后被调整成0,此时满足AVL树的性质,插入成功。
      1. 如果pParent的平衡因子为正负1,说明插入前pParent的平衡因子一定为0,插入后被更新成正负1,此时以pParent为根的树的高度增加,需要继续向上更新。
      1. 如果pParent的平衡因子为正负2,则pParent的平衡因子违反平衡树的性质,需要对其进行旋转处理。
    • 实现的模板如下,旋转下面会具体讲解:

    	bool Insert(const pair<K, V>& kv)
    	{
    		if (_root == nullptr)
    		{
    			_root = new Node(kv);
    			return true;
    		}
    
    		//找find
    		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->_right = cur;
    		}
    		else
    		{
    			parent->_left = cur;
    		}
    		cur->_parent = parent;
    
    		//更新平衡因子
    		while (parent)	//		从下往上更新,最坏情况全部更新完
    		{
    			if (cur == parent->_right)
    			{
    				parent->_bf++;
    			}
    			else      //cur==paren->_left
    			{
    				parent->_bf--;
    			}
    
    			//是否继续更新?
    			if (parent->_bf == 0)
    			{
    				//高度不变,更新结束
    				break;
    			}
    			else if (parent->_bf == 1 || parent->_bf == -1)	//-1 、1
    			{
    				//子树高度变了,继续更新祖先
    				cur = parent;
    				parent = parent->_parent;
    			}
    			else if (parent->_bf == 2 || parent->_bf == -2)	//平衡旋转
    			{
    			     // 双亲的平衡因子为正负2,违反了AVL树的平衡性,需要对以pParent;为根的树进行旋转处理
    			
    				//旋转:左、右、左右、右左
    				if (parent->_bf == 2 && cur->_bf == 1)
    				{
    					RotateL(parent);
    				}
    				else if (parent->_bf == -2 && cur->_bf == -1)
    				{
    					RotateR(parent);
    				}
    				else if (parent->_bf == -2 && cur->_bf == 1)
    				{
    					RotateLR(parent);
    				}
    				else if (parent->_bf == 2 && cur->_bf == -1)
    				{
    					RotateRL(parent);
    				}
    				
    				break;
    			}
    			else
    			{
    				assert(false);
    			}
    		}
    		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

    注意:

    • 更新方法:右平衡因子++;左平衡因子–。当遇到parent平衡因子没变化(0)说明高度没变,(-1和1)一定由0变化的即树高度变了,-2和2需要旋转。

    4. AVL树的旋转

    • 原因:
      • 如果在一棵原本是平衡的AVL树中插入一个新节点,可能造成不平衡,此时必须调整树的结构,使之平衡化。
    • 根据节点插入位置的不同,AVL树的旋转分为四种:

    下面讲解旋转实现的方法。

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

    • 抽象图:
      在这里插入图片描述

    • 实例化图:
      下面5这个结点不为nulltptr的情况在这里插入图片描述

      下面5这个结点为nulltptr的情况
      在这里插入图片描述
      代码实现如下:(注意原根的parent,它可能不为空即可能为一个树中的子树)

    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 (parent == _root)
    		{
    			_root = subR;
    			_root->_parent = nullptr;
    		}
    		else
    		{
    			if (ppNode->_left == parent)
    			{
    				ppNode->_left = subR;
    			}
    			else  //ppNode->_right == parent
    			{
    				ppNode->_right = subR;
    			}
    			subR->_parent = ppNode;
    		}
    
    		//更新平衡因子
    		parent->_bf = 0;
    		subR->_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

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

    • 抽象图:
      在这里插入图片描述

    • 实例化图:
      下面5这个结点不为nulltptr的情况在这里插入图片描述
      下面5这个结点为nulltptr的情况
      在这里插入图片描述
      代码实现如下:

    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)
    		{
    			_root = subL;
    			_root->_parent = nullptr;
    		}
    		else
    		{
    			if (ppNode->_left == parent)
    			{
    				ppNode->_left = subL;
    			}
    			else
    			{
    				ppNode->_right = subL;
    			}
    			subL->_parent = ppNode;
    		}
    
    		//更新平衡因子
    		parent->_bf = 0;
    		subL->_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

    通过左右旋转的方法,我们知道了旋转是如何做到的。

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

    其实就是对左、右旋转的复用。

    • 抽象图:
      在这里插入图片描述
    • 实例化图
      在这里插入图片描述
      代码实现如下:
    void RotateLR(Node* parent)	//左右旋转
    	{
    		Node* subL = parent->_left;
    		Node* subLR = subL->_right;
    		int bf = subLR->_bf;
    
    		RotateL(parent->_left);
    		RotateR(parent);
    
    		//更新平衡因子
    		if (bf == 0)
    		{
    			subLR->_bf = 0;
    			subL->_bf = 0;
    			parent->_bf = 0;
    		}
    		else if (bf == 1)
    		{
    			subLR->_bf = 0;
    			subL->_bf = -1;
    			parent->_bf = 0;
    		}
    		else if (bf == -1)
    		{
    			subLR->_bf = 0;
    			subL->_bf = 0;
    			parent->_bf = 1;
    		}
    		else
    		{
    			//旋转前就有问题
    			assert(false);
    		}
    	}
    
    • 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

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

    其实就是对左、右旋转的复用。

    • 抽象图:
      在这里插入图片描述
    • 实例化图:
      在这里插入图片描述
      代码实现如下:
    void RotateRL(Node* parent)	//右左旋转
    	{
    		Node* subR = parent->_right;
    		Node* subRL = subR->_left;
    		int bf = subRL->_bf;
    
    		RotateR(parent->_right);
    		RotateL(parent);
    
    		//更新平衡因子
    		if (bf == 0)
    		{
    			subRL->_bf = 0;
    			subR->_bf = 0;
    			parent->_bf = 0;
    		}
    		else if (bf == 1)
    		{
    			subRL->_bf = 0;
    			subR->_bf = 0;
    			parent->_bf = -1;
    		}
    		else if (bf == -1)
    		{
    			subRL->_bf = 0;
    			subR->_bf = 1;
    			parent->_bf = 0;
    		}
    		else
    		{
    			//旋转前就有问题
    			assert(false);
    		}
    	}
    
    • 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

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

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

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

    5. AVL树的验证

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

    1. 验证其为二叉搜索树
      如果中序遍历可得到一个有序的序列,就说明为二叉搜索树。

    2. 验证其为平衡树

      • 每个节点子树高度差的绝对值不超过1(注意节点中如果没有平衡因子)
      • 节点的平衡因子是否计算正确。
    // AVL树的验证
    	bool IsAVLTree()
    	{
    		return _IsAVLTree(_root);
    	}
    
    	void InOrder()
    	{
    		_InOrder(_root);
    		cout << endl;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    具体实现如下:

    验证其为平衡树思路:计算每棵树的高度差,再与平衡因子比较即可。

    int _Height(Node* root)
    	{
    		if (root == nullptr)
    		{
    			return 0;
    		}
    
    		return max(_Height(root->_left), _Height(root->_right)) + 1;
    	}
    	
    	bool _IsAVLTree(Node* root)
    	{
    		//空数也是AVL树
    		if (nullptr == root)
    		{
    			return true;
    		}
    
    		//计算_root结点的平衡因子
    		int leftHeight = _Height(root->_left);
    		int rightHeight = _Height(root->_right);
    		int diff = rightHeight - leftHeight;
    		//判断
    		if (abs(diff) >= 2)
    		{
    			cout << root->_kv.first << "结点平衡因子异常" << endl;
    			return false;
    		}
    		else if (diff != root->_bf)
    		{
    			cout << root->_kv.first << "结点平衡因子异常" << endl;
    			return false;
    		}
    
    		return  _IsAVLTree(root->_left) && _IsAVLTree(root->_right);
    	}
    
    • 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
    void _InOrder(Node* root)
    	{
    		if (root == nullptr)
    		{
    			return;
    		}
    
    		_InOrder(root->_left);
    		cout << root->_kv.second << " ";
    		_InOrder(root->_right);
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    6. 本篇总代码

    #pragma once
    #include
    #include
    #include
    #include
    #include
    
    using namespace std;
    
    template<class K, class V>
    struct AVLTreeNode
    {
    	AVLTreeNode<K, V>* _left;
    	AVLTreeNode<K, V>* _right;
    	AVLTreeNode<K, V>* _parent;
    
    	pair<K, V> _kv;
    	int _bf; //平衡因子
    
    	AVLTreeNode(const pair<K, V>& kv)
    		:_left(nullptr)
    		, _right(nullptr)
    		, _parent(nullptr)
    		, _kv(kv)
    		, _bf(0)
    	{}
    };
    
    template<class K, class V>
    class AVLTree
    {
    public:
    	typedef AVLTreeNode<K, V> Node;
    	AVLTree()
    	{}
    
    	bool Insert(const pair<K, V>& kv)
    	{
    		if (_root == nullptr)
    		{
    			_root = new Node(kv);
    			return true;
    		}
    
    		//找find
    		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->_right = cur;
    		}
    		else
    		{
    			parent->_left = cur;
    		}
    		cur->_parent = parent;
    
    		//更新平衡因子
    		while (parent)	//		从下往上更新,最坏情况全部更新完
    		{
    			if (cur == parent->_right)
    			{
    				parent->_bf++;
    			}
    			else      //cur==paren->_left
    			{
    				parent->_bf--;
    			}
    
    			//是否继续更新?
    			if (parent->_bf == 0)
    			{
    				//高度不变,更新结束
    				break;
    			}
    			else if (parent->_bf == 1 || parent->_bf == -1)	//-1 、1
    			{
    				//子树高度变了,继续更新祖先
    				cur = parent;
    				parent = parent->_parent;
    			}
    			else if (parent->_bf == 2 || parent->_bf == -2)	//平衡旋转
    			{
    				//旋转:左、右、左右、右左
    				if (parent->_bf == 2 && cur->_bf == 1)
    				{
    					RotateL(parent);
    				}
    				else if (parent->_bf == -2 && cur->_bf == -1)
    				{
    					RotateR(parent);
    				}
    				else if (parent->_bf == -2 && cur->_bf == 1)
    				{
    					RotateLR(parent);
    				}
    				else if (parent->_bf == 2 && cur->_bf == -1)
    				{
    					RotateRL(parent);
    				}
    				
    				break;
    			}
    			else
    			{
    				assert(false);
    			}
    		}
    		return true;
    	}
    
    	// AVL树的验证
    	bool IsAVLTree()
    	{
    		return _IsAVLTree(_root);
    	}
    
    	void InOrder()
    	{
    		_InOrder(_root);
    		cout << endl;
    	}
    
    private:
    	void _InOrder(Node* root)
    	{
    		if (root == nullptr)
    		{
    			return;
    		}
    
    		_InOrder(root->_left);
    		cout << root->_kv.second << " ";
    		_InOrder(root->_right);
    	}
    
    	int _Height(Node* root)
    	{
    		if (root == nullptr)
    		{
    			return 0;
    		}
    
    		return max(_Height(root->_left), _Height(root->_right)) + 1;
    	}
    	bool _IsAVLTree(Node* root)
    	{
    		//空数也是AVL树
    		if (nullptr == root)
    		{
    			return true;
    		}
    
    		//计算_root结点的平衡因子
    		int leftHeight = _Height(root->_left);
    		int rightHeight = _Height(root->_right);
    		int diff = rightHeight - leftHeight;
    		//判断
    		if (abs(diff) >= 2)
    		{
    			cout << root->_kv.first << "结点平衡因子异常" << endl;
    			return false;
    		}
    		else if (diff != root->_bf)
    		{
    			cout << root->_kv.first << "结点平衡因子异常" << endl;
    			return false;
    		}
    
    		return  _IsAVLTree(root->_left) && _IsAVLTree(root->_right);
    	}
    
    	void RotateRL(Node* parent)	//右左旋转
    	{
    		Node* subR = parent->_right;
    		Node* subRL = subR->_left;
    		int bf = subRL->_bf;
    
    		RotateR(parent->_right);
    		RotateL(parent);
    
    		//更新平衡因子
    		if (bf == 0)
    		{
    			subRL->_bf = 0;
    			subR->_bf = 0;
    			parent->_bf = 0;
    		}
    		else if (bf == 1)
    		{
    			subRL->_bf = 0;
    			subR->_bf = 0;
    			parent->_bf = -1;
    		}
    		else if (bf == -1)
    		{
    			subRL->_bf = 0;
    			subR->_bf = 1;
    			parent->_bf = 0;
    		}
    		else
    		{
    			//旋转前就有问题
    			assert(false);
    		}
    	}
    
    	void RotateLR(Node* parent)	//左右旋转
    	{
    		Node* subL = parent->_left;
    		Node* subLR = subL->_right;
    		int bf = subLR->_bf;
    
    		RotateL(parent->_left);
    		RotateR(parent);
    
    		//更新平衡因子
    		if (bf == 0)
    		{
    			subLR->_bf = 0;
    			subL->_bf = 0;
    			parent->_bf = 0;
    		}
    		else if (bf == 1)
    		{
    			subLR->_bf = 0;
    			subL->_bf = -1;
    			parent->_bf = 0;
    		}
    		else if (bf == -1)
    		{
    			subLR->_bf = 0;
    			subL->_bf = 0;
    			parent->_bf = 1;
    		}
    		else
    		{
    			//旋转前就有问题
    			assert(false);
    		}
    	}
    
    	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)
    		{
    			_root = subL;
    			_root->_parent = nullptr;
    		}
    		else
    		{
    			if (ppNode->_left == parent)
    			{
    				ppNode->_left = subL;
    			}
    			else
    			{
    				ppNode->_right = subL;
    			}
    			subL->_parent = ppNode;
    		}
    
    		//更新平衡因子
    		parent->_bf = 0;
    		subL->_bf = 0;
    	}
    
    	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 (parent == _root)
    		{
    			_root = subR;
    			_root->_parent = nullptr;
    		}
    		else
    		{
    			if (ppNode->_left == parent)
    			{
    				ppNode->_left = subR;
    			}
    			else  //ppNode->_right == parent
    			{
    				ppNode->_right = subR;
    			}
    			subR->_parent = ppNode;
    		}
    
    		//更新平衡因子
    		parent->_bf = 0;
    		subR->_bf = 0;
    	}
    
    	Node* _root = nullptr;
    };
    
    void TectAVLTree1()
    {
    	int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
    	//int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
    	//int a[] = { 1,2,3,4,5,6,7,8,9 };
    	AVLTree<int, int> t;
    	for (auto e : a)
    	{
    		t.Insert(make_pair(e, e));
    	}
    
    	t.InOrder();
    
    	if (t.IsAVLTree())
    		cout << "ture" << endl;
    	else
    		cout << "false" << 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
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358

    在这里插入图片描述

  • 相关阅读:
    顺序表的简单描述及代码的简单实现
    springboot高校学生宿舍水电费报修考勤管理系统
    web网页设计期末课程大作业:旅游网页主题网站设计——中国风的温泉酒店预订网(13页)HTML+CSS+JavaScript
    nsoftware Cloud SMS 2022 .NET 22.0.8 Crack
    第四章 使用管理门户监视IRIS - 监控SQL活动
    Linux之Shell变量和引用
    【第53篇】MAFormer: 基于多尺度注意融合的Transformer网络视觉识别
    tomcat命令行下启动中文乱码(解释原理版)
    MySQL 数据库 定义参数【连接查询】
    Otsu阈值分割详解
  • 原文地址:https://blog.csdn.net/Dingyuan0/article/details/127653662