home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!sun4nl!and!jos
- From: jos@and.nl (Jos Horsmeier)
- Newsgroups: comp.lang.c
- Subject: Re: general tree
- Message-ID: <3222@dozo.and.nl>
- Date: 13 Aug 92 08:01:57 GMT
- References: <1992Aug11.200208.19716@infonode.ingr.com>
- Organization: AND Software BV Rotterdam
- Lines: 104
-
- 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:
-
- [ data structure deleted ... ]
-
- |problem
- |-------
- |
- |My problem is in initializing the structure. Algorithms I have seen for
- |building binary trees from a file or array do not appear to me to be easily
- |adaptible to the general case, so I am looking for some ideas on how to get
- |this accomplished. Perhaps there is a better implementation of a general tree
- |that would make this easier...any ideas will be greatly appreciated. Thanks,
-
- You can easily adapt these binary tree algorithms to the general case,
- because every tree can be represented as a binary tree. As an example
- consider the following tree:
-
- R(A(E F G) B(H) C(I J K) D(L M))
-
- It reads: R is the root with 4 children (A, B, C and D), A has three
- children (E, F and G) etc.
-
- This tree can be converted as follows:
-
- - the left pointer of a node points to a list containing the children
- of the node.
-
- - the right pointer points to the next node at the same level (siblings).
-
- The tree would look something like this:
-
- R
- |
- A-----B-----C-----D
- | | | |
- E-F-G H I-J-K L-M
-
- Well, at least it looks like a binary tree to me ;-)
-
- Throwing in some grammar stuff might help implementing the initialization
- of such a tree:
-
- Tree -> Node Children
-
- Children -> ( Tree NextChild
- -> /* nothing */
-
- NextChild -> Tree NextChild
- -> )
-
- Directly from this small grammar, the following (heavily recursive)
- functions can be derived:
-
- ---------------------------------------------------------------------------
- NODE *Tree() {
-
- NODE *Root= Node(); /* Read current node */
-
- Root->Left= Children(); /* Link the list of kids */
- Root->Right= NULL; /* All alone at the top level */
-
- return Root;
-
- } /* Tree */
-
- NODE *Children() {
-
- NODE *Node= NULL; /* Assume no kids */
-
- if (has_children()) { /* Some appropriate predicate */
- Node= Tree(); /* Read the first child */
- Node->Right= NextChild(); /* Link the siblings */
- }
-
- return Node;
-
- } /* Children */
-
- NODE *NextChild() {
-
- NODE *Node= NULL; /* Assume no sibblings */
-
- if (has_sibblings()) { /* Some appropriate predicate */
- Node= Tree(); /* Get first sibling */
- Node->Right= NextChild(); /* Link the others */
- }
-
- return Node;
-
- } /* NextChild */
- ---------------------------------------------------------------------------
-
- Note that a lot of tail recursion can be removed by using an iteration
- (a while loop) instead.
-
- I hope this is of any help to you,
-
- kind regards,
-
- Jos aka jos@and.nl
-