AVL树

一、创建结点类
package com.milearn.structrue.AVLtree;

public class Node {
    int value;
    Node left;
    Node right;

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

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

    //左旋转
    public void leftRotate() {

        //1、创建一个与根节点值想等的结点
        Node newNode = new Node(value);

        //2、把新的结点的左子树设置为当前结点的左子树
        newNode.left = left;

        //3、把新的结点的右子树设置当前结点的右子树的左子树
        newNode.right = right.left;

        //4、把当前结点的值换位右子节点的值
        value = right.value;

        //5、把当前结点右子树设置当前结点的右子树的右子树
        right = right.right;

        //6、当前的左子节点设置为新的结点
        left = newNode;
    }

    //右旋转 步骤与左旋相似
    public void rightRotate() {
        Node newNode = new Node(value);
        newNode.right = right;
        newNode.left = left.right;
        value = left.value;
        left = left.left;
        right = newNode;
    }

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

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

    //返回当前结点的高度
    public int height() {
        return Math.max(left == null ? 0 : left.height(),right == null ? 0 : right.height()) + 1;
    }


    //添加结点
    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);
            }
        }



        //添加完结点可以判断是否需要左旋转调节高度
        if (rightHeight() - leftHeight() > 1) {
            //如果右子树的左子树的高度大于右子树的高度
            if (right != null && right.leftHeight() > right.rightHeight()) {
                //先右子树右旋转,再全体右旋转
                right.rightRotate();
            }
            leftRotate();
            //已经满足就没必要进行下一步
            return;
        }

        //判断是否需要右旋转调节高度
        if (leftHeight() - rightHeight() > 1) {
            //如果左子树的右子树的高度大于左子树的高度
            if (left != null && left.rightHeight() > left.leftHeight()) {
                //先左子树左旋转,再全体右旋转
                left.leftRotate();
            }
            rightRotate();
        }
    }
    //中序遍历
    public void indexOrder() {
        if (this.left != null) {
            this.left.indexOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.indexOrder();
        }
    }

    //查除删除结点
    public Node search(int value) {
        if (value == this.value) {
            return this;
        }else if(value < this.value) {
            if (this.left == null) {
                return null;
            }
            return this.left.search(value);
        }else {
             if (this.right == null) {
                 return null;
             }
             return this.right.search(value);
        }
    }

    //查出要删除结点的父节点
    public Node searchParent(int value) {
        //判断是父节点
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
            return this;
        }else if(value < this.value && this.left != null){
            return this.left.searchParent(value);
        }else if(value >= this.value && this.right != null) {
            return this.right.searchParent(value);
        }else {
            return null;
        }
    }

}

二、创建二叉树
package com.milearn.structrue.AVLtree;

public class AVLTree {
    private Node root;

    public Node getRoot() {
        return root;
    }

    //添加结点
    public void addNode(Node node) {
        if (root == null) {
            root = node;
        }else {
            root.add(node);
        }
    }
    //遍历
    public void indexOrder() {
        if (root != null) {
            root.indexOrder();
        }else {
            System.out.println("二叉树为空! ");
        }
    }

    //查找结点
    public Node search(int value) {
        if (root == null) {
            return null;
        }else {
            return root.search(value);
        }
    }

    //查找父节点
    public Node searchParent(int value){
        if (root == null) {
            return null;
        }else {
            return root.searchParent(value);
        }
    }

    //删除结点
    public void delNode(int value) {
        if (root == null) {
            return;
        }else {
            Node targetNode = search(value);
            if (targetNode == null) {
                return;
            }
            //没有父节点
            if(root.left == null && root.right == null) {
                root = null;
                return;
            }
            //查找要删除结点的父节点
            Node parent = searchParent(value);
            if (targetNode.left == null && targetNode.right == null) { //子节点
                //判断targetNode是左子节点还是右子节点
                if (parent.left != null && parent.left.value == value) { //左子节点
                    parent.left = null;
                } else if(parent.right != null && parent.right.value == value) { //右子节点
                    parent.right = null;
                }
            }else if (targetNode.left != null && targetNode.right != null) { //有两个子节点的结点
                //相当于覆盖掉要删除的结点
                int minValue = delRightTreeMin(targetNode.right);
                targetNode.value = minValue;
            }else {//中只有一个子节点
                //有左子节点
                if (targetNode.left != null) {
                    if(parent != null) {
                        //如果targetNode是parent的左子节点
                        if (targetNode.left.value == value) {
                            parent.left = targetNode.left;
                        }else { //targetNode是parent的右子节点
                            parent.right = targetNode.left;
                        }
                    }else {
                        root = targetNode.left;
                    }
                    //有右子节点
                }else {
                    if (parent != null) {
                        //如果targetNode是parent的左子节点
                        if (parent.left.value == value) {
                            parent.left = targetNode.right;
                        }else {//targetNode是parent的右子节点
                            parent.right = targetNode.right;
                        }
                    }else {
                        root = targetNode.right;
                    }
                }
            }
        }
    }

    //删除最小结点的值
    public int delRightTreeMin(Node node) {
        Node target = node;
        //找出最小结点的值
        while (target.left != null) {
            target = target.left;
        }
        //删除最小结点
        delNode(target.value);
        return target.value;
    }
}

三、测试
package com.milearn.structrue.AVLtree;

public class Main {
    public static void main(String[] args) {
        //int arr[] = {4,3,6,7,8};

        //int arr[] = {10,12,8,9,7,6};

        int arr[] = {10,11,7,6,8,9};

        //创建树
        AVLTree avlTree = new AVLTree();
        //赋值
        for (int i = 0; i < arr.length; i++) {
            avlTree.addNode(new Node(arr[i]));
        }
        //遍历
        System.out.println("中序遍历");
        avlTree.indexOrder();

        System.out.println("树的高度=:" + avlTree.getRoot().height());
        System.out.println("左子树的高度=:" + avlTree.getRoot().leftHeight());
        System.out.println("右子树的高度=:" + avlTree.getRoot().rightHeight());
        
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值