Skip to main content

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)
Space Complexity: O(ALPHABET_SIZE * k * n) where n is number of words

Example

const trie = new Trie<{ id: string }>();
trie.insert('hello', { id: '1' });
const results = trie.searchByPrefix('hel');

Type Parameters

T

T Type of data stored with each word

Constructors

Constructor

new Trie<T>(options?): Trie<T>
Defined in: trie.ts:102 Creates a new Trie instance

Parameters

options?
Configuration options
caseInsensitive?
boolean
maxResults?
number

Returns

Trie<T>

Throws

Error if options validation fails

Accessors

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(word): boolean
Defined in: trie.ts:219 Deletes a word from the Trie

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(word): boolean
Defined in: trie.ts:175 Checks if a word exists in the Trie

Parameters

word
string

Returns

boolean

insert()

insert(word, data): this
Defined in: trie.ts:127 Inserts a word with associated data into the Trie

Parameters

word
string
data
T

Returns

this This Trie instance for chaining

insertMany()

insertMany(entries): this
Defined in: trie.ts:156 Bulk insert multiple words with data

Parameters

entries
[string, T][]

Returns

this This Trie instance for chaining
search(word): T | null
Defined in: trie.ts:167 Searches for an exact word match

Parameters

word
string

Returns

T | null The associated data or null if not found

searchByPrefix()

searchByPrefix(prefix, limit?): TrieSearchResult<T>[]
Defined in: trie.ts:194 Finds all words starting with the given prefix

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(prefix): boolean
Defined in: trie.ts:183 Checks if any word starts with the given prefix

Parameters

prefix
string

Returns

boolean