Tree traversal: Difference between revisions
m
→Haskell :: Rose Tree: Tidied
(→{{header|Haskell}}: Rose tree version: levelOrder as concat . levels, with linear definition of levels) |
m (→Haskell :: Rose Tree: Tidied) |
||
Line 4,971:
---------------------- TREE TRAVERSAL --------------------
inorder, postorder, preorder :: a -> [[a]] -> [a]
preorder x xs = x : concat xs▼
inorder x [] = [x]
inorder x (y : xs) = y <> [x] <> concat xs
postorder x xs = concat xs <> [x]
▲preorder x xs = x : concat xs
levelOrder :: Tree a -> [a]
Line 4,993 ⟶ 4,989:
in [x] <> h : foldr go t xs
nodeCount,
treeSum :: Int -> [Int] -> Int▼
treeDepth,
treeSum x xs = x + sum xs▼
treeMax,
treeMin,
treeProduct,
treeSum,
treeWidth ::
nodeCount = const (succ . sum)▼
treeDepth = const (succ . foldr max 1)
treeProduct :: Int -> [Int] -> Int▼
treeMax x xs = maximum (x : xs)
treeMin x xs = minimum (x : xs)
▲nodeCount = const (succ . sum)
▲treeSum x xs = x + sum xs
treeWidth _ [] = 1▼
--------------------------- TEST -------------------------
Line 5,043 ⟶ 5,038:
<> justifyLeft 6 ' ' (show $ foldTree f tree)
)
<$> [ ("
("
("Max", treeMax),
("Min", treeMin),
("
("
("
]
)
Line 5,079 ⟶ 5,074:
[1,2,3,4,5,6,7,8,9]
Max -> 9
Min -> 1
=={{header|Icon}} and {{header|Unicon}}==
|