delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/04/27/22:10:49.1

From: Endlisnis <s257m AT unb DOT ca>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Freeing memory from a tree
Date: Tue, 27 Apr 1999 14:38:58 -0300
Organization: BrunNet
Lines: 38
Message-ID: <3725F632.D16884EE@unb.ca>
References: <37255021 DOT 5017576 AT news DOT iafrica DOT com>
NNTP-Posting-Host: ftnts2c9.brunnet.net
Mime-Version: 1.0
X-Mailer: Mozilla 4.04 [en] (Win95; U)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019