高阶数据结构——AVL树

文章目录

  • 一、AVL树的概念
  • 二、AVL树中节点的定义
  • 三、AVL树的插入
    • 1.调节节点的平衡因子
    • 2.AVL树的旋转
      • 1.右单旋
      • 2.左单旋
      • 3.先右单旋再左单旋
      • 4.先左单旋再右单旋
  • 四、判断一下是否是AVL树

一、AVL树的概念

二叉搜索树虽可以缩短查找的效率,但如果数据有序或接近有序二叉树将退化成单支树,查找元素相当于在顺序表中搜索元素,效率低下。于是引出了AVL树的概念,当向二叉搜索树中插入新节点后,如果能保证每个节点的左右子树高度之差的绝对值不超过1(需要对树中节点进行调整),即可降低树的高度,从而减少平均搜索长度。(左右子树高度之差(简称平衡因子)的绝对值不超过(-1/0/1)

二、AVL树中节点的定义

struct AVLTreeNode
{
	pair<K,V> _kv;
	//使用三叉链
	AVLTreeNode<K,V>*_left;
	AVLTreeNode<K,V>*_right;
	AVLTreeNode<K,V>*_parent;
	//右子树-左子树的高度差
	int _bf;//平衡调节因子

	AVLTreeNode(const pair<K,V>&kv)
		:_kv(kv)
		,_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
	{}
};

三、AVL树的插入

1.调节节点的平衡因子

2.AVL树的旋转

1.左单旋

 

//左单旋
void RotateL(Node*parent)
	{
		Node*subR=parent->_right;
		Node*subRL=subR->_left;

		parent->_right=subRL;
		if(subRL)
			subRL->_parent=parent;
		Node*ppNode=parent->_parent;
		subR->_left=parent;
		parent->_parent=subR;
		if(parent==_root)
		{
			_root=subR;
			_root->_parent=nullptr;
		}
		else
		{
			if(parent==ppNode->_left)
			{
				ppNode->_left=subR;
			}
			else
			{
				ppNode->_right=subR;
			}
			subR->_parent=ppNode;
		}
		parent->_bf=suR->_bf=0;
	}

 

 

 

//右单旋
void RotateR(Node*parent)
	{
		Node*subL=parent->_left;
		Node*subLR=subL->_right;

		if(subLR)
		{
			subLR->_parent=subL;
		}
		Node*ppNode=parent->_parent;
		subL->_right=parent;
		parent->_parent=subL;
		if(parent==_root)
		{
			_root=subL;
			_root->_parent=nullptr;
		}
		else
		{
			if(parent==ppNode->_left)
			{
				ppNode->_left=subL;
			}
			else
			{
				ppNode->_right=subL;
			}
			subL->_parent=ppNode;
		}
		subL->_bf=parent->_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 = 0;
			subL->_bf = 0;
			subLR->_bf = 0;
		}
		else if (bf == 1)
		{
			parent->_bf = 0;
			subL->_bf = -1;
			subLR->_bf = 0;
		}
		else if (bf == -1)
		{
			parent->_bf = 0;
			subL->_bf = 0;
			subLR->_bf = 1;
		}
		else
			assert(false);
	}

 

 

//先右单旋再左单旋
	void RotateRL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		int bf = subRL->_bf;
		RotateR(parent->_right);
		RotateL(parent);
		if (bf == 0)
		{
			subRL->_bf = 0;
			parent->_bf = -1;
			subR->_bf = 0;
		}
		else if (bf == 1)
		{
			subRL->_bf = 0;
			parent->_bf = -1;
			subR->_bf = 0;
		}
		else
		{
			subRL->_bf = 0;
			parent->_bf = 0;
			subR->_bf = 1;
		}
		else
		{
		    assert(false);
        }
	}

四、判断一下是否是AVL树

bool _isBalanceTree(Node* root)
	{
		if (root == nullptr)
			return true;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		int diff = rightHeight->leftHeight;
		if (abs(diff) >= 2)
		{
			return false;
		}
		else if (diff != root->_bf)
		{
			return false;
		}
		return _isBalanceTree(root->_left) && _isBalanceTree(root->right);
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

‘(尐儍苽-℡

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

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

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

打赏作者

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

抵扣说明:

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

余额充值