A Trie for Prefix Completions

A trie can store query completions by common prefixes, allowing navigation to one prefix and enumeration of its matching descendants.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a partial query
  2. Choose a completion order
  3. Follow the prefix, then enumerate allowed descendants
  4. Filter and order suggestions using the public contract

Trace a partial query

With data, database, and date stored, prefix dat can reach all three. Prefix data reaches data and database. See following trie paths.

Choose a completion order

Alphabetical order and popularity answer different questions. Keep the dictionary traversal and ranking separate when both are needed. Order by keys describes the tie rule.[1]

Follow the prefix, then enumerate allowed descendants

Store rail, rails and rain, all sharing prefix rai. Walking that prefix reaches a node with branches L and N. Continuing with l makes rain unreachable and leaves rail and rails as possible completions. See trie prefix traversal.

rai → rail, rails, rain
rail → rail, rails
Exact terminal marker keeps rail available although it has a child

Filter and order suggestions using the public contract

A trie finds prefixes but supplies no built-in relevance policy. Exclude suggestions the current reader may not see. Test a complete term, an empty prefix and a prefix with no descendants.

References

  1. Christopher D. Manning, Prabhakar Raghavan and Hinrich Schütze, Introduction to Information Retrieval ↩