C++数据结构--AVL树

一.什么是AVL树

AVL树,也叫二叉平衡搜索树,就是在BST树的基础上加了平衡操作(平衡的定义是任意节点的左右子树的高度差不超过1)。

其基本框架与BST树相同,不过为了保证其平衡,添加了节点的旋转操作:

cpp 复制代码
template<typename T>
class AVLTree
{
public:
	AVLTree()
		:root(nullptr)
	{ }
	~AVLTree(){}

private:
	struct Node
	{
		Node(T data=T())
			:m_data(data)
			,left(nullptr)
			,right(nullptr)
			,height(1)
		{ }
		


		T m_data;
		Node* left;
		Node* right;
		int height;//节点高度,用于旋转操作
	};
	Node* root;
};
//获取节点高度
int getHeight(Node* node)
{
	return node == nullptr ? 0 : node->height;
}

二.常见操作实现

1.平衡操作

(1).右旋转操作

如果添加一个元素后,左孩子的左子树太高了,那么需要进行右旋转,如图:。

这里的节点node与节点child的孩子节点都发生了变化,它们的高度需要进行更新。

cpp 复制代码
//右旋转操作
Node* rightRotate(Node* node)
{
	Node* child = node->left;
	node->left = child->right;
	child->right = node;

	node->height = max(getHeight(node->left), getHeight(node->right)) + 1;
	child->height = max(getHeight(child->left), getHeight(child->right)) + 1;
	return child;

}

(2).左旋转操作

如果添加一个元素后,右孩子的右子树太高了,那么需要进行左旋转,如图:

这里的节点node与节点child的孩子节点都发生了变化,它们的高度需要进行更新。

cpp 复制代码
//左旋转操作
Node* leftRotate(Node* node)
{
	Node* child = node->right;
	node->right = child->left;
	child->left = node;

	node->height = max(getHeight(node->left), getHeight(node->right)) + 1;
	child->height = max(getHeight(child->left), getHeight(child->right)) + 1;
	return child;

}

(3)左平衡操作

如果添加一个元素后,左孩子的右子树太高了,那么需要进行左平衡,如图:

cpp 复制代码
//左平衡操作(左-右)
Node* leftBalance(Node* node)
{
	node->left= leftRotate(node->left);
	return rightRotate(node);


}

(4)右平衡操作

如果添加一个元素后,右孩子的左子树太高了,那么需要进行右平衡,如图:

cpp 复制代码
//右平衡操作(右-左)
Node* rightBalance(Node* node)
{
	node->right= rightRotate(node->right);

	 return leftRotate(node);


}

2.插入操作

cpp 复制代码
Node* insert(const T& val,Node* node)
{
	if (node == nullptr)
	{
		return new Node(val);

	}
	if (node->m_data == val)
	{
		return node;
	}
	else if (node->m_data > val)
	{
		node->left = insert(val, node->left);
		if ((getHeight(node->left) - getHeight(node->right))>1)
		{
			if (getHeight(node->left->left) >= getHeight(node->left->right))
			{
				node = rightRotate(node);
			}
			else 
			{
				node = rightBalance(node);
			}
		}
		

	}
	else
	{
		node->right = insert(val, node->right);
		if ((getHeight(node->right) - getHeight(node->left)) > 1)
		{
			if (getHeight(node->right->right) >= getHeight(node->right->left))
			{
				node = leftRotate(node);
			}
			else
			{
				node = leftBalance(node);
			}
		}
	}
	node->height = max(getHeight(node->left), getHeight(node->right)) + 1;


	return node;
}

3.删除操作

cpp 复制代码
Node* remove(const T& val, Node* node)
{
	if (node == nullptr)
	{
		return nullptr;
	}
	if (node->m_data > val)
	{
		node->left = remove(val, node->left);
		if ((getHeight(node->right) - getHeight(node->left)) > 1)
		{
			if (getHeight(node->right->right) >= getHeight(node->right->left))
			{
				node = leftRotate(node);
			}
			else
			{
				node = leftBalance(node);
			}
		}
		
	}
	else if(node->m_data < val)
	{
		node->right = remove(val, node->right);
		if ((getHeight(node->left) - getHeight(node->right)) > 1)
		{
			if (getHeight(node->left->left) >= getHeight(node->left->right))
			{
				node = rightRotate(node);
			}
			else
			{
				node = rightBalance(node);
			}
		}
	}
	else
	{
		if (node->left != nullptr && node->right != nullptr)
		{
			if (getHeight(node->left) >= getHeight(node->right))
			{
				Node* pre = node->left;
				while (pre->right != nullptr)
				{
					pre = pre->right;
				}
				node->m_data = pre->m_data;
				
				node->left = remove( pre->m_data, node->left);
			}
			else
			{
				Node* last = node->right;
				while (last->left != nullptr)
				{
					last = last->left;
				}
				node->m_data = last->m_data;
				node->right = remove( last->m_data, node->right);

			}
		}
		else
		{
			if (node->left != nullptr)
			{
				Node* left = node->left;
				delete node;
				return left;
			}
			else if (node->right != nullptr)
			{
				Node* right = node->right;
				delete node;
				return right;
			}
			else
			{
				delete node;
				return nullptr;
			}
		}
	}
	node->height = max(getHeight(node->left), getHeight(node->right)) + 1;

	return node;

}
相关推荐
Darling噜啦啦1 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
小小工匠2 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
玖玥拾3 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
Qres8213 天前
算法复键——树状数组
数据结构·算法
牛油果子哥q3 天前
并查集(DSU)超精讲,路径压缩、按秩合并、万能模板、连通性判定、最小生成树与刷题实战全解
数据结构·c++·最小生成树·并查集
凌波粒3 天前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode
WL学习笔记3 天前
单项不带头不循环链表
数据结构·链表
小糯米6013 天前
JS 数组
数据结构·算法·排序算法
小欣加油3 天前
leetcode3612 用特殊操作处理字符串I
数据结构·c++·算法·leetcode·职场和发展
凌波粒3 天前
LeetCode--90.子集II(回溯算法)
数据结构·算法·leetcode