Class: CompSci::ChildNode

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

Overview

like Node but with a reference to its parent

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #value

Instance Method Summary collapse

Methods inherited from Node

#inspect, #new_child, #to_s

Constructor Details

#initialize(value) ⇒ ChildNode

Returns a new instance of ChildNode.



43
44
45
46
# File 'lib/compsci/tree.rb', line 43

def initialize(value)
  @parent = nil
  super(value)
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



41
42
43
# File 'lib/compsci/tree.rb', line 41

def parent
  @parent
end

Instance Method Details

#add_child(node) ⇒ Object



48
49
50
51
52
# File 'lib/compsci/tree.rb', line 48

def add_child(node)
  node.parent ||= self
  raise "node has a parent: #{node.parent}" if node.parent != self
  super(node)
end

#add_parent(node) ⇒ Object



54
55
56
57
# File 'lib/compsci/tree.rb', line 54

def add_parent(node)
  @parent = node
  super(node)
end