X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=Data%2FTree.hs;h=e0a7cb6de2f6d9eba90052c06378bddc8f979c99;hb=e9db07b2af145dba541f82aeec1b626f572f485f;hp=c68e66e86a680dd098657ac3a964414ded8b5ef0;hpb=2cd6a935d4b4b0649748790da04821b159fe25d1;p=haskell-directory.git diff --git a/Data/Tree.hs b/Data/Tree.hs index c68e66e..e0a7cb6 100644 --- a/Data/Tree.hs +++ b/Data/Tree.hs @@ -18,14 +18,32 @@ module Data.Tree( drawTree, drawForest, -- * Extraction flatten, levels, + -- * Building trees + unfoldTree, unfoldForest, + unfoldTreeM, unfoldForestM, + unfoldTreeM_BF, unfoldForestM_BF, ) where #ifdef __HADDOCK__ import Prelude #endif +import Control.Applicative (Applicative(..)) +import Control.Monad +import Data.Monoid (Monoid(..)) +import Data.Sequence (Seq, empty, singleton, (<|), (|>), fromList, + ViewL(..), ViewR(..), viewl, viewr) +import Data.Foldable (Foldable(foldMap), toList) +import Data.Traversable (Traversable(traverse)) +import Data.Typeable + +#include "Typeable.h" + -- | Multi-way trees, also known as /rose trees/. -data Tree a = Node a (Forest a) -- ^ a value and zero or more child trees. +data Tree a = Node { + rootLabel :: a, -- ^ label value + subForest :: Forest a -- ^ zero or more child trees + } #ifndef __HADDOCK__ deriving (Eq, Read, Show) #else /* __HADDOCK__ (which can't figure these out by itself) */ @@ -35,12 +53,20 @@ instance Show a => Show (Tree a) #endif type Forest a = [Tree a] +INSTANCE_TYPEABLE1(Tree,treeTc,"Tree") + instance Functor Tree where fmap = mapTree mapTree :: (a -> b) -> (Tree a -> Tree b) mapTree f (Node x ts) = Node (f x) (map (mapTree f) ts) +instance Traversable Tree where + traverse f (Node x ts) = Node <$> f x <*> traverse (traverse f) ts + +instance Foldable Tree where + foldMap f (Node x ts) = f x `mappend` foldMap (foldMap f) ts + -- | Neat 2-dimensional drawing of a tree. drawTree :: Tree String -> String drawTree = unlines . draw @@ -66,6 +92,60 @@ flatten t = squish t [] -- | Lists of nodes at each level of the tree. levels :: Tree a -> [[a]] -levels t = map (map root) $ takeWhile (not . null) $ iterate subforest [t] - where root (Node x _) = x - subforest f = [t | Node _ ts <- f, t <- ts] +levels t = map (map rootLabel) $ + takeWhile (not . null) $ + iterate (concatMap subForest) [t] + +-- | Build a tree from a seed value +unfoldTree :: (b -> (a, [b])) -> b -> Tree a +unfoldTree f b = let (a, bs) = f b in Node a (unfoldForest f bs) + +-- | Build a forest from a list of seed values +unfoldForest :: (b -> (a, [b])) -> [b] -> Forest a +unfoldForest f = map (unfoldTree f) + +-- | Monadic tree builder, in depth-first order +unfoldTreeM :: Monad m => (b -> m (a, [b])) -> b -> m (Tree a) +unfoldTreeM f b = do + (a, bs) <- f b + ts <- unfoldForestM f bs + return (Node a ts) + +-- | Monadic forest builder, in depth-first order +#ifndef __NHC__ +unfoldForestM :: Monad m => (b -> m (a, [b])) -> [b] -> m (Forest a) +#endif +unfoldForestM f = mapM (unfoldTreeM f) + +-- | Monadic tree builder, in breadth-first order, +-- using an algorithm adapted from +-- /Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design/, +-- by Chris Okasaki, /ICFP'00/. +unfoldTreeM_BF :: Monad m => (b -> m (a, [b])) -> b -> m (Tree a) +unfoldTreeM_BF f b = liftM getElement $ unfoldForestQ f (singleton b) + where getElement xs = case viewl xs of + x :< _ -> x + EmptyL -> error "unfoldTreeM_BF" + +-- | Monadic forest builder, in breadth-first order, +-- using an algorithm adapted from +-- /Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design/, +-- by Chris Okasaki, /ICFP'00/. +unfoldForestM_BF :: Monad m => (b -> m (a, [b])) -> [b] -> m (Forest a) +unfoldForestM_BF f = liftM toList . unfoldForestQ f . fromList + +-- takes a sequence (queue) of seeds +-- produces a sequence (reversed queue) of trees of the same length +unfoldForestQ :: Monad m => (b -> m (a, [b])) -> Seq b -> m (Seq (Tree a)) +unfoldForestQ f aQ = case viewl aQ of + EmptyL -> return empty + a :< aQ -> do + (b, as) <- f a + tQ <- unfoldForestQ f (foldl (|>) aQ as) + let (tQ', ts) = splitOnto [] as tQ + return (Node b ts <| tQ') + where splitOnto :: [a'] -> [b'] -> Seq a' -> (Seq a', [a']) + splitOnto as [] q = (q, as) + splitOnto as (_:bs) q = case viewr q of + q' :> a -> splitOnto (a:as) bs q' + EmptyR -> error "unfoldForestQ"