Class: CompSci::FlexNode

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

Overview

FlexNode API is #add_child, #add_parent, #new_child

Direct Known Subclasses

ChildFlexNode

Instance Attribute Summary

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, metadata: {}) ⇒ FlexNode

Returns a new instance of FlexNode.



11
12
13
14
15
# File 'lib/compsci/flex_node.rb', line 11

def initialize(value, metadata: {})
  @value = value
  @children = []
  @metadata = 
end

Instance Method Details

#add_child(node) ⇒ Object



17
18
19
# File 'lib/compsci/flex_node.rb', line 17

def add_child(node)
  @children << node
end

#add_parent(node) ⇒ Object



25
26
27
# File 'lib/compsci/flex_node.rb', line 25

def add_parent(node)
  node.add_child(self)
end

#bf_search(&blk) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/compsci/flex_node.rb', line 39

def bf_search(&blk)
  destinations = [self]
  while !destinations.empty?
    node = destinations.shift
    return node if yield node
    destinations += node.children
  end
  nil
end

#df_search(&blk) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/compsci/flex_node.rb', line 29

def df_search(&blk)
  return self if yield self
  stop_node = nil
  @children.each { |child|
    stop_node = child.df_search(&blk)
    break if stop_node
  }
  stop_node
end

#new_child(value) ⇒ Object



21
22
23
# File 'lib/compsci/flex_node.rb', line 21

def new_child(value)
  self.add_child self.class.new(value)
end

#open_parent(child_slots) ⇒ Object



53
54
55
# File 'lib/compsci/flex_node.rb', line 53

def open_parent(child_slots)
  self.bf_search { |n| n.open_parent?(child_slots) }
end

#open_parent?(child_slots) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/compsci/flex_node.rb', line 49

def open_parent?(child_slots)
  @children.size < child_slots
end

#push(value, child_slots) ⇒ Object



57
58
59
# File 'lib/compsci/flex_node.rb', line 57

def push(value, child_slots)
  self.open_parent(child_slots).new_child value
end