平衡二叉树(AVL)

AVL二叉平衡搜索树

AVL树可以是一棵空树
AVL树:二叉搜索树+每个节点增加平衡因子
平衡因子:右子树高度-左子树高度
平衡因子的绝对值不能超过1
平衡因子: -1 0 1
高度:logN

AVL树的概念

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

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

  • 它的左右子树都是AVL树
  • 左右子树高度之差(简称平衡因子)的绝对值不超过1(-1/0/1)
    在这里插入图片描述
    如果一棵二叉搜索树是高度平衡的,它就是AVL树。如果它有n个结点,其高度可保持在 ,搜索时间复杂度O(logN)。

AVL树节点的定义

template<class T>
struct AVLTreeNode
{
	AVLTreeNode(const T& data = T())
		: _pLeft(nullptr)
		, _pRight(nullptr)
		, _data(data)
		, _bf(0)
	{}
	AVLTreeNode<T>* _pLeft;
	AVLTreeNode<T>* _pRight;
	T _data;
	int _bf;//当前节点的平衡因子
};

AVL树的插入

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

1.空树—直接插入
2.非空:

  1. 按照二叉搜索树的性质找到待插入节点在树中的位置
  2. 插入新节点 pCur
  3. 更新 pCur的平衡因子

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的平衡因子违反平衡树的性质,需要对其进行旋转处理

AVL树的旋转

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

左单旋

新节点插入到较高右子树的右侧–右右—>左单旋
在这里插入图片描述

右单旋

新节点插入到较高右子树的右侧–左左—>右单旋

在这里插入图片描述

左右双旋

新节点插入较高左子树的右侧—左右
在这里插入图片描述

右左双旋

新节点插入较高右子树的左侧—右左
在这里插入图片描述

总结

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

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

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

AVL树的验证

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

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

验证用例:

常规场景1 {16, 3, 7, 11, 9, 26, 18, 14, 15}
特殊场景2 {4, 2, 6, 1, 3, 5, 15, 7, 16, 14}

AVL树的性能

AVL树是一棵绝对平衡的二叉搜索树,其要求每个节点的左右子树高度差的绝对值都不超过1,这样可以保证查询时高效的时间复杂度,即 。但是如果要对AVL树做一些结构修改的操作,性能非常低下,比如:

插入时要维护其绝对平衡,旋转的次数比较多,更差的是在删除时,有可能一直要让旋转持续到根的位置。

因此: 如果需要一种查询高效且有序的数据结构,而且数据的个数为静态的(即不会改变),可以考虑AVL树, 但一个结构经常修改,就不太适合。

AVL树代码实现

#pragma once
#include <iostream>
using namespace std;
template<class T>
struct AVLTreeNode
{
	AVLTreeNode(const T& data = T())
		: _pLeft(nullptr)
		, _pRight(nullptr)
		, _pParent(nullptr)
		, _data(data)
		, _bf(0)
	{}
	AVLTreeNode<T>* _pLeft;
	AVLTreeNode<T>* _pRight;
	AVLTreeNode<T>* _pParent;
	T _data;
	int _bf;//当前节点的平衡因子
};
template<class T>
class AVLTree
{
	typedef AVLTreeNode<T> Node;
public:
	AVLTree()
		:_pRoot(nullptr)
	{}
	bool Insert(const T& data)
	{
		if (nullptr == _pRoot)
		{
			_pRoot = new Node(data);
			return true;
		}

		//非空
		//按照二叉搜索树的性质:找待插入节点在AVL树中的位置
		Node* pCur = _pRoot;
		Node* pParent = nullptr;
		while (pCur)
		{
			pParent = pCur;
			if (data < pCur->_data)
				pCur = pCur->_pLeft;
			else if (data > pCur->_data)
				pCur = pCur->_pRight;
			else
				return false;
		}
		//插入新节点
		pCur = new Node(data);
		if (data < pParent->_data)
			pParent->_pLeft = pCur;
		else
			pParent->_pRight = pCur;
			
		pCur->_pParent = pParent;
		//是否满足AVL树的性质
		//可能会导致parent节点的平衡因子不满足AVL树的性质
		while (pParent)
		{
			//必须更新parent的平衡因子
			if (pCur == pParent->_pLeft)
				pParent->_bf--;
			else
				pParent->_bf++;

			if (0 == pParent->_bf)
				return true;
			else if (-1 == pParent->_bf || 1 == pParent->_bf)
			{
				pCur = pParent;
				pParent = pCur->_pParent;
			}
			else
			{
				//双亲的平衡因子不满足AVL树的性质
				//双亲的节点的平衡因子为:2或者-2
				//需要对以双亲为根的二叉树进行旋转处理
				if (2==pParent->_bf)
				{
					//右子树高
					if (1 == pCur->_bf)
						_RotateLeft(pParent);
					else
						_RotateRL(pParent);
				}
				else
				{
					//左子树高 
					if (-1 == pCur->_bf)
						_RotateRight(pParent);
					else
						_RotateLR(pParent);
				}
				break;
			}
		}
		return true;
	}

	void InOrder()
	{
		_InOrder(_pRoot);
	}

	bool IsValidAVLTree()
	{
		return _IsValidAVLTree(_pRoot);
	}
protected:
	bool _IsValidAVLTree(Node* pRoot)//检测AVL树
	{
		if (nullptr == pRoot)
			return true;

		int leftHeight = _Height(pRoot->_pLeft);
		int rightHeight = _Height(pRoot->_pRight);
		if (!(pRoot->_bf >= -1 && pRoot->_bf <= 1 && pRoot->_bf == rightHeight - leftHeight))
			return false;
		return _IsValidAVLTree(pRoot->_pLeft) && _IsValidAVLTree(pRoot->_pRight);
	}
	size_t _Height(Node* pRoot)
	{
		if (nullptr == pRoot)
			return 0;

		size_t leftHeight = _Height(pRoot->_pLeft);
		size_t rightHeight = _Height(pRoot->_pRight);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}
	void _InOrder(Node* pRoot)
	{
		if (pRoot)
		{
			_InOrder(pRoot->_pLeft);
			cout << pRoot->_data << " ";
			_InOrder(pRoot->_pRight);
		}
	}
	void _RotateRight(Node* pParent)//右单旋
	{
		Node *pSubL = pParent->_pLeft;
		Node* pSubLR = pSubL->_pRight;
		
		//更新孩子指针域
		pParent->_pLeft = pSubLR;
		if (pSubLR)
			pSubLR->_pParent = pParent;
		pSubL->_pRight = pParent;
		
		//更新双亲指针域
		Node* pPParent = pParent->_pParent;
		pParent->_pParent = pSubL;
		pSubL->_pParent = pPParent; 
		
		//判断pParent:根节点||非根节点(pParent可能为其双亲的左或右孩子)
		if (nullptr == pPParent)
			_pRoot = pSubL;
		else
		{
			if (pParent == pPParent->_pLeft)
				pPParent->_pLeft = pSubL;
			else
				pPParent->_pRight = pSubL;
		}
		pParent->_bf = pSubL->_bf = 0;
	}
		
	void _RotateLeft(Node* pParent)//左单旋
	{
		Node *pSubR = pParent->_pRight; 
		Node* pSubRL = pSubR->_pLeft;

		//更新孩子指针域
		pParent->_pRight = pSubRL;
		if (pSubRL)
			pSubRL->_pParent = pParent;
		pSubR->_pLeft = pParent;

		//更新双亲指针域
		Node* pPParent = pParent->_pParent;
		
		pSubR->_pParent = pPParent;
		pParent->_pParent = pSubR;
		//判断pParent:根节点||非根节点(pParent可能为其双亲的左或右孩子)
		if (nullptr == pPParent)
			_pRoot = pSubR;
		else
		{
			if (pParent == pPParent->_pLeft)
				pPParent->_pLeft = pSubR;
			else
				pPParent->_pRight = pSubR;
		}
		pParent->_bf = pSubR->_bf = 0;
	}
	void _RotateRL(Node* pParent)
	{
		Node* pSubR = pParent->_pRight;
		Node* pSubRL = pSubR->_pLeft;
		int bf = pSubRL->_bf;
		_RotateRight(pParent->_pRight);
		_RotateLeft(pParent);

		if (-1 == bf)
			pSubR->_bf = 1;
		else if (1 == bf)
			pParent->_bf = -1;
	}
	void _RotateLR(Node* pParent)
	{
		Node* pSubL = pParent->_pLeft;
		Node* pSubLR = pSubL->_pRight;
		int bf = pSubLR->_bf;
		_RotateLeft(pParent->_pLeft);
		_RotateRight(pParent);

		if (-1 == bf)
			pSubL->_bf = 1;
		else if (1 == bf)
			pParent->_bf = -1;
	}
private:
	Node* _pRoot;
};
void TestAVLTree()
{
	//int array[] = { 16,3,7,11,9,26,18,14,15 };
	int array[] = { 4,2,6,1,3,5,15,7,16,14 };//最强检测
	AVLTree<int> t;
	for (auto e : array)
		t.Insert(e);

	t.InOrder();
	if (t.IsValidAVLTree())
	{
		cout << "是有效的AVL树" << endl;
	}
	else
		cout << "不是有效的AVL树" << endl;
}
int main()
{
    TestAVLTree();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值