Skip to content

Instantly share code, notes, and snippets.

@evansb
Created June 7, 2014 11:13
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 evansb/14ad77785fe9c28ada34 to your computer and use it in GitHub Desktop.
Save evansb/14ad77785fe9c28ada34 to your computer and use it in GitHub Desktop.
Balanced Binary Tree from List in Haskell.... Literally just copying the definition.
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