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樹的實現

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;
		}
		//非空。
		//按照二叉搜索樹的性質,找到要插入的位置
		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 Node(data);
		if (pParent->_data>data)
		{
			pParent->_pLeft = pCur;
		}
		else
		{
			pParent->_pRight = pCur;
		}
		pCur->_pParent = pParent;

		//現在必須檢查跟新parent平衡因子
		//新節點肯定插入在雙親的左側或者右側,parent的平衡因子肯定改變
		while (pParent)
		{
			//跟新平衡因子
			if (pCur == pParent->_pLeft)
			{
				pParent->_bf--;
			}
			else
			{
				pParent->_bf++;
			}
			//可能會導致雙親節點的平衡因子不滿足AVL樹的性質
			if (0 == pParent->_bf)
			{
				//雙親節點如果變成0的話,
				//肯定不會影響上層的平衡因子
				//所以就是AVL樹了,返回true
				return true;
			}
			//如果雙親節點變成-1,或者1 說明
			//雙親節點曾經是0,現在高度增加
			//一定會影響上層的平衡因子
			else if (-1 == pParent->_bf || 1 == pParent->_bf)
			{
				//移動pCur, pParent
				//循環檢查是否要更新平衡因子
				pCur = pParent;
				pParent = pCur->_pParent;
			}
			else{
				//雙親的平衡因子不滿足AVL樹的性質
				//雙親的節點的平衡因子爲  2或者-2
				//需要對以雙親爲根的,以雙親爲根!!的二叉樹進行旋轉處理
				if (2 == pParent->_bf)
				{
					//平衡因子是2 ,說明右子樹高
					if (1 == pCur->_bf)
					{
						//同號單旋轉
						_RotateL(pParent);
					}
					else
					{
						//異號雙旋
						_RotateRL(pParent);
					}
				}
				else
				{
					//左子樹高
					//pParent 是2/-2的話,pCur 肯定是1/-1
					if (-1 == pCur->_bf)
					{
						//同號單旋
						_RotateR(pParent);
					}
					else
					{
						_RotateLR(pParent);
					}
				}
				//旋轉完了,肯定就平衡了,所以就退出了!!
				break;
			}
		}
		return true;
	}
	//對外只提供接口就行,也不要加參數
	void InOrder()
	{
		_InOrder(_pRoot);
	}
	bool IsValidAVLTree()
	{
		return _IsValidAVLTree();
	}
protected:
	bool _IsValidAVLTree(Node* pRoot)
	{
		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 = (pRoot->_pLeft);
		size_t rightHeight = (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 _RotateR(Node* pParent)
	{
		//右旋,左子樹高!!
		Node* pSubL = pParent->_pLeft;
		//右旋肯定 pSubL的左孩子不懂,右孩子改變
		Node* pSubLR = pParent->_pRight;
		//更新孩子指針域
		pParent->_pLeft = pSubLR;
		//更新雙親指針域
		if (pSubLR)
		{
			pSubLR->_pParent = pParent;
		}
		Node* pPParent = pParent->_pParent;
		pSubL->_pRight = 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;//雙親和較高的孩子平衡因子先置0!!
	}
	//左旋
	void _RotateL(Node* pParent)
	{
		//左旋,右子樹高
		Node* pSubR = pParent->_pRight;
		//pSubR 的右子樹不會改變,只需要改變其左子樹
		Node* pSubRL = pSubR->_pLeft;

		pParent->_pLeft = pSubRL;
		if (pSubRL)
		{
			pSubRL->_pParent = pParent;
		}

		Node* pPParent = pParent->_pParent;
		pSubR->_pLeft = pParent;
		pParent->_pParent = pSubR;
		pSubR->_pParent = pPParent;

		//判斷pParent 是根節點還是非根節點
		if (nullptr == pPParent)
		{
			_pRoot = pSubR;
		}
		else
		{
			if (pParent == pPParent->_pLeft)
			{
				pPParent->_pLeft = pSubR;
			}
			else
			{
				pPParent->_pRight = pSubR;
			}
		}
		//先將平衡因子置爲0
		//雙親和較高的孩子的平衡因子置0
		pParent->_bf = pSubR->_bf = 0;
	}
	//異號
	//旋轉結束,最後平衡因子可能會出錯,所以先保存pSubR的平衡因子
	void _RotateRL(Node* pParent)
	{
		Node* pSubR = pParent->_pRight;
		Node* pSubRL = pSubR->_pLeft;
		int bf = pSubRL->_bf;

		_RotateR(pParent->_pRight);
		_RotateR(pParent);

		//調整特殊點的平衡因子
		if (-1 == bf)
		{
			pSubR->_bf = 1;
		}
		else if (1 == bf)
		{
			pParent->_data = -1;
		}

	}
	void _RotateLR(Node* pParent)
	{
		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;
		}

	}


private:
	Node* _pRoot;
};

AVL樹的性能

AVL樹是一棵絕對平衡的二叉搜索樹,其要求每個節點的左右子樹高度差的絕對值不超過1,這樣可能保證查詢時高效的時間複雜度,即log(N)。但是如果要對AVL樹做一些結構修改的操作,性能非常低下,比如:**插入時要維護其絕對平衡,旋轉的此時比較多,更差的是在刪除時,有可能一直要讓旋轉持續到根的位置。**因此:如果需要一種查詢高效且有序的數據結構,而且數據的個數爲靜態的(即不會改變),可以考慮AVL樹,但一個結構經常修改就不太適合。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章