Maintainer | Ivan.Miljenovic@gmail.com |
---|---|
Safe Haskell | Safe-Inferred |
Data.Tree exports drawTree
and drawForest
, which provide 2D
pretty-printing of rose-trees, but in a left-to-right fashion.
The functions here draw trees more "naturally" in a top-down fashion.
For example, consider the following tree:
tree :: Tree String tree = Node "hello" [ Node "foo" [] , Node "bars" [ Node "oi!" [] , Node "baz" [ Node "a" [ Node "b" [] , Node "c" []] , Node "d" [ Node "e" []]]] , Node "foobar" []]
Comparing drawTree
and drawVerticalTree
:
>>>
putStrLn $ drawTree tree
hello | +- foo | +- bars | | | +- oi! | | | `- baz | | | +- a | | | | | +- b | | | | | `- c | | | `- d | | | `- e | `- foobar
>>>
putStrLn $ drawVerticalTree
hello | ------------------- / | \ foo bars foobar | ------ / \ oi! baz | ---- / \ a d | | -- e / \ b c
Also consider the Diagrams.TwoD.Layout.Tree
module from
diagrams-contrib for actual image rendering of rose-trees:
http://hackage.haskell.org/package/diagrams-contrib
Drawing trees
drawVerticalTree :: Tree String -> StringSource
Draw a tree top-down.
drawVerticalTreeWith :: Width -> Tree String -> StringSource
Draw a tree top-down using the specified gap between sub-trees.
Drawing forests
drawVerticalForest :: Forest String -> StringSource
Draw a forest with each tree being top-down.
drawVerticalForestWith :: Width -> Forest String -> StringSource
Draw a forest with each tree being top-down and the specified horizontal gap between trees.
Widths of gaps between trees.
The size of the gap to use. It is recommended that you use a
value >=2
for best results (with 2
being the default).