File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Binary Tree's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Binary Tree Watch "Binary Tree New topic
Author

Binary Tree's

Bix Beiderbecke
Greenhorn

Joined: Jun 28, 2004
Posts: 15
Help, I'm trying to traverse a Binary Tree in preorder and at the same time writing it to a txt file using FileWriter and PrintWriter, any Ideas?

Thanks
Kalai Selvan
Ranch Hand

Joined: Jul 07, 2004
Posts: 79
hi,



The above function will perform preorder traversing on the tree. It's an recursive function, you have to pass the root node of the tree to the function.

Kalai Selvan T.
Bix Beiderbecke
Greenhorn

Joined: Jun 28, 2004
Posts: 15
I understand how to do the preorder traversal, the problem is in the actual writing

Maybe this can help someone figure out what i'm doing wrong
Bix Beiderbecke
Greenhorn

Joined: Jun 28, 2004
Posts: 15
the code should say savePreOrder(t.left) sorry
Kalai Selvan
Ranch Hand

Joined: Jul 07, 2004
Posts: 79
Hi,



I hope the code fragment will solve ur problem.....

Kalai Selvan T.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
For the uninitiated, the answer was (as nicely demonstrated by Kalai Selvan T's example) that the output file was being recreated every time savePreOrder() was called (i.e. for each node in the tree). The resulting output file would simply have contained the data for a single tree node.

I'd better be right about that now!

Jules
 
wood burning stoves
 
subject: Binary Tree's