Printing "pretty" Binary tree?

I have a Binary tree, with "x" elements, i just want to print this tree in a "visual" mode (in the console of course), its my homework, but i don't know how to start.
Any idea to create this?

My node:
1
2
3
4
5
6
7
8
  class nodo
{
    public:
        char caracter;
        int frecuencia;
        nodo *der;
        nodo *izq;
};


Last edited on
Have you had a "print a pyramid" homework? Where result is:
   *
  ***
 *****
*******

Could printing a tree be similar?

If you would know how deep your tree is, then you could compute how many lines to print and how much indentation the previous levels need. There are some twists though.
Consider thinking about your tree as a linked structure; something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

(root
 (a
  (c
   (...)
   (...))
  (d
   (...)
   (...)))
 (b
  (e
   (...)
   (...))
  (f
   (...)
   (...))))



Just build the entire string by walking through the tree. The base case occurs for a child when that child is null. You can indent it nicely by adding spaces corresponding to the depth of the node.
Topic archived. No new replies allowed.