Class: CompSci::ChildNode

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

Overview

like Node but with a reference to its parent

Direct Known Subclasses

ChildFlexNode

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #value

Instance Method Summary collapse

Methods inherited from Node

#inspect, #to_s

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

#parentObject

Returns the value of attribute parent.



70
71
72
# File 'lib/compsci/node.rb', line 70

def parent
  @parent
end

Instance Method Details

#genObject

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

#siblingsObject



82
83
84
# File 'lib/compsci/node.rb', line 82

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