Class: Abroad::Serializers::Trie

Inherits:
Object
  • Object
show all
Defined in:
lib/abroad/serializers/trie.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = nil) ⇒ Trie

Returns a new instance of Trie.


7
8
9
# File 'lib/abroad/serializers/trie.rb', line 7

def initialize(root = nil)
  @root = root || TrieNode.new
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root


5
6
7
# File 'lib/abroad/serializers/trie.rb', line 5

def root
  @root
end

Instance Method Details

#add(key_enum, value) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/abroad/serializers/trie.rb', line 11

def add(key_enum, value)
  node = root

  key_enum.each do |key|
    if node.has_child?(key)
      node = node.child_at(key)
    else
      node = node.add_child(key, TrieNode.new)
    end
  end

  node.value = value
end

#find(key_enum) ⇒ Object


25
26
27
28
29
30
31
32
# File 'lib/abroad/serializers/trie.rb', line 25

def find(key_enum)
  node = root
  key_enum.each do |key|
    node = node.child_at(key)
    return nil unless node
  end
  node.value
end