平衡二叉树AVL

package com.atguigu.AVL;

public class avl {

    public static void main(String[] args) {
        int[] arr={10,11,7,6,8,9};
        BinarySortTree binarySortTree = new BinarySortTree();
        for (int i = 0; i <arr.length ; i++) {
            binarySortTree.add(new Node(arr[i]));

        }
        System.out.println("中序遍历");
        binarySortTree.infixorder();
        System.out.println(binarySortTree.getRoot().height());
        System.out.println(binarySortTree.getRoot().leftHeight());

    }
}
//创建二叉排序树
class BinarySortTree{
    private Node root;
    public Node getRoot(){
        return root;
    }
    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);
        }
    }
    //编写方法

    /**
     *
     * @param node 传入的结点(当作二叉排序树的跟结点)
     * @return:放回的是以node为根节点的二叉排序树的最小值
     */
    public int delRightTreeMin(Node node){
        Node target=node;
        //循环的查找左子节点,就会找到最小值
        while (target.left!=null){
            target=target.left;
        }
        //这时target就指向了最小值
        delNode(target.value);
        return target.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 node = searchParent(value);
            //如果删除的结点是叶子节点
            if (targetNode.left==null && targetNode.right==null){
                //判断targetParent是父节点的左子节点还是右子节点
                if (node.left!=null && node.left.value==value){
                    node.left=null;

                }else if (node.right!=null && node.right.value==value) {
                    node.right = null;
                }

            }else if (targetNode.left!=null && targetNode.right!=null){//删除有两颗子树结点
                int minvalue= delRightTreeMin(targetNode.right);
                targetNode.value=minvalue;

            }else {//删除只有一颗子树的结点
                //如果要删除的是结点的左子节点
                if (targetNode.left!=null){
                    if (node!=null) {
                        //如果targentNode是parent的做左子节点
                        if (node.left.value == value) {
                            node.left = targetNode.left;
                        } else {
                            node.right = targetNode.left;
                        }
                    }else {
                        root=targetNode.left;
                    }
                }else {//如果要删除的结点有右子节点
                    //如果targetNode是parent的做左子节点
                    if (node!=null) {
                        if (node.left.value == value) {
                            node.left = targetNode.right;
                        } else {                 //如果targetNode是parent的做右子节点
                            node.right = targetNode.right;
                        }
                    }else {
                        root=targetNode.right;
                    }
                }

            }

        }
    }
    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("当前二叉排序树为空");
        }
    }
}
//创建结点
class Node{
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }
    public int leftHeight(){
        if (left==null){
            return 0;
        }else {
            return left.height();
        }
    }
    //返回右子树高度
    public int rightheight(){
        if (right==null){
            return 0;
        }else {
            return right.height();
        }
    }
    //返回以该结点为根节点的树的高度
    public int height(){
        return Math.max(left==null ?0:left.height(), right==null ? 0:right.height())+1;
    }
    //左旋转方法
    public void leftRotate(){
        //创建一个新的结点,以根结点的值
        Node node = new Node(value);
        //把新的结点的左子树设置成当前结点的左子树
        node.left=left;
        //把当前结点的右子树设置成带你过去的右子树的左子树
        node.right=right.left;
        //把当前结点的值替换成右子节点的值
        value=right.value;

        //把当前结点的右子树设置成当前结点的右子树的右子树
        right=right.right;
        //把当前结点的左子节点设置成新的结点
        left=node;

    }
    //右旋转方法
    public void rightRotate(){
        Node newNode = new Node(value);

        newNode.right=right;
        newNode.left=left.right;
        value=left.value;
        left=left.left;
        right=newNode;

    }

    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;
            }
        }
    }
    public void add(Node no){
        if (no==null){
            return;
        }
        if (no.value<this.value){
            if (this.left==null){
                this.left=no;
            }else {
                this.left.add(no);
            }
        }else {
            if (this.right==null){
                this.right=no;
            }else {
                this.right.add(no);
            }
        }
        if ((rightheight()-leftHeight())>1){
            if (right!=null && right.rightheight()<right.leftHeight()){
                right.rightRotate();
                leftRotate();
            }else {
                leftRotate();
            }
            return;
        }
        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();
        }
    }

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值