Tree

import Data.Tree

preOrder :: (a -> b) -> Tree a -> [b]
preOrder f (Node v subtrees) = f v : subtreesTraversed
  where 
    subtreesTraversed = concatMap (preOrder f) subtrees

postOrder :: (a -> b) -> Tree a -> [b]
postOrder f (Node v subtrees) = subtreesTraversed <> [f v]
  where 
    subtreesTraversed = concatMap (postOrder f) subtrees
  • Practical Haskell p.128

Last updated