avl树(leetcode每日打卡)

2021/10/23 23:09:46

本文主要是介绍avl树(leetcode每日打卡),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

public class AVLTreeDemo {
    public static void main(String[] args) {
//        int []arr=new int[]{4,3,6,5,7,8};
        int []arr=new int[]{10,12,8,9,7,6};
        AVLTree avlTree = new AVLTree();
        for (int i = 0; i < arr.length; i++) {
            avlTree.add(new Node(arr[i]));
        }
        int height = avlTree.getRoot().height();
        int rightHeight = avlTree.getRoot().rightHeight();
        int leftHeight = avlTree.getRoot().leftHeight();
        System.out.println(height);
        System.out.println(rightHeight);
        System.out.println(leftHeight);

    }

}
class AVLTree{
   Node root;
//
    public Node getRoot() {
        return root;
    }

    public void add(Node node){
        if (this.root==null){
            root=node;
        }else {
            this.root.add(node);
        }
    }
    public void fixOrders(){
        if (this.root==null){
            System.out.println("二叉树为空");
        }else {
            this.root.fixOrder();
        }
    }
    public Node searchNode(int value){
        if (root!=null){
            return  root.searchNode(value);
        }else{
            System.out.println("二叉树为空");
        }
        return null;
    }
    public Node searchParent(int value){
        if (root==null){
            System.out.println("二叉树为空");
        }else {
            return root.seacherParent(value);
        }
        return null;
    }
    public int deleteRight(Node node){
        Node target=node;
        while (target.left!=null){
            target=target.left;
        }
        deleteNode(target.value);
        return target.value;
    }
    public void deleteNode(int value){
        if (root==null){
            return;
        }else {
           Node targetNode = searchNode(value);
            if (targetNode==null){
                return;
            }
            if (root.left==null&&root.right==null){
                root=null;
                return;
            }else {
           Node parentNode = searchParent(value);
                if (targetNode.left==null&&targetNode.right==null){
                    if (parentNode.left!=null&&parentNode.left.value==value){
                        parentNode.left=null;
                    }else  if (parentNode.right!=null&&parentNode.right.value==value){
                        parentNode.right=null;
                    }
                }else if(targetNode.left!=null&&targetNode.right!=null){//删除有两个子点的节点
                    //最小节点与要删除的节点
                    int minValue = deleteRight(targetNode.right);//也可以从目标节点左子树找最小的
                    targetNode.value=minValue;

                }else {//删除只有一颗子节点的节点
                    //如果要删除的节点有左子节点
                    if (targetNode.left!=null){
                        if (parentNode!=null){
                            if (parentNode.left.value==value){
                                parentNode.left=targetNode.left;
                            }else {
                                parentNode.right=targetNode.left;
                            }
                        }else {
                            root=targetNode.left;
                        }

                    }else {
                        if (parentNode!=null){
                            if (parentNode.left.value==value){   //如果要删除的节点有右子节点
                                parentNode.left=targetNode.right;
                            }else {
                                parentNode.right=targetNode.right;
                            }
                        }else {
                            root=targetNode.right;

                        }


                    }
                }

            }
        }
    }
}

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

    public Node(int value) {
        this.value = value;
    }
    public int height(){
        return Math.max(left==null ? 0 : left.height(),right==null ? 0 :right.height())+1;
    }
    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 void leftRotate(){
        Node newNode = new Node(this.value);//新建一个节点
        newNode.left=this.left;//新建节点的左子节点为当前节点的左节点
        newNode.right=this.right.left;//新建节点的右子节点为当前节点的右子节点
        //以上相当于构建了一颗新的二叉树
        //以下则是将其连上旧的二叉树

        this.value=right.value;//用当前节点的右节点替换当前节点的值
        this.right=this.right.right;//把当前节点的右子树往上提一下
        this.left=newNode;//把当前节点左树连上新节点的树
    }
    public void rightRotate(){
        Node newNode = new Node(this.value);
        newNode.right=this.right;
        newNode.left=this.left.right;
        this.value=left.value;
        this.left=this.left.left;
        this.right=newNode;
    }
    public Node searchNode(int value){
        if (this.value==value){
            return this;
        }
        else if (value<this.value){
            if (this.left!=null){
                return   this.left.searchNode(value);
            }
        }else {
            if (this.right!=null){
                return    this.right.searchNode(value);
            }
        }
        return null;
    }
    public Node seacherParent(int value){
        //如果当前节点是要删除的节点的父节点,就返回
        if (this.left!=null&&this.left.value==value||this.right!=null&&this.right.value==value){
            return this;
        }if (this.right!=null&&value>this.value){
            return this.right.seacherParent(value);
        }if (this.left!=null&&value<=this.value){//要查找的节点小于当前节点往左子树递归查找
            return   this.left.seacherParent(value);
        }
        return null;
    }

    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(rightHeight()-leftHeight()>1){
            if (this.right.leftHeight()>this.right.rightHeight()){
               this.right.rightRotate();
               leftRotate();
            }else {
                leftRotate();
            }
        }else if (leftHeight()-rightHeight()>1){
            if (this.left.rightHeight()>this.left.leftHeight()){
                this.left.leftRotate();
                rightRotate();
            }else {
                rightRotate();

            }
        }
    }
    public void fixOrder(){//中序遍历
        if (this.left!=null){
            this.left.fixOrder();
        }
        System.out.println(this);
        if (this.right!=null){
            this.right.fixOrder();
        }
    }
    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
}



这篇关于avl树(leetcode每日打卡)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程