Created
June 7, 2014 11:13
-
-
Save evansb/14ad77785fe9c28ada34 to your computer and use it in GitHub Desktop.
Balanced Binary Tree from List in Haskell.... Literally just copying the definition.
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 Tree a = Leaf | |
| Node Integer (Tree a) a (Tree a) | |
deriving (Show, Eq) | |
foldTree :: [a] -> Tree a | |
foldTree [] = Leaf | |
foldTree xs = Node height | |
(foldTree $ take half xs) | |
(xs !! half) | |
(foldTree $ drop (half + 1) xs) | |
where | |
len = length xs | |
half = len `div` 2 | |
height = floor $ logBase 2 (fromIntegral len) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment