AVl平衡二叉树的增、删、查 java实现

为什么引入AVL树?

给你一个数列{1,2,3,4,5,6},要求创建一颗二叉排序树(BST), 并分析问题所在.
在这里插入图片描述
上边BST 存在的问题分析:

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

解决方案-平衡二叉树(AVL)

基本介绍

  • 平衡二叉树也叫平衡二叉搜索树(Self-balancing binary search tree)又被称为AVL树,可以保证查询效率较高。

特点:

  • 它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,
  • 并且左右两个子树都是一棵平衡二叉树。

在进行代码书写时,我们先了解几个概念

  1. 左旋转
    在这里插入图片描述

  2. 右旋转
    在这里插入图片描述

  3. 双旋转
    在这里插入图片描述

代码实现

Node

package com.edu.tree.avltree;

/**
 * @Date 2020/6/11 8:54
 * @Author by LiShiYan
 * @Description AVL树结点
 */
public class Node {
    private Integer data;
    private Node left;
    private Node right;

    public Node(Integer data) {
        this.data = data;
    }

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

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

    /**
     * @return 返回 以该结点为根结点的树的高度
     */
    public int height() {
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    /**
     * 左旋转
     */
    private void leftRotate() {
        //创建新的结点,以当前根结点的值
        Node newNode = new Node(data);
        //把新的结点的左子树设置成当前结点的左子树
        newNode.left = this.left;
        //把新的结点的右子树设置成带你过去结点的右子树的左子树
        newNode.right = this.right.left;
        //把当前结点的值替换成右子结点的值
        this.data = right.data;
        //把当前结点的右子树设置成当前结点右子树的右子树
        this.right = this.right.right;
        //把当前结点的左子树(左子结点)设置成新的结点
        this.left = newNode;
    }

    /**
     * 左旋转
     */
    private void rightRotate() {
        //创建新的结点,以当前根结点的值
        Node newNode = new Node(data);
        //把新的结点的右子树设置成当前结点的右子树
        newNode.right = this.right;
        //把新的结点的左子树设置成带你过去结点的左子树的右子树
        newNode.left = this.left.right;
        //把当前结点的值替换成左子结点的值
        this.data = this.left.data;
        //把当前结点的左子树设置成当前结点左子树的左子树
        this.left = this.left.left;
        //把当前结点的右子树(右子结点)设置成新的结点
        this.right = newNode;
    }

    /**
     * 查找要删除结点的父结点
     *
     * @param data 删除结点
     * @return 删除结点的父结点, 如果没有返回null
     */
    public Node searchParent(int data) {
        //如果当前结点是删除结点的父节点,返回true
        boolean isParent = (this.left != null && this.left.data == data) || (this.right != null && this.right.data == data);
        if (isParent) {
            //返回当前结点
            return this;
        } else {
            //如果查找的值小于当前结点的值,并且当前结点的左子结点不为空
            if (data < this.data && this.left != null) {
                //向左子树递归
                return this.left.searchParent(data);
            } else if (data >= this.data && this.right != null) {
                //向右子树递归
                return this.right.searchParent(data);
            } else {
                //没有找到父结点
                return null;
            }
        }
    }

    /**
     * 查找要删除的结点
     *
     * @param data 要删除结点的值
     * @return 如果找到返回该结点,否则返回null
     */
    public Node search(int data) {
        //找到该结点
        if (data == this.data) {
            return this;
        }
        //如果查找的值小于当前结点,向左子树递归查找
        else if (data < this.data) {
            //如果左子树为空,返回null
            if (this.left == null) {
                return null;
            }
            return this.left.search(data);
        }
        //如果查找的值大于等于当前结点,向右子树递归查找
        else {
            if (this.right == null) {
                return null;
            }
            return this.right.search(data);
        }
    }

    /**
     * 递归的形式添加节点
     *
     * @param node 要添加的结点
     */
    public void add(Node node) {
        if (node == null) {
            return;
        }
        //判断传入结点的值和当前子树的根节点的值的关系
        //1.添加结点的值小于当前结点的值
        if (node.data < this.data) {
            if (this.left == null) {
                //当前结点的left指针指向node结点
                this.left = node;
            } else {
                //递归的向左子树添加
                this.left.add(node);
            }
        } else {
            //2.添加的结点值大于 当前结点的值
            if (this.right == null) {
                //当前结点的right指针指向node结点
                this.right = node;
            } else {
                //递归向右子树添加
                this.right.add(node);
            }
        }

        //当添加完一个结点后,如果: (右子树的高度-左子树的高度) > 1 , 左旋转
        if (rightHeight() - leftHeight() > 1) {
            //如果它的右子树的左子树的高度大于它的右子树的右子树的高度
            if (right != null && right.leftHeight() > right.rightHeight()) {
                //先对右子结点进行右旋转
                right.rightRotate();
                //然后在对当前结点进行左旋转
                leftRotate(); //左旋转..
            } else {
                //直接进行左旋转即可
                leftRotate();
            }
            return; //必须要!!!
        }

        //当添加完一个结点后,如果 (左子树的高度 - 右子树的高度) > 1, 右旋转
        if (leftHeight() - rightHeight() > 1) {
            //如果它的左子树的右子树高度大于它的左子树的高度
            if (left != null && left.rightHeight() > left.leftHeight()) {
                //先对当前结点的左结点(左子树)->左旋转
                left.leftRotate();
                //再对当前结点进行右旋转
                rightRotate();
            } else {
                //直接进行右旋转即可
                rightRotate();
            }
        }
    }

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

    public Integer getData() {
        return data;
    }

    public void setData(Integer data) {
        this.data = data;
    }

    public Node getLeft() {
        return left;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public Node getRight() {
        return right;
    }

    public void setRight(Node right) {
        this.right = right;
    }

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

AvlTree

package com.edu.tree.avltree;

/**
 * @Date 2020/6/13 9:28
 * @Author by LiShiYan
 * @Description 平衡二叉树
 */
public class AvlTree {
    private Node root;

    public Node getRoot() {
        return root;
    }


    /**
     * 1. 返回的 以node 为根结点的二叉排序树的最小结点的值
     * 2. 删除node 为根结点的二叉排序树的最小结点
     *
     * @param node 传入的结点(当做二叉排序树的根节点)
     * @return 以node为根节点的二叉排序树的最小结点的值
     */
    public int delRightTreeMin(Node node) {
        Node targetNode = node;
        //循环的查找左子结点,就会找到最小值
        while (targetNode.getLeft() != null) {
            targetNode = targetNode.getLeft();
        }
        //循环结束,此时target指向了最小的结点
        //删除最小结点
        delNode(targetNode.getData());

        return targetNode.getData();
    }

    /**
     * @param data 要删除结点
     */
    public void delNode(int data) {
        if (root == null) {
            return;
        } else {
            //1.找到要删除结点
            Node targetNode = search(data);
            //如果没有找到要删除的结点,返回
            if (targetNode == null) {
                return;
            }
            //如果当前这颗二叉排序树只有一个结点
            if (root.getLeft() == null && root.getRight() == null) {
                root = null;
                return;
            }
            //2.找到targetNode父结点
            Node parent = searchParent(data);
            //3.1如果删除的结点是叶子结点
            if (targetNode.getLeft() == null && targetNode.getRight() == null) {
                //判断targetNode是父结点的左子结点还是右子结点
                if (parent.getLeft() != null && parent.getLeft().getData() == data) {
                    //左子结点
                    parent.setLeft(null);
                }
                if (parent.getRight() != null && parent.getRight().getData() == data) {
                    //右子结点
                    parent.setRight(null);
                }
            }
            //3.2 删除有两颗子树的节点
            else if (targetNode.getLeft() != null && targetNode.getRight() != null) {
                //1.找到最小结点
                int minValue = delRightTreeMin(targetNode.getRight());
                //2.设置删除结点的值为最小结点的值
                targetNode.setData(minValue);
            }
            //3.3 删除只有一颗子树的结点
            else {
                //1.如果要删除的结点有 左子结点: targetNode.getLeft()
                if (targetNode.getLeft() != null) {
                    //父结点不为空
                    if (parent != null) {
                        //1.2如果targetNode是 parent 左子结点
                        if (parent.getLeft().getData() == data) {
                            //将parent 左结点指针 指向targetNode的左子结点
                            parent.setLeft(targetNode.getLeft());
                        } else {
                            //1.3如果targetNode是 parent 右子结点
                            parent.setRight(targetNode.getLeft());
                        }
                    } else {
                        //父结点为空,让目标结点的左节点成为根结点
                        root = targetNode.getLeft();
                    }
                } else {
                    //2. 如果要删除的结点有 右子结点: targetNode.getRight()
                    if (parent != null) {
                        //2.2如果targetNode是 parent 左子结点
                        if (parent.getLeft().getData() == data) {
                            parent.setLeft(targetNode.getRight());
                        } else {
                            //2.3如果targetNode是 parent 右子结点
                            parent.setRight(targetNode.getRight());
                        }
                    } else {
                        //父结点为空,让目标结点的右节点成为根结点
                        root = targetNode.getRight();
                    }
                }
            }
        }
    }

    /**
     * 查找要删除的结点
     *
     * @param data 要删除结点
     * @return 以删除结点
     */
    public Node search(int data) {
        if (root == null) {
            return null;
        } else {
            return root.search(data);
        }
    }

    /**
     * 查找父结点
     *
     * @param data 要删除结点
     * @return 要删除结点的父结点
     */
    public Node searchParent(int data) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(data);
        }
    }

    /**
     * 添加结点
     *
     * @param node 结点
     */
    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("二叉排序树为空,不能遍历");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值