1. ## Print Tree Preorder

I am trying to write a function to print a binary tree as graph. Preorder print is chosen and the code I come out is:

Code:
```void preorder_print(root*tree){
if (tree!=NULL){
printf("    %d\n",tree->key);
printf("\t");
preorder_print(tree->left);
printf("\n");
preorder_print(tree->right);
}
}```
I want the tree to look something like that.
5
├3
│├2
│└5
└7

└8

I know that I can use ASCII characters but
my problem is how to find the right tab to print each line (vertical tree graph would be fine).
1.any proposals on that.
2.any opinions about using the goto??

2. wow... this will get complicated if you have a lot of data in your tree! ...would it not be easier to code and to read using something like a table output? ...graphical outputs with ASCII chars all over the place look really messy and are really hard to follow! tabular outputs are simple and easy to map the exact whereabouts of any data! ...well... thats my opionion! ...I remember at uni one coursework was to write a digital circuit simulator in C, and it ended up easier to do printouts of the circuits in tabular format and say what type of gate each gate was and give the ID numbers of the parents for each gate!

well.. good luck!

3. I though about tabular outputs and if I had a choice I will do it. The problem is that I have to do with ASCII chars or similar chars. It is just an exercise. My problem is that I can not find how to count the tabs I must go right with each recursion and then back left in meet a node.
I do not want the code just an idea on how to implement it. I guess that the solution would be the same regardless the implementation table or ASCII.

4. ## Solution????????

OK I have thought of a solution. I post it here for evaluation and opinions. The idea is:
Convert the binary tree to an 2 dimmesnion array and each line will have the following info:

data arrayposition of the left child arrayposition of the right child

We will built the tree graph verticaly.
1 the root next line (with the goto)
├left child
|
| (how many depends of the position of the right child)
|(must be enough to fit all the node between left and right
|
L right child

Then move one space right and start the process again from rhe fisrt left child.

I will try to right a draft code for that later but any opinions on that????

5. I'd love to comment, but I have no clue what you're trying to say. Maybe the code will make it clearer.

6. The board has wiped out my answer and logged me out while I was answering - can this security measure be relaxed a bit?

7. ## Print tree as array

OK here is one solution that I come up with.

Code:
```void printArrayAsTree(ArrayNode a[], int n)
{
printf("%d(", a[n].data);
if (a[n].left > 0)
printArrayAsTree(a, a[n].left);
printf("|");
if (a[n].right > 0)
printArrayAsTree(a, a[n].right);
printf(")");
}```
Will this function print a tree as a tree in the screen like the tree in the beginning of the post??

8. New idea why to convert the tree to table an then print the table? Can you do it like this?
Code:
```void printTree (tree t)
{
if (t != NULL) {
printf("%d(", t->data);
printTree(t->left);
printf("|");
printTree(t->right);
printf(")");
}
}```

9. How to change the above function so that they will print the tree vertically and not in one line. The only think I can think is goto but any other suggestions.

10. Umm . . . use a loop?

11. Originally Posted by enisxisi
New idea why to convert the tree to table an then print the table? Can you do it like this?
Code:
```void printTree (tree t)
{
if (t != NULL) {
printf("%d(", t->data);
printTree(t->left);
printf("|");
printTree(t->right);
printf(")");
}
}```