Skip to content

Instantly share code, notes, and snippets.

@scottcreynolds
Created December 9, 2013 22:25
Show Gist options
  • Save scottcreynolds/7882110 to your computer and use it in GitHub Desktop.
Save scottcreynolds/7882110 to your computer and use it in GitHub Desktop.
class BST
attr_accessor :data, :left, :right
def initialize(data)
@data = data
end
def insert(data)
if data <= self.data
do_insert("left", data)
else
do_insert("right", data)
end
end
def do_insert(side, data)
if self.send(side)
self.send(side).insert(data)
else
self.send("#{side}=",BST.new(data))
end
end
def each(&block)
self.left.each(&block) if self.left
yield data
self.right.each(&block) if self.right
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment