class RubyTrie::Trie
Trie
implements a Trie
structure via an underlying implementation using Tree Trie
has a single attribute (root) which is the root TrieNode
Attributes
root[R]
The Trie
just holds the root node
Public Class Methods
new()
click to toggle source
create an empty Trie
# File lib/ruby_trie.rb, line 107 def initialize @root = TrieNode.new('') end
Public Instance Methods
add(string, value) → TrieContent
click to toggle source
add a string with optional value to the Trie
Note - will overwrite the value if the node already exists
# File lib/ruby_trie.rb, line 117 def add(string, value=true) @root.add_node(string, value) end
children(string) → Array
click to toggle source
get all the children of a given prefix including the prefix, if it exists itself
# File lib/ruby_trie.rb, line 133 def children(string) parent = @root.get_node(string) return nil unless parent parent.inject([]) do |a,n| a << n.content.string if n.content a end end
children_content(string) → Array
click to toggle source
get the content of all children of a given prefix including the prefix, if it exists itself
# File lib/ruby_trie.rb, line 162 def children_content(string) parent = @root.get_node(string) return nil unless parent parent.inject([]) do |a,n| a << n.content.to_a if n.content a end end
children_with_values(string) → Array
click to toggle source
get all the children of a given prefix with thier values (as a [key,value] pair) including the prefix, if it exists itself
# File lib/ruby_trie.rb, line 148 def children_with_values(string) parent = @root.get_node(string) return nil unless parent parent.inject([]) do |a,n| a << [n.content.string, n.content.value] if n.content a end end
get(string) → Object
click to toggle source
get the value at a node returns nil if the node does not exist
# File lib/ruby_trie.rb, line 125 def get(string) @root.get_node_value(string) end