AVL树


public class AVL{
    public Node root = null;


    //===================================================
    //插入AVL树
    public void insert(int key){
        if(root == null){
            root = new Node(key,null);
            return;
        }

        Node cur = root;
        Node parent = null;
        while(cur!= null){
            if(key==(cur.key)){
                throw new RuntimeException("key重复了  "+key);
            }
            else if(key<cur.key){
                parent = cur;
                cur = cur.left;
            }else{
                parent = cur;
                cur = cur.right;
            }
        }

        //找到null的位置,插入
        if(key<parent.key){
            parent.left = new Node(key,parent);
            cur = parent.left;
        }else{
            parent.right = new Node(key,parent);
            cur = parent.right;
        }



        while(true){
            //更新parent的平衡因子
            if(cur ==parent.left){
                parent.bf++;
            }else{
                parent.bf--;
            }
            //分情况处理,处理结束全都是break
            if(parent.bf==0){
                break;
            }else if(parent.bf==2){
                //修复 左左或者左右

                if(cur.bf==1){
                    //左左
                   fixll(parent);
                }else{
                    //左右
                    fixlr(parent);
                }
                break;
            }else if(parent.bf==-2){
                //修复 右右 或 右左
                if(cur.bf==-1){
                    //右右
                    fixrr(parent);
                }else{
                    //右左
                    fixrl(parent);
                }
                break;
            }else if(parent ==root){
                //到根
                break;
            }

            //若未处理,需要继续向上蔓延
            cur = parent;
            parent = cur.parent;
        }


    }

  //=======================================================================

    //左旋
    private void leftRotate(Node parent){
        Node cur = parent.right;
        Node pp = parent.parent;
        Node cc = cur.left;

        //1.使cur的父为p父,使p父的子节点变成cur
        if(pp!=null){
            if(parent==pp.left){
                pp.left = cur;
                cur.parent = pp;
            }else{
                pp.right = cur;
                cur.parent = pp;
            }
        }else{
            root = cur;//将root更新为cur
            cur.parent =null;
        }

        //2.使cur左变成p右
        if(cc!=null){
            parent.right = cc;
            cc.parent = parent;
        }else{
            parent.right =null;
        }



        //3.使p的父为cur,使cur的子为p
        parent.parent = cur;
        cur.left = parent;


    }

    //===================================================
    private void rightRotate(Node parent){
        Node pp = parent.parent;
        Node cur = parent.left;
        Node cc = cur.right;


        //1.p、cur和 pp的父子关系
        if(pp!=null){
            if(parent==pp.left){
                pp.left = cur;
                cur.parent = pp;
            }else{
                pp.right = cur;
                cur.parent = pp;
            }
        }else{
            root = cur;
            cur.parent =null;
        }

        //2.cur 和p的父子关系
        cur.right = parent;
        parent.parent = cur;

        //3.cc和p的父子关系
        if(cc!=null){
            parent.left = cc;
            cc.parent = parent;
        }else{
            parent.left=null;
        }
    }
    //=====================右左==============================
    private void fixrl(Node parent) {
        Node node = parent;
        Node right = parent.right;
        Node left = right.left;

        rightRotate(right);
        leftRotate(parent);

        if(left.bf==1){
            node.bf =0;
            right.bf=-1;
            left.bf =0;
        }else if(left.bf==-1){
            node.bf =1;
            right.bf=0;
            left.bf =0;
        }else{
            node.bf =0;
            left.bf =0;
            right.bf=0;
        }
    }

    //====================左右===============================
    private void fixlr(Node parent) {
        Node node = parent;
        Node left = parent.left;
        Node right = left.right;

        leftRotate(left);
        rightRotate(parent);

        if(right.bf==1){
            node.bf =-1;
            left.bf =0;
            right.bf=0;
        }else if(right.bf==-1){
            node.bf =0;
            left.bf =1;
            right.bf=0;
        }else{
            node.bf =0;
            left.bf =0;
            right.bf=0;
        }
    }

    //=====================右右==============================
    private void fixrr(Node parent) {
        Node node = parent;
        Node right = parent.right;

        leftRotate(parent);
        node.bf = right.bf =0;
    }

    //======================左左=============================
    private void fixll(Node parent) {
        Node node = parent;
        Node left = parent.left;

        rightRotate(parent);
        node.bf = left.bf =0;
    }

    //===================================================
    //在AVL树中查找对应的key
    public boolean contains(int key){
        Node cur = root;
        while(cur!= null){
            if(key==(cur.key)){
                 return true;
            }
            else if(key<cur.key){
                cur = cur.left;
            }else{
                cur = cur.right;            }
        }
        return false;
    }


}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Node {
     int key;
    //平衡因子
     int bf;
     Node left;
     Node right;

    //记录父节点如果当前节点为根,则此为null
     Node parent ;

   public Node(int key, Node parent){
        this.key = key;
        this.bf = 0;
        this.left = null;
        this.right = null;
        this.parent = parent;
    }

}


class Main{
    public static void main(String[] args) {
        Random random = new Random();
        AVL树  tree = new AVL();
        for(int i = 0 ; i <20;i++){
            try{
                tree.insert(random.nextInt(100000));
            }catch (RuntimeException e){
                System.out.println(e.getMessage());
            }
        }
        verify(tree);

    }

//验证AVL树是否正确
    public static void verify(AVL树 tree){
        List<Integer> list = new ArrayList<>();
        中序遍历(tree.root,list);
        List<Integer> list1= new ArrayList<>(list);
        Collections.sort(list1);
        //中序遍历
        if(!list1.equals(list)){
            throw new RuntimeException("该树中序遍历无序");
        }else{
            System.out.println("1.中序遍历有序");
        }

      try{
          bf(tree.root);//平衡因子
      }catch (RuntimeException e){
          System.out.println("平衡因子算错");
      }
          System.out.println("2.平衡因子正确");



    }

    public static void 中序遍历(Node root,List<Integer> list){
        if(root==null) return;
        Node cur = root;
        中序遍历(cur.left,list);
        list.add(cur.key);
        中序遍历(cur.right,list);
    }

    //
    public static void bf(Node root){
        if(root==null){return;}
        int left = height(root.left);
        int right =height(root.right);
        //平衡因子不是1 -1 0 以及平衡因子不等于左右树高度之差
        //抛出异常
        if(left-right!=root.bf||root.bf>1||root.bf<-1){
            throw new RuntimeException();
        }
        bf(root.left);
        bf(root.right);
    }


    //获得树高度
    public static int height(Node root){
        if(root==null){
            return 0;
        }
       int a = height(root.right);
       int b =  height(root.left);
       return Math.max(a,b)+1;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值