平衡二叉树(AVL)【java实现+图解】

目录

一、平衡二叉树(AVL)

二、平衡二叉树的四种旋转

1.右旋转

2.左旋转

3. 左右旋转

4. 右左旋转

 三、基于二叉搜索树之平衡二叉树的代码实现

1.具体方法思路

2.java代码实现 


一、平衡二叉树(AVL)

一种自平衡二叉搜索树,它是在每个节点上增加一个平衡因子,然后通过调整树中节点的高度来保持树的平衡。平衡因子是左子树的高度减去右子树的高度,用它可以表示出当前节点的平衡程度。对于任意一个结点左子树和右子树的高度差不能超过1,当一个节点的平衡因子绝对值大于1时,这个节点就被称为不平衡节点。

二、平衡二叉树的四种旋转

1.右旋转

 

    //左旋转
    public Node leftSpin(Node x){
        Node y = x.right;
        Node t2 = y.left;

        x.right = t2;
        y.left = x;
        //更新节点的高度
        x.height = Math.max(getHight(x.left),getHight(x.right))+1;
        y.height = Math.max(getHight(y.left),getHight(y.right))+1;
        return y;
    }

 

2.左旋转

 

    //右旋转
    public Node rightSpin(Node x){
        Node y = x.left;
        Node t2 = y.right;

        x.left = t2;
        y.right = x;
        //更新节点的高度
        x.height = Math.max(getHight(x.left),getHight(x.right))+1;
        y.height = Math.max(getHight(y.left),getHight(y.right))+1;
        return y;
    }

3. 左右旋转

先旋转成上面只需要右旋转的情况,再经过右旋转成平衡二叉树

4. 右左旋转

先旋转成上面只需要左旋转的情况,再经过左旋转成平衡二叉树

 三、基于二叉搜索树之平衡二叉树的代码实现

1.具体方法思路

主要方法:

添加:每次添加一个节点后更新输的高度,平衡因子可能改变,依据平衡因子对二叉树进行调整;

删除:每删除一个节点后,也需要更新输的高度,平衡因子可能改变,依据平衡因子对二叉树进行调整。

辅助方法:

判断是否是二叉搜索树:通过中序遍历,将节点的前一个值与后一个值比较;

判断是否是平衡二叉树:通过判断每个子树的平衡因子的是否在[-1,1]区间内。

2.java代码实现 

import java.util.*;
import java.util.stream.Collectors;

public class AVL<T extends Comparable<T>> {

    public class Node {
        T val;
        Node left;
        Node right;
        //以该节点为根的树的高度
        int height;

        //统计单词出现的次数
        int count;

        public Node(T val) {
            this.val = val;
            this.height = 1;//默认高度为1
            this.count = 1;
        }

        @Override
        public String toString() {
            return String.format("val:%s,heiget:%d,count:%d",this.val,this.height,this.count);
        }
    }

    private Node root;
    private Integer size;

    public AVL() {
        this.root = null;
        this.size = 0;
    }

    public boolean isEmpt() {
        return this.size == 0;
    }

    public Integer getSize() {
        return this.size;
    }


    //验证是否是二叉搜索树
    public  boolean isBinaryTree(){
        List<T> list = new ArrayList<>();
        midTraversal(list,this.root);
        for (int i = 1; i < list.size(); i++) {
            if (list.get(i-1).compareTo(list.get(i))>0){
                return false;
            }
        }
        return true;
    }

    //获取当前节点的高度
    public int getHight(Node node){
        if (node==null){
            return 0;
        }
        return node.height;
    }
    //获取当前节点的平衡因子
    public int getBalanceFactor(Node node){
        if (node==null){
            return 0;
        }
        return getHight(node.left)-getHight(node.right);
    }
    

    public boolean isBalanceTree(){
        return isBalanceTree(this.root);
    }

    //判断以node为节点的树是否是平衡二叉树
    private boolean  isBalanceTree(Node node){
        if (node==null){
            return true;
        }
        int balanceFactor = Math.abs(getBalanceFactor(node));

        if (balanceFactor>1){
            return false;
        }else {
            return isBalanceTree(node.left)&&isBalanceTree(node.right);
        }
    }
    
    //左旋转
    public Node leftSpin(Node x){
        Node y = x.right;
        Node t2 = y.left;

        x.right = t2;
        y.left = x;
        //更新节点的高度
        x.height = Math.max(getHight(x.left),getHight(x.right))+1;
        y.height = Math.max(getHight(y.left),getHight(y.right))+1;
        return y;
    }
    //右旋转
    public Node rightSpin(Node x){
        Node y = x.left;
        Node t2 = y.right;

        x.left = t2;
        y.right = x;
        //更新节点的高度
        x.height = Math.max(getHight(x.left),getHight(x.right))+1;
        y.height = Math.max(getHight(y.left),getHight(y.right))+1;
        return y;
    }

    //添加
    public void add(T val) {
        this.root = add(this.root, val);
    }

    private Node add(Node node, T val) {
        if (node == null) {
            this.size++;
            Node leafNode = new Node(val);
            return leafNode;
        }
        //当前节点的值小于添加的val,因此val做右孩子
        if (node.val.compareTo(val) < 0) {
            node.right = add(node.right, val);
            //更新高度
            node.height = Math.max(getHight(node.left),getHight(node.right))+1;
        } else if (node.val.compareTo(val) > 0){
            node.left = add(node.left, val);
            //更新高度
            node.height = Math.max(getHight(node.left),getHight(node.right))+1;
        }else {
            node.count++;
        }

        //添加后还要判断是否是平衡二叉树
        Node resNode = node;
        int balanceFactor = getBalanceFactor(node);
        if (balanceFactor>1&&getBalanceFactor(node.left)>=0){
            //右
            resNode = rightSpin(node);
        }else if (balanceFactor>1&&getBalanceFactor(node.left)<0){
            //左右
            node.left = leftSpin(node.left);
            resNode = rightSpin(node);
        }else if (balanceFactor<-1&&getBalanceFactor(node.right)<=0){
            //左
            resNode = leftSpin(node);
        }else if (balanceFactor<-1&&getBalanceFactor(node.right)>0){
            //右左
            node.right = rightSpin(node.right);
            resNode = leftSpin(node);
        }
        return resNode;
    }


    //删除操作
    //删除树中的val
    public void remove(T val){
        if (!contain(val)){
            return;
        }
        this.root = remove(this.root,val);
    }

    /**
     * 删除val
     * @param node
     * @param val
     * @return
     */
    public Node remove(Node node, T val){
        //    递归终止条件
        if (node == null) {
            return null;
        }
        Node resNode = null;
        if (node.val.compareTo(val) == 0) {
            this.size--;
            if (node.right==null){
                //右子树为空
                resNode = node.left;
            }else if (node.left==null){
                //左子树为空
                resNode = node.right;
            }else {
                //    左右子树都不为空
                //    1.找到删除节点的后继
                 Node suffixNode = getMinDG(node.right);
                // 2.删除后继
                suffixNode.right = remove(node.right,getMinDG(node.right).val);
                // 3.连接
                suffixNode.left = node.left;
                this.size++;
                //    返回删除后的根
                resNode = suffixNode;
            }
        }else if (node.val.compareTo(val)<0){
            node.right = remove(node.right,val);
            resNode = node;
        }else {
            node.left = remove(node.left,val);
            resNode =  node;
        }

        //删除节点可能为叶子结点
        if (resNode==null){
            return null;
        }
        //更新高度
        resNode.height = Math.max(getHight(resNode.left),getHight(resNode.right))+1;
        int balanceFactor = getBalanceFactor(resNode);
        if (balanceFactor>1&&getBalanceFactor(resNode.left)>=0){
            //右
            resNode = rightSpin(resNode);
        }else if (balanceFactor>1&&getBalanceFactor(resNode.left)<0){
            //左右
            resNode.left = leftSpin(resNode.left);
            resNode = rightSpin(resNode);
        }else if (balanceFactor<-1&&getBalanceFactor(resNode.right)<=0){
            //左
            resNode = leftSpin(resNode);
        }else if (balanceFactor<-1&&getBalanceFactor(resNode.right)>0){
            //右左
            resNode.right = rightSpin(resNode.right);
            resNode = leftSpin(resNode);
        }
        return resNode;
    }

    private Node getMinDG(Node node) {
        // 递归终止条件
        if (node.left == null) {
            return node;
        }
        //   递归操作
        return getMinDG(node.left);
    }

    public String midTraversal() {
        List<T> res = new ArrayList<>();
        midTraversal(res, this.root);
        return res.stream().map(item -> item.toString()).collect(Collectors.joining(","));
    }

    /**
     * 中序遍历
     *
     * @param result
     * @param node   当前节点
     * @return
     */
    private void midTraversal(List<T> result, Node node) {
        if (node == null) {
            return;
        }
        midTraversal(result, node.left);
        result.add(node.val);
        midTraversal(result, node.right);
    }

    public void showTree(){
        showTree(this.root);
    }
    private void showTree(Node node){
        if (node == null) {
            return;
        }
        showTree(node.left);
        System.out.print(node);
        System.out.println("BalanceFactor:"+getBalanceFactor(node));
        showTree(node.right);
    }

