# Listing binary tree numbers by level

• 06-16-2008
Nazgulled
Listing binary tree numbers by level
I know there is an algorithm called Breadth-first traversal but I think it's a bit complicated and I didn't want to mix it with queues.

I basically want to print the numbers in a binary tree level by level and I don't want to use a queue to store the values, just print them.

This is a problem that will be (for sure) on my girlfriend's exam but she doesn't understand much of programming in general and I'm trying to help her doing it, though I'm having difficulties making this work because it's been a while since I coded in C and I always had problems regarding pointers.

Could anyone help me with the code to the a function that lists the binary tree values level by level, without queues, just print them directly to the screen in the most easy way to understand?

I would really appreciate it...
• 06-16-2008
tabstop
I'm pretty sure you're going to have to deal with queues in the end. (You can pretend you're not using a queue by simulating it with an array or something, but it's still going to act like a queue.)
• 06-16-2008
Nazgulled
Why? I think that using queues would complicate a lot the exercise for the final exam where this question only weights 2 points in 20... There must be a simpler way to do it if it's all it's worth is 2 points... Otherwise, I don't think this question would be in the exam considering this is the first year and the class does not usually gets too complicated...
• 06-16-2008
tabstop
I would guess that it's worth 2 points out of 20 (which is still 10%, which at least in the US is a letter grade) is that if you're allowed to say "suppose I have a queue implementation" the algorithm takes less than a minute to write down. If you have to build the queue as well and don't have it around somewhere, it may take longer. (And for that matter, it's not that hard to simulate a queue with an array, if you're willing to have a fixed limit -- you update the index of the head and the tail as you insert and remove.)
• 06-16-2008
Nazgulled
Well, I don't know if I can suppose I have a queue implementation but I'm think there won't be one so I can't really use a queue because I would have to code it also and I'm almost sure that's not what the teacher wants and/or is asking for this exercise.

I could simulate it with an array as you say, but it would need to have a fixed limit and that will actually depend on the problem. I'm not sure if the question will display an input for the binary tree or if we should make a function that works for any tree...

Yes, I know, it's hard to help me when I don't even know exactly what do I have at my disposal for this problem...
• 06-16-2008
Nazgulled
I ended up with this:
Code:

```void printLevelOrderAux(Tree t, int level) {         if(t) {                 if(level == 1) {                         printf(" %d ", t->val);                 } else if (level > 1) {                         printLevelOrderAux(t->left, level-1);                         printLevelOrderAux(t->right, level-1);                 }         } } void printLevelOrder(Tree t) {         int i;         for(i = 1; i <= countSize(t); i++) {                 printLevelOrderAux(t, i);         } }```