数据结构与算法---AVL树

本文介绍了AVL树的概念,当插入节点导致失衡时如何寻找最小失衡子树,并详细阐述了平衡调整的LL型、LR型、RL型、RR型四种情况及对应的旋转操作,旨在理解AVL树如何保持平衡并降低高度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AVL树

定义
在这里插入图片描述
在这里插入图片描述

  • 当我们在一个平衡二叉树上插入一个结点时,有可能导致失衡,即出现平衡因子绝对值大于1的结点。则必须重新调整树的结构,使之恢复平衡。当失衡结点不止一个时,则找失衡子树最小的根结点。
  • 左子树-右子树的绝对值不能大于等于2。

在这里插入图片描述
上图中的失衡结点应找16结点。

平衡调整
在这里插入图片描述
平衡调整的四种类型:

LL型,插入结点在失衡结点左子树的左子树上
LR型,插入结点在失衡结点左子树的右子树上
RL型,插入结点在失衡结点右子树的左子树上
RR型,插入结点在失衡结点右子树的右子树上
调整原则:1)降低高度 2)保持二叉排序树性质—左边子树比根结点小,右边子树比根结点大

  • 左旋转(RR型):降低右子树高度

在这里插入图片描述
失衡结点(A结点)高度:( h )- ( h+2)= -2
B结点带右子树β一起上升
A结点成为B的左孩子(A结点这边的一定比B结点小)
原来B结点的左子树α作为A的右子树(B结点左边的一定比A结点大)

调整后:
在这里插入图片描述

  • 右旋转(LL型):降低左子树高度

在这里插入图片描述
失衡结点(A结点)高度 = ( h+1+1 )- h = 2
B结点带左子树α一起上升
A结点作为B结点的右孩子
原来B结点的右子树β作为A的左子树(B结点的右子树一定比A结点小,但是比B结点大)

调整后:
在这里插入图片描述

  • 双旋转(LR型):

在这里插入图片描述
C结点穿过A、B结点上升
B结点成为C的左孩子
A结点成为C的右孩子
原来C结点的左子树β作为B的右子树
原来C结点的右子树γ作为A的左子树
在这里插入图片描述
RL型:
在这里插入图片描述

  • 代码实现

求树的高度可以分解为求树的子树的高度再加1,子树的高度同样等于子树的子树的高度加1,这样就可以一层一层的向下分解,直到最后一层叶子节点,它没有左右子树了,这时它的高度是1。
结点类:

class Node {
	int value;
	Node left;
	Node right;

	
	/**
	 * 因为树的高度=左右子树中高的+根结点,所以最后要加1
	 * @return返回当前结点的高度
	 */
	public int height() {
		return Math.max(left == null ? 0 : left.height(), 
				right == null ? 0 : right.height()) + 1;
	}

	/**
	 * @return返回左子树的高度
	 */
	public int leftHeight() {
		if (left == null) {
			return 0;
		}
		return left.height();
	}

	/**
	 * @return返回右子树的高度
	 */
	public int rightHeight() {
		if (right == null) {
			return 0;
		}
		return right.height();

	}

	/**
	 * 左旋转方法
	 */
	public void leftRotate() {
//		创建新的结点(以当前根结点的值创建)
		Node newNode = new Node(value);
//		把新结点的左子树设置为当前结点的左子树
		newNode.left = left;
//		把新结点的右子树设置为当前结点右子树的左子树
		newNode.right = right.left;
//		把当前结点的值替换成右子结点的值
		value = right.value;
//		把当前结点的右子树设置为右子树的右子树
		right = right.right;
//		把当前结点的左子树设置为新节点
		left = newNode;
	}
	
	public void rightRotate() {
//		创建新的结点(以当前根节点的值创建)
		Node newNode = new Node(value);
//		把新结点的左子树设置为当前结点左子树的右子树
		newNode.left = left.right;
//		把新结点的右子树设置为当前结点的右子树
		newNode.right = right;
//		把当前结点的值替换成左子结点的值
		value = left.value;
//		把当前结点的左子树设置为左子树的左子树
		left = left.left;
//		把当前结点的右子树设置为新结点
		right = newNode;
	}

	/**
	 * 查找要删除的结点
	 * 
	 * @param value 要查找结点的值
	 * @return返回要查找的结点
	 */
	public Node searchNode(int value) {
		if (this.value == value) {
			return this;
		} else if (this.value > value) {
			if (this.left != null) {
				return this.left.searchNode(value);
			}
			return null;
		} else {
			if (this.right != null) {
				return this.right.searchNode(value);
			}
			return null;
		}
	}