    //查询是否存在val
    public boolean contain(T val){
        return contain(this.root,val);
    }
    private boolean contain(Node node,T val){
    //    递归的终止条件
    //    查询到低也没有找到
        if (node==null){
            return false;
        }
    //    递归操作
        if (node.val.compareTo(val)==0){
            return true;
        }else if (node.val.compareTo(val)<0){
            return contain(node.right,val);
        }else {
            return  contain(node.left,val);
        }
    }
    //测试
    public static void main(String[] args) {
        AVL<String> bst = new AVL<>();

        List<String> list = ReadBookUtil.readBook("pride-and-prejudice.txt");
        list.stream().forEach(item->{
            bst.add(item);
        });
        System.out.println("是否是二分搜索树"+bst.isBinaryTree());
        System.out.println("是否是平衡二叉树"+bst.isBalanceTree());
        bst.showTree();
  
    }
}
平衡二叉树AVL树)是一种自平衡的二叉搜索树,它的左子树和右子树的高度差不超过1。在Java中,可以通过以下步骤实现平衡二叉树: 1. 定义节点类:首先定义一个节点类,包含节点值、左子节点和右子节点等属性。 ```java class Node { int value; Node left; Node right; public Node(int value) { this.value = value; this.left = null; this.right = null; } } ``` 2. 实现平衡二叉树类:创建一个平衡二叉树类,包含插入节点、删除节点、旋转操作等方法。 ```java class AVLTree { private Node root; // 插入节点 public void insert(int value) { root = insertNode(root, value); } private Node insertNode(Node root, int value) { if (root == null) { return new Node(value); } if (value < root.value) { root.left = insertNode(root.left, value); } else if (value > root.value) { root.right = insertNode(root.right, value); } else { // 如果存在相同值的节点,可以根据需求进行处理 return root; } // 更新节点的高度 root.height = 1 + Math.max(getHeight(root.left), getHeight(root.right)); // 平衡操作 int balance = getBalance(root); // 左左情况,进行右旋操作 if (balance > 1 && value < root.left.value) { return rightRotate(root); } // 右右情况,进行左旋操作 if (balance < -1 && value > root.right.value) { return leftRotate(root); } // 左右情况,先左旋再右旋 if (balance > 1 && value > root.left.value) { root.left = leftRotate(root.left); return rightRotate(root); } // 右左情况,先右旋再左旋 if (balance < -1 && value < root.right.value) { root.right = rightRotate(root.right); return leftRotate(root); } return root; } // 删除节点 public void delete(int value) { root = deleteNode(root, value); } private Node deleteNode(Node root, int value) { // 空树或未找到节点 if (root == null) { return root; } if (value < root.value) { root.left = deleteNode(root.left, value); } else if (value > root.value) { root.right = deleteNode(root.right, value); } else { // 找到要删除的节点 // 节点只有一个子节点或无子节点 if (root.left == null || root.right == null) { Node temp = null; if (temp == root.left) { temp = root.right; } else { temp = root.left; } // 无子节点的情况 if (temp == null) { temp = root; root = null; } else { // 一个子节点的情况 root = temp; } } else { // 节点有两个子节点,找到右子树中最小的节点 Node temp = minValueNode(root.right); // 将右子树中最小节点的值赋给要删除的节点 root.value = temp.value; // 删除右子树中最小的节点 root.right = deleteNode(root.right, temp.value); } } // 更新节点的高度 root.height = 1 + Math.max(getHeight(root.left), getHeight(root.right)); // 平衡操作 int balance = getBalance(root); // 左左情况,进行右旋操作 if (balance > 1 && getBalance(root.left) >= 0) { return rightRotate(root); } // 左右情况,先左旋再右旋 if (balance > 1 && getBalance(root.left) < 0) { root.left = leftRotate(root.left); return rightRotate(root); } // 右右情况,进行左旋操作 if (balance < -1 && getBalance(root.right) <= 0) { return leftRotate(root); } // 右左情况,先右旋再左旋 if (balance < -1 && getBalance(root.right) > 0) { root.right = rightRotate(root.right); return leftRotate(root); } return root; } // 获取节点的高度 private int getHeight(Node node) { if (node == null) { return 0; } return node.height; } // 获取节点的平衡因子 private int getBalance(Node node) { if (node == null) { return 0; } return getHeight(node.left) - getHeight(node.right); } // 右旋操作 private Node rightRotate(Node y) { Node x = y.left; Node T2 = x.right; x.right = y; y.left = T2; y.height = Math.max(getHeight(y.left), getHeight(y.right)) + 1; x.height = Math.max(getHeight(x.left), getHeight(x.right)) + 1; return x; } // 左旋操作 private Node leftRotate(Node x) { Node y = x.right; Node T2 = y.left; y.left = x; x.right = T2; x.height = Math.max(getHeight(x.left), getHeight(x.right)) + 1; y.height = Math.max(getHeight(y.left), getHeight(y.right)) + 1; return y; } // 获取最小值节点 private Node minValueNode(Node node) { Node current = node; while (current.left != null) { current = current.left; } return current; } } ``` 以上是一个简单的平衡二叉树Java实现,包括插入节点、删除节点、旋转操作等方法。你可以根据需要进行调整和扩展。
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小俱的一步步

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

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

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

打赏作者

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

抵扣说明:

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

余额充值