| | |
help with binary search trees and saving them
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
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
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?
•
•
•
•
should i have the save function called in the case statement or after the loop finishes executing?
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
•
•
Join Date: Jul 2005
Posts: 1,673
Reputation:
Solved Threads: 261
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.
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
•
•
Join Date: Jul 2005
Posts: 1,673
Reputation:
Solved Threads: 261
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.
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
![]() |
Similar Threads
- binary search trees (Java)
- Great Binary Search Program, Unable to Output to File. Please Help! (C++)
- Binary Search Trees - Need Help Indicating Node Level (C++)
- binary search trees (Computer Science)
- please help me: build a binary search tree by Lisp (Legacy and Other Languages)
- Binary search tree removal (C++)
Other Threads in the C++ Forum
- Previous Thread: I need advice on removing suffixes.......
- Next Thread: Adding struct in container in sorted way
| Thread Tools | Search this Thread |
api array beginner bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






