Tree traversal: Difference between revisions

Content added Content deleted
m (→‎Data.Tree nodes: Small reduction of levels function)
Line 4,989: Line 4,989:
[] -> ([], [])
[] -> ([], [])
(y : ys) -> (y, ys)
(y : ys) -> (y, ys)
in [x] <> h : foldr go t xs
in (x : h) : foldr go t xs


nodeCount,
nodeCount,
Line 5,012: Line 5,012:


treeWidth _ [] = 1
treeWidth _ [] = 1
treeWidth _ xs = sum xs
treeWidth _ xs = sum xs



treeLeaves :: Tree a -> [a]
treeLeaves :: Tree a -> [a]
treeLeaves = go
treeLeaves = go
where
where
go (Node x []) = [x]
go (Node x []) = [x]
go (Node _ xs) = xs >>= go
go (Node _ xs) = xs >>= go
Line 5,041: Line 5,040:
print $ levelOrder tree
print $ levelOrder tree
putStrLn ""
putStrLn ""

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


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