module Data.NavTree
( module Data.NavTree
, module Data.Tree.NTree.TypeDefs
)
where
import Data.Tree.NTree.TypeDefs
import Data.Maybe
data NavTree a = NT
(NTree a)
[NavTree a]
[NTree a]
[NTree a]
deriving (Show, Eq, Ord)
ntree :: NTree a -> NavTree a
ntree nd
= NT nd [] [] []
subtreeNT :: NavTree a -> NTree a
subtreeNT (NT nd _ _ _)
= nd
dataNT :: NavTree a -> a
dataNT (NT (NTree a _) _ _ _)
= a
upNT
, downNT
, leftNT
, rightNT :: NavTree a -> Maybe (NavTree a)
upNT (NT _ (p:_) _ _) = Just p
upNT (NT _ [] _ _) = Nothing
downNT t@(NT (NTree _ (c:cs)) u _ _) = Just (NT c (t:u) [] cs)
downNT (NT (NTree _ [] ) _ _ _) = Nothing
leftNT (NT s u (l:ls) r) = Just (NT l u ls (s:r))
leftNT (NT _ _ [] _) = Nothing
rightNT (NT s u l (r:rs)) = Just (NT r u (s:l) rs)
rightNT (NT _ _ _ [] ) = Nothing
preorderNT :: NavTree a -> [NavTree a]
preorderNT
= visit []
where
visit k t = t : maybe k (visit' k) (downNT t)
visit' k t = visit (maybe k (visit' k) (rightNT t)) t
revPreorderNT :: NavTree a -> [NavTree a]
revPreorderNT t
= t : concatMap revPreorderNT (reverse (children t))
where
children = maybe [] (maybeStar rightNT) . downNT
getChildrenNT :: NavTree a -> [NavTree a]
getChildrenNT node
= maybe [] follow (downNT node)
where
follow n = n : maybe [] follow (rightNT n)
o' :: (Monad m) => (b -> m c) -> (a -> m b) -> (a -> m c)
f `o'` g = \x -> g x >>= f
maybeStar, maybePlus :: (a -> Maybe a) -> a -> [a]
maybeStar f a = a : maybe [] (maybeStar f) (f a)
maybePlus f a = maybe [] (maybeStar f) (f a)