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

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.

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.

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.

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

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.

Edited 7 Years Ago by Lerner: n/a

This article has been dead for over six months. Start a new discussion instead.