z****n 发帖数: 28 | 1 I want to print out a tree and the display output is like
4
2 6
1 3 5 7
.....
There are two steps in my algorithm:
First, Using DFS, compute the necessary space for each node. Do the
depth-first traversal AND keep track of which nodes have children so that you
can then print the lines to the children. It is a recursive function.
Second, to use BFS to print node.
I tried to figure out the space for each node, but failed.
Thnaks a lot for your suggestion! | g*****g 发帖数: 34805 | 2 I don't see why you need DFS first,
use a Q, BFS is straight. Use a function to format
your number to fix length before prin out.
【在 z****n 的大作中提到】 : I want to print out a tree and the display output is like : 4 : 2 6 : 1 3 5 7 : ..... : There are two steps in my algorithm: : First, Using DFS, compute the necessary space for each node. Do the : depth-first traversal AND keep track of which nodes have children so that you : can then print the lines to the children. It is a recursive function. : Second, to use BFS to print node.
|
|