Class: Jisx0402::Tree::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNode

Returns a new instance of Node.



25
26
27
28
# File 'lib/jisx0402/tree.rb', line 25

def initialize
  @data = {}
  @value = nil
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



23
24
25
# File 'lib/jisx0402/tree.rb', line 23

def value
  @value
end

Instance Method Details

#insert(key, remain, val) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/jisx0402/tree.rb', line 30

def insert(key, remain, val)
  @data[key] ||= Node.new
  if remain.empty?
    @data[key].value = val
  else
    @data[key].insert(remain.shift, remain, val)
  end
end

#search(key, remain) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/jisx0402/tree.rb', line 39

def search(key, remain)
  return nil unless @data[key]
  if remain.empty?
    @data[key].values
  else
    @data[key].search(remain.shift, remain)
  end
end

#valuesObject



48
49
50
# File 'lib/jisx0402/tree.rb', line 48

def values
  [value, *@data.values.map(&:values)].compact
end