I have to print a binary tree level by level like this:

          (5,g)
          /   \
      (3,f)   (6,n)
      /  \
  (1,k)   (2,c)

and it has to print

-(6,n)
(5,g)
--(2,c)
-(3,f)
--(1,k)

The hyphens being the level the node is in.

The part about printing it I think I have already figured out:

void print_tree (binario_t tree) {
    while (tree != NULL) {
       print_tree (tree -> right);
       printf (...);
       print_tree (tree -> left);
   }
}

I can't figure out how to count the levels and print the hyphens.

These are the definitions used for the tree:

struct rep_info {
  int num;
  char *frase; 
};
struct rep_binario {
    info_t dato;
    rep_binario *left;
    rep_binario *right;
}; 

0 Answers