Skip to content

Instantly share code, notes, and snippets.

@craftybones
Created December 1, 2017 17:42
Show Gist options
  • Save craftybones/1621a40fa4cee3089a86ec859bbdc816 to your computer and use it in GitHub Desktop.
Save craftybones/1621a40fa4cee3089a86ec859bbdc816 to your computer and use it in GitHub Desktop.
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
createTree :: (Ord a) => a -> Tree a
createTree x = Node x EmptyTree EmptyTree
insert :: (Ord a) => Tree a -> a -> Tree a
insert EmptyTree x = Node x EmptyTree EmptyTree
insert (Node x left right) y
| x == y = Node x left right
| x > y = Node x (insert left y) right
| otherwise = Node x left (insert right y)
find :: (Ord a) => Tree a -> a -> Bool
find EmptyTree x = False
find (Node x left right) y
| x == y = True
| x < y = find right y
| otherwise = find left y
toList ::(Ord a) => Tree a -> [a]
toList EmptyTree = []
toList (Node x left right) = (toList left) ++ [x] ++ (toList right)
instance Functor Tree where
fmap _ EmptyTree = EmptyTree
fmap f (Node x left right) = Node (f x) (fmap f left) (fmap f right)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment