Class: Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil, *sub_nodes) ⇒ Node

Returns a new instance of Node.



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

def initialize(data=nil, *sub_nodes)
  @data = data
  sub_nodes.each { |sub_node| check_sub_node_type(sub_node) }
  @sub_nodes = sub_nodes
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#sub_nodesObject (readonly)

Returns the value of attribute sub_nodes.



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

def sub_nodes
  @sub_nodes
end

Instance Method Details

#<<(sub_node) ⇒ Object



28
29
30
31
# File 'lib/node.rb', line 28

def <<(sub_node)
  check_sub_node_type(sub_node)
  @sub_nodes << sub_node
end

#[](sub_node_index) ⇒ Object



19
20
21
# File 'lib/node.rb', line 19

def [](sub_node_index)
  @sub_nodes[sub_node_index]
end

#[]=(sub_node_index, sub_node) ⇒ Object



23
24
25
26
# File 'lib/node.rb', line 23

def []=(sub_node_index, sub_node)
  check_sub_node_type(sub_node)
  @sub_nodes[sub_node_index] = sub_node
end

#delete(sub_node) ⇒ Object



47
48
49
# File 'lib/node.rb', line 47

def delete(sub_node)
  @sub_nodes.delete(sub_node)
end

#each_index(&block) ⇒ Object



43
44
45
# File 'lib/node.rb', line 43

def each_index(&block)
  @sub_nodes.each_with_index { |sub_node, index| block[index] }
end

#each_node(&block) ⇒ Object Also known as: each



37
38
39
# File 'lib/node.rb', line 37

def each_node(&block)
  @sub_nodes.each_with_index { |sub_node, index| block[sub_node] }
end

#each_pair(&block) ⇒ Object



33
34
35
# File 'lib/node.rb', line 33

def each_pair(&block)
  @sub_nodes.each_with_index { |sub_node, i| block[i, sub_node] }
end

#leaf?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/node.rb', line 58

def leaf?
  @sub_nodes.empty?
end

#nb_sub_nodesObject Also known as: size, length



51
52
53
# File 'lib/node.rb', line 51

def nb_sub_nodes
  @sub_nodes.size
end

#pre_depth_first(&block) ⇒ Object



62
63
64
65
66
# File 'lib/node.rb', line 62

def pre_depth_first(&block)
  block[self]
  @sub_nodes.each { |sub_node| sub_node.pre_depth_first(&block) }
  nil
end