平衡二叉树的创建和插入

平衡二叉树的创建和插入 
import java.util.Random;

public class BalancedBiSearchTree {
   private BTreeNode root;

    public BTreeNode getRoot() {
    return root;
   }

    public void setRoot(BTreeNode root) {
    this.root = root;
   }
    public void creatAVL(int[] arr)
    {
        for(int i=0;i<arr.length;i++)
        {
            this.insertAVL(arr[i]);
        }
    }
    public void insertAVL(int target)
    {   
        this.insertAVL(root,target,null);
    }
    public static boolean taller=false;
    public int insertAVL(BTreeNode tempNode,int target,BTreeNode preNode)
    {
        if(tempNode==null)
        {   
            tempNode=new BTreeNode(); tempNode.setValue(target);
            tempNode.setBF(0); taller=true; 
            if(preNode==null) this.setRoot(tempNode);
            else {
                if(preNode.getValue()>target) preNode.setLeftchild(tempNode);
                else preNode.setRightchild(tempNode);
            }
        }
        else {
            if(tempNode.getValue()==target) {taller=false; return 0;}
            if(tempNode.getValue()>target)
            {   
                if(this.insertAVL(tempNode.getLeftchild(), target,tempNode)==0) return 0;
                if(taller)
                switch(tempNode.getBF())
                {
                 case 1:
                     leftBalance(tempNode); taller=false; break;
                 case 0:
                     tempNode.setBF(1);taller=true;break;
                 case -1:
                     tempNode.setBF(0);taller=false;break;
                }
            }
            else {
                if(this.insertAVL(tempNode.getRightchild(), target,tempNode)==0) return 0;
                if(taller)
                switch(tempNode.getBF())
                {
                case 1:
                    tempNode.setBF(0);taller=false;break;
                case 0:
                    tempNode.setBF(-1);taller=true;break;
                case -1:
                    rightBalance(tempNode); taller=false;break;
                }
            }
        }
        return 1;
    }
    public void leftBalance(BTreeNode tempNode)
    {   
        this.searchParent(root, tempNode, null);
        BTreeNode preNode=this.preNode;
        this.preNode=null;
        BTreeNode lc=tempNode.getLeftchild();
        switch(lc.getBF())
        {
        //左左旋转
         case   1:
             tempNode.setBF(0);lc.setBF(0);
             this.R_Rotate(tempNode,preNode);break;
         //右左旋转
         case  -1:
             BTreeNode rd=lc.getRightchild();
             switch(rd.getBF())
             {
               case 1:tempNode.setBF(-1);lc.setBF(0);break;
               case 0:tempNode.setBF(0);lc.setBF(0);break;
               case -1:tempNode.setBF(0);lc.setBF(1);break;
             }
             rd.setBF(0);
             this.L_Rotate(lc,tempNode);
             this.R_Rotate(tempNode,preNode);
        }
    }
    public void rightBalance(BTreeNode tempNode)
    {   
        this.searchParent(root, tempNode, null);
        BTreeNode preNode=this.preNode;
        this.preNode=null;
        BTreeNode rc=tempNode.getRightchild();
        switch(rc.getBF())
        {
        //右右旋转
         case -1:tempNode.setBF(0);rc.setBF(0);
               this.L_Rotate(tempNode,preNode);break;
         //左右旋转
         case 1:
               BTreeNode ld=rc.getLeftchild();
               switch(ld.getBF())
               {
               case 1:tempNode.setBF(0);rc.setBF(-1);break;
               case 0:tempNode.setBF(0);rc.setBF(0);break;
               case -1:tempNode.setBF(1);rc.setBF(0);break;
               }
               ld.setBF(0);
               this.R_Rotate(rc,tempNode);
               this.L_Rotate(tempNode,preNode);
        }
    }
    //右旋转
    public void R_Rotate(BTreeNode tempNode,BTreeNode preNode)
    {   
        BTreeNode lc=tempNode.getLeftchild();
        tempNode.setLeftchild(lc.getRightchild());
        lc.setRightchild(tempNode);
        tempNode=lc;
        if(preNode!=null)
        {
            if(preNode.getValue()<tempNode.getValue()) preNode.setRightchild(tempNode);
            else preNode.setLeftchild(tempNode);
        }
        else this.setRoot(tempNode);
    }
    //左旋转
    public void L_Rotate(BTreeNode tempNode,BTreeNode preNode)
    {
        BTreeNode rc=tempNode.getRightchild();
        tempNode.setRightchild(rc.getLeftchild());
        rc.setLeftchild(tempNode);
        tempNode=rc;
        if(preNode!=null)
        {
            if(preNode.getValue()<tempNode.getValue()) preNode.setRightchild(tempNode);
            else preNode.setLeftchild(tempNode);
        }
        else this.setRoot(tempNode);
    }
    public static BTreeNode preNode=null;
    public void searchParent(BTreeNode tempNode,BTreeNode targetNode,BTreeNode preNode)
    {
        if(tempNode!=null)
        {   
            if(tempNode.equals(targetNode))     
                this.preNode=preNode;
            else {
                this.searchParent(tempNode.getLeftchild(), targetNode, tempNode);
                this.searchParent(tempNode.getRightchild(), targetNode, tempNode);
            }

        }
    }
    //中序遍历
    public void inOrderVisitBST()
    {   
        this.inOrderVisitBST(root);
        System.out.println();
    }
    private void inOrderVisitBST(BTreeNode tempNode)
    {   

        if(tempNode!=null)
        {   
            //System.out.println("111");
            this.inOrderVisitBST(tempNode.getLeftchild());
            System.out.print(tempNode.getValue()+" ");          
            this.inOrderVisitBST(tempNode.getRightchild()); 
        }   
    }
    //先序遍历
    public void preVisitBBST()
    {
        this.preVisitBBST(root);
        System.out.println();
    }
    public void preVisitBBST(BTreeNode tempNode)
    {
        if(tempNode!=null)
        {
            System.out.print(tempNode.getValue()+" ");
            this.preVisitBBST(tempNode.getLeftchild());
            this.preVisitBBST(tempNode.getRightchild());
        }
    }
    public static void main(String[] args)
   {
       int[] arr=new int[10];
       for(int i=0;i<10;i++)
       {
           arr[i]=new Random().nextInt(100);
           System.out.println("arr["+i+"]="+arr[i]);
       }
       BalancedBiSearchTree bbst=new BalancedBiSearchTree();
       bbst.creatAVL(arr);
       bbst.inOrderVisitBST();
       bbst.preVisitBBST();
   }
}

class BTreeNode
{
    private int value;
    private BTreeNode leftchild,rightchild;
    private int BF;
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public BTreeNode getLeftchild() {
        return leftchild;
    }
    public void setLeftchild(BTreeNode leftchild) {
        this.leftchild = leftchild;
    }
    public BTreeNode getRightchild() {
        return rightchild;
    }
    public void setRightchild(BTreeNode rightchild) {
        this.rightchild = rightchild;
    }
    public int getBF() {
        return BF;
    }
    public void setBF(int bF) {
        BF = bF;
    }   
}
    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/qq_36368627/article/details/82430276
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