AVL樹

當二叉搜索樹插入接近有序的節點時,二叉樹會發生退化現象(看下圖),搜索效率就會大大降低,這樣期望的搜索效率就不會達到,然後就有人提出了高度平衡二叉搜索樹(AVL樹)。

                 

AVL樹:

  • AVL樹又稱爲高度平衡的二叉搜索樹,是1962年有俄羅斯的數學家G.M.Adel'son-Vel'skii和E.M.Landis提出來的。它能保持二叉樹的高度平衡,儘量降低二叉樹的高度,減少樹的平均搜索長度。

AVL樹性質:

1. 左子樹和右子樹的高度之差的絕對值不超過1
2. 樹中的每個左子樹和右子樹都是AVL樹
3. 每個節點都有一個平衡因子(balance factor--bf),任一節點的平衡因子是-1,0,1。

注:每個節點的平衡因子等於右子樹的高度減去左子樹的高度 

AVL樹效率:

一棵AVL樹的節點爲N,性質爲前提,這樣他的高度就可以近似爲log2N,這樣就可以達到期望效率。

現在看具體代碼實現:

AVL樹節點:


代碼實現:

#include <iostream>
#include <queue>

using namespace std;

template <class K, class V>
//三叉鍊形式
struct AVLTreeNode
{
	typedef AVLTreeNode<K, V> Node;

	K _key;
	V _value;
	int _bf;		//平衡因子
	Node* _left;	//左孩子
	Node* _right;	//右孩子
	Node* _parent;	//父親

	AVLTreeNode(const K& key, const V& value)
		:_key(key)
		,_value(value)
		,_bf(0)
		,_left(NULL)
		,_right(NULL)
		,_parent(NULL)
	{}
};

