home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!gumby!wupost!csus.edu!netcom.com!qualtrak
- From: qualtrak@netcom.com (Qual Trak)
- Subject: Re: general tree
- Message-ID: <6t4myyq.qualtrak@netcom.com>
- Date: Wed, 12 Aug 92 15:05:40 GMT
- Organization: QualTrak Corporation
- References: <1992Aug11.220049.861@ugle.unit.no> <chuckb.713584418@milton> <1992Aug12.092635.27271@ugle.unit.no>
- Lines: 43
-
- In article <1992Aug12.092635.27271@ugle.unit.no> bjornmu@idt.unit.no (Bj|rn P. Munch) writes:
- >In article <chuckb.713584418@milton>, chuckb@milton.u.washington.edu (Chuck Bass) writes:
- >|> bjornmu@idt.unit.no (Bj|rn P. Munch) writes:
- >|>
- >|> >In article <1992Aug11.200208.19716@infonode.ingr.com>, dpm@wizard.b29.ingr.com (David Mikesell) writes:
- >|> >|>
- >|> >|> I am beginning work on a C program that will manipulate a general tree, i.e.
- >|> >|> a tree in which each node can have an arbitrary number of children. An ex-
- >|> >|> ample declaration of the structure follows:
- >|> >|>
- >|> >|> struct node
- >|> >|> {
- >|> >|> DATA data;
- >|> >|> struct node *parent;
- >|> >|> struct node **children;
- >|> >|> };
- >|>
- >|> >In stead of putting the children into an array (what do you do if this
- >|> >array suddenly isn't big enough?), you may consider putting them in a
- >|> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- >|> This is a perfect time to use something like ... realloc.
- >
- >I also thought about realloc(), but soon realized this would be a
- >rather bad idea. Maybe I should have explained why...
- >
- >Remember: realloc() may move your array to another address. If you
- >have references to the children that were stored there (like parent
- >pointers in _their_ children), then you're in deep trouble.
- >
-
- Seems to me realloc () will work quite adequately if you reference the
- children pointer explicitly thru the node - i.e.
- struct node *nodeptr, (nodeptr1;
-
- nodeptr1 = nodeptr->children [0];
-
- Will always return you the same value even if nodeptr->children has been
- realloced ();
-
- ------------------------------------------------------------------------
- John Birchfield - QualTrak Corp (408) 730-2674 Home (408) 736-3852
- jb@QualTrak.COM
- ------------------------------------------------------------------------
-