AVL树实现

一.AVL树的概念

二叉搜索树虽可以缩短查找的效率,但如果数据有序或接近有序二叉搜索树将退化为单支树,查 找元素相当于在顺序表中搜索元素,效率低下。因此,两位俄罗斯的数学家G.M.Adelson-Velskii 和E.M.Landis在1962年发明了一种解决上述问题的方法:当向二叉搜索树中插入新结点后,如果能保证每个结点的左右 子树高度之差的绝对值不超过1(需要对树中的结点进行调整),即可降低树的高度,从而减少平均搜索长度。

一棵AVL树或者是空树,或者是具有以下性质的二叉搜索树:

它的左右子树都是AVL树。

左右子树高度之差(简称平衡因子)的绝对值不超过1(-1/0/1)。

如果一棵二叉搜索树是高度平衡的,它就是AVL树。如果它有n个结点,其高度可保持在 $O(log_2 n)$,搜索时间复杂度O($log_2 n$)。

二.AVL树的实现

1.AVL树节点的定义

2.AVL树的插入

AVL树就是在二叉搜索树的基础上引入了平衡因子,因此AVL树也可以看成是二叉搜索树。那么 AVL树的插入过程可以分为两步:

1. 按照二叉搜索树的方式插入新节点:新增节点在左子树,父亲的平衡因子--;在右子树,父亲的                                                                平衡因子++。更新后,父亲的平衡因子为0,不用继续上                                                                  更新;父亲的平衡因子为-1或1,继续往上更新;父亲的平                                                                衡因子为-2或2,违反规则,进行调整。

2. 调整节点的平衡因子

bool Insert(const T& data)
	{
		if (_pRoot == nullptr)
		{
			Node* newroot = new Node(data);
			_pRoot = newroot;
			return true;
		}
		Node* cur = _pRoot;
		Node* parent = nullptr;
		while (cur)
		{
			if (cur->_data > data)
			{
				parent = cur;
				cur = cur->_pLeft;
			}
			else if (cur->_data < data)
			{
				parent = cur;
				cur = cur->_pRight;
			}
			else
			{
				return false;
			}
		}
		cur= new Node(data);
		if (parent->_data > data)
		{
			parent->_pLeft = cur;
			cur->_pParent = parent;
		}
		else if (parent->_data < data)
		{
			parent->_pRight = cur;
			cur->_pParent = parent;
		}
		while (parent)
		{
			//修改平衡因子
			if (cur == parent->_pLeft)
			{
				parent->_bf--;
			}

			else
			{
				parent->_bf++;
			}

			//判断要不要往上更新
			if (parent->_bf == 0)
			{
				break;
			}

			else if (parent->_bf == 1 || parent->_bf == -1)
			{
				cur = parent;
				parent = parent->_pParent;
			}

			else if (parent->_bf == 2 || parent->_bf == -2)
			{
				//调整
				if (parent->_bf == 2 && cur->_bf == 1)//在较高右子树的右侧,左单旋
				{
					RotateL(parent);
				}
				else if (parent->_bf == -2 && cur->_bf == -1)//在较高左子树的左侧,右单旋
				{
					RotateR(parent);
				}
				else if (parent->_bf == 2 && cur->_bf == -1)//在较高右子树的左侧,右左双旋
				{
					RotateRL(parent);
				}
				else if (parent->_bf == -2 && cur->_bf == 1)//在较高左子树的右侧,左右双旋
				{
					RotateLR(parent);
				}
				break;
			}

			else
			{
				assert(false);
			}
		}

		return true;
	}

3.AVL树的旋转

左单旋:新节点插入较高右子树的右侧。

