Skip to content

Instantly share code, notes, and snippets.

@burke
Created March 17, 2009 09:13
Show Gist options
  • Save burke/80417 to your computer and use it in GitHub Desktop.
Save burke/80417 to your computer and use it in GitHub Desktop.
class Tree
def initialize*d;@d,=d;end
def to_s;@l||@r?"<#{@d},<#{@l}>,<#{@r}>>":@d;end
def total;(@d.is_a?(Numeric)?@d:0)+(@l?@l.total: 0)+(@r?@r.total: 0);end
def insert d
alias g instance_variable_get
p=lambda{|s,o|d.to_s.send(o,@d.to_s)&&
(g(s).nil??instance_variable_set(s,Tree.new(d)):g(s).insert(d))}
@d?p[:@l,:<]||p[:@r,:>]:@d=d
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment