Tree traversal: Difference between revisions

Content added Content deleted
(→‎Haskell Rose Tree: Corrected treeWidth)
Line 5,011: Line 5,011:
treeSum x xs = x + sum xs
treeSum x xs = x + sum xs


treeWidth = const (foldr (+) 1)
treeWidth _ [] = 1
treeWidth _ xs = sum xs


treeLeaves :: Tree a -> [a]
treeLeaves = go
where
go (Node x []) = [x]
go (Node _ xs) = xs >>= go


--------------------------- TEST -------------------------
--------------------------- TEST -------------------------
Line 5,033: Line 5,041:
print $ levelOrder tree
print $ levelOrder tree
putStrLn ""
putStrLn ""

(putStrLn . unlines)
(putStrLn . unlines)
( ( \(k, f) ->
( ( \(k, f) ->
Line 5,049: Line 5,057:
]
]
)
)


justifyLeft, justifyRight :: Int -> Char -> String -> String
justifyLeft, justifyRight :: Int -> Char -> String -> String