Created
September 6, 2013 14:11
-
-
Save cy6erGn0m/6464364 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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