m-way trees 
Author Message
 m-way trees

Quote:
> 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

make_tree/2 below does what you want: call it with first argument the top of the tree

make_tree(X,tree(X,LX)) :- bagof(TZ,Z^(linked(X,Z),make_tree(Z,TZ)),LX) , ! .
make_tree(X,tree(X,[])) .

the query:

        ?- make_tree(a,T) .

answers

        T = tree(a,[tree(b,[tree(d,[]),tree(e,[]),tree(f,[])]),tree(c,[])])

this is perhaps not the fastest solution, neither does it work if your linked/2
database does not represent a tree ... but it is nice that it is so easy to
write such a tree constructor in Prolog




Sun, 02 Aug 1992 22:52:59 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Self-Adjusting Binary Search Trees (Splay Trees)

2. Self-Adjusting Binary Search Trees (Splay Trees)

3. TREE browse .. Ultra Tree Template - Att Marius

4. cast iron tree stand for Christmas tree

5. Alternate solution to tree -> tree problem

6. B plus trees and b star trees

7. Searching for B-tree/B*-tree package...

8. Need to build a tree buttom-up (parse tree)

9. BLT Tree -vs- BWidget Tree

10. AVL Tree,Binary Tree,Sorting..

11. ANNOUNCE: tree-3.3 - tree widget for Tk-3.3b3

12. searching trees (AVL, 2-3-4 and red-black trees)

 

 
Powered by phpBB® Forum Software