help with binary search trees and saving them

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 3
Reputation: nightninja12 is an unknown quantity at this point 
Solved Threads: 0
nightninja12 nightninja12 is offline Offline
Newbie Poster

help with binary search trees and saving them

 
0
  #1
27 Days Ago
i have a project for my programming class where we have to create a binary search tree and then when the program exits we need to save the tree to a text file and then restore it when the program opens. the basic functions were given to us, but the textbook isnt clear on how to save and restore data to and from the text file. im just not sure where to put the code. would the restore code go in the trees constructor or do i need a seperate function to initialize all the nodes? also, because exit is a case statement, should i have the save function called in the case statement or after the loop finishes executing? any help would be appreciated, theres a lot of code which is why i didnt put it up, but i will if itll help
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
0
  #2
27 Days Ago
would the restore code go in the trees constructor or do i need a seperate function to initialize all the nodes?
I would put it in a separate constructor, as well as a public method. The actual implementation could be in a private method that is shared between the two.

should i have the save function called in the case statement or after the loop finishes executing?
Well... That is really up to your application's logic, but there is nothing stopping you from specifying an internal flag that an object should be saved and doing the save in the destructor.

There are as many ways to do this as there are programmers who can write it, so maybe you should try coming up with several proposals and figuring out which of them best meets your needs.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: nightninja12 is an unknown quantity at this point 
Solved Threads: 0
nightninja12 nightninja12 is offline Offline
Newbie Poster
 
0
  #3
27 Days Ago
ok so do i need to do the save function in one of the transversal functions or can i just do like a "save tree" kind of command, if there is one.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,673
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso
 
0
  #4
27 Days Ago
Not a traversal function. You want to be able to traverse without needing to write to file each time you do a traverse.

A separate save function is probably best. That gives you more control. In addition it allows you to save the entire tree or any subtree of the original tree just by passing it the "root" of the (sub)tree you want to save.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: nightninja12 is an unknown quantity at this point 
Solved Threads: 0
nightninja12 nightninja12 is offline Offline
Newbie Poster
 
0
  #5
27 Days Ago
so its like a copy function. only how does the code for the save load function work? the book is really terrible for this class
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,673
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso
 
0
  #6
26 Days Ago
I'd write a function called WriteTreeToFile, or something similarly obvious. I would pass the root of the tree I want to write to file to the function. In the function body I would basically write an in order traversal, but rather than display desired information of in order traversal to the screen I would write the desired information to file each time a new node was found. I would leave the destruction/deletion of the tree to a completely different function than traversing and writing to file functions.

ReadTreeFromFile could be a function name to read information to form a tree from a file. I would read in the information for one node and call the insert/build/add function to insert/add each node to the tree as the information is read in from the file. If information in the file is read in in the same sequence as input into the original tree and the same protocol is used to insert each new node, whether read from file or input by some other mechanism to generate the original tree written to file, then I would expect regenerated tree to duplicate the original.
Last edited by Lerner; 26 Days Ago at 7:38 pm.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC