Skip to main content

Trie

Trie (prefix tree) for efficient string prefix operations. Space-optimized prefix tree supporting insert, exact search, and prefix-based autocomplete operations. :::note Memory: O(total characters stored) - shared prefixes ::: :::note Operations: O(length of string) for insert/search :::

Example:

Public Methods


insert

inline
Insert a word into the trie.

Parameters

  • w Word to insert (ASCII characters)
Word can be found via search() :::note Duplicate inserts are idempotent :::
const
Search for exact word.

Parameters

  • w Word to search for

Returns

true if word exists in trie

starts_with

const
Find all words starting with prefix.

Parameters

  • prefix Prefix to search for

Returns

Vector of all words starting with prefix Returns empty vector if prefix not found

Private Attributes


root_

Root node.

Private Methods


collect

const
Collect all words with given prefix.