Rekursion über Bäume (Beispiele)

data Tree a    = Leaf
 | Branch { left :: Tree a, key :: a, right :: Tree a }

summe :: Tree Int -> Int
summe t = case t of
  Leaf {} -> 0 ;  Branch {} -> 
    summe (left t) + key t + summe (right t)
preorder :: Tree a -> [a]
preorder t = case t of
  Leaf {} -> [] ;  Branch {} -> 
    key t : preorder (left t) ++ preorder (right t)



Johannes Waldmann 2014-07-10