template <class K, class V>
class AVLTree
{
	typedef AVLTreeNode<K, V> Node;
public:
	AVLTree()
		:_root(NULL)
	{}
public:
	bool Insert(const K& key, const V& value)
	{
		if(_root == NULL)
		{
			_root = new Node(key, value);
			return true;
		}
		Node* cur = _root;
		Node* prev = NULL;
		while(cur)
		{
			if(key > cur->_key) //key大,走右子樹
			{
				prev = cur;
				cur = cur->_right;
			}
			else if(key < cur->_key) //key小,走左子樹
			{
				prev = cur;
				cur = cur->_left;
			}
			else
			{
				return false;	//有存在的元素,結束,插入失敗
			}
		}
		//插入新節點
		//cur就是要插入的位置
		cur = new Node(key, value);
		cur->_parent = prev;
	
		//if(cur->_key < key)
		if(prev->_key >key)
		{
			prev->_left = cur;

		}
		else
		{
			prev->_right = cur;
		}

		//更新平衡因子
		Node* parent = cur->_parent;
		while(parent)
		{
			//更新平衡因子的值
			if(parent->_left == cur)
			{
				parent->_bf--;	//往左子樹走
			}
			else
			{
				parent->_bf++;	//往右子樹走
			}
			
			if(parent->_bf == 0)
			{
				break;
			}
			else if(parent->_bf == -1 || parent->_bf == 1)
			{
				cur = parent;
				parent = parent->_parent;
			}
			else
			{
				//調整,使其再次成爲AVL樹
				if(parent->_bf == 2)
				{
					if(cur->_bf == 1)
					{
						_RotateL(parent); //左單旋,bf(2,1)
					}
					else
					{
						_RotateRL(parent);//右左雙旋,bf(2,-1)
					}
				}
				else
				{
					if(cur->_bf == -1)
					{
						_RotateR(parent);//右單旋,bf(-2,-1)
					}
					else
					{
						_RotateLR(parent);//左右雙旋,bf(-2,1)
					}
				}
				break;
			}
		}

	}
	int Height()
	{
		return _Height(_root);
	}
	bool IsBalance()
	{
		return _IsBalance(_root);	
	}
	void LevelOrder()
	{
		_LevelOrder(_root);
		cout<<endl;
	}
	void InOrder()
	{
		_InOrder(_root);
		cout<<endl;
	}
	
protected:
	int _Height(Node* root)
	{
		if(root == NULL)
		{
			return 0;
		}
		int left = _Height(root->_left);
		int right = _Height(root->_right);
		return left > right ? left+1 : right+1;
	}
	bool _IsBalance(Node* root)
	{
		if(root == NULL)
		{
			return true;
		}
		if(abs(_Height(root->_right) - _Height(root->_left)) > 1 || abs(root->_bf) > 1)
		{
			cout<<"false: "<<root->_key<<endl;
			return false;
		}
		return _IsBalance(root->_left) && _IsBalance(root->_right);
	}
	void _LevelOrder(Node* root)
	{
		if(root == NULL)
		{
			return;
		}
		queue<Node*> q;
		q.push(root);
		while(!q.empty())
		{
			if(q.front()->_left != NULL)
			{
				q.push(q.front()->_left);
			}
			if(q.front()->_right != NULL)
			{
				q.push(q.front()->_right);
			}
			cout<<q.front()->_key<<" ";
			q.pop();
		}
	}
	void _InOrder(Node* root)
	{
		if(root == NULL)
		{
			return;
		}
		_InOrder(root->_left);
		cout<<root->_key<<" ";
		_InOrder(root->_right);
	}
	void _RotateL(Node* &parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if(subRL)
		{
			subRL->_parent = parent;
		}
		subR->_left = parent;
		Node* ppNode = parent->_parent;
		parent->_parent = subR;

		subR->_parent = ppNode;
		if(ppNode == NULL)
		{
			_root = subR;
		}
		else
		{
			if(ppNode->_left == parent)
			{
				ppNode->_left = subR;
			}
			else
			{
				ppNode->_right = subR;
			}
		}
		parent->_bf = subR->_bf = 0;
		
		//parent=subR;//
	}
	void _RotateR(Node*& parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		
		parent->_left = subLR;
		if(subLR)
		{
			subLR->_parent = parent;
		}
		subL->_right = parent;
		Node* ppNode = parent->_parent;
		parent->_parent = subL;

		subL->_parent = ppNode;
		if(ppNode == NULL)
		{
			_root = subL;
		}
		else
		{
			if(ppNode->_left == parent)
			{
				ppNode->_left = subL;
			}
			else
			{
				ppNode->_right = subL;
			}
		}
		//更新平衡因子
		parent->_bf = subL->_bf = 0;
		//parent=subL;
	}
	void _RotateRL(Node* &parent)
	{
		Node* sub = parent;
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		int bf = subRL->_bf;

		_RotateR(parent->_right);
		_RotateL(parent);

		if(bf == 1)
		{
			sub->_bf = -1;
		}
		else
		{
			subR->_bf = 1; 
		}
	}
	void _RotateLR(Node* &parent)
	{
		Node* sub = parent;
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		int bf = subLR->_bf;	

		_RotateL(parent->_left);
		_RotateR(parent);

		if(bf == -1)
		{
			sub->_bf = 1;
		}
		else
		{
			subL->_bf = -1;
		}
	}
private:
	Node* _root;
};


void Test()
{
	int arr[] = {4, 2, 6, 1, 3, 5, 15, 7, 16, 14};
	AVLTree<int, int> at;
	for(int i = 0; i<sizeof(arr)/sizeof(arr[0]); ++i)
	{
		at.Insert(arr[i], i);
	}
	at.LevelOrder();
	at.InOrder();
	cout<<"IsBalance? "<<at.IsBalance()<<endl;
	cout<<at.Height()<<endl;
}

int main()
{
	Test();
	system("pause");
	return 0;
}


測試結果:


以上就是我對AVL樹的理解,能力有限,若有錯誤,請指出,不甚感激!!吐舌頭吐舌頭吐舌頭




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