Lukas' Notes

Definition

Trie

A trie stores a finite set of strings over an alphabet as a rooted tree, with one node per distinct prefix:

The root represents the empty string . Each edge appends one letter, so reading a root-to-node path spells the prefix represented by that node. Outgoing edges have distinct labels: a next letter selects at most one child.

The terminal nodes mark complete stored strings. Reaching a node means the path exists; reaching a terminal node means the string is stored. A terminal node may still have children. Shared prefixes use the same path, rather than separate copies.

The node labels above explain the paths; an implementation need not store a full prefix string at every node. If , the root is terminal. For , only the non-terminal root remains.

Compressed Trie

Definition

Compressed Trie

A compressed trie represents the same set of strings as a trie, but removes every non-root, non-terminal node with exactly one child. Each resulting chain becomes one edge whose label concatenates the original labels:

The root, branching nodes, and terminal nodes remain. Edges carry non-empty strings, and outgoing edges still have distinct first characters. Reading a root-to-node path spells the same string before and after compression; membership still requires ending at a terminal node.

In the diagram, a, b, and ba disappear, but an remains because it is stored. Compression shortens paths in edges, not in characters: an edge labelled bar still requires checking three characters.

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.