Skip to content

Instantly share code, notes, and snippets.

@cy6erGn0m
Created September 6, 2013 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cy6erGn0m/6464364 to your computer and use it in GitHub Desktop.
Save cy6erGn0m/6464364 to your computer and use it in GitHub Desktop.
data Node a = Node a (Node a) (Node a) | Empty
treeHeight :: Node a -> Int
treeHeight Empty = 0
treeHeight tree = treeHeightImpl [(1, tree)] 1
where
treeHeightImpl :: [ (Int, (Node a)) ] -> Int -> Int
treeHeightImpl ((currentDepth, (Empty)) : xs) maxDepth = treeHeightImpl xs maxDepth
treeHeightImpl [] maxDepth = maxDepth
treeHeightImpl ((currentDepth, (Node _ left right)) : xs) maxDepth =
treeHeightImpl (newQueue xs left right currentDepth) (max currentDepth maxDepth)
where newQueue old left right depth = old ++ [ (newPair left depth), (newPair right depth)]
where newPair node depth = (depth + 1, node)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment