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 : inorder (left t) ++ inorder (right t)



Johannes Waldmann 2011-01-18