2

I want to know the code to print a binary tree level by level, I mean, if I have this tree:

    5
   / \
  3   2
 /     \
4       6

I want to print it like: 5 3 2 4 6.

I know I need to do the tree depth method and I already did it, but I don't know what else to do.

8
  • Use a queue to enqueue the child node and pop out the next node to print. Commented Jul 2, 2012 at 2:49
  • @nhahtdh I can't use any other data structure rather than the binary tree Commented Jul 2, 2012 at 2:55
  • @alfasin No, I don't know what is BFS, how can I use that in this method? Commented Jul 2, 2012 at 2:56
  • If you'll google it you'll find thousands of implementations, for example: divms.uiowa.edu/~sriram/21/fall05/ExamplePrograms/ReaderFiles/… you can read more about BFS here: en.wikipedia.org/wiki/Breadth-first_search Commented Jul 2, 2012 at 2:57
  • @nhahtdh EDIT: it's for homework Commented Jul 2, 2012 at 2:59

3 Answers 3

3

You can use the level traversal algorithm to print them.

The algorithm works as follows:

queue := < root >

while queue is not empty

 v := queue.front

 print v

 foreach s : s is a son of v

    queue.enqueue(s)

 queue.dequeue
Sign up to request clarification or add additional context in comments.

6 Comments

and by the way, push and pop are methods of stuck, not queue. For queue it's usually enqueue and dequeue`
@JohnnyDahdah why not? A queue is so easy to implement. There only need an array with two variables recording the head and the tail.
@KunHuang: I know but the teacher said "You cannot use any data structure rather than the binary tree you have"
@alfasin Thanks for your remind. I usually use C++, so I mix up the queue in STL and in common algorithm representation. But you also has a mistake: stack is spelt as stuck. :-)
@JohnnyDahdah eh, you can add a pointer field in every nodes of the binary tree to link the nodes as the output order.
|
1

Okay, I think I figured it out:
1. extend the class Node and add a property called height (int)
2. Calculate the height of each node of the tree (easy recursive function - no data-structure is needed)
3. use a for-loop, and run in-order traversal, for each height (level) and print the nodes of that level

Comments

0
public void displayByLevel(){
    ArrayList<Node> ar = new ArrayList<>();
    ar.add(root);
    displayByLevelHelper(ar);
}
private void displayByLevelHelper(ArrayList<Node> ar){
    if(ar.isEmpty()){
        return;
    }
    ArrayList<Node> nextAr= new ArrayList<>();
    for(Node n:ar){
        System.out.print(n.data + " ");
        if(n.left!=null){
            nextAr.add(n.left);
        }
        if(n.right!=null){
            nextAr.add(n.right);
        }
    }
    System.out.println();
    displayByLevelHelper(nextAr);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.