[Haskell-cafe] How to get all the paths of a tree

Santoemma Enrico Enrico.Santoemma at beta80group.it
Mon Sep 19 06:48:09 EDT 2005


Hello,

I can't find the right recursive function to get a list of all the possible paths of a multi-way tree like this:

data Tree= Node String [Tree]

root = Node "root" [x, y]
x    = Node "x"    [x1, x2]
x1   = Node "x1"   []       
x2   = Node "x2"   [x21, x22]     
x21  = Node "x21"  []       
x22  = Node "x22"  []       
y    = Node "y"    []       

This is the Data.Tree.drawTree output:
root
|
+- x
|  |
|  +- x1
|  |
|  `- x2
|     |
|     +- x21
|     |
|     `- x22
|
`- y

And this is the list I'm trying to obtain:
[ [x, x1]
 ,[x, x2, x21]
 ,[x, x2, x22]
 ,[y]
]

Well, the actual problem I'm trying to solve is to print a set of lines like these:
"leaf: x -> x1 \n"
"leaf: x -> x2 -> x21 \n"
...

and I thought I first need the list above.
I'll be very grateful for any help

Enrico



More information about the Haskell-Cafe mailing list