I'm working on a project for my data structures class that wants me to read a text file and put each line on a balanced binary tree. It is my understanding 开发者_Go百科that this structure will look like the following:
1
/ \ 2 3 / \ / \ 4 5 6 7
1 Representing the first line, 2 the second, etc.
If I want to read this in order, how do I go about that? How I see it, if I use the order (node, left, right) I would get 1,2,4,5,3,6,7
Is the only way to do this is assign an integer along with each string that represents which line it is and then sort the tree to look like:
4
/ \ 2 6 / \ / \ 1 3 5 7
From what I understand, you want to try a breadth-first search to read a tree in level order.
http://en.wikipedia.org/wiki/Breadth-first_traversal
That wiki explains a good way to go about implementing a level-order traversal. Hope that helps.
you can use two queues to print the tree level wise where each level is printed in different line
精彩评论