module Data.Sifflet.Tree
(T.Tree(..), tree, leaf, isLeaf, treeSize, treeDepth
, Repr(..)
, Name(..), nameTree
, putTree, putTreeR, putTreeRs, putTreeS
, drawTreeShow
)
where
import Data.Tree as T
import Text.Sifflet.Repr
tree :: e -> Forest e -> T.Tree e
tree root subtrees = T.Node {rootLabel = root, subForest = subtrees}
leaf :: e -> T.Tree e
leaf x = tree x []
isLeaf :: T.Tree e -> Bool
isLeaf (T.Node _root []) = True
isLeaf _ = False
treeSize :: T.Tree e -> Int
treeSize (T.Node _root subtrees) = 1 + sum (map treeSize subtrees)
treeDepth :: T.Tree e -> Int
treeDepth (T.Node _root []) = 1
treeDepth (T.Node _root subtrees) = 1 + maximum (map treeDepth subtrees)
nameTree :: T.Tree String -> T.Tree Name
nameTree = fmap Name
putTree :: (Show e) => T.Tree e -> IO ()
putTree = putTreeS
putTreeR :: (Repr e) => T.Tree e -> IO()
putTreeR = putStrLn . drawTree . fmap repr
putTreeRs :: (Repr e) => T.Tree e -> IO()
putTreeRs = putStrLn . drawTree . fmap reprs
putTreeS :: (Show e) => T.Tree e -> IO ()
putTreeS = putStrLn . drawTreeShow
drawTreeShow :: (Show e) => T.Tree e -> String
drawTreeShow = drawTree . fmap show