void RotateL(Node* pParent)
	{
		Node* subR = pParent->_pRight;
		Node* subRL = subR->_pLeft;
		Node* grandparent = pParent->_pParent;
		pParent->_pRight = subRL;
		if (subRL)
		{
			subRL->_pParent = pParent;
		}
		subR->_pLeft = pParent;
		pParent->_pParent = subR;
		if (_pRoot == pParent)
		{
			_pRoot = subR;
			subR->_pParent = nullptr;
		}
		else
		{
			if (pParent == grandparent->_pLeft)
			{
				grandparent->_pLeft = subR;
			}
			else
			{
				grandparent->_pRight = subR;
			}
			subR->_pParent = grandparent;
		}
		pParent->_bf = subR->_bf = 0;
	}

右单旋:新节点插入较高左子树的左侧。

void RotateR(Node* pParent)
	{
		Node* subL = pParent->_pLeft;
		Node* subLR = subL->_pRight;
		Node* grandparent = pParent->_pParent;
		pParent->_pLeft = subLR;
		if (subLR)
		{
			subLR->_pParent = pParent;
		}
		subL->_pRight = pParent;
		pParent->_pParent = subL;

		if (_pRoot == pParent)
		{
			_pRoot = subL;
			subL->_pParent = nullptr;
		}
		else
		{
			if (pParent == grandparent->_pLeft)
			{
				grandparent->_pLeft = subL;
			}
			else
			{
				grandparent->_pRight = subL;
			}
			subL->_pParent = grandparent;
		}
		pParent->_bf = subL->_bf = 0;
	}

左右双旋:新节点插入较高左子树的右侧。

