BiTree显示Test.java

package A类有价值的回顾的;

public class BiTreeTest {

    public static void main(String[] args){


        BiTree root = new BiTree(1);
        root.add(new BiTree(2));
        root.add(new BiTree(27));
        root.add(new BiTree(4));
        root.add(new BiTree(55));
        root.add(new BiTree(6));
        root.add(new BiTree(7));
        root.add(new BiTree(85));
        root.add(new BiTree(9));

        //root.mid_trav();
        root.show();


    }

}


class BiTree {//二叉树本来就是递归定义的,所以用递归来做非常方便
    private int v;
    private BiTree l;
    private BiTree r;

    public BiTree(int x){
        v = x;
    }
    public void add(BiTree b){
        if(b.v < v){
            if(l == null) l = b;//如果能插就插进去,不能插再递归
            else l.add(b);
        }
        else{
            if(r == null) r = b;
            else r.add(b);
        }
    }
    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);


    }

    public int getHeigth(){
        int h=2;
        int hl= l==null?0:l.getHeigth();
        int hr= r==null?0:r.getHeigth();

        return h+Math.max(hl, hr);
    }
    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[getHeigth()][getWith()];
        printInBuf(buf, 0, 0);
        showBuf(buf);

    }

}











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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值