Created
March 17, 2009 09:13
-
-
Save burke/80417 to your computer and use it in GitHub Desktop.
This file contains 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
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