Class: CompSci::ChildNode
Overview
like Node but with a reference to its parent
Instance Attribute Summary collapse
-
#parent ⇒ Object
Returns the value of attribute parent.
Attributes inherited from Node
Instance Method Summary collapse
-
#[]=(idx, node) ⇒ Object
update both sides of the relationship.
-
#gen ⇒ Object
O(log n) recursive.
-
#initialize(value, children: 2) ⇒ ChildNode
constructor
A new instance of ChildNode.
- #siblings ⇒ Object
Methods inherited from Node
#[], #display, display_line, #inspect, #to_s
Constructor Details
#initialize(value, children: 2) ⇒ ChildNode
Returns a new instance of ChildNode.
146 147 148 149 |
# File 'lib/compsci/node.rb', line 146 def initialize(value, children: 2) @parent = nil super(value, children: children) end |
Instance Attribute Details
#parent ⇒ Object
Returns the value of attribute parent.
144 145 146 |
# File 'lib/compsci/node.rb', line 144 def parent @parent end |
Instance Method Details
#[]=(idx, node) ⇒ Object
update both sides of the relationship
161 162 163 164 165 |
# File 'lib/compsci/node.rb', line 161 def []=(idx, node) node.parent ||= self raise "node has a parent: #{node.parent}" if node.parent != self @children[idx] = node end |
#gen ⇒ Object
O(log n) recursive
152 153 154 |
# File 'lib/compsci/node.rb', line 152 def gen @parent ? @parent.gen + 1 : 0 end |
#siblings ⇒ Object
156 157 158 |
# File 'lib/compsci/node.rb', line 156 def siblings @parent ? @parent.children : [] end |