----------------------------------------------------------------------------- -- | -- Module : Data.Tree -- Copyright : (c) The University of Glasgow 2002 -- License : BSD-style (see the file libraries/base/LICENSE) -- -- Maintainer : libraries@haskell.org -- Stability : experimental -- Portability : portable -- -- Multi-way trees (/aka/ rose trees) and forests. -- -----------------------------------------------------------------------------

moduleData.Tree(Tree(..),Forest,drawTree,drawForest,flatten,levels, )where-- | Multi-way trees, also known as /rose trees/.dataTreea=Nodea(Foresta) -- ^ a value and zero or more child trees.deriving(Eq,Read,Show)typeForesta=[Treea]instanceFunctorTreewherefmap=mapTreemapTree::(a->b)->(Treea->Treeb)mapTreef(Nodexts)=Node(fx) (map(mapTreef)ts) -- | Neat 2-dimensional drawing of a tree.drawTree::Showa=>Treea->StringdrawTree=unlines.draw.mapTreeshow-- | Neat 2-dimensional drawing of a forest.drawForest::Showa=>Foresta->StringdrawForest=unlines.mapdrawTreedraw::TreeString->[String]draw(Nodexts0)=grpthis(space(lengththis)) (stLoopts0)wherethis=s1++x++" "spacen=replicaten' 'stLoop[]=[""]stLoop[t]=grps2" " (drawt)stLoop(t:ts)=grps3s4(drawt)++[s4]++rsLooptsrsLoop[]=error"rsLoop:Unexpected empty list."rsLoop[t]=grps5" " (drawt)rsLoop(t:ts)=grps6s4(drawt)++[s4]++rsLooptsgrpfst0rst=zipWith(++) (fst0:repeatrst) [s1,s2,s3,s4,s5,s6]=["- ", "--", "-+", " |", " `", " +"] -- | The elements of a tree in pre-order.flatten::Treea->[a]flattent=squisht[]wheresquish(Nodexts)xs=x:foldrsquishxsts-- | Lists of nodes at each level of the tree.levels::Treea->[[a]]levelst=map(maproot)$takeWhile(not.null)$iteratesubforest[t]whereroot(Nodex_)=xsubforestf=[t|Node_ts<-f,t<-ts]

Index

(HTML for this module was generated on 2013-09-19. About the conversion tool.)