Skip to content

Instantly share code, notes, and snippets.

@TheSeamau5
Created January 5, 2015 15:15
Show Gist options
  • Save TheSeamau5/92175edc3e0a053d2cab to your computer and use it in GitHub Desktop.
Save TheSeamau5/92175edc3e0a053d2cab to your computer and use it in GitHub Desktop.
Binary Tree in Elm
type BinaryTree a = Nil | Node (BinaryTree a) a (BinaryTree a)
insert : comparable -> BinaryTree comparable -> BinaryTree comparable
insert node tree =
case tree of
Nil -> Node Nil node Nil
Node left leaf right ->
if | leaf == node -> Node left leaf right
| leaf < node -> Node left leaf (insert node right)
| leaf > node -> Node (insert node left) leaf right
isEmpty : BinaryTree a -> Bool
isEmpty tree =
case tree of
Nil -> True
_ -> False
contains : comparable -> BinaryTree comparable -> Bool
contains node tree =
case tree of
Nil -> False
Node left leaf right ->
if | leaf == node -> True
| leaf < node -> contains leaf left
| leaf > node -> contains leaf right
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment