Search code examples
javaalgorithmcomputer-science

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. 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). For example for the following BinarySearchTree (The examples in BinarySearchTree so it will be easier to understand how it needs to print):

    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:

----------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.


Solution

  • 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;
    }