Skip to content

Instantly share code, notes, and snippets.

@nicolasff
Created November 3, 2009 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nicolasff/224926 to your computer and use it in GitHub Desktop.
Save nicolasff/224926 to your computer and use it in GitHub Desktop.
module Main where
data Bst a = Nil | Node {
elt :: a,
l :: Bst a,
r :: Bst a} deriving Show
insert_ntr Nil x = Node x Nil Nil
insert_ntr n x | x < elt n = n {l = insert_ntr (l n) x}
| x > elt n = n {r = insert_ntr (r n) x}
| otherwise = n
insert n x = recur n x id where
recur Nil x fun = fun $ Node x Nil Nil
recur n x fun | x < elt n = recur (l n) x $ fun . (\k -> n {l = k})
| x > elt n = recur (r n) x $ fun . (\k -> n {r = k})
| otherwise = n
mkTree f = foldl f Nil
main = do
let l = [5,8,4,2,1,9,6,7,3,5]
putStrLn . show . mkTree insert_ntr $ l
putStrLn . show . mkTree insert $ l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment