AVL树的实现

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

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

2.AVL树节点的定义

template<class K,class V>
struct  AVLTreeNode
{

	AVLTreeNode(const pair<K, V>& data)
		:_pLeft(nullptr)
		,_pRight(nullptr)
		,_pParent(nullptr)
		,_data(data)
		,_bf(0)
	{}
public:
	AVLTreeNode<K, V>* _pLeft;
	AVLTreeNode<K, V>* _pRight;
	AVLTreeNode<K, V>* _pParent;
	pair<K, V> _data;
	int _bf;	//节点的平衡因子
};

3.AVL树的插入
AVL树就是在二叉搜索树的基础上引入了平衡因子,因此AVL树也可以看成是二叉搜索树。那么AVL树的插入 过程可以分为两步:
1. 按照二叉搜索树的方式插入新节点
2. 调整节点的平衡因子
插入部分的代码如下:

bool Insert(const pair<K, V>& data)
	{
		//按照二叉搜索树得规则将新节点插入到AVL树中
		if (nullptr == _pRoot)
		{
			_pRoot = new Node(data);
			return true;
		}

		//找待插入结点在AVL树中的位置
		Node* pCur = _pRoot;
		Node* pParent = nullptr;
			while(pCur)
			{
				pParent = pCur;
				if (data.first < pCur->_data.first)
					pCur = pCur->_pLeft;
				else if (data.first > pCur->_data.first)
					pCur = pCur->_pRight;
				else
					return false;
			}
		//插入结点
		pCur = new Node(data);
		if (data.first < pParent->_data.first)
			pParent->_pLeft = pCur;
		else
			pParent->_pRight = pCur;
		pCur->_pParent = pParent;
		while (pParent)
		{
			//AVL:必须更新pParent结点的平衡因子
			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
			{
				//pParent结点的平衡因子已经为正负2
				if (2 == pParent->_bf)
				{
					if (1 == pCur->_bf)	//左单旋
						RotateL(pParent);
					else
					{
						RotateRL(pParent);
					}
				}
				else
				{
					if (-1 == pCur->_bf)	//右单旋
						RotateR(pParent);
					else
					{
						RotateLR(pParent);
					}

				}
				break;
			}
		}
		return true;
		
	}

4. AVL树的旋转
如果在一棵原本是平衡的AVL树中插入一个新节点,可能造成不平衡,此时必须调整树的结构,使之平衡化。根据节点插入位置的不同,AVL树的旋转分为四种:
1.新节点插入较高左子树的左侧—左左:右单旋
在这里插入图片描述
上图在插入前,AVL树是平衡的,新节点插入到50的左子树(注意:此处不是左孩子)中,50左子树增加 了一层,导致以60为根的二叉树不平衡,要让60平衡,只能将60左子树的高度减少一层,右子树增加一 层, 即将左子树往上提,这样60转下来,因为60比50大,只能将其放在50的右子树,而如果50有右子树,右子树根的值一定大于50,小于60,只能将其放在60的左子树,旋转完成后,更新节点的平衡因子即可。在旋 转过程中,有以下几种情况需要考虑: 1. 50节点的右孩子可能存在,也可能不存在 2. 60可能是根节点,也可能是子树 ,如果是根节点,旋转完成后,要更新根节点 ,如果是子树,可能是某个节点的左子树,也可能是右子树。

2.新节点插入较高右子树的右侧—右右:左单旋
在这里插入图片描述
旋转细节参照上面右单旋。
3. 新节点插入较高左子树的右侧—左右:先左单旋再右单旋
在这里插入图片描述
这种情况仅仅对60节点进行左单旋或者右单旋是无法达到平衡的,此时要先对60的左节点50进行一次左单旋,旋转后的二叉树就与上面第一种情况相同,只需要对60节点进行一次右单旋即可达到平衡,旋转之前,保存插入结点的父亲节点的平衡因子,两次旋转完成之后,需要根据该平衡因子来调整其他节点的平衡因子(细节看代码)。

4. 新节点插入较高右子树的左侧—右左:先右单旋再左单旋
在这里插入图片描述
这种情况的旋转细节,与上面第三种类似,这里不再赘述。
看了上面几种旋转的情况想必大家有点感觉了,下来可以看看下面完整版的代码,再好好捋一捋思路:

#pragma once
/*
四种不同类型的旋转:
单旋转
1.新节点插在较高左子树左侧--> 左左--->右单旋
2.插在较高右子树的右侧-->右右--->左单旋
双旋转:
较高左子树的右侧-->左单旋右单旋   
较高右子树的左侧-->右单旋左单旋

注意:更新平衡因子
*/
#include<iostream>
using namespace std;
template<class K,class V>
struct  AVLTreeNode
{

	AVLTreeNode(const pair<K, V>& data)
		:_pLeft(nullptr)
		,_pRight(nullptr)
		,_pParent(nullptr)
		,_data(data)
		,_bf(0)
	{}
public:
	AVLTreeNode<K, V>* _pLeft;
	AVLTreeNode<K, V>* _pRight;
	AVLTreeNode<K, V>* _pParent;
	pair<K, V> _data;
	int _bf;	//节点的平衡因子
};

template<class K,class V>
class AVLTree
{
	typedef AVLTreeNode<K, V> Node;
public:
	AVLTree()
		:_pRoot(nullptr)
	{}

