Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fread and fwrite and binary trees?

Status
Not open for further replies.

Jasp

Programmer
Oct 22, 1999
2
CA
I'm fascinated with the binary tree data structure but am having trouble creating and maintaining a file using the binary tree. <br>
<br>
Am having trouble appending information in the form of a binary tree into an existing file.<br>
<br>
Any thoughts or hints?<br>
<br>

 
Pointers to memory aren't relevant to files. In other words if you are saving the pointers that link your tree structure to a file they become meaningless. <br>
Regards,<br>
Greg Martin.<br>

 
I agree with greg you're problem is probably the fact that you are trying to save pointers. I usually try to set out some structure to a text file. The easiest way is probably to view a binary tree as an array with the indexes like that of a binary tree i.e. the value in the second index is less than the value in the first and the value in the third is greater than the first.<br>
<br>
e.g<br>
<br>
{4,2,5,1,3} is a valid binary tree represented as an array.<br>
like:<br>
_____4<br>
____/_\<br>
___2___5<br>
__/_\<br>
_1___3<br>
(sorry about the lines but the posting process removes spaces :)<br>
once you've got things down to a linear structure they get easier to write to a file, they just get harder to understand.
 
If I got you correctly. Do you want to save a binary tree into a file ? If yes, then my idea is to follow one of the three algorithms :<br>
1) preorder traversal,<br>
2) Postorder traversal,<br>
and 3) Inorder traversal.<br>
<br>
Save the content into a file . And when you want to fetch data into a binary tree. You've to do backtracking . ie., instead of printf use scanf. <br>
Does it answer your question ?<br>
Thanx<br>
Siddhartha Singh<br>
<A HREF="mailto:ssingh@aztecsoft.com">ssingh@aztecsoft.com</A>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top