AVL树

AVL树

二叉搜索树虽然可以缩短查找的效率,但如果数据有序或接近有序二叉搜索树退化为单支树,查找元素相当与在顺序表中搜索元素,效率低下。
当向二叉搜索树中插入新节点后,如果能保证每个节点的左右子树高度之差绝对值不超过1(需要对树中的节点进行调整),即可降低树的高度,从而减少平均搜索长度。

空树也是AVL树。

AVL树的性质是:

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

AVL树的节点的定义

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

AVL树的实现

#pragma once
#include<iostream>
using namespace std;

template<class T>
struct AVLTreeNode{
	AVLTreeNode(const T&data = T())
	:_pLeft(nullptr)
	, _pRight(nullptr)
	, _pParent(nullptr)
	, _bf(0)
	, _data(data)
	{}
	AVLTreeNode<T>* _pLeft;
	AVLTreeNode<T>* _pRight;
	AVLTreeNode<T>* _pParent;
	int _bf;
	T _data;
};


template<class T>
class AVLTree{
	typedef AVLTreeNode<T> Node;
public:
	AVLTree()
		:_pRoot(nullptr)
	{}
	//插入操作
	bool Insert(const T& data)
	{
		if (nullptr == _pRoot)
		{
			_pRoot = new AVLTreeNode<T>(data);
			return true;
		}
		//找到要插入的位置
		Node* pCur = _pRoot;
		Node* pParent = nullptr;
		while (pCur)
		{
			pParent = pCur;
			if (pCur->_data > data)
			{
				pCur = pCur->_pLeft;
			}
			else if (pCur->_data<data)
			{
				pCur = pCur->_pRight;
			}
			else
			{
				return false;
			}
		}
		//已经找到要插入的位置了
		pCur = new AVLTreeNode<T>(data);
		if (data < pParent->_data)
		{
			pParent->_pLeft = pCur;
		}
		else{
			pParent->_pRight = pCur;
		}
		pCur->_pParent = pParent;


		while (pParent)
		{
			//更新平衡因子
			if (pCur == pParent->_pLeft)
			{
				pParent->_bf--;
			}
			else{
				pParent->_bf++;
			}
			//pParent 的平衡因子改变
			//对pParent的平衡因子进行分析
			//当pParent 的平衡因子变为0,时,说明以前的平衡因子是1/-1,则增加一个节点
			//不会改变pParent上层的平衡因子 直接返回就行
			if (pParent->_bf == 0)
			{
				return true;
			}
			if (pParent->_bf == 1 || pParent->_bf == -1)
			{
				//pParent 的平衡因子是1或者-1时,说明以前的平衡因子是0,则增加一个节点
				//会影响其上层的平衡因子
				//向上返回找到不满足条件的节点
				pCur = pParent;
				pParent = pParent->_pParent;
			}
			else{
				if (pParent->_bf == 2)
				{
					//此时需要旋转!!!
					//同号单旋转
					if (pCur->_bf == 1)
					{
						//右子树高,左旋
						_RotateL(pParent);
					}
					else{
						//异号双旋
						_RotateRL(pParent);
					}
				}
				else if (pParent->_bf == -2)
				{
					if (pCur->_bf == 1)
					{
						//异号双旋
						_RotateLR(pParent);
					}
					else
					{
						_RotateR(pParent);
					}

				}
				break;
			}
		}
		return true;
	}
	//对外只需要提供接口就行
	void InOrdor()
	{
		_InOrdor(_pRoot);
		cout << endl;
	}
	bool IsValidAVLTree()
	{
		return _IsValidAVLTree(_pRoot);
	}
protected:
	void _InOrdor(Node* pRoot)
	{
		if (nullptr == pRoot)
		{
			return;
		}
		_InOrdor(pRoot->_pLeft);
		cout << pRoot->_data << " ";
		_InOrdor(pRoot->_pRight);
	}
	bool _IsValidAVLTree(Node* pRoot)
	{
		if (nullptr == pRoot)
		{
			return true;
		}
		size_t _leftHeight = _Height(pRoot->_pLeft);
		size_t _rightHeight = _Height(pRoot->_pRight);

		if (!(pRoot->_bf == (_rightHeight - _leftHeight) && (-1 == pRoot->_bf || 1 == pRoot->_bf)))
		{
			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 _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;

		//判断pParent 是根节点还是非根节点
		if (nullptr == pPParent)
		{
			_pRoot = pSubR;
			pParent->_bf = pSubR->_bf = 0;
			return;
		}
		if (pPParent->_pLeft == pParent)
		{
			pPParent->_pLeft = pSubR;
		}
		else{
			pPParent->_pRight = pSubR;
		}
		//先将平衡因子置0
		//pParent pSubR
		pParent->_bf = pSubR->_bf = 0;
	}
	void _RotateR(Node* pParent)
	{
		//左子树高
		Node* pSubL = pParent->_pLeft;
		Node* pSubLR = pSubL->_pRight;

		pParent->_pLeft = pSubLR;
		if (pSubLR)
		{
			pSubLR->_pParent = pParent;
		}
		Node* pPParent = pParent->_pParent;
		pSubL->_pRight = pParent;
		pParent->_pParent = pSubL;
		pSubL->_pParent = pPParent;
		//判断pParent是根节点还是非根节点

		if (nullptr == pPParent)
		{
			_pRoot = pSubL;
			pParent->_bf = pSubL->_bf = 0;
			return;
		}
		if (pPParent->_pLeft == pParent)
		{
			pPParent->_pLeft = pSubL;
		}
		else
		{
			pPParent->_pRight = pSubL;
		}
		//先将平衡因子置为0
		pParent->_bf = pSubL->_bf = 0;
	}
	void _RotateLR(Node* pParent)
	{
		//旋转结束后,平衡因子可能会出错,所以先保持pSubRL的平衡因子
		Node* pSubL = pParent->_pLeft;
		Node* pSubLR = pSubL->_pRight;
		int bf = pSubLR->_bf;

		_RotateL(pParent->_pLeft);
		_RotateR(pParent);

		if (-1 == bf)
		{
			pParent->_bf = 1;
		}
		else if (1 == bf)
		{
			pSubL->_bf = -1;
		}
	}
	void _RotateRL(Node* pParent)
	{
		Node* pSubR = pParent->_pRight;
		Node* pSubRL = pSubR->_pLeft;
		int bf = pSubRL->_bf;

		_RotateR(pParent->_pRight);
		_RotateL(pParent);
		//调整特殊点的平衡因子
		if (-1 == bf)
		{
			pSubR->_bf = 1;
		}
		else if (1 == bf)
		{
			pParent->_bf = -1;
		}
	}
private:
	Node* _pRoot;
};#pragma once
#include<iostream>
using namespace std;

template<class T>
struct AVLTreeNode{
	AVLTreeNode(const T&data = T())
	:_pLeft(nullptr)
	, _pRight(nullptr)
	, _pParent(nullptr)
	, _bf(0)
	, _data(data)
	{}
	AVLTreeNode<T>* _pLeft;
	AVLTreeNode<T>* _pRight;
	AVLTreeNode<T>* _pParent;
	int _bf;
	T _data;
};


template<class T>
class AVLTree{
	typedef AVLTreeNode<T> Node;
public:
	AVLTree()
		:_pRoot(nullptr)
	{}
	//插入操作
	bool Insert(const T& data)
	{
		if (nullptr == _pRoot)
		{
			_pRoot = new AVLTreeNode<T>(data);
			return true;
		}
		//找到要插入的位置
		Node* pCur = _pRoot;
		Node* pParent = nullptr;
		while (pCur)
		{
			pParent = pCur;
			if (pCur->_data > data)
			{
				pCur = pCur->_pLeft;
			}
			else if (pCur->_data<data)
			{
				pCur = pCur->_pRight;
			}
			else
			{
				return false;
			}
		}
		//已经找到要插入的位置了
		pCur = new AVLTreeNode<T>(data);
		if (data < pParent->_data)
		{
			pParent->_pLeft = pCur;
		}
		else{
			pParent->_pRight = pCur;
		}
		pCur->_pParent = pParent;


		while (pParent)
		{
			//更新平衡因子
			if (pCur == pParent->_pLeft)
			{
				pParent->_bf--;
			}
			else{
				pParent->_bf++;
			}
			//pParent 的平衡因子改变
			//对pParent的平衡因子进行分析
			//当pParent 的平衡因子变为0,时,说明以前的平衡因子是1/-1,则增加一个节点
			//不会改变pParent上层的平衡因子 直接返回就行
			if (pParent->_bf == 0)
			{
				return true;
			}
			if (pParent->_bf == 1 || pParent->_bf == -1)
			{
				//pParent 的平衡因子是1或者-1时,说明以前的平衡因子是0,则增加一个节点
				//会影响其上层的平衡因子
				//向上返回找到不满足条件的节点
				pCur = pParent;
				pParent = pParent->_pParent;
			}
			else{
				if (pParent->_bf == 2)
				{
					//此时需要旋转!!!
					//同号单旋转
					if (pCur->_bf == 1)
					{
						//右子树高,左旋
						_RotateL(pParent);
					}
					else{
						//异号双旋
						_RotateRL(pParent);
					}
				}
				else if (pParent->_bf == -2)
				{
					if (pCur->_bf == 1)
					{
						//异号双旋
						_RotateLR(pParent);
					}
					else
					{
						_RotateR(pParent);
					}

				}
				break;
			}
		}
		return true;
	}
	//对外只需要提供接口就行
	void InOrdor()
	{
		_InOrdor(_pRoot);
		cout << endl;
	}
	bool IsValidAVLTree()
	{
		return _IsValidAVLTree(_pRoot);
	}
protected:
	void _InOrdor(Node* pRoot)
	{
		if (nullptr == pRoot)
		{
			return;
		}
		_InOrdor(pRoot->_pLeft);
		cout << pRoot->_data << " ";
		_InOrdor(pRoot->_pRight);
	}
	bool _IsValidAVLTree(Node* pRoot)
	{
		if (nullptr == pRoot)
		{
			return true;
		}
		size_t _leftHeight = _Height(pRoot->_pLeft);
		size_t _rightHeight = _Height(pRoot->_pRight);

		if (!(pRoot->_bf == (_rightHeight - _leftHeight) && (-1 == pRoot->_bf || 1 == pRoot->_bf)))
		{
			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 _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;

		//判断pParent 是根节点还是非根节点
		if (nullptr == pPParent)
		{
			_pRoot = pSubR;
			pParent->_bf = pSubR->_bf = 0;
			return;
		}
		if (pPParent->_pLeft == pParent)
		{
			pPParent->_pLeft = pSubR;
		}
		else{
			pPParent->_pRight = pSubR;
		}
		//先将平衡因子置0
		//pParent pSubR
		pParent->_bf = pSubR->_bf = 0;
	}
	void _RotateR(Node* pParent)
	{
		//左子树高
		Node* pSubL = pParent->_pLeft;
		Node* pSubLR = pSubL->_pRight;

		pParent->_pLeft = pSubLR;
		if (pSubLR)
		{
			pSubLR->_pParent = pParent;
		}
		Node* pPParent = pParent->_pParent;
		pSubL->_pRight = pParent;
		pParent->_pParent = pSubL;
		pSubL->_pParent = pPParent;
		//判断pParent是根节点还是非根节点

		if (nullptr == pPParent)
		{
			_pRoot = pSubL;
			pParent->_bf = pSubL->_bf = 0;
			return;
		}
		if (pPParent->_pLeft == pParent)
		{
			pPParent->_pLeft = pSubL;
		}
		else
		{
			pPParent->_pRight = pSubL;
		}
		//先将平衡因子置为0
		pParent->_bf = pSubL->_bf = 0;
	}
	void _RotateLR(Node* pParent)
	{
		//旋转结束后,平衡因子可能会出错,所以先保持pSubRL的平衡因子
		Node* pSubL = pParent->_pLeft;
		Node* pSubLR = pSubL->_pRight;
		int bf = pSubLR->_bf;

		_RotateL(pParent->_pLeft);
		_RotateR(pParent);

		if (-1 == bf)
		{
			pParent->_bf = 1;
		}
		else if (1 == bf)
		{
			pSubL->_bf = -1;
		}
	}
	void _RotateRL(Node* pParent)
	{
		Node* pSubR = pParent->_pRight;
		Node* pSubRL = pSubR->_pLeft;
		int bf = pSubRL->_bf;

		_RotateR(pParent->_pRight);
		_RotateL(pParent);
		//调整特殊点的平衡因子
		if (-1 == bf)
		{
			pSubR->_bf = 1;
		}
		else if (1 == bf)
		{
			pParent->_bf = -1;
		}
	}
private:
	Node* _pRoot;
};

int main()
{
	AVLTree<int> t;
	//{16, 3, 7, 11, 9, 26, 18, 14, 15}
	//t.Insert(16);
	//t.Insert(3);
	//t.Insert(7);
	//t.Insert(11);
	//t.Insert(9);
	//t.Insert(26);
	//t.Insert(18);
	//t.Insert(14);
	//t.Insert(15);
	//4, 2, 6, 1, 3, 5, 15, 7, 16, 14
	t.Insert(4);
	t.Insert(2);
	t.Insert(6);
	t.Insert(1);
	t.Insert(3);
	t.Insert(5);
	t.Insert(15);
	t.Insert(7);
	t.Insert(16);
	t.Insert(14);
	t.InOrdor();
	bool ret = t.IsValidAVLTree();
	cout << ret << endl;
	return 0;
}

AVL树的性能

AVL树是一棵绝对平衡的二叉搜索树,其要求每个节点的左右子树高度差的绝对值不超过1,这样可能保证查询时高效的时间复杂度,即log(N)。但是如果要对AVL树做一些结构修改的操作,性能非常低下,比如:**插入时要维护其绝对平衡,旋转的此时比较多,更差的是在删除时,有可能一直要让旋转持续到根的位置。**因此:如果需要一种查询高效且有序的数据结构,而且数据的个数为静态的(即不会改变),可以考虑AVL树,但一个结构经常修改就不太适合。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值