Class: Trie<T>
Defined in: trie.ts:91 Trie (Prefix Tree) for efficient prefix-based autocomplete Time Complexity:- insert: O(k) where k is word length
- search: O(k) for exact match
- startsWith: O(k + m) where m is number of results
- delete: O(k)
Example
Type Parameters
T
T
Type of data stored with each word
Constructors
Constructor
new Trie<Defined in: trie.ts:102 Creates a new Trie instanceT>(options?):Trie<T>
Parameters
options?
Configuration optionscaseInsensitive?
boolean
maxResults?
number
Returns
Trie<T>
Throws
Error if options validation failsAccessors
length
Get Signature
get length(): number
Defined in: trie.ts:228
Returns the number of words in the Trie
Returns
number
Methods
clear()
clear(): void
Defined in: trie.ts:235
Clears all words from the Trie
Returns
void
delete()
delete(Defined in: trie.ts:219 Deletes a word from the Trieword):boolean
Parameters
word
string
Returns
boolean
True if the word was deleted
getAllWords()
getAllWords(): object[]
Defined in: trie.ts:243
Gets all words in the Trie
Returns
object[]
has()
has(Defined in: trie.ts:175 Checks if a word exists in the Trieword):boolean
Parameters
word
string
Returns
boolean
insert()
insert(Defined in: trie.ts:127 Inserts a word with associated data into the Trieword,data):this
Parameters
word
string
data
T
Returns
this
This Trie instance for chaining
insertMany()
insertMany(Defined in: trie.ts:156 Bulk insert multiple words with dataentries):this
Parameters
entries
[string, T][]
Returns
this
This Trie instance for chaining
search()
search(Defined in: trie.ts:167 Searches for an exact word matchword):T|null
Parameters
word
string
Returns
T | null
The associated data or null if not found
searchByPrefix()
searchByPrefix(Defined in: trie.ts:194 Finds all words starting with the given prefixprefix,limit?):TrieSearchResult<T>[]
Parameters
prefix
string
The prefix to search for
limit?
number
Maximum number of results (defaults to maxResults option)
Returns
TrieSearchResult<T>[]
Array of search results sorted by relevance
startsWith()
startsWith(Defined in: trie.ts:183 Checks if any word starts with the given prefixprefix):boolean
Parameters
prefix
string
Returns
boolean