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
Link to originalCompressed 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, andbadisappear, butanremains because it is stored. Compression shortens paths in edges, not in characters: an edge labelledbarstill requires checking three characters.
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