class RubyIndexer::PrefixTree

A PrefixTree is a data structure that allows searching for partial strings fast. The tree is similar to a nested hash structure, where the keys are the characters of the inserted strings.

Example

tree = PrefixTree[String].new
# Insert entries using the same key and value
tree.insert("bar", "bar")
tree.insert("baz", "baz")
# Internally, the structure is analogous to this, but using nodes:
# {
#   "b" => {
#     "a" => {
#       "r" => "bar",
#       "z" => "baz"
#     }
#   }
# }
# When we search it, it finds all possible values based on partial (or complete matches):
tree.search("") # => ["bar", "baz"]
tree.search("b") # => ["bar", "baz"]
tree.search("ba") # => ["bar", "baz"]
tree.search("bar") # => ["bar"]

A PrefixTree is useful for autocomplete, since we always want to find all alternatives while the developer hasn’t finished typing yet. This PrefixTree implementation allows for string keys and any arbitrary value using the generic Value type.

See en.wikipedia.org/wiki/Trie for more information

Constants

>Value

Public Class Methods

new()

Public Instance Methods

delete(key)

insert(key, value)

search(prefix)