平衡二叉树AVL的代码Java版

这篇文章展示了如何使用Java实现AVL树的数据结构,包括添加节点、删除节点、查找节点以及保持树的平衡(通过左旋和右旋)。代码详细解释了每个操作的过程,特别是添加节点后的旋转逻辑,以确保树始终保持平衡。
摘要由CSDN通过智能技术生成
package avl;

/**
 * @author yzy
 * @version 1.0
 */
public class AVLDemo {
    public static void main(String[] args) {
        //int[] arr = {4,3,6,5,7,8};
        //int[] arr = {10,12,8,9,7,6};
        int[] arr = {10,11,7,6,8,9};
        AVLTree avlTree = new AVLTree();
        for(int value : arr){
            avlTree.add(new Node(value));
        }
        //测试
        avlTree.minOrder();
        System.out.println(avlTree.root.height());
        System.out.println(avlTree.root.leftHeight());
        System.out.println(avlTree.root.rightHeight());



    }
}
//树类和节点类基础代码来自于bst树,只是在其基础上增加了一些方法
class AVLTree{
    //node类中写添加等,是底层代码,在bst树中需要对其进行封装,使得在主函数能直接调用bst的方法
    Node root;
    //在bst的add方法的基础上修改后的,主要是增加了后面的旋转逻辑,来一个检测一个
    public void add(Node node){
        if(node == null){
            return;
        }
        if(root == null){
            root = node;
        }else {
            root.add(node);
        }
        //当添加完一个结点后,如果发现右子树的高度-左子树的高度大于1,则应该左旋转
        if((root.rightHeight() - root.leftHeight()) > 1 ){
            //如果root的右子树的左子树的高度大于右子树的右子树的高度
            if(root.right.leftHeight() > root.right.rightHeight()){
                root.left.rightHeight();
            }
            root.leftRotate();
            return;//来一个处理一个,这里已经处理好了,不要再进行下面的代码了,防止出现意外情况
        }
        //当添加完一个结点后,如果发现左子树的高度-右子树的高度大于1,则应该右旋转
        if((root.leftHeight() - root.rightHeight()) > 1 ){
            //如果root的左子树的右子树的高度大于左子树的左子树的高度,先对root的左子节点进行左旋
            if(root.left.rightHeight() > root.left.leftHeight()){
                root.left.leftRotate();
            }
            root.rightRotate();
        }
    }
    //遍历
    public void minOrder(){
        if(root != null){
            root.midOrder();
        }
    }
    //查找待删节点
    public Node search(int value){
        if(root == null){
            return null;
        }else {
            return root.search(value);
        }
    }
    //查找父节点
    public Node searchParentNode(int value){
        if(root == null){
            return null;
        }else {
            return root.searchParentNode(value);
        }
    }
    //删除
    public void delete(int value){
        Node target = root.search(value);
        Node parent = root.searchParentNode(value);
        Node temp;
        if(target == null || root == null){
            System.out.println("不可删除");
            return;
        }else {
            if(target.left == null && target.right == null){//情况一叶子结点
                //有了target不一定有parent
                if(parent == null){
                    root = null;
                }
                if(parent.value > target.value){
                    parent.left = null;
                }else {
                    parent.right = null ;
                }
            }else if(target.left == null && target.right != null){//情况二
                //有了target不一定有parent
                if(parent == null){
                    root = target.right;
                }
                if(parent.value > target.value){
                    parent.left = target.right;
                }else {
                    parent.right = target.right ;
                }

            }else if(target.left != null && target.right == null){//情况三
                //有了target不一定有parent
                if(parent == null){
                    root = target.left;
                }
                if(parent.value > target.value){
                    parent.left = target.left;
                }else {
                    parent.right = target.left ;
                }

            }else {//情况四,两个子树的结点
                temp = target.right;//表示右子树的最小值
                while(true){
                    if(temp.left != null){
                        temp = temp.left;
                        continue;
                    }else {
                        break;
                    }
                }
                Node tempParent = root.searchParentNode(temp.value);
                target.value = temp.value;
                //删除叶子结点
                if(temp.value < tempParent.value){
                    tempParent.left = null;
                }else {
                    tempParent.right = null;
                }
            }

        }
    }
}
//节点类
//只新增了五个方法
class Node{
    int value;
    Node left;
    Node right;

    //构造器
    public Node(int value) {
        this.value = value;
    }
    //添加
    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);
            }
        }


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

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
    //查找
    public Node search(int value){
        if(this.value == value){
            return this;
        }
        if(this.left !=  null){
            if(value < this.value){
                return this.left.search(value);
            }
        }
        if(this.right !=  null){
            if(value >= this.value){
                return this.right.search(value);
            }
        }
        return null;
    }
    //查找待删节点的父节点
    public Node searchParentNode(int value){
        if (this.search(value) == null){
            return null;
        }
        //根节点的父节点返回null
        if(value == this.value){
            return null;
        }
        if((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)  ){
            return this;
        }else if(value < this.value){
            return this.left.searchParentNode(value);
        }else {
            return this.right.searchParentNode(value);
        }

    }
    //新增方法,返回已该节点为根的树的高度
    public int height(){
        return Math.max(this.left == null?0:left.height(),this.right == null?0:right.height()) + 1;
    }
    //新增方法,返回左子树的高度
    public int leftHeight(){
        if(this.left == null){
            return 0;
        }else {
            return this.left.height();
        }
    }
    //新增方法,返回右子树的高度
    public int rightHeight(){
        if(this.right == null){
            return 0;
        }else {
            return this.right.height();
        }
    }
    //新增代码,左旋转
    public void leftRotate(){
        //1、创建新结点
        Node newNode = new Node(value);
        //2、将新节点的左子树设置为当前结点的左子树
        newNode.left = left;
        //3、将新节点的右子树设置为当前节点的右子树的左子树
        newNode.right = this.right.left;
        //4、当前结点的值改为其右子节点的值
        this.value = this.right.value;
        //5、当前结点的左子树设置为新结点
        this.left = newNode;
        //6、当前结点的右子树设置为当前结点的右子树的右子树
        this.right = this.right.right;
    }
    //新增代码,右旋转
    public void rightRotate(){
        Node newNode = new Node(value);
        newNode.right = right;
        newNode.left = this.left.right;
        this.value = this.left.value;
        this.right = newNode;
        this.left = this.left.left;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值