Created
January 5, 2015 15:15
-
-
Save TheSeamau5/92175edc3e0a053d2cab to your computer and use it in GitHub Desktop.
Binary Tree in Elm
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
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