Mail Archives: djgpp/1999/04/27/22:10:49.1
Marc Brooker wrote:
> How would I go about freeing the memory I allocated, all the methods I
> use miss some of the buds, in the end I created an array storing
> pointers to all the buds on the tree and free them in turn. I am very
> sure that this is not the most efficient way to do this. Amongst other
> things, I am storing two copies of everything in memory. What is a
> better way?
Well, without any knowledge of the actual data structures you are using, I
can only guess. But, you should do it recursively. Here is some UNTESTED
code to free the memory of a binary tree, use at your own risk.
struct Node {
datatype stuff;
Node* Child1, * Child2;
};
void DeleteTree(Node* Root) //Assume it is prepopulated...
{
if(Root) {
DeleteTree(Root->Child1);
DeleteTree(Root->Child2);
free(Root); //I can't remember if this is the correct function name to
unallocate in C.
}
}
--
(\/) Endlisnis (\/)
s257m AT unb DOT ca
Endlisnis AT BrunNet DOT Net
Endlisnis AT HotMail DOT com
ICQ: 32959047
- Raw text -