AVLTreeTest.java

来源:互联网 发布:楼梯踏步高度算法 编辑:程序博客网 时间:2024/06/05 02:39
package A类有价值的回顾的;public class AVLTreeTest {public static void main(String[] args){    AVLTree root = new AVLTree(1);        root = root.add(new AVLTree(2));        root = root.add(new AVLTree(4));        root = root.add(new AVLTree(32));        root = root.add(new AVLTree(5));        root = root.add(new AVLTree(6));        root = root.add(new AVLTree(7));        root = root.add(new AVLTree(8));        root = root.add(new AVLTree(9));        root = root.add(new AVLTree(0));        root = root.add(new AVLTree(14));        root = root.add(new AVLTree(13));        root = root.add(new AVLTree(15));        root = root.add(new AVLTree(16));        root = root.add(new AVLTree(17));        root = root.add(new AVLTree(18));        root = root.add(new AVLTree(19));        root = root.add(new AVLTree(22));        root = root.add(new AVLTree(24));        root = root.add(new AVLTree(23));        root = root.add(new AVLTree(5));        root = root.add(new AVLTree(26));        root = root.add(new AVLTree(27));        root = root.add(new AVLTree(28));        root = root.add(new AVLTree(29));        //root.mid_trav();        root.show();}}class AVLTree{    private int v;    private AVLTree l;    private AVLTree r;    private int balance = 0;    public AVLTree(int v){        this.v = v;    }    public int getHeight(){        int h=2;//数字和|分别占据两行        int hl= l==null?0:l.getHeight();        int hr= r==null?0:r.getHeight();        return h+Math.max(hl, hr);    }    private void calcu_balance(){        int hl = l==null?0:l.getHeight();        int hr = r==null?0:r.getHeight();        balance = hl-hr;    }    public int getBalance(){        return balance;    }                    //LL类型的向R转    private AVLTree adjustLL(){//当前的this是A,对A进行调整        AVLTree root = l;        l = root.r;//把根B的右边让出来        root.r = this;        return root;    }    private AVLTree adjustRR(){        AVLTree root = r;        r = root.l;        root.l = this;        return root;    }    private AVLTree adjustLR(){        l = l.adjustRR();        return adjustLL();    }    private AVLTree adjustRL(){        r = r.adjustLL();        return adjustRR();    }    public AVLTree add(AVLTree the){        AVLTree root = this;        if(the.v<v){            if(l==null) l=the;            else l = l.add(the);//        }        else{            if(r==null) r=the;            else r=r.add(the);        }        calcu_balance();        if(balance>2){//数字和|分别占据两行,其实是一个节点,只不过与上面的显示结合起来            if(l.getBalance()>0)//需要判断两层,到底是左子树的左子树偏了还是左子树的右子树,调整的时候兼顾三层                root = adjustLL();            else                root = adjustLR();        }        if(balance<-2){            if(r.getBalance()<0)                root = adjustRR();            else                root = adjustRL();        }        calcu_balance();        return root;    }    public void mid_trav(){//递归类型的中序遍历,还有前序遍历        if(l != null) l.mid_trav();        System.out.println(v);        if(r != null) r.mid_trav();    }    private int getWith(){        int w = (" "+v).length();        if(l!=null) w += l.getWith();        if(r!=null) w += r.getWith();        return w;    }    private int getRootPos(int x){        return (l==null) ? x:x+l.getWith();    }                                        //x,y相当于   /--3----\ 起点所在的横纵坐标        private void printInBuf(char[][] buf,int x,int y){        String sv = " "+ v;                  int p1 = (l==null)? x :l.getRootPos(x);          int p2 = getRootPos(x);        int p3 = (r==null)? p2:r.getRootPos(p2+sv.length());        buf[y][p2] = '|';        for(int i= p1;i<p3;i++) buf[y+1][i] = '-';        for(int i=0;i<sv.length();i++) buf[y+1][p2+i] = sv.charAt(i);                                  //用数字覆盖掉'-'        if(p1<p2) buf[y+1][p1] ='/';        if(p3>p2) buf[y+1][p3] ='\\';//必须要转义        if(l!=null) l.printInBuf(buf, x, y+2);//再从y+2往下递归        if(r!=null) r.printInBuf(buf, p2+sv.length(), y+2);    }    private void showBuf(char[][] x){        for(int i=0;i<x.length;i++){            for(int j=0;j<x[i].length;j++){                System.out.print(x[i][j]==0?' ':x[i][j]);                                //char ch='0'; ch的值是字符'0'的ascii码值,即0x30                                //char未初始化的话赋值为0            }            System.out.println();        }    }    public void show(){        char[][] buf = new char[getHeight()][getWith()];        printInBuf(buf, 0, 0);        showBuf(buf);    }}