AVLTree简单实现

AVL树的性质

1. 左子树和右子树的高度之差的绝对值不超过1
2. 树中的每个左子树和右子树都是AVL树
3. 每个节点都有一个平衡因子(balance factor--bf),任一节点的平衡因子是-1,0,1。(每个节点的平衡因子等于右子树的高度减去左子
树的高度 )

AVL树的效率

一棵AVL树有N个节点,其高度可以保持在log2N,插入/删除/查找的时间复杂度也是log2N。

(ps:log2N是表示log以2为底N的对数。)

调整情况有四种:

1:左单旋



2:右单旋:


3:左右双旋:


4:右左双旋:


#include<iostream>
#include<math.h>
using namespace std;

template <class K, class V>
struct AVLTreeNode
{
	AVLTreeNode<K, V>* _left;
	AVLTreeNode<K, V>* _right;
	AVLTreeNode<K, V>* _parent;

	K _key;
	V _value;
	int _bf;   //平衡因子
	AVLTreeNode(const K& key, const V& value)
		:_bf(0), _left(NULL), _right(NULL), _parent(NULL), _key(key), _value(value)
	{}
};
template<class K, class V>
class AVLTree
{
	typedef AVLTreeNode<K, V> Node;
public:
	AVLTree()
		:_root(NULL)
	{}
	bool Insert(const K& key, const V& value)
	{
		if (_root == NULL)
		{
			_root = new Node(key, value);
			return true;
		}
		Node* cur = _root;
		Node* parent = NULL;
		while (cur)
		{
			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
				return false;
		}
		Node* tmp;
		if (parent->_key>key)
		{
			tmp = new Node(key, value);
			parent->_left = tmp;
			tmp->_parent = parent;
		}
		else
		{
			tmp = new Node(key, value);
			parent->_right = tmp;
			tmp->_parent = parent;
		}

		//更新_bf
		bool isRotate = false;
		cur = tmp;
		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 = cur->_parent;
			}
			else   //用旋转来调整平衡因子
			{
				if (parent->_bf == 2)
				{
					if (cur->_bf == 1)
						RotateL(parent);
					else
						RotateRL(parent);
				}
				else
				{
					if (cur->_bf == -1)
						RotateR(parent);
					else
						RotateLR(parent);
				}
				isRotate = true;
				break;
			}
		}
		if (isRotate)
		{
			Node* ppNode = parent->_parent;
			if (ppNode == NULL)
				_root = parent;
			else if (ppNode->_key > parent->_key)
				ppNode->_left = parent;
			else
				ppNode->_right = parent;
		}
		return true;
	}
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
	bool IsBlance()
	{
		return _IsBlance(_root);
	}
protected:

	void _InOrder(Node* root)
	{
		if (root == NULL)
			return;
		_InOrder(root->_left);
		cout << root->_key << " ";
		_InOrder(root->_right);
	}

	int _Height(Node* root)
	{
		if (root == NULL)
			return NULL;

		int left = _Height(root->_left);
		int right = _Height(root->_right);

		return left > right ? left + 1 : right + 1;
	}
	bool _IsBlance(Node* root)
	{
		if (root == NULL)
			return true;
		int bf = _Height(root->_right) - _Height(root->_left);
		if (root->_bf != bf)
			cout << "平衡因子异常" << root->_key << endl;
		return root->_bf == bf&&abs(bf) < 2 && _IsBlance(root->_left) && _IsBlance(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;
		subR->_parent = parent->_parent;
		parent->_parent = 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;
		subL->_parent = parent->_parent;
		parent->_parent = subL;

		parent->_bf = subL->_bf = 0;
		parent = subL;
	}
	void RotateRL(Node* &parent)
	{
		Node* pNode = parent;
		Node* subRNode = parent->_right;
		Node* subRLNode = subRNode->_left;
		int bf = subRLNode->_bf;

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

		if (bf == 1)
		{
			subRNode->_bf = 0;
			pNode->_bf = -1;
		}
		else if (bf == -1)
		{
			subRNode->_bf = 1;
			pNode->_bf = 0;
		}
		else
		{
			subRNode->_bf = 0;
			pNode->_bf = 0;
		}

		subRLNode->_bf = 0;
	}
	void RotateLR(Node* &parent)
	{
		Node* pNode = parent;
		Node* subLNode = parent->_left;
		Node* subLRNode = subLNode->_right;
		int bf = subLRNode->_bf;

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

		if (bf == -1)
		{
			subLNode->_bf = 0;
			pNode->_bf = 1;
		}
		else if (bf == 1)
		{
			subLNode->_bf = -1;
			pNode->_bf = 0;
		}
		else
		{
			subLNode->_bf = 0;
			pNode->_bf = 0;
		}

		subLRNode->_bf = 0;
	}
protected:
	Node* _root;
};
void test()
{
	AVLTree<int, int> t;
	int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
	for (size_t i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
	{
		t.Insert(a[i], i);
	}

	t.InOrder();

	cout << "t是否平衡?" << t.IsBlance() << endl;

}
int main()
{
	test();
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值