Class: CompSci::ChildNode
Overview
like Node but with a reference to its parent
Direct Known Subclasses
Instance Attribute Summary collapse
-
#parent ⇒ Object
Returns the value of attribute parent.
Attributes inherited from Node
Instance Method Summary collapse
-
#gen ⇒ Object
O(log n) recursive.
-
#initialize(value, children: []) ⇒ ChildNode
constructor
A new instance of ChildNode.
- #set_child(idx, node) ⇒ Object
- #set_parent(idx, node) ⇒ Object
- #siblings ⇒ Object
Methods inherited from Node
Constructor Details
#initialize(value, children: []) ⇒ ChildNode
Returns a new instance of ChildNode.
72 73 74 75 |
# File 'lib/compsci/node.rb', line 72 def initialize(value, children: []) @parent = nil super(value, children: children) end |
Instance Attribute Details
#parent ⇒ Object
Returns the value of attribute parent.
70 71 72 |
# File 'lib/compsci/node.rb', line 70 def parent @parent end |
Instance Method Details
#gen ⇒ Object
O(log n) recursive
78 79 80 |
# File 'lib/compsci/node.rb', line 78 def gen @parent ? @parent.gen + 1 : 0 end |
#set_child(idx, node) ⇒ Object
86 87 88 89 90 |
# File 'lib/compsci/node.rb', line 86 def set_child(idx, node) node.parent ||= self raise "node has a parent: #{node.parent}" if node.parent != self @children[idx] = node end |
#set_parent(idx, node) ⇒ Object
92 93 94 95 |
# File 'lib/compsci/node.rb', line 92 def set_parent(idx, node) @parent = node @parent.set_child(idx, self) end |
#siblings ⇒ Object
82 83 84 |
# File 'lib/compsci/node.rb', line 82 def siblings @parent ? @parent.children : [] end |