[comp.ai] m-way tree building

skumar@cbnewsh.ATT.COM (swaminathan.ravikumar) (02/10/90)

I have the following facts asserted in the prolog database:

	linked(a, b).
	linked(a, c).
	linked(b, d).
	linked(b, e).
	linked(b, f).

and I want to construct an "m-way" tree strcuture as follows:

		a
		|
	     -------
	     |     |
	     b     c
	     |
           -----	   
	   | | |
	   d e f

I would appreciate any suggestions or pointers to reference
materials on doing this.

Thanks.

-- ravi
skumar@hocus.att.com

fargo@pawl.rpi.edu (Irwin M. Fargo) (02/12/90)

In article <8053@cbnewsh.ATT.COM> skumar@cbnewsh.ATT.COM (swaminathan.ravikumar) writes:
>
>I have the following facts asserted in the prolog database:
>
>	linked(a, b).
>	linked(a, c).
>	linked(b, d).
>	linked(b, e).
>	linked(b, f).
>
>and I want to construct an "m-way" tree strcuture as follows:
>
>		a
>		|
>	     -------
>	     |     |
>	     b     c
>	     |
>           -----	   
>	   | | |
>	   d e f
>

Instead of using Prolog predicates to link the elements, you might want to
use Prolog lists.  The book I used when learning Prolog (Knowledge Systems
& Prolog, edited by Adrian Walker), showed how lists can be interpreted as
a tree structure.  You might want to take a look at a good text on Prolog.

You can also create n-way trees in languages like C and Pascal by using
linked lists.

Hope this helps.

-- 
Thank you and happy hunting!             Actually: Ethan M. Young
  ____ [> SB <]    "Travel IS life"      Internet: fargo@pawl.rpi.edu
 /__   -=>??<=-       - Irwin M. Fargo   Bitnet (??): usergac0@rpitsmts.bitnet
/   ARGO : 3000 years of regression from the year 4990

BRL102@psuvm.psu.edu (Ben Liblit) (02/13/90)

Some free advice from someone who knows more about linked lists than ai.

In article <D?88P-@rpi.edu>, fargo@pawl.rpi.edu (Irwin M. Fargo) says:
>
>You can also create n-way trees in languages like C and Pascal by using
>linked lists.

This is true.  However, the implementation of this that first comes to mind is
absolutely nightmarish to work with.  One's first thought is to associate with
each node a linked list of pointers to children.  Something like this:

    A
    |------|
    B      C
    |-|-|
    D E F

When you actually sit down to program, though, this really gets hairy.  A much
nicer way of handling general trees is to have each node point to its first
chile and its next sibling.  Thus:

    A
    |
    B--------C
    |
    D--E--F

Admittedly, this doesn't resemble the original relationships as the first
technique, amd there *are* some applications for which it is not appropriate.
However, I strongly recommend that anyone out there who is thinking of
implementing an m-tree in a language like Pascal or C take the time to evaluate
this approach.  It may be the better of the two in the long run.

                      Ben Liblit
                      BRL102 @ psuvm.bitnet -- BRL102 @ psuvm.psu.edu
                      "Fais que tes reves soient plus longs que la nuit."