Skip to content

Instantly share code, notes, and snippets.

@neworld
Created January 22, 2017 19:51
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 neworld/8ae9b00a9aecfd4325fa7db142d7ecaa to your computer and use it in GitHub Desktop.
Save neworld/8ae9b00a9aecfd4325fa7db142d7ecaa to your computer and use it in GitHub Desktop.
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