Skip to content

Instantly share code, notes, and snippets.

@md2perpe
Forked from guiocavalcanti/q03.hs
Created June 15, 2011 21:05
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 md2perpe/1028103 to your computer and use it in GitHub Desktop.
Save md2perpe/1028103 to your computer and use it in GitHub Desktop.
data Tree t = EmptyTree
| Node t (Tree t) (Tree t)
deriving Show
nivel :: Int -> Int
nivel n = nivel' n 0
where
nivel' 0 _ = 0
nivel' 1 x = x
nivel' n x = nivel' (n `div` 2) (x + 1)
criarArvore :: Int -> Int -> Int -> Tree Int
criarArvore n _ 0 = EmptyTree
criarArvore n x y
| x > n = EmptyTree
| otherwise = Node x (criarArvore n (x*2) (y-1)) (criarArvore n ((x*2) + 1) (y-1))
criarArvoreCompleta :: Int -> Tree Int
criarArvoreCompleta 0 = EmptyTree
criarArvoreCompleta n = Node 1 (criarArvore n 2 $ nivel n) (criarArvore n 3 $ nivel n)
balanceRaiz :: Tree Int -> Int
balanceRaiz EmptyTree = 0
balanceRaiz (Node t a b) = (valorPeso a) - (valorPeso b)
valorPeso :: Tree Int -> Int
valorPeso EmptyTree = 0
valorPeso (Node t a b) = mod ((mod t 113111) + (valorPeso a) + (valorPeso b)) 11311
main = do
putStrLn $ show $ criarArvoreCompleta 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment