Created
April 29, 2014 16:27
-
-
Save daniel-trinh/11405333 to your computer and use it in GitHub Desktop.
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
defmodule BST do | |
defstruct data: nil, left: nil, right: nil | |
@spec insert(BST.t, any) :: BST.t | |
def insert(nil, value) do | |
%BST{data: value} | |
end | |
def insert(%BST{data: d, left: l, right: _r} = bst, value) when value > d do | |
%BST{data: d, left: l, right: insert(bst.right, value)} | |
end | |
def insert(%BST{data: d, left: _l, right: r} = bst, value) when value <= d do | |
%BST{data: d, left: insert(bst.left, value), right: r} | |
end | |
end | |
# Dialyzer output: | |
# BST.ex:9: The call _@1:'right'() requires that _@1 is of type atom() | tuple() not #{} | |
# BST.ex:12: The call _@1:'left'() requires that _@1 is of type atom() | tuple() not #{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment