一 点睛
平衡二叉查找树,简称平衡二叉树,由苏联数学家 Adelson-Velskii 和 Landis 提出,所以又被称为 AVL 树。
平衡二叉树或为空树,或为具有以下性质的平衡二叉树
1 左右子树高度差的绝对值不超过 1。
2 左右子树也是平衡二叉树。
节点左右子树的高度只差被称为平衡因子。在平衡二叉树中,每个节点的平衡因子的绝对值不超过1即为平衡二叉树。
对平衡二叉树在动态修改后出现的不平衡,只需局部调整平衡即可,不需要对整课树进行调整。
二 调整 ALV 平衡的方法
1 LL 型
2 RR 型
3 LR 型
4 RL 型
三 平衡二叉树的常见操作
1 插入
2 创建
3 删除
四 平衡二叉树的综合应用
1 代码
package alv;
import java.util.Scanner;
public class AVLDemo {
public static void main(String[] args) {
AVLTree avlTree = new AVLTree();
avlTree.createAVL();
System.out.println("中序遍历");
avlTree.infixOrder();
System.out.println("平衡处理后:");
System.out.println("树的高度=" + avlTree.getRoot().height());
System.out.println("树的左子树高度=" + avlTree.getRoot().leftHeight());
System.out.println("树的右子树高度=" + avlTree.getRoot().rightHeight());
System.out.println("当前的根结点=" + avlTree.getRoot());
}
}
/**
* @className: AVLTreeDemo
* @description: 创建AVLTree
* @date: 2021/3/27
* @author: cakin
*/
class AVLTree {
// 根节点
private Node root;
public Node getRoot() {
return root;
}
/**
* 功能描述:查找要删除的结点
*
* @param value 要删除节点的值
* @return Node 要删除的节点
* @author cakin
* @date 2021/3/25
*/
public Node search(int value) {
if (root == null) {
return null;
} else {
return root.search(value);
}
}
/**
* 功能描述:要删除节点的父节点
*
* @param value 要删除节点的值
* @return Node 要删除节点的父节点
* @author cakin
* @date 2021/3/25
* @description:
*/
public Node searchParent(int value) {
if (root == null)