void RotateLR(Node* pParent)
	{
		Node* subL = pParent->_pLeft;
		Node* subLR = subL->_pRight;
		int bf = subLR->_bf;
		RotateL(subL);
		RotateR(pParent);
		if (bf == 0)
		{
			//subLR自己就是新增
			pParent->_bf = subLR->_bf = subL->_bf = 0;
		}
		else if (bf == 1)
		{
			//subLR的右子树新增
			pParent->_bf = 0;
			subLR->_bf = 0;
			subL->_bf = -1;
		}
		else if (bf == -1)
		{
			//subLR的左子树新增
			pParent->_bf = 1;
			subLR->_bf = 0;
			subL->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}

右左双旋:. 新节点插入较高右子树的左侧。

void RotateRL(Node* pParent)
	{
		Node* subR = pParent->_pRight;
		Node* subRL = subR->_pLeft;
		int bf = subRL->_bf;
		RotateR(pParent->_pRight);
		RotateL(pParent);
		if (bf == 0)
		{
			//subRL自己就是新增
			pParent->_bf = subRL->_bf = subR->_bf = 0;
		}
		else if (bf == 1)
		{
			//subRL的右子树新增
			pParent->_bf = -1;
			subRL->_bf = 0;
			subR->_bf = 0;
		}
		else if (bf == -1)
		{
			//subRL的左子树新增
			pParent->_bf = 0;
			subRL->_bf = 0;
			subR->_bf = 1;
		}
		else
		{
			assert(false);
		}
	}

4.AVL树的验证

AVL树是在二叉搜索树的基础上加入了平衡性的限制,因此要验证AVL树,可以分两步:

1. 验证其为二叉搜索树 如果中序遍历可得到一个有序的序列,就说明为二叉搜索树

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

	void _InOrder(Node* root)
	{
		if (root == nullptr)
			return;
		
		_InOrder(root->_pLeft);
		cout << root->_data << "->";
		_InOrder(root->_pRight);
	}

2. 验证其为平衡树 :每个节点子树高度差的绝对值不超过1(注意节点中如果没有平衡因子)

                                 节点的平衡因子是否计算正确

bool IsAVLTree()
	{
		return _IsAVLTree(_pRoot);
	}
size_t _Height(Node* pRoot)
	{
		if (pRoot == nullptr) return 0;
		size_t left = _Height(pRoot->_pLeft);
		size_t right = _Height(pRoot->_pRight);
		return left > right ? left + 1 : right + 1;
	}
	// 根据AVL树的概念验证pRoot是否为有效的AVL树
	bool _IsAVLTree(Node* pRoot)
	{
		if (pRoot == nullptr) return true;

		size_t heightleft = _Height(pRoot->_pLeft);
		size_t heightright = _Height(pRoot->_pRight);

		int diff = heightright - heightleft;
		if ((abs(diff) > 1))
		{
			cout << pRoot->_data << "节点平衡因子异常" << endl;
			return false;
		}
		if (diff != pRoot->_bf)
		{
			cout << pRoot->_data << "节点平衡因子与root的平衡因子不等,不符合实际" << endl;
			return false;
		}

		return _IsAVLTree(pRoot->_pLeft) && _IsAVLTree(pRoot->_pRight);
	}

完整代码如下:

#pragma once
#include<iostream>
#include<assert.h>
#include<vector>
using namespace std;

template<class T>
struct AVLTreeNode
{
	AVLTreeNode(const T& data = T())
		: _pLeft(nullptr)
		, _pRight(nullptr)
		, _pParent(nullptr)
		, _data(data)
		, _bf(0)
	{}

	AVLTreeNode<T>* _pLeft;
	AVLTreeNode<T>* _pRight;
	AVLTreeNode<T>* _pParent;
	T _data;
	int _bf;   // 节点的平衡因子
};


// AVL: 二叉搜索树 + 平衡因子的限制
template<class T>
class AVLTree
{
	typedef AVLTreeNode<T> Node;
public:
	AVLTree()
		: _pRoot(nullptr)
	{}

	// 在AVL树中插入值为data的节点
	bool Insert(const T& data)
	{
		if (_pRoot == nullptr)
		{
			Node* newroot = new Node(data);
			_pRoot = newroot;
			return true;
		}
		Node* cur = _pRoot;
		Node* parent = nullptr;
		while (cur)
		{
			if (cur->_data > data)
			{
				parent = cur;
				cur = cur->_pLeft;
			}
			else if (cur->_data < data)
			{
				parent = cur;
				cur = cur->_pRight;
			}
			else
			{
				return false;
			}
		}
		cur= new Node(data);
		if (parent->_data > data)
		{
			parent->_pLeft = cur;
			cur->_pParent = parent;
		}
		else if (parent->_data < data)
		{
			parent->_pRight = cur;
			cur->_pParent = parent;
		}
		while (parent)
		{
			//修改平衡因子
			if (cur == parent->_pLeft)
			{
				parent->_bf--;
			}

			else
			{
				parent->_bf++;
			}

			//判断要不要往上更新
			if (parent->_bf == 0)
			{
				break;
			}

			else if (parent->_bf == 1 || parent->_bf == -1)
			{
				cur = parent;
				parent = parent->_pParent;
			}

			else if (parent->_bf == 2 || parent->_bf == -2)
			{
				//调整
				if (parent->_bf == 2 && cur->_bf == 1)//在较高右子树的右侧,左单旋
				{
					RotateL(parent);
				}
				else if (parent->_bf == -2 && cur->_bf == -1)//在较高左子树的左侧,右单旋
				{
					RotateR(parent);
				}
				else if (parent->_bf == 2 && cur->_bf == -1)//在较高右子树的左侧,右左双旋
				{
					RotateRL(parent);
				}
				else if (parent->_bf == -2 && cur->_bf == 1)//在较高左子树的右侧,左右双旋
				{
					RotateLR(parent);
				}
				break;
			}

			else
			{
				assert(false);
			}
		}

		return true;
	}

	// AVL树的验证
	bool IsAVLTree()
	{
		return _IsAVLTree(_pRoot);
	}

	//AVL树输出
	void InOrder()
	{
		_InOrder(_pRoot);
		cout << endl;
	}

	void _InOrder(Node* root)
	{
		if (root == nullptr)
			return;
		
		_InOrder(root->_pLeft);
		cout << root->_data << "->";
		_InOrder(root->_pRight);
	}

private:

	size_t _Height(Node* pRoot)
	{
		if (pRoot == nullptr) return 0;
		size_t left = _Height(pRoot->_pLeft);
		size_t right = _Height(pRoot->_pRight);
		return left > right ? left + 1 : right + 1;
	}
	// 根据AVL树的概念验证pRoot是否为有效的AVL树
	bool _IsAVLTree(Node* pRoot)
	{
		if (pRoot == nullptr) return true;

		size_t heightleft = _Height(pRoot->_pLeft);
		size_t heightright = _Height(pRoot->_pRight);

		int diff = heightright - heightleft;
		if ((abs(diff) > 1))
		{
			cout << pRoot->_data << "节点平衡因子异常" << endl;
			return false;
		}
		if (diff != pRoot->_bf)
		{
			cout << pRoot->_data << "节点平衡因子与root的平衡因子不等,不符合实际" << endl;
			return false;
		}

		return _IsAVLTree(pRoot->_pLeft) && _IsAVLTree(pRoot->_pRight);
	}
	// 右单旋
	void RotateR(Node* pParent)
	{
		Node* subL = pParent->_pLeft;
		Node* subLR = subL->_pRight;
		Node* grandparent = pParent->_pParent;
		pParent->_pLeft = subLR;
		if (subLR)
		{
			subLR->_pParent = pParent;
		}
		subL->_pRight = pParent;
		pParent->_pParent = subL;

		if (_pRoot == pParent)
		{
			_pRoot = subL;
			subL->_pParent = nullptr;
		}
		else
		{
			if (pParent == grandparent->_pLeft)
			{
				grandparent->_pLeft = subL;
			}
			else
			{
				grandparent->_pRight = subL;
			}
			subL->_pParent = grandparent;
		}
		pParent->_bf = subL->_bf = 0;
	}

	// 左单旋
	void RotateL(Node* pParent)
	{
		Node* subR = pParent->_pRight;
		Node* subRL = subR->_pLeft;
		Node* grandparent = pParent->_pParent;
		pParent->_pRight = subRL;
		if (subRL)
		{
			subRL->_pParent = pParent;
		}
		subR->_pLeft = pParent;
		pParent->_pParent = subR;
		if (_pRoot == pParent)
		{
			_pRoot = subR;
			subR->_pParent = nullptr;
		}
		else
		{
			if (pParent == grandparent->_pLeft)
			{
				grandparent->_pLeft = subR;
			}
			else
			{
				grandparent->_pRight = subR;
			}
			subR->_pParent = grandparent;
		}
		pParent->_bf = subR->_bf = 0;
	}

	// 左右双旋
	void RotateLR(Node* pParent)
	{
		Node* subL = pParent->_pLeft;
		Node* subLR = subL->_pRight;
		int bf = subLR->_bf;
		RotateL(subL);
		RotateR(pParent);
		if (bf == 0)
		{
			//subLR自己就是新增
			pParent->_bf = subLR->_bf = subL->_bf = 0;
		}
		else if (bf == 1)
		{
			//subLR的右子树新增
			pParent->_bf = 0;
			subLR->_bf = 0;
			subL->_bf = -1;
		}
		else if (bf == -1)
		{
			//subLR的左子树新增
			pParent->_bf = 1;
			subLR->_bf = 0;
			subL->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}

	// 右左双旋
	void RotateRL(Node* pParent)
	{
		Node* subR = pParent->_pRight;
		Node* subRL = subR->_pLeft;
		int bf = subRL->_bf;
		RotateR(pParent->_pRight);
		RotateL(pParent);
		if (bf == 0)
		{
			//subRL自己就是新增
			pParent->_bf = subRL->_bf = subR->_bf = 0;
		}
		else if (bf == 1)
		{
			//subRL的右子树新增
			pParent->_bf = -1;
			subRL->_bf = 0;
			subR->_bf = 0;
		}
		else if (bf == -1)
		{
			//subRL的左子树新增
			pParent->_bf = 0;
			subRL->_bf = 0;
			subR->_bf = 1;
		}
		else
		{
			assert(false);
		}
	}

private:
	Node* _pRoot;
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sakura&NANA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值