平衡二叉树的实现与总结(Java)

平衡二叉树

平衡二叉树的引入

案例:给定数组{1,2,3,4,5,6},要求创建一棵二叉排序树(BST)。

按照此数组创建的二叉排序树如图所示:
二叉排序树

分析上图BST树可发现如下问题所在:

  • 左子树全部为空,从形式上看,更像一个单链表
  • 查询速度明显降低,不能发挥BST的优势,因为每次还需要比较左子树,其查询速度比单链表还慢

基于这些问题,我们的解决方案就是引入平衡二叉树

平衡二叉树基本介绍

  • 平衡二叉树也叫平衡二叉搜索树(Self-balancing binary search tree)又被称为AVL树,可以保证查询效率较高。
  • 平衡二叉树具有以下特点:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。
  • 平衡二叉树的常用实现方法有红黑树、AVL、替罪羊树、Treap、伸展树等。
  • 平衡二叉树一定是二叉排序树,反之不一定成立。

平衡二叉树的案例分析


情况一:单旋转(右旋转)
给定数组{15,9,18,7,12,5},分析如下图所示
图解分析
步骤分析

  • 创建一个与当前节点值相同的新节点
  • 新节点的右子节点指向当前节点的右子节点
  • 新节点的左子节点指向当前节点的左子节点的右子节点
  • 将当前节点的左子节点的值赋给当前节点
  • 当前节点的左子节点指向当前节点的左子节点的左子节点
  • 当前节点的右子节点指向新节点

情况二:单旋转(左旋转)
给定数组{5,3,8,7,10,11},分析如下图所示
图解分析
步骤分析

  • 首先new一个新节点,新节点的值与当前节点相同
  • 新节点的左子节点指向当前节点的左子节点
  • 新节点的右子节点指向当前节点的右子节点的左子节点
  • 将当前节点的右子节点的值赋给当前节点
  • 当前节点的右子节点指向当前节点的右子节点的右子节点
  • 当前节点的左子节点指向新节点

情况三:双旋转
给定数组{5,3,8,7,10,6},分析如下图

图解分析发现无法用左旋转解决问题,此时应采用双旋转。
即先将以根节点的右子节点为根节点树先右旋转,在将整棵树左旋转,旋转方法与上面分析的一样。注:还有一种情况这里不做分析,请动动脑筋自行分析


代码实现

先创建一个节点类,封装数据。(注:主要实现方法都写在这个类中,请仔细分析。)

/**
 * 封装节点的类
 */
class Node {
    int value;
    Node left;
    Node right;

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

    /**
     * 添加方法
     *
     * @param node 待添加的节点
     */
    public void add(Node node) {
        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 (getRightHeight() - getLeftHeight() > 1) {
            //进到这里说明当前节点的右子树高度与左子树高度的差值大于1
            if (this.right != null &&
                    this.right.getLeftHeight() > this.right.getRightHeight()) {
                //进入这里说明当前节点的右子节点的左子树高度大于右子树高度
                //需要先将右子节点进行右旋转
                this.right.rightRotation();
            }
            //左旋转
            leftRotation();
            //执行到这里已经将二叉树调整为平衡二叉树,切记结束方法
            return;
        }
        if (getLeftHeight() - getRightHeight() > 1) {
            //进到这里说明当前节点的左子树高度与右子树高度的差值大于1
            if (this.left != null &&
                    this.left.getRightHeight() > this.left.getLeftHeight()) {
                //进入这里说明当前节点的左子节点的右子树高度大于左子树高度
                //需要先将左子节点进行左旋转
                this.left.leftRotation();
            }
            //右旋转
            rightRotation();
        }
    }

    /**
     * 中序遍历
     */
    public void inorder() {
        if (this.left != null) {
            this.left.inorder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.inorder();
        }
    }

    /**
     * 获取高度的方法
     *
     * @return 返回当前以当前节点为父节点的树的高度
     */
    public int getHeight() {
        return Math.max(this.left == null ? 0 : this.left.getHeight(),
                this.right == null ? 0 : this.right.getHeight()) + 1;
    }

    /**
     * 获取左子树高度
     *
     * @return 返回以当前节点的左子节点为父节点的树的高度
     */
    public int getLeftHeight() {
        if (this.left == null) {
            return 0;
        }
        return this.left.getHeight();
    }

    /**
     * 获取右子树高度
     *
     * @return 返回以当前节点的右子节点为父节点的树的高度
     */
    public int getRightHeight() {
        if (this.right == null) {
            return 0;
        }
        return this.right.getHeight();
    }

    /**
     * 左旋转
     */
    public void leftRotation() {
        //1.首先new一个新节点,新节点的值与当前节点相同
        Node newNode = new Node(this.value);
        //2.新节点的左子节点指向当前节点的左子节点
        newNode.left = this.left;
        //3.新节点的右子节点指向当前节点的右子节点的左子节点
        newNode.right = this.right.left;
        //4.将当前节点的右子节点的值赋给当前节点
        this.value = this.right.value;
        //5.当前节点的右子节点指向当前节点的右子节点的右子节点
        this.right = this.right.right;
        //6.当前节点的左子节点指向新节点
        this.left = newNode;
    }

    /**
     * 右旋转
     */
    public void rightRotation() {
        //1.首先new一个新节点
        Node newNode = new Node(this.value);
        //2.新节点的右子节点指向当前节点的右子节点
        newNode.right = this.right;
        //3.新节点的左子节点指向当前节点的左子节点的右子节点
        newNode.left = this.left.right;
        //4.将当前节点的左子节点的值赋给当前节点
        this.value = this.left.value;
        //5.当前节点的左子节点指向当前节点的左子节点的左子节点
        this.left = this.left.left;
        //6.当前节点的右子节点指向新节点
        this.right = newNode;
    }

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

在编写一个管理平衡二叉树的类

/**
 * 管理平衡二叉树的类
 */
class AVLTree {
    Node root;

    /**
     * 添加的方法
     *
     * @param node 待添加的节点
     */
    public void add(Node node) {
        if (node == null) {
            return;
        }
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    public void inorder() {
        if (root == null) {
            System.out.println("平衡二叉树为空,无法遍历");
            return;
        }
        root.inorder();
    }
}

主方法中进行测试

package com.atschool.avltree;

/**
 * Description:
 * Author:江洋大盗
 * Date:2021/1/15 11:58
 */
public class AVLTreeDemo {
    public static void main(String[] args) {
        int[] arr = {5, 3, 8, 7, 10, 6};
        AVLTree avlTree = new AVLTree();
        for (int i : arr) {
            avlTree.add(new Node(i));
        }
        avlTree.inorder();
        System.out.println("树的高度:" + avlTree.root.getHeight());
        System.out.println("左子树的高度:" + avlTree.root.left.getHeight());
        System.out.println("右子树的高度:" + avlTree.root.right.getHeight());
    }
}

测试结果:
(注:因为情况三最为复杂,这里只测试案例中给的情况三。)

测试结果


结语

只要能收获甜蜜,荆棘丛中也有蜜蜂忙碌的身影,未来的你一定会感谢现在努力的自己。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
平衡二叉树(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实现,包括插入节点、删除节点、旋转操作等方法。你可以根据需要进行调整和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值