Skip to content

Instantly share code, notes, and snippets.

@brookr
Forked from philwilt/bst
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brookr/a57afc2110eadce3eda6 to your computer and use it in GitHub Desktop.
Save brookr/a57afc2110eadce3eda6 to your computer and use it in GitHub Desktop.
class BinarySearchTree
constructor: () ->
@val = null
@size = 0
@right = null
@left = null
insert: (val) ->
if @val == val
false
else if @val == null
@val = val
@size++
else if val < @val
@left = new BinarySearchTree unless @left
@size++ if @left.insert(val)
else
@right = new BinarySearchTree unless @right
@size++ if @right.insert(val)
contains: (val) ->
if @val == val
true
else if val < @val
return false unless @left
@left.contains(val)
else
return false unless @right
@right.contains(val)
exports.BinarySearchTree = BinarySearchTree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment