Skip to content

Trie

Defined in: packages/data/src/trie/trie.ts:49

A prefix tree over sequences of a caller-chosen segment type.

Generic over the segment, not hard-coded to string/char: a key is a readonly S[], so S can be a character, a path component, a token, an enum member. Split the string yourself and the same structure indexes routes, namespaces or n-grams without a second implementation.

What a trie buys over a Map is prefix access: withPrefix, hasPrefix and longestPrefixOf are what a hash map cannot answer at all. A plain Map is faster for exact lookup and should be preferred when that is all that is needed.

Time complexity, for a key of k segments: O(k) for get, set, has and delete — independent of how many keys are stored. withPrefix is O(k + size of the subtree). Space is O(total segments), shared across common prefixes.

S

The segment type.

V extends DefinedValue

The value type.

new Trie<S, V>(iterable?): Trie<S, V>

Defined in: packages/data/src/trie/trie.ts:53

Iterable<readonly [readonly S[], V], any, any>

Trie<S, V>

[iterator](): Iterator<readonly [readonly S[], V]>

Defined in: packages/data/src/trie/trie.ts:61

Iterator<readonly [readonly S[], V]>

ITrie.[iterator]


clear(): void

Defined in: packages/data/src/trie/trie.ts:70

Removes all elements from the collection.

void

ITrie.clear


count(): number

Defined in: packages/data/src/trie/trie.ts:66

The number of stored keys. O(1).

number

ITrie.count


delete(key): boolean

Defined in: packages/data/src/trie/trie.ts:122

O(k). Prunes every node left holding neither a value nor a child.

readonly S[]

boolean

ITrie.delete


entries(): IterableIterator<readonly [readonly S[], V]>

Defined in: packages/data/src/trie/trie.ts:163

Every stored entry, in depth-first order.

IterableIterator<readonly [readonly S[], V]>

ITrie.entries


get(key): V | undefined

Defined in: packages/data/src/trie/trie.ts:78

O(k).

readonly S[]

V | undefined

ITrie.get


has(key): boolean

Defined in: packages/data/src/trie/trie.ts:117

O(k). Whether this exact key is stored — see hasPrefix for the other question.

readonly S[]

boolean

ITrie.has


hasPrefix(prefix): boolean

Defined in: packages/data/src/trie/trie.ts:188

Whether any stored key starts with prefix. O(k).

Distinct from has: a prefix can exist as a path without being a key.

readonly S[]

boolean

ITrie.hasPrefix


keys(): IterableIterator<readonly S[]>

Defined in: packages/data/src/trie/trie.ts:149

Every stored key, in depth-first order.

IterableIterator<readonly S[]>

ITrie.keys


longestPrefixOf(key): readonly S[] | undefined

Defined in: packages/data/src/trie/trie.ts:199

The longest stored key that is a prefix of key, or undefined if none is. O(k).

This is the lookup behind longest-prefix routing and dictionary matching: the whole point of holding the keys in a trie rather than a Map.

readonly S[]

readonly S[] | undefined

ITrie.longestPrefixOf


set(key, value): void

Defined in: packages/data/src/trie/trie.ts:90

O(k). Overwrites a key already stored.

The empty key is legal and addresses the root, so a trie can carry a “default” value alongside its prefixed ones.

readonly S[]

V

void

ITrie.set


values(): IterableIterator<V>

Defined in: packages/data/src/trie/trie.ts:156

Every stored value, in the same order as keys.

IterableIterator<V>

ITrie.values


withPrefix(prefix): IterableIterator<readonly [readonly S[], V]>

Defined in: packages/data/src/trie/trie.ts:171

Every entry whose key starts with prefix, including prefix itself when it is stored. O(k + the size of the subtree).

readonly S[]

IterableIterator<readonly [readonly S[], V]>

ITrie.withPrefix