views:

797

answers:

1

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.

Problem: I would like to level sort (which i have working using recursion) and print it out in the general shape of a tree.

So say i have this:

    1 
   / \
  2   3
 /   / \
4   5   6

My code prints out the level order like this:

1 2 3 4 5 6

I want to print it out like this:

1
2 3
4 5 6

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.

I don't know if it will help, but here is my code so far:

/**
  * 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);
 }

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.

Sorry if this is to much, but some tips would be nice. :)

A: 

Here is how I would do it:

levelOrder(List<TreeNode> n) {
    List<TreeNode> next = new List<TreeNode>();
    foreach(TreeNode t : n) {
        print(t);
        next.Add(t.left);
        next.Add(t.right);
    }
    println();
    levelOrder(next);
}

(Was originally going to be real code - got bored partway through, so it's psueodocodey)

Anon.
I don't think that will work with recursion though.
JavaFail
Looks good to me. +1
danben
but is a list First in First out??? I thought that was only a queue?
JavaFail
`Add` appends to the end of the list. `foreach` starts taking from the beginning. Thus we get the items in order.
Anon.
so it would print the example out like this:123456???
JavaFail
but on different lines?
JavaFail
What's stopping you trying it and seeing for yourself?
Anon.
nothing, i tried a list and am fixing the errors as we speak :P
JavaFail
it prints out what i currently have printing out :| nothing new
JavaFail
So, what is it printing out?
Anon.
it prints it like: 1 2 34 56
JavaFail
Are you sure you implemented it correctly?
Anon.
i think so... what does this mean: ? 0 : It is in one of my return statements, my teacher told me about it, but i dont understand what it does. heres is the whole thing: return (node.getLeft() == null ? 0 : numberOfNodesAtLevel(node.getLeft(), level-1)) what does the '? 0 :' part do???
JavaFail
Your code doesn't appear to be anything like what I have suggested. Why ask for my help if you're going to ignore what I say?
Anon.
after a couple more tries i got it, it prints out level by level, each level on a different line :) thanks for the help
JavaFail
ps, this only works for up to 3 levels... :|
JavaFail
No, there's nothing preventing the algorithm working for any arbitrary depth. If it's not working for you, I would suspect you've implemented it incorrectly.
Anon.
Then i must have done that; unfortunately it would have been useful for debugging my current assignment. And since this was extra work i chose to do, my teacher won't help me.
JavaFail
What is the code as you've implemented it so far?
Anon.
Your code shouldn't modify a collection while enumerating through it. This will throw an InvalidOperationException.
Eric Hauser