Created
January 22, 2017 19:51
-
-
Save neworld/8ae9b00a9aecfd4325fa7db142d7ecaa 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 = Empty | Tree a (Tree a) (Tree a) deriving (Show) | |
singleton :: a -> Tree a | |
singleton x = Tree x Empty Empty | |
addToTree :: (Ord a) => a -> Tree a -> Tree a | |
addToTree new Empty = singleton new | |
addToTree new (Tree value left right) | |
| new < value = Tree value (addToTree new left) right | |
| otherwise = Tree value left (addToTree new right) | |
searchTree :: (Ord a) => a -> Tree a -> Maybe (Tree a) | |
searchTree _ Empty = Nothing | |
searchTree x cur@(Tree value left right) | |
| x == value = Just cur | |
| x < value = searchTree x left | |
| x > value = searchTree x right |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment