Class: LambdaGem::Node
- Inherits:
-
Object
- Object
- LambdaGem::Node
- Defined in:
- lib/node.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#left ⇒ Object
Returns the value of attribute left.
-
#right ⇒ Object
Returns the value of attribute right.
Instance Method Summary collapse
-
#initialize(data = nil) ⇒ Node
constructor
A new instance of Node.
- #leaf? ⇒ Boolean
- #purge ⇒ Object
- #traverse(order = :infix) ⇒ Object
Constructor Details
#initialize(data = nil) ⇒ Node
Returns a new instance of Node.
9 10 11 |
# File 'lib/node.rb', line 9 def initialize data=nil @data, @left, @right = data, nil, nil end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
7 8 9 |
# File 'lib/node.rb', line 7 def data @data end |
#left ⇒ Object
Returns the value of attribute left.
7 8 9 |
# File 'lib/node.rb', line 7 def left @left end |
#right ⇒ Object
Returns the value of attribute right.
7 8 9 |
# File 'lib/node.rb', line 7 def right @right end |
Instance Method Details
#leaf? ⇒ Boolean
13 14 15 |
# File 'lib/node.rb', line 13 def leaf? @left.nil? and @right.nil? end |
#purge ⇒ Object
17 18 19 |
# File 'lib/node.rb', line 17 def purge @data, @left, @right = nil, nil, nil end |
#traverse(order = :infix) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/node.rb', line 21 def traverse order=:infix if leaf? @data else left_child, right_child = @left.traverse(order), @right.traverse(order) strs = case order when :prefix then [@data, left_child, right_child] when :infix then [left_child, @data, right_child] when :postfix then [left_child, right_child, @data] else [] end "(" + strs.join(" ") + ")" end end |