# Thread: need help with printing out a tree

1. ## need help with printing out a tree

* Each node should be printed on a separate line;
* the left subtree should be printed after the root;
* The right subtree should be printed before the root;
* Tthe root should not be indented, the keys in its subtrees should be intended 2 spaces, the keys in their subtrees 4 spaces, and so on.
Code:
```For example, the complete tree containing {1,2,3,4,5,6} would be printed like this:

6
5
4
3
2
1```

if you tilt the above diagram, you can see a tree with 4 being the root and 1,3,5 being the leaves....

should i start printing the largest number first?
but the values of keys in the tree does not need to be always 1 less than the previous one...so i can;t just search for the next key that is 1 less than the previous one...
confuesd...need some help, thx >.<

2. You have to print the values in the tree by traversing your tree.I think you have inserted the values to a tree in the tree format.You simply follow the same way to print the values.While printing the values,first print right subtree until it reaches null,then root and then you print left sub tree.If you face any problem,revert me.

3. Actually, you are trying to print the tree in reverse of inorder traversal.

In order traversal prints the tree in sorting order and it follow LVR (left, visit, Right )

But you are trying to print reverse sorting order so you are following RVL ( Right , visit , Left ).

No need to worry about largest number or something. If you are using the Binary Tree it always have the left node is less than its parent and right node is greater than its parent.

I just gave you sample algorithm.
Code:
```void printtree(root)
{
printtree(root->right);
print node;
printtree(root->left);
}```
For indenting you can use the depth of the tree. If the depth of the node is 0 no need to print any space. If the node depth is 1 you can print 2 spaces. Like that you can use depth.

4. Originally Posted by sganesh
Actually, you are trying to print the tree in reverse of inorder traversal.

In order traversal prints the tree in sorting order and it follow LVR (left, visit, Right )

But you are trying to print reverse sorting order so you are following RVL ( Right , visit , Left ).

No need to worry about largest number or something. If you are using the Binary Tree it always have the left node is less than its parent and right node is greater than its parent.

I just gave you sample algorithm.
Code:
```void printtree(root)
{
inorder(root->right);
print node;
inorder(root->left);
}```
For indenting you can use the depth of the tree. If the depth of the node is 0 no need to print any space. If the node depth is 1 you can print 2 spaces. Like that you can use depth.
I think I understand what you mean...
But now I am looking at this tree from wikipedia...
Binary search tree - Wikipedia, the free encyclopedia
When I reach 14, I print out 14
but how do I get back to 13?
and even i get to 13, how do i get to 10?

do i have to make a function something like "delete" to delete 14 and 13?
so that when i reach 10, 10->right will be null?

5. I didn't understand what you have explained.Did you follow the algorithm mentioned by ganesh?
Tell me whether you stored all the values what you have mentioned in tree format or not.You can print the values in the tree if you insert those values in correct order.If not,you can't get it.

6. Originally Posted by vivekraj
I didn't understand what you have explained.Did you follow the algorithm mentioned by ganesh?
Tell me whether you stored all the values what you have mentioned in tree format or not.You can print the values in the tree if you insert those values in correct order.If not,you can't get it.
my question is...when i am trying to print root->right, how do i print root->right->left as well...?

7. ## Re: need help with printing out a tree

print tree is a recursive process. It will take one node then it will apply traversal method.(In your case Right, visit, Left).

8. As ganesh mentioned,its a recursive process.When you call the print function with root->right as an argument.Then in the print function,you will print the right node of the current node which is

root->right->right ,then you print the current node which is root->right and then left node which is root->right->left.