Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Binary Tree's

 
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
 
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.
 
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
Boost this thread!