Skip to content

Instantly share code, notes, and snippets.

@tuttlem
Created January 3, 2013 21:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuttlem/4447669 to your computer and use it in GitHub Desktop.
Save tuttlem/4447669 to your computer and use it in GitHub Desktop.
haskell - tree insert
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 x left right
| x < a = Node a (treeInsert x left) right
| x > a = Node a left (treeInsert x right)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment