## How to Pretty Print a Binary Tree

September 18, 2010

Have you ever thought of pretty-printing a binary tree?

Wouldn’t it be cool to output your binary tree like this?

```              ______________30______________
/                              \
______20______                  ______40______
/              \                /              \
__10__            25__            35            __50
/      \               \                        /
5      15              28                      41
```

Or, if you prefer, output the tree like this instead? (to save some horizontal space)

```        ______30______
/              \
__20__          __40__
/      \        /      \
10      25      35      50
/  \       \            /
5  15      28          41
```

I thought it would be really useful to you while you are cracking on binary tree interview questions. Checking if your binary tree is read in correctly into the program takes a lot of effort while you are testing your algorithm.

That’s why I wrote the following C++ code myself. Check out the 60+ lines of code below. Please note that the displayed code below did not include some functions, such as maxHeight() and intToString(). For the full source code which includes a sample driver demo program, please download the source file below.

Implementation:
This implementation is based on my previous post: Printing a Binary Tree in Level Order. I use a deque (double-ended queue) instead of a queue because I want to use std::iterator (in C++, deque supports it but not queue).

For a level, each node is spaced equally with neighboring nodes. This allows a Math equation to be derived easily to calculate the exact location a node will appear in a level.

Last time, when we see an “empty” node, we do not push anything onto the queue. However, this time we need to push two “empty” nodes when we see an “empty” node, since we need to print out the “empty” node as spaces in order to produce a pretty output. Besides, it eases the programming a lot since we do not need to keep track of the number of empty nodes that appear before a valid node.

In the next post, I will talk about the methods I used to read/write a binary tree from/to a file, so that you can run test cases easily.

Huge Update Planned:

I am planning to update all (most, if not all) of my previous posts with sample input/output data sets, as requested by one of my fellow readers, thanks . In the future, you should be able to test your algorithms easily!

VN:F [1.9.22_1171]
How to Pretty Print a Binary Tree, 5.0 out of 5 based on 3 ratings

### 10 responses to How to Pretty Print a Binary Tree

1. Can u please post java version?? It would be really helpful..

VA:F [1.9.22_1171]
0
2. Here is a easier version of printing a binary tree, though in a rotated fashion i.e
left-right maps to bottom-up:-

start the call to displayTree with level = 0, lik displayTree(root,0);

void displayTree(NODE* root,int level){
int i;
if(root!=NULL){
displayTree(root->right, level+1);
printf(“\n”);
for(i=0;idata);
displayTree(root->left, level+1);
}
}

VN:F [1.9.22_1171]
0
3. It crashes when it reaches a certain height.

VA:F [1.9.22_1171]
0
• You need to set a larger size of your console window

VA:F [1.9.22_1171]
+1
4. Great!!!!! thank you very much to 1337c0d3r, you save me a lot of time.
It work really great with some adjust to my purpose and the behavior I need.

VA:F [1.9.22_1171]
0
5. Great! Thanks for great binary tree visualization in console

VN:F [1.9.22_1171]
0
6. Dude. This makes my life infinitely simpler when trying to deal with binary search trees. THANK YOU!

VA:F [1.9.22_1171]
0
7. You are really good at what u doing,dude

VN:F [1.9.22_1171]
0
8. Please tell me how you got the formulas for branchLen, nodeSpaceLen, startLen?

VA:F [1.9.22_1171]
+1
9. Awesome! Thanks!

VA:F [1.9.22_1171]
0