	/**
	 * 查找要删除结点的父结点
	 * 
	 * @param value 查找的值
	 * @return删除结点的父结点
	 */
	public Node searchParent(int value) {
		if ((this.left != null && this.left.value == value) || 
				(this.right != null && this.right.value == value)) {
			return this;
		} else {
			if (this.left != null && this.value > value) {
				return this.left.searchParent(value);
			} else if (this.right != null && this.value <= value) {
				return this.right.searchParent(value);
			} else {
				return null;
			}
		}
	}

	/**
	 * 递归的添加结点
	 * 
	 * @param node
	 */
	public void add(Node node) {
		if (node == null) {
			return;
		}
		if (node.value < this.value) {
			if (this.left == null) {
				this.left = node;
			} else {
				this.left.add(node);
			}
		} else {
			if (this.right == null) {
				this.right = node;
			} else {
				this.right.add(node);
			}
		}
//		右边的高度大于左边
		if ((leftHeight() - rightHeight()) < -1) {
//			如果右子树的左子树高度大于右子树的右子树
//			对右子树进行右旋转
			if (right != null && right.leftHeight() > right.rightHeight()) {
				right.rightRotate();
			}
			leftRotate();
			return;
		}
//		左边的高度大于右边
		if ((leftHeight() - rightHeight()) > 1) {
//			如果左子树的右子树高度大于左子树的左子树
//			要先对左子树(当前结点的左节点)进行左旋转
			if (left != null && left.leftHeight() < left.rightHeight()) {
				left.leftRotate();
			}
			rightRotate();
		}
	}

	public void infixOrder() {
		if (this.left != null) {
			this.left.infixOrder();
		}
		System.out.println(this);
		if (this.right != null) {
			this.right.infixOrder();
		}
	}

	public Node(int value) {
		this.value = value;
	}

	@Override
	public String toString() {
		return "Node [value=" + value + "]";
	}
}

AVL

class AVLTree {
	private Node root;

	public Node getRoot() {
		return root;
	}

	public void add(Node node) {
		if (root == null) {
			root = node;
		} else {
			root.add(node);
		}
	}

	public void infixOrder() {
		if (root != null) {
			root.infixOrder();
		} else {
			System.out.println("该二叉排序树为空");
		}
	}

	public Node searchNode(int value) {
		if (root == null) {
			return null;
		}
		return root.searchNode(value);
	}

	public Node searchParent(int value) {
		if (root == null) {
			return null;
		}
		return root.searchParent(value);
	}

	/**
	 * 删除以Node为根结点的二叉排序树的最小结点
	 * 
	 * @param right 当作二叉排序树的根结点
	 * @return以right为根结点的二叉排序树的最小结点的值
	 */
	public int delRightTreeMin(Node right) {
		Node target = right;
		while (target.left != null) {
			target = target.left;
		}
		delNode(target.value);
		return target.value;
	}

	public void delNode(int value) {
		if (root == null) {
			return;
		} else {
			Node targetNode = root.searchNode(value);
			if (targetNode == null) {
				return;
			}
			if (root.left == null && root.right == null) {
				root = null;
				return;
			}
			Node parentNode = root.searchParent(value);
			if (targetNode.left == null && targetNode.right == null) {
				if (parentNode.left != null && parentNode.left.value == value) {
					parentNode.left = null;
				} else {
					parentNode.right = null;
				}
			} else if (targetNode.left != null && targetNode.right != null) {
				int minVal = delRightTreeMin(targetNode.right);
				targetNode.value = minVal;
			} else {
				if (targetNode.left != null) {
					if (parentNode != null) {
						if (parentNode.left.value == value) {
							parentNode.left = targetNode.left;
						} else if (parentNode.right.value == value) {
							parentNode.right = targetNode.left;
						}
					} else {
						root = targetNode.left;
					}
				} else {
					if (parentNode != null) {
						if (parentNode.left.value == value) {
							parentNode.left = targetNode.right;
						} else {
							parentNode.right = targetNode.right;
						}
					} else {
						root = targetNode.right;
					}
				}
			}
		}
	}

	public int height() {
		if (root == null) {
			return 0;
		}
		return root.height();
	}

	public int leftHeight() {
		if (root == null || root.left == null) {
			return 0;
		}
		return root.leftHeight();
	}

	public int rightHeight() {
		if (root == null || root.right == null) {
			return 0;
		}
		return root.rightHeight();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值