假设我有一棵二叉树。
main = putStrLn $ printTree tree
data Tree = Empty | Node Int (Tree) (Tree) deriving (Show)
tree = Node 4 (Node 3 Empty (Node 2 Empty Empty)) Empty
printTree :: Tree -> String
printTree x = case x of
Node num treeA treeB -> show num ++ "\n" ++ printTree treeA ++ "\n" ++ printTree treeB
Empty -> "Empty"
输出
*Main> main
4
3
Empty
2
Empty
Empty
Empty
所需的输出(由制表符或双倍空格分隔)
*Main> main
4
3
Empty
2
Empty
Empty
Empty
答案 0 :(得分:2)
您可以使用累加器(此处为depth
)来跟踪您当前在树中的深度 - 然后创建与该行所在深度相对应的多个空格:
main = putStrLn $ printTree tree
data Tree = Empty | Node Int (Tree) (Tree) deriving (Show)
tree = Node 4 (Node 3 Empty (Node 2 Empty Empty)) Empty
printTree :: Tree -> String
printTree x = printTree' x 0
where
printTree' x depth = case x of
Node num treeA treeB -> (replicate (2 * depth) ' ') ++ show num ++ "\n" ++ (printTree' treeA (depth + 1)) ++ "\n" ++ (printTree' treeB (depth + 1))
Empty -> (replicate (2 * depth) ' ') ++ "Empty"
输出:
*Main> main
4
3
Empty
2
Empty
Empty
Empty
答案 1 :(得分:1)
这是在GHC中使用-XImplicitParams的解决方案
{-# LANGUAGE ImplicitParams #-}
module ImplicitTabs where
data Tree = Empty | Node Int (Tree) (Tree) deriving (Show)
tree = Node 4 (Node 3 Empty (Node 2 Empty Empty)) Empty
tab :: (?tab_level :: Int) => String
tab = replicate (2 * ?tab_level) ' '
printTree :: (?tab_level :: Int) => Tree -> String
printTree x = let
?tab_level = ?tab_level + 1
in case x of
Node num treeA treeB -> tab ++ show num ++ "\n" ++ tab ++ printTree treeA ++ "\n" ++ tab ++ printTree treeB
Empty -> tab ++ "Empty"
main = let ?tab_level = -1 in putStrLn $ printTree tree
> runhaskell implicit-tabulation.hs
4
3
Empty
2
Empty
Empty
Empty
答案 2 :(得分:0)
好的,我认为我得到了它的工作。
printTree :: Tree -> String
printTree x = printT 0 x
space x = take x $ cycle " "
printT :: Int -> Tree -> String
printT num x =
case x of
(Node o treeA treeB) -> show o ++
"\n" ++
space num ++
printT (num+1) treeA ++
"\n" ++
space num ++
printT (num+1) treeB
Empty -> "Empty"
答案 3 :(得分:0)
如果您转换为Data.Tree
,那么您可以使用库函数drawTree
,它几乎可以满足您的要求(它还使用ASCII艺术绘制分支)。