平衡二叉树

package Myjava;

public class AVLtree {
    class AVLtreeNode{
        private int key;
        private AVLtreeNode left;
        private AVLtreeNode right;
        private int hight;
        public AVLtreeNode(int key,AVLtreeNode left,AVLtreeNode right){
            this.key=key;
            this.left=left;
            this.right=right;
            this.hight=0;
        }

    }
    private AVLtreeNode root;//根节点
    public AVLtree(){
        root=null;
    }

    private int getHight(AVLtreeNode tree){//返回树的高度
        if(tree!=null){
            return tree.hight;
        }
        return 0;
    }
    public int getHight(){
        return getHight(root);
    }

     public int max(int a,int b){
        return a>b?a:b;
     }
    /*
     * LL:左左对应的情况(左单旋转)。
     *        2
     *     A                     B
     *    /  1                 /   \
     *   B   -->>LL--->>      X     A
     *  /  0
     * X
     * 返回值:旋转后的根节点
     * (插入的节点是左子树的左边节点)
     */
     private AVLtreeNode llRotation(AVLtreeNode a){
        AVLtreeNode b=null;

        b=a.left;
        a.left=b.right;
        b.right=a;

        a.hight=max(getHight(a.left),getHight(a.right))+1;
        b.hight=max(getHight(b.left),a.hight)+1;

        return b;
     }
    /*
     * RR:右右对应的情况(右单旋转)。
     *
     *      -2                              0
     *     A                               B
     *      \ -1                         /0  \0
     *       B         -->>RR--->>      A     X
     *        \ 0
     *         X
     *
     *(插入节点是右子树的右边节点)
     * 返回值:旋转后的根节点
     */
     private AVLtreeNode rrRotation(AVLtreeNode a){
         AVLtreeNode b=null;

         b=a.right;
         a.right=b.left;
         b.left=a;

         a.hight=max(getHight(a.left),getHight(a.right))+1;
         b.hight=max(getHight(b.right),a.hight)+1;

         return b;
     }

     private AVLtreeNode lrROtation(AVLtreeNode a){
         return this.llRotation(this.rrRotation(a.left));
     }

     private AVLtreeNode rlRotation(AVLtreeNode a){
         return this.rrRotation(this.llRotation(a.right));
     }

     public void insert(int key){
         root=insert(root,key);
     }
     private AVLtreeNode insert(AVLtreeNode tree,int key){
         if(tree==null){
             tree=new AVLtreeNode(key,null,null);
         }else{
             int cmp=key-tree.key;//新加节点的值与原节点的值进行比较
             if(cmp<0){
                 tree.left=insert(tree.left,key);
                 if(getHight(tree.left)-getHight(tree.right)==2){
                     if(key-tree.left.key<0){//LL
                         tree=llRotation(tree);
                     }else{//LR
                         tree=lrROtation(tree);
                     }
                 }
             }else if(cmp>0){
                 tree.right=insert(tree.right,key);
                 if(getHight(tree.right)-getHight(tree.left)==2){
                     if(key-tree.right.key>0){//RR
                         tree=rrRotation(tree);
                     }else{//RL
                         tree=rlRotation(tree);
                     }
                 }
             }else {
                 System.out.println("新加节点与原节点相同!");
             }
         }
         tree.hight=max(getHight(tree.left),getHight(tree.right))+1;
         return tree;
     }
     private void preOrder(AVLtreeNode tree){
         if(tree!=null){
             System.out.print(tree.key+" ");
             preOrder(tree.left);
             preOrder(tree.right);
         }

     }
     public void preOrder(){//先序遍历
         preOrder(root);
     }
     private void inOrder(AVLtreeNode tree){
         if(tree!=null){
             inOrder(tree.left);
             System.out.print(tree.key+" ");
             inOrder(tree.right);
         }
     }
     public void inOrder(){//中序遍历
         inOrder(root);
     }
     private void postOrder(AVLtreeNode tree){
         if(tree!=null){
             postOrder(tree.left);
             postOrder(tree.right);
             System.out.print(tree.key+" ");
         }
     }
     public void postOrder(){//后序遍历
         postOrder(root);
     }
    private void print(AVLtreeNode tree, int key, int direction) {
        if(tree != null) {
            if(direction==0)    // tree是根节点
                System.out.printf("%2d is root\n", tree.key, key);
            else                // tree是分支节点
                System.out.printf("%2d is %2d's %6s child\n", tree.key, key, direction==1?"right" : "left");

            print(tree.left, tree.key, -1);
            print(tree.right,tree.key,  1);
        }
    }

    public void print() {
        if (root != null)
            print(root, root.key, 0);
    }

    public static void main(String[] args) {
        int i;
        AVLtree tree = new AVLtree();
        int arr[]= {3,2,1,4,5,6,0,10,9,8};
        System.out.printf("== 依次添加: ");
        for(i=0; i<arr.length; i++) {

            tree.insert(arr[i]);
            System.out.printf("%d ", arr[i]);
        }
        System.out.printf("\n== 前序遍历: ");
        tree.preOrder();
        System.out.println();

        System.out.printf("\n== 中序遍历: ");
        tree.inOrder();
        System.out.println();

        System.out.printf("\n== 后序遍历: ");
        tree.postOrder();
        System.out.println();
        tree.print();
        System.out.printf("高度: %d\n", tree.getHight());
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dream答案

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值