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

J. Garrett Morris trevion at gmail.com
Mon Sep 19 09:26:25 EDT 2005


In this case, I think all you need is:

> type Path = [String]

> allPaths :: Path -> Tree -> [Path]
> allPaths p (Node s []) = [s:p]
> allPaths p (Node s subTrees) = concatMap (allPaths (s:p)) subTrees

> paths = map reverse . allPaths []

In your test case, "root" will be at the beginning of each path
produced, but you can map tail over the list if you don't want it.

 /g


More information about the Haskell-Cafe mailing list