Class: CompSci::ChildFlexNode

Inherits:
FlexNode show all
Defined in:
lib/compsci/flex_node.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #metadata, #value

Instance Method Summary collapse

Methods inherited from FlexNode

#bf_search, #df_search, #new_child, #open_parent, #open_parent?, #push

Methods inherited from Node

#[], #[]=, #display, display_line, #inspect, #to_s

Constructor Details

#initialize(value, metadata: {}) ⇒ ChildFlexNode

Returns a new instance of ChildFlexNode.



65
66
67
68
# File 'lib/compsci/flex_node.rb', line 65

def initialize(value, metadata: {})
  super(value, metadata: )
  @parent = nil
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



63
64
65
# File 'lib/compsci/flex_node.rb', line 63

def parent
  @parent
end

Instance Method Details

#add_child(node) ⇒ Object



79
80
81
82
83
# File 'lib/compsci/flex_node.rb', line 79

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

#add_parent(node) ⇒ Object



85
86
87
88
# File 'lib/compsci/flex_node.rb', line 85

def add_parent(node)
  @parent = node
  @parent.add_child(self)
end

#genObject

O(log n) recursive



71
72
73
# File 'lib/compsci/flex_node.rb', line 71

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

#siblingsObject



75
76
77
# File 'lib/compsci/flex_node.rb', line 75

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