Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data Tree a = Tree a (Maybe (Tree a)) (Maybe (Tree a))
- deriving (Show)
- treeHeight :: Maybe (Tree a) -> Int
- treeHeight Nothing = 0
- treeHeight (Just (Tree _ Nothing Nothing )) = 1
- treeHeight (Just (Tree _ (Just left) Nothing )) = 1 + treeHeight (Just left)
- treeHeight (Just (Tree _ Nothing (Just right))) = 1 + treeHeight (Just right)
- treeHeight (Just (Tree _ (Just left) (Just right)))
- | leftHeight > rightHeight = 1 + leftHeight
- | leftHeight < rightHeight = 1 + rightHeight
- | otherwise = 1
- where leftHeight = treeHeight (Just left)
- rightHeight = treeHeight (Just right)
Advertisement
Add Comment
Please, Sign In to add comment