AVLTree

AVL树又称为高度平衡的二叉搜索树,是1962年有俄罗斯的数学家G.M.Adel'son-Vel'skii和E.M.Landis提出来的。它能保持二叉树的高度平衡,尽量降低二叉树的高度,减少树的平均搜索长度。

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

AVL树增删查的效率都是O(Lg(N))

代码实现:

#include <iostream>
using namespace std;

template <class K, class V>
struct AVLTreeNode      //AVLTree的节点
{
	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)   //构造
		:_left(NULL)
		, _right(NULL)
		, _parent(NULL)
		, _key(key)
		, _value(value)
		, _bf(0)   //新创节点的平衡因子为0(叶子节点)   bf取值:1/0/-1
	{}
};

template <class K, class V>
class AVLTree
{
	typedef AVLTreeNode<K, V> Node;

public:
	AVLTree()         //构造
		:_root(NULL)
	{}

	~AVLTree()     //析构
	{}

	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 = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return false;   //有相同的key
			}
		}

		cur = new Node(key, value);
		//找到了空位置,判断插入在parent的左边还是右边
		if (parent->_key < key)
		{
			parent->_right = cur;
			cur->_parent = parent;
		}
		else if (parent->_key > key)
		{
			parent->_left = cur;
			cur->_parent = parent;
		}

		//更新平衡因子
		while (parent)    //当前节点的parent不为空时
		{
			if (cur == parent->_left)
			{
				parent->_bf--;
			}
			else if (cur == parent->_right)
			{
				parent->_bf++;
			}

			//检查bf是否正确,是否需要调整
			if (parent->_bf == 0)        //插入完成,bf更新之后为0时,说明此时高度未发生变化,不需要调整
			{
				break;
			}
			else if (parent->_bf == 1 || parent->_bf == -1)  //此时说明高度发生变化,往上检查,看是否存在不正确的bf
			{
				cur = parent;
				parent = cur->_parent;
			}
			else    //bf为2/-2,需要旋转
			{
				if (parent->_bf == 2)    //右边高
				{
					Node* subR = parent->_right;
					
					if (subR->_bf == 1)
					{
						RotateL(parent);   //左单旋
					}
					else if (subR->_bf == -1)
					{
						RotateRL(parent);     //右左双旋
					}
				}

				else if (parent->_bf == -2)   //左边高
				{
					Node* subL = parent->_left;
					if (subL->_bf == -1)
					{
						RotateR(parent);   //右单旋
					}

					else if (subL->_bf == 1)
					{
						RotateLR(parent);     //左右双旋
					}
				}
				break;
			}
		}
		return true;
	}

	void RotateL(Node* parent)    //左单旋
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		parent->_right = subRL;
		if (subRL != NULL)
		{
			subRL->_parent = parent;
		}
		subR->_left = parent;
		Node* pparent = parent->_parent;
		parent->_parent = subR;
		if (pparent == NULL)    //说明最开始的parent为根节点
		{
			_root = subR;
			subR->_parent = NULL;
		}
		else   //pparent不为空
		{
			if (parent == pparent->_left)
			{
				pparent->_left = subR;
				subR->_parent = pparent;
			}
			else if (parent == pparent->_right)
			{
				pparent->_right = subR;
				subR->_parent = pparent;
			}
		}
		parent->_bf = subR->_bf = 0;
	}


	void RotateR(Node* parent)   //右单旋
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		parent->_left = subLR;
		if (subLR != NULL)
		{
			subLR->_parent = parent;
		}
		subL->_right = parent;
		Node* pparent = parent->_parent;
		parent->_parent = subL;

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

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


	void RotateLR(Node* parent)   //左右双旋
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		RotateL(subL);     //以subL为轴节点左旋
		RotateR(parent);    //以parent为轴节点右旋
		int bf = subLR->_bf;

		if (bf == 0)
		{
			subLR->_bf = parent->_bf = subL->_bf = 0;
		}

		else if (bf == 1)
		{
			subLR->_bf = 0;
			parent->_bf = 1;
			subL->_bf = 0;
		}

		else if (bf == -1)
		{
			subLR->_bf = 0;
			parent->_bf = 0;
			subL->_bf = -1;
		}
	}

	void RotateRL(Node* parent)   //右左双旋
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		RotateR(subR);     //以subR为轴节点右旋
		RotateL(parent);     //以parent为轴节点左旋

		int bf = subRL->_bf;    //subRL为调整后的parent节点

		if (bf == 0)
		{
			//parent为subRL的左节点,subR为subRL的右节点
			subRL->_bf = parent->_bf = subR->_bf = 0;   //此时为完全平衡
		}

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

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

	void Inorder()     //中序遍历
	{
		_Inorder(_root);
		cout << endl;
	}

	bool IsBalance()    //判断是否平衡
	{
		int height = 0;
		return _IsBalance(_root, height);
	}

	size_t _Height(Node* root)    //高度
	{
		if (root == NULL)
		{
			return 0;
		}
		int l = _Height(root->_left);
		int r = _Height(root->_right);

		return l > r ? l + 1 : r + 1;
	}

protected:
	void _Inorder(Node* root)     //中序
	{
		if (root == NULL)
		{
			return;
		}

		_Inorder(root->_left);
		cout << root->_key << " ";
		_Inorder(root->_right);
	}

	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 ? left + 1 : right + 1;

			if (root->_bf != right - left)
			{
				cout << "平衡因子异常:" << root->_key << endl;
				return false;
			}

			return true;
		}
		else
		{
			return false;
		}
	}


protected:
	Node* _root;
};



#include "AVLTree.h"
void TestAVLtree()
{
	AVLTree<int, int> p;
	int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };

	for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
	{
		p.Insert(a[i], i);    //第一个为key,第二个为value
	}
	p.Inorder();
	cout << "IsBalance?" << p.IsBalance() << endl;
}
int main()
{
	TestAVLtree();
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值