Lukas' Notes

data-structures string-algorithms

Definition

Trie

A trie is a rooted tree that represents a finite set of strings over an alphabet .

Each edge is labelled by one letter from , and outgoing edges of the same node have pairwise distinct labels. The root represents the empty string . For any node , the concatenation of edge labels on the path from the root to is the prefix represented by .

A node is marked as terminal iff its represented prefix is a string in .

Compressed Trie

Definition

Compressed Trie

A compressed trie is a trie where non-branching chains are contracted into single edges.

Edges may be labelled by whole strings, and terminal nodes are preserved.

Link to original

Examples

Word membership

Let .

The string is in because the path

ends in a terminal node. The string is not in if the node for is not terminal, even though it is a prefix of stored strings.

Shared prefixes

The strings and share the prefix . In a trie, the path for this prefix is stored once, and the two strings branch only at the last edge.

A standard trie groups strings by prefixes, not by suffixes.