I'm planning on writing a program that will display a binary tree of a small height like so:
Code:
5
/ \
10 11
/ \ / \
3 4 5 1
/ \ / \ / \ / \
2 6 1 9 6 8 7 6
The best way I could think of was to place the root in a queue, then loop until the queue is empty placing the left and right children at the end of the queue and then printing and popping the front. Something like:
Code:
queue.push(root);
while (!queue.empty());
{
queue.push(front.left);
queue.push(front.right);
print front;
pop front;
}
The problem with this idea though is that it doesn't account for empty spots, so would mess up with something like:
Code:
5
/ \
10 11
/ \
3 1
Any ideas how to get around this? My brain is kinda fried at the moment unfortunately...