Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Binary Tree's

 
Bix Beiderbecke
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code should say savePreOrder(t.left) sorry
 
Kalai Selvan
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



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

Kalai Selvan T.
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic