AVLTree——平衡搜索树

AVLTree——平衡搜索树

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

插入时需要满足的一些规则
  1. 新增结点cur在parent的左边,parent的bf--
  2. 新增结点cur在parent的右边,parent的bf++
  3. 若parent的bf == 0,说明parent所在这棵子树的高度不变,不需要向上更新。
    若parent的 |bf| == 1, 说明parent所在这棵子树的高度变了,需要向上更新。
    若parent的 |bf| == 2,不再更新,旋转使其平衡。
然后我们先解释如何旋转





在插入时 我们最主要的是要 调节平衡因子
左旋和右旋时按照上面的规则没有什么问题,但双旋的时候,我们还要考虑调节到节点时它的左边或者右边有没有结点,下面我们用图来解释。






接下来是判断 是否平衡
首先有一个概念  递归的时间复杂度 = 递归总次数 * 每次递归运算的次数

我们最先想到的可能是求出左右树的高度,再满足左右子树高度之差的绝对值不超过1,这样其时间复杂度是N^2
我们要将其时间复杂度优化到N,思路是:将高度当成参数传进去,从最后一层的结点开始比较,这样就会将时间复杂度降为N,减少了每次递归的次数。

下面是完整代码
template<class K,class V>
struct AVLTreeNode
{
	AVLTreeNode<K, V>* _parent;
	AVLTreeNode<K, V>* _left;
	AVLTreeNode<K, V>* _right;
	K _key;
	V _value;
	int _bf;//平衡因子 只能是-1,0,1

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

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* parent = NULL;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return false;
			}
		}

		cur = new Node(key, value);

		if (parent->_key > key)
		{
			parent->_left = cur;
			cur->_parent = parent;
		}
		else
		{
			parent->_right = cur;
			cur->_parent = 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 //parent->_bf == 2 || parent->_bf == -2
			{
				//不再更新,旋转
				if (parent->_bf == 2)
				{
					if (cur->_bf == 1)//左单旋
					{
						RotateL(parent);
					}
					else              //右左双旋
					{
						RotateRL(parent);
					}
				}
				if (parent->_bf == -2)
				{
					if (cur->_bf == -1)   //右单旋
					{
						RotateR(parent);
					}
					else                   //左右双旋
					{
						RotateLR(parent);
					}
				}
				break;
			}
		}
		return true;
	}


	bool IsBanlance()
	{
		int height = 0;
		return _IsBanlanceOP(_root,height);

	}

	bool _IsBanlanceOP(Node* root, int& height)
	{
		if (root == NULL)
		{
			height = 0;
			return true;
		}

		int LeftHeight = 0;
		int RightHeight = 0;

		if (_IsBanlanceOP(root->_left, LeftHeight) == false)
		{
			return false;
		}
		if (_IsBanlanceOP(root->_right, RightHeight) == false)
		{
			return false;
		}
		height = LeftHeight > RightHeight ? LeftHeight + 1 : RightHeight + 1;

		return abs(RightHeight - LeftHeight) < 2;
	}

	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		//调整指向
		parent->_right = subRL;
		subR->_left = parent;
		if (subRL) //subRL非空
		{
			subRL->_parent = parent;
		}
	
		Node* ppNode = parent->_parent;

		//判断parent是否有父节点	
		if (_root == parent)
		{
			_root = subR;
			subR->_parent = NULL;
		}
		else
		{
			if (ppNode->_left == parent)
			{
				ppNode->_left = subR;
				subR->_parent = ppNode;
			}
			else
			{
				ppNode->_right = subR;
				subR->_parent = ppNode;
			}
		}
		parent->_parent = subR;

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

	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		//调整指向
		parent->_left = subLR;
		subL->_right = parent;

		if (subLR)
		{
			subLR->_parent = parent;
		}

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

		parent->_bf = 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)
		{
			parent->_bf = subL->_bf = subLR->_bf = 0;
		}
		if (bf == 1)
		{
			subLR->_bf = 0;
			parent->_bf = -1;
			subL->_bf = 0;
		}
		else//bf == -1
		{
			subLR->_bf = 0;
			parent->_bf = 0;
			subL->_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 = subR->_bf = subRL->_bf = 0;
		}
		else if (bf == 1)
		{
			parent->_bf = 0;
			subRL->_bf = 0;
			subR->_bf = -1;
		}
		else
		{
			subR->_bf = 0;
			subRL->_bf = 0;
			parent->_bf = 1;
		}
	}

	void InOrder()
	{
		 _InOrder(_root);
		 cout << endl;
	}

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

//测试
void TestAVLTree()
{
	AVLTree<int,int> t;
	//int a[] = { 5, 3, 4, 1, 7, 8, 2, 6, 0, 9 };
	int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
	for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
	{
		t.Insert(a[i], i);
		cout << a[i]<< "平衡?" << t.IsBanlance() <<endl;
	}
	t.InOrder();
	cout << t.IsBanlance() << endl;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值