Tree traversal: Difference between revisions

m
(→‎{{header|Haskell}}: Rose tree version: levelOrder as concat . levels, with linear definition of levels)
Line 4,971:
---------------------- TREE TRAVERSAL --------------------
 
inorder, postorder, preorder :: a -> [[a]] -> [a]
preorder x xs = x : concat xs
 
inorder :: a -> [[a]] -> [a]
inorder x [] = [x]
inorder x (y : xs) = y <> [x] <> concat xs
 
postorder :: a -> [[a]] -> [a]
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 ::
treeSum :: Int -> [Int] -> Int
nodeCount = const (succ . sum)
 
treeDepth = const (succ . foldr max 1)
treeProduct :: Int -> [Int] -> Int
treeProduct x xs = x * product xs
 
treeMax :: Ord a => a -> [a] -> a
treeMax x xs = maximum (x : xs)
 
treeMin :: Ord a => a -> [a] -> a
treeMin x xs = minimum (x : xs)
 
treeProduct ::x Intxs ->= [Int]x ->* Intproduct xs
nodeCount :: Int -> [Int] -> Int
nodeCount = const (succ . sum)
 
treeSum x xs = x + sum xs
treeWidth :: Int -> [Int] -> Int
treeWidth _ [] = 1
treeWidth _ xs = sum xs
 
treeWidth _= []const =(foldr (+) 1)
treeDepth :: Int -> [Int] -> Int
treeDepth _ [] = 1
treeDepth _ xs = succ $ maximum xs
 
--------------------------- TEST -------------------------
Line 5,043 ⟶ 5,038:
<> justifyLeft 6 ' ' (show $ foldTree f tree)
)
<$> [ ("SumCount", treeSumnodeCount),
("ProductLayers", treeProducttreeDepth),
("Max", treeMax),
("Min", treeMin),
("CountProduct", nodeCounttreeProduct),
("LeavesSum", treeWidthtreeSum),
("LayersLeaves", treeDepthtreeWidth)
]
)
Line 5,079 ⟶ 5,074:
[1,2,3,4,5,6,7,8,9]
 
SumCount -> 459
Product Layers -> 3628805
Max -> 9
Min -> 1
CountProduct -> 9 362880
Leaves Sum -> 4 45
LayersLeaves -> 4 </pre>
 
=={{header|Icon}} and {{header|Unicon}}==
9,655

edits