Created
March 29, 2014 19:15
-
-
Save anonymous/9861119 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 = EmptyTree | Node a (Tree a) (Tree a) deriving (Show) | |
singleton :: a -> Tree a | |
singleton x = Node x EmptyTree EmptyTree | |
treeInsert :: (Ord a) => a -> Tree a -> Tree a | |
treeInsert x EmptyTree = singleton x | |
treeInsert x (Node a left right) | |
| x == a = Node a left right | |
| x < a = Node a (treeInsert x left) right | |
| x > a = Node a left (treeInsert x right) | |
treeElem :: (Ord a) => a -> Tree a -> Bool | |
treeElem x EmptyTree = False | |
treeElem x (Node a left right) | |
| x == a = True | |
| x < a = treeElem x left | |
| x > a = treeElem x right | |
main = do | |
let nums = [8,4,3,0,1,4,5,4,5,3] | |
let numsTree = foldr treeInsert EmptyTree nums | |
print $ numsTree |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment