1

假设我有一棵二叉树。

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 
4

4 回答 4

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
于 2013-02-26T02:05:25.933 回答
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
于 2013-02-26T14:42:45.667 回答
0

如果您转换为 a Data.Tree,那么您可以使用库函数drawTree,它几乎可以满足您的需求(它还使用 ASCII 艺术绘制分支)。

于 2013-03-07T07:28:54.647 回答
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"  
于 2013-02-26T02:16:57.237 回答