简体   繁体   English

如何在Java中打印BinaryTree?

[英]How to print BinaryTree in Java?

public class BinaryNode<T> {

    protected T data;
    protected BinaryNode<T> left;
    protected BinaryNode<T> right;

    public BinaryNode(T element) {
        if (element == null)
            throw new IllegalArgumentException();
        this.data = element;
        left = null;
        right = null;
    }

    public int height() {
        int leftH = -1, rightH = -1;
        if (left != null)
            leftH = left.height();
        if (right != null)
            rightH = right.height();
        return Math.max(leftH, rightH) + 1;
    }

    public int size() {
        int leftS = 0, rightS = 0;
        if (left != null)
            leftS = left.size();
        if (right != null)
            rightS = right.size();
        return leftS + rightS + 1;
    }

    private String spaces(int count){
        String spaces="";
        while(count>0){
            spaces=spaces+"  ";
            count=count-1;
        }
        return spaces;
    }

    public String toString(){
        String str="";
        if(left!=null)
            str=str+spaces(left.height())+left.toString(); //left
        str=str+spaces(left.height()-1)+data.toString()+"\n";//root
        if(right!=null)
            str=str+spaces(right.height())+right.toString();//right
        return str;
    }
}

I need to build toString function in BinaryNode class. 我需要在BinaryNode类中构建toString函数。 The method works so that if we print the string it returns we will get one print line per vertex in the tree. 该方法有效,因此,如果我们打印字符串,则返回该字符串,我们将在树中每个顶点获得一条打印行。 In this row, 2*d spaces will appear, where d is the depth of the vertex in the tree and then the information on the vertex will be printed (in the same row). 在此行中,将出现2 * d个空格,其中d是树中顶点的深度,然后将打印有关顶点的信息(在同一行中)。 For example for the following BinarySearchTree (The examples in BinarySearchTree so it will be easier to understand how it needs to print): 例如,下面的BinarySearchTree(BinarySearchTree中的示例,因此将更易于理解其打印方式):

    BinarySearchTree t4 = new BinarySearchTree(c);
    t4.insert(8);
    t4.insert(7);
    t4.insert(6);
    t4.insert(5);
    t4.insert(4);
    t4.insert(3);
    t4.insert(2);
    t4.insert(1);
    System.out.println("----------t4:----------\n" + t4);

toString need to print: toString需要打印:

----------t4:----------

              1

            2

          3

        4

      5

    6

  7

8

I wrote above the code that I create but it's doesn't working, the problem is that I know why it doesn't working but I don't know how to fix it. 我在上面创建的代码上写了代码,但是它不起作用,问题是我知道为什么它不起作用,但是我不知道如何解决它。 Basically, I don't know to do it. 基本上,我不知道这样做。
Appreciate any help. 感谢任何帮助。

Got the solution for those who need it: 为需要它的人提供了解决方案:

private String spaces(int count){
    String spaces="";
    while(count>0){
        spaces=spaces+"  ";
        count=count-1;
    }
    return spaces;
}

private String toString(int depth){
    String str="";
    if(left!=null)
    {
        str=str+left.toString(depth+1);
    }
    str=str+spaces(depth)+data.toString()+"\n";
    if(right!=null)
    {
        str=str+right.toString(depth+1);
    }
    return str;
}

private String toString(String str){
    if(left!=null)
        str=str+left.toString("  ");
    str=str+data.toString()+"\n";
    if(right!=null)
        str=str+right.toString("  ");
    return str;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM