title: Java Printing a Binary Tree using Level-Order in a Specific Format
body: <p>Okay, I have read through all the other related questions and cannot find one that helps with java.  I get the general idea from deciphering what i can in other languages; but i am yet to figure it out.</p>
  
  <p>Problem: I would like to level sort (which i have working using recursion) and print it out in the general shape of a tree.</p>
  
  <p>So say i have this:</p>
  
  <pre><code>    1 
     / \
    2   3
   /   / \
  4   5   6
  </code></pre>
  
  <p>My code prints out the level order like this:</p>
  
  <pre><code>1 2 3 4 5 6
  </code></pre>
  
  <p>I want to print it out like this:</p>
  
  <pre><code>1
  2 3
  4 5 6
  </code></pre>
  
  <p>Now before you give me a moral speech about doing my work... I have already finished my AP Comp Sci project and got curious about this when my teacher mentioned the Breadth First Search thing.</p>
  
  <p>I don't know if it will help, but here is my code so far:</p>
  
  <pre><code>/**
    * Calls the levelOrder helper method and prints out in levelOrder.
    */
   public void levelOrder()
   {
    q = new QueueList();
    treeHeight = height();
    levelOrder(myRoot, q, myLevel);
   }
  
   /**
    * Helper method that uses recursion to print out the tree in 
    * levelOrder
    */
   private void levelOrder(TreeNode root, QueueList q, int curLev)
   {
    System.out.print(curLev);
    if(root == null)
    {
     return;
    }
  
    if(q.isEmpty())
    {
     System.out.println(root.getValue());
    }
    else
    {
     System.out.print((String)q.dequeue()+", ");
    }
  
    if(root.getLeft() != null)
    {
     q.enqueue(root.getLeft().getValue());
     System.out.println();
    }
    if(root.getRight() != null)
    {
     q.enqueue(root.getRight().getValue());
     System.out.println();
     curLev++;
    }
  
    levelOrder(root.getLeft(),q, curLev);
    levelOrder(root.getRight(),q, curLev);
   }
  </code></pre>
  
  <p>From what i can figure out, i will need to use the total height of the tree, and use a level counter... Only problem is my level counter keeps counting when my levelOrder uses recursion to go back through the tree.</p>
  
  <p>Sorry if this is to much, but some tips would be nice. :)</p>