	bool Insert(const pair<K, V>& data)
	{
		//按照二叉搜索树得规则将新节点插入到AVL树中
		if (nullptr == _pRoot)
		{
			_pRoot = new Node(data);
			return true;
		}

		//找待插入结点在AVL树中的位置
		Node* pCur = _pRoot;
		Node* pParent = nullptr;
			while(pCur)
			{
				pParent = pCur;
				if (data.first < pCur->_data.first)
					pCur = pCur->_pLeft;
				else if (data.first > pCur->_data.first)
					pCur = pCur->_pRight;
				else
					return false;
			}
		//插入结点
		pCur = new Node(data);
		if (data.first < pParent->_data.first)
			pParent->_pLeft = pCur;
		else
			pParent->_pRight = pCur;
		pCur->_pParent = pParent;
		while (pParent)
		{
			//AVL:必须更新pParent结点的平衡因子
			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
			{
				//pParent结点的平衡因子已经为正负2
				if (2 == pParent->_bf)
				{
					if (1 == pCur->_bf)	//左单旋
						RotateL(pParent);
					else
					{
						RotateRL(pParent);
					}
				}
				else
				{
					if (-1 == pCur->_bf)	//右单旋
						RotateR(pParent);
					else
					{
						RotateLR(pParent);
					}

				}
				break;
			}
		}
		return true;
		
	}
	void RotateR(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;
		pSubL->_pParent = pPParent;
		pParent->_pParent = pSubL;

		//在旋转之前pParent是根结点
		if(nullptr == pPParent)
		{
			_pRoot = pSubL;
		}
		else
		{
			//pParent可能是其双亲的左孩子||右孩子
			if (pPParent->_pLeft == pParent)
				pPParent->_pLeft = pSubL;
			else
				pPParent->_pRight = pSubL;
		}
		pSubL->_bf = pParent->_bf = 0;
	}

	void RotateL(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;
		pParent->_pParent = pSubR;
		pSubR->_pParent = pPParent;

		if (pPParent == nullptr)
			_pRoot = pSubR;
		else
		{
			if (pParent == pPParent->_pLeft)
			{
				pPParent->_pLeft = pSubR;
			}
			else
			{
				pPParent->_pRight = pSubR;
			}
		}
		pSubR->_bf = pParent->_bf = 0;
	}

		//右左双旋
	void RotateRL(Node* pParent)
	{
		Node* SubR = pParent->_pRight;
		Node* SubRL = SubR->_pLeft;
		//旋转前保存SubLR的平衡因子
		int  bf = SubRL ->_bf;
		RotateR(pParent->_pRight);
		RotateL(pParent);
		if(1==bf)
		pParent->_bf = -1;
		else if(-1 == bf)
		pParent->_bf = 1;
	}

	//左右双旋
	void RotateLR(Node* pParent)
	{
		Node* SubL = pParent->_pLeft;
		Node* SubLR = SubL->_pRight;
		//旋转前保存SubLR的平衡因子
		int  bf = SubLR->_bf;
		
		RotateL(pParent->_pLeft);
		RotateR(pParent);
		if(1==bf)
		pParent->_bf = -1;
		else if(-1 == bf)
		pParent->_bf = 1;
	}
	void Inorder()
	{
		_Inorder(_pRoot);
	}

	private:
		size_t _Height(Node* pRoot)
		{
			if (nullptr == pRoot)
				return 0;
			size_t leftHeight = _Height(pRoot->_pLeft) + 1;
			size_t rightHeight = _Height(pRoot->_pRight) + 1;
			return leftHeight > rightHeight ? leftHeight : rightHeight;
		}

		//判断是否是AVL树
		bool _IsBalanceTree(Node* pRoot)
		{
			if (nullptr == pRoot)
				return true;

			size_t leftHeight = _Height(pRoot->_pLeft);
			size_t rightHeight = _Height(pRoot->_pRight);
			int bf = rightHeight - leftHeight
			if (abs(bf) > 1|| bf != pRoot->_bf)
					return false;
			return 	_IsBalanceTree(pRoot->_pLeft) && _IsBalanceTree(pRoot->_pRight);
		}
		void _Inorder(Node* pRoot)
		{
			if (pRoot)
			{
				_Inorder(pRoot->_pLeft);
				cout << pRoot->_data.first << " ";
				_Inorder(pRoot->_pRight);
			}
		}
private:
	
	Node* _pRoot;
};

5.AVL树的删除
因为AVL树也是二叉搜索树,可按照二叉搜索树的方式将节点删除,然后再更新平衡因子,只不错与删除不 同的时,删除节点后的平衡因子更新,差情况下一直要调整到根节点的位置。AVL树的删除相对比较复杂,这里我们不做讨论,具体大家可以下去自己了解学习一下。
6.AVlL树的性能
AVL树是一棵绝对平衡的二叉搜索树,其要求每个节点的左右子树高度差的绝对值都不超过1,这样可以保证 查询时高效的时间复杂度,即 。但是如果要对AVL树做一些结构修改的操作,性能非常低下,比如: 插入时要维护其绝对平衡,旋转的次数比较多,更差的是在删除时,有可能一直要让旋转持续到根的位置。 因此:如果需要一种查询高效且有序的数据结构,而且数据的个数为静态的(即不会改变),可以考虑AVL树, 但一个结构经常修改,就不太适合。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值