The hardest part of doing this is the spacing the output correctly. Before you do any output, you'll want to fine the "widest" node in the tree, and use that to determine the spacing for all nodes. Consider this output:
Code:
|-----|-----|-10--|
|-----|--4--|-----|-20--|
|--1--|-----|-15--|-----|-30--|
|-----|-----|-----|-----|-----|10000|
Here, the widest node is "10000" with 5 characters, which is also the width used for all nodes. You can literally think of this as a table, where each cell is 5 characters wide with a space inbetween each cell (drawn as "|" in the example). As you do your in-order traversal of the tree, maintain the current row and column where node belongs. You will also have to know which cells are already occupied. For instance, if the subtree at 1 also had nodes 2 and 3, and the subtree at 15 also has 14:
Code:
|-----|-----|-10--|
|-----|--4--|-----|-20--|
|--1--|-----|-15--|
|-----|--2--|
|-----|-----|--3--|
When you go to add 14 to a cell, you will detect that the cell is already occupied by 2, so you have to shift over. Since you are using in-order traversal (the items in bold have not been placed yet), this is easily accoplished by following a simple rule: when placeing a left child in a cell, it must be the last cell on that row. With that rule, you'll end up with:
Code:
|-----|-----|-10--|
|-----|--4--|-----|-----|-20--|
|--1--|-----|-----|-15--|-----|-30--|
|-----|--2--|-14--|-----|-----|-----|10000|
|-----|-----|--3--|
Notice that the subtree at 20 has effectively been shifted over to so that the 2 and 14 don't occupy the same cell.
So we have a fairly simple algorithm, but replace the "-" and "|" characters with spaces and you're left with something that doesn't really look like a tree. This can be fixed by inserting some "ascii art" between each row like so:
Code:
|-----|-----|-10--|
/ \
|-----|--4--|-----|-----|-20--|
/ / \
|--1--|-----|-----|-15--|-----|-30--|
\ / \
|-----|--2--|-14--|-----|-----|-----|10000|
\
|-----|-----|--3--|
If a node has a left child, place a "/" on the next line right below the "|", and do the same with "\" if it has a right child. Here's what the final output would look like:
Code:
10
/ \
4 20
/ / \
1 15 30
\ / \
2 14 10000
\
3
This is an overall design that lays out one way this can be done.
gg