1. 程式人生 > >AVL樹實現與分析

AVL樹實現與分析

一、背景知識

AVL樹是高度平的二叉搜尋樹,它能降低二叉樹的高度,減少樹的平均搜尋長度.

二、AVL樹的性質

1、左子樹和右子樹的高度差的絕對值不超過1

2、樹中的每個左子樹和右子樹都是AVL樹

3、每個節點都有一個平衡因子(balance factor--bf),任一節點的平衡因子都為-1,0,1。(每個節點的平衡因子等於右子樹的高度減去左子樹的高度)

三、AVL的效率

  一棵AVL樹有N個節點,高度可以保持在log2n,插入/刪除/查詢的時間複雜度也是log2n.

(注:log2n表示log以2為底n的對數。)

四、AVL的實現(程式碼及分析)

  AVL樹(K/V)的結點

template<class K,class T>
struct AVLTreeNode
{
	K _key;     //搜尋的關鍵字
	T _value;   //數值
	int _bf;    //平衡因子
	AVLTreeNode<K, T>* _left;
	AVLTreeNode<K, T>* _right;
	AVLTreeNode<K, T>* _parent;
	AVLTreeNode(const K& key, const T& value)
		:_key(key)
		, _value(value)
		, _bf(0)
		, _left(NULL)
		, _right(NULL)
		, _parent(NULL)
	{}
};
AVL樹的旋轉

當增加一個元素,如果破壞了-1,0,1這樣的平衡因子的時候,就需要通過旋轉來修改平衡因子。


程式碼實現(左單旋轉)

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;
		if (ppNode == NULL)
		{
			_root = subR;
			subR->_parent = NULL;
		}
		else
		{
			if (ppNode->_left == parent)
				ppNode->_left = subR;
			else if (ppNode->_right == parent)
				ppNode->_right = subR;
			subR->_parent = ppNode;
		}
		
		subR->_bf = parent->_bf = 0;
	}

右單旋轉



右單旋轉(程式碼實現)

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->_parent = subL;

		if (ppNode == NULL)
		{
			_root = subL;
			subL->_parent = NULL;
		}
		else
		{
			if (ppNode->_left == parent)
			{
				ppNode->_left = subL;
			}
			else
			if (ppNode->_right == parent)
			{
				ppNode->_right = subL;
			}
			subL->_parent = ppNode;
		}

		parent->_bf = 0;
		subL->_bf = 0;
	}
左右雙旋

  


左右雙旋實現:

	void RotateLR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		int bf = subLR->_bf;

		RotateL(parent->_left);
		RotateR(parent);

		if (bf == 0)
		{
			subLR->_bf = parent->_bf = subL->_bf = 0;
		}
		else if (bf == 1)
		{
			parent->_bf = 0;
			subL->_bf = -1;
			subLR->_bf = 0;
		}
		else//bf == -1
		{
			subL->_bf = 0;
			subLR->_bf = 0;
			parent->_bf = 1;
		}
	}
右左雙旋轉


程式碼實現:

void RotateRL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		int bf = subRL->_bf;

		RotateR(parent->_right);//先右單旋轉
		RotateL(parent);//整體左單旋

		if (bf == 0)
		{
			parent->_bf = subRL->_bf = 0;
		}
		else if (bf == 1)
		{
			subR->_bf = 0;
			parent->_bf = -1;
			subRL->_bf = 0;
		}
		else // bf == -1
		{
			parent->_bf = 0;
			subR->_bf = 1;
			subRL->_bf = 0;
		}
	}

AVL樹的所有程式碼:

#include<string>

template<class K,class T>
struct AVLTreeNode
{
	K _key;
	T _value;
	int _bf;
	AVLTreeNode<K, T>* _left;
	AVLTreeNode<K, T>* _right;
	AVLTreeNode<K, T>* _parent;
	AVLTreeNode(const K& key, const T& value)
		:_key(key)
		, _value(value)
		, _bf(0)
		, _left(NULL)
		, _right(NULL)
		, _parent(NULL)
	{}
};

template<class K,class T>
class AVLTree
{
	typedef AVLTreeNode<K, T> Node;
protected:
	Node * _root;
public:
	AVLTree()
		:_root(NULL)
	{}
	~AVLTree()
	{}
	void Insert(const K& key, const T& value)//AVL樹的插入
	{
		if (_root == NULL)
		{
			_root = new Node(key, value);
			return;
		}
		Node* cur = _root;
		Node* parent = NULL;
		while (cur)
		{
			if (key < cur->_key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (key >cur->_key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
				break;
		}

		cur = new Node(key, value);
		if (parent->_key < key)
		{
			parent->_right = cur;
			cur->_parent = parent;
		}
		else if (parent->_key > key)
		{
			parent->_left = cur;
			cur->_parent = parent;
		}

		//平衡平衡因子
		while (parent)
		{
			if (cur == parent->_left)
				parent->_bf--;
			else if (cur == parent->_right)
				parent->_bf++;
			if (parent->_bf == 0)
				break;
			else if (parent->_bf == 1 || parent->_bf == -1)
			{
				cur = parent;
				parent = cur->_parent;
			}
			else  //
			{
				if (parent->_bf == -2)
				{
					Node* subL = parent->_left;
					if (subL->_bf == -1)
						RotateR(parent);
					else
						RotateLR(parent);
				}
				else if (parent->_bf == 2)
				{
					Node* subR = parent->_right;
					if (subR->_bf == 1)
						RotateL(parent);
					else
						RotateRL(parent);
				}
				break;
			}

		}
		return;
	}
	void InOrder()  //中序遍歷
	{
		InOrder(_root);
	}
	bool IsBalance()  //判斷是否平衡
	{
		int height = 0;
		return IsBalance(_root, height);
	}
	size_t Height()  //求樹的高度
	{
		Height(_root);
	}
protected:
	bool IsBalance(Node* root, int& height)
	{
		if (root == NULL)
		{
			height = 0;
			return true;
		}
		int left, right ;

		if (IsBalance(root->_left, left) && IsBalance(root->_right, right)
			&& abs(right - left) < 2)
		{
			height = left < right ? right + 1 : left + 1;
			
			if (root->_bf != right - left)
			{
				cout << "平衡因子異常" << root->_key << endl;
				return false;
			}

			return true;
		}
		else
			return false;

	}
	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->_parent = subL;

		if (ppNode == NULL)
		{
			_root = subL;
			subL->_parent = NULL;
		}
		else
		{
			if (ppNode->_left == parent)
			{
				ppNode->_left = subL;
			}
			else
			if (ppNode->_right == parent)
			{
				ppNode->_right = subL;
			}
			subL->_parent = ppNode;
		}

		parent->_bf = 0;
		subL->_bf = 0;
	}

	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;
		if (ppNode == NULL)
		{
			_root = subR;
			subR->_parent = NULL;
		}
		else
		{
			if (ppNode->_left == parent)
				ppNode->_left = subR;
			else if (ppNode->_right == parent)
				ppNode->_right = subR;
			subR->_parent = ppNode;
		}
		
		subR->_bf = parent->_bf = 0;
	}
	
	void RotateRL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		int bf = subRL->_bf;

		RotateR(parent->_right);
		RotateL(parent);

		if (bf == 0)
		{
			parent->_bf = subRL->_bf = 0;
		}
		else if (bf == 1)
		{
			subR->_bf = 0;
			parent->_bf = -1;
			subRL->_bf = 0;
		}
		else // bf == -1
		{
			parent->_bf = 0;
			subR->_bf = 1;
			subRL->_bf = 0;
		}
	}
	void RotateLR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		int bf = subLR->_bf;

		RotateL(parent->_left);
		RotateR(parent);

		if (bf == 0)
		{
			subLR->_bf = parent->_bf = subL->_bf = 0;
		}
		else if (bf == 1)
		{
			parent->_bf = 0;
			subL->_bf = -1;
			subLR->_bf = 0;
		}
		else//bf == -1
		{
			subL->_bf = 0;
			subLR->_bf = 0;
			parent->_bf = 1;
		}
	}
	void InOrder(Node* root)
	{
		if (root == NULL)
			return;
		InOrder(root->_left);
		cout << root->_value << " ";
		InOrder(root->_right);
	}
	size_t Height(Node* root)
	{
		if (root == NULL)
			return 0;

		int left = Height(root->_left);
		int right = Height(root->_right);
		return left < right ? right + 1 : left + 1;
	}
};

AVL樹雖然實用性和效率並不是十分高,但是瞭解其實現過程,對紅黑樹、以及相關效能更高的樹有著鋪墊作用。