Difference between revisions of "99 questions/Solutions/72"

From HaskellWiki
Jump to navigation Jump to search
 
(categorize)
 
Line 14: Line 14:
 
bottom_up_aux (Node x ts) xs = foldr bottom_up_aux (x:xs) ts
 
bottom_up_aux (Node x ts) xs = foldr bottom_up_aux (x:xs) ts
 
</haskell>
 
</haskell>
  +
  +
[[Category:Programming exercise spoilers]]

Latest revision as of 03:52, 10 January 2017

(*) Construct the bottom-up order sequence of the tree nodes.

Write a predicate bottom_up(Tree,Seq) which constructs the bottom-up sequence of the nodes of the multiway tree Tree.

bottom_up :: Tree a -> [a]
bottom_up (Node x ts) = concatMap bottom_up ts ++ [x]

A more efficient version using an accumulator:

bottom_up :: Tree a -> [a]
bottom_up t = bottom_up_aux t []
  where bottom_up_aux :: Tree a -> [a] -> [a]
        bottom_up_aux (Node x ts) xs = foldr bottom_up_aux (x:xs) ts