Class: CompSci::ChildNode

Inherits:
Node
  • Object
show all
Defined in:
lib/compsci/node.rb

Overview

like Node but with a reference to its parent

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #metadata, #value

Instance Method Summary collapse

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

#parentObject

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

#genObject

O(log n) recursive


152
153
154
# File 'lib/compsci/node.rb', line 152

def gen
  @parent ? @parent.gen + 1 : 0
end

#siblingsObject


156
157
158
# File 'lib/compsci/node.rb', line 156

def siblings
  @parent ? @parent.children : []
end