### Prefix trees

←Return to chapter

## 99.1. Prefix trees

Our main result is achieved by reducing a tree inclusion problem to the following problem.

String prefix matching.Consider the following computational problem over strings. Let Σ\SigmaΣ be a finite alphabet and consider W=(Σ∗)w\mathcal{W} = (\Sigma^*)^w W=(Σ∗)w the set of w w w-tuples of strings over Σ\SigmaΣ. For a string tuple (s 1,…,s w)∈W(s_1, \dots, s_w) \in \mathcal{W}(s 1​,…,s w​)∈W and a set of string tuples D⊆W\mathcal{D} \subseteq \mathcal{W}D⊆W, the w w w-dimensional string prefix matching consists in finding the set

{(p 1,…,p w)∈D ∣ for all 1≤i≤w: p i is a prefix of s i}.\{ (p_1, \dots, p_w) \in \mathcal{D} \ | \ \text{for all }1 \leq i \leq w: p_i\text{ is a prefix of }s_i \}.{(p 1​,…,p w​)∈D ∣ for all 1≤i≤w: p i​ is a prefix of s i​}.

This string problem can be solved using a w w w-dimensional prefix tree. We give a short introduction to prefix trees for the string case but refer to standard literature for more details Knuth, 1999Donald Knuth. 1999. The Art of Computer Programming: Sorting and Searching, Volume 3. Addison-Wesley, Reading MA.

One-dimensional prefix tree.Let P 1,…,P ℓ∈A∗P_1, \dots, P_\ell \in \mathcal{A}^\ast P 1​,…,P ℓ​∈A∗ be strings on some alphabet A\mathcal{A}A. Given an input string s∈A∗s\in\mathcal{A}^\asts∈A∗, we wish to find the set of patterns {P 1≤i≤ℓ∣P i⊆s}\{ P_{1 \leq i \leq \ell} | P_i \subseteq s\}{P 1≤i≤ℓ​∣P i​⊆s}, i.e. P i P_i P i​ is a prefix of s ss.

The prefix tree of P 1,…,P ℓ P_1, \dots, P_\ell P 1​,…,P ℓ​ is a tree with a tree node for each prefix of a pattern. The children of an internal node are the strings that extend the prefix by one character. The root of the tree is the empty string. Each tree node also stores a list of matching patterns, with each pattern stored in the unique corresponding node. Every prefix tree has an empty string node, which is the root of the tree. For every inserted pattern of length at most L LL nodes are inserted, one for every non-empty prefix of the pattern. Thus a one-dimensional prefix tree has at most ℓ⋅L+1\ell \cdot L + 1ℓ⋅L+1 nodes and can be constructed in time O(ℓ⋅L)O(\ell \cdot L)O(ℓ⋅L).

Given an input s∈A∗s \in \mathcal{A}^\asts∈A∗, we can find the set of matching patterns by traversing the prefix tree of P 1,…,P ℓ P_1, \dots, P_\ell P 1​,…,P ℓ​ starting from the root. We report the list of matching patterns at the current node and move to the child node that is still a prefix of s ss, if it exists. This procedure continues until no more such child exists. In total the traversal takes time O(∣s∣)O(|s|)O(∣s∣), as every character of s ss is visited at most once.

Note that in theory the number of reported pattern matches can dominate the runtime of the algorithm. We can avoid this by returning the list of matches as an iterator, stored as a list of pointers to the tree nodes matching lists.

Multi-dimensional prefix tree.A w w w-dimensional prefix tree for w>1 w > 1 w>1 is defined recursively as a one-dimensional prefix tree that at each node stores a w−1 w-1 w−1-dimensional prefix tree. Given an input w w w-tuple (s 1,…,s w)∈(A∗)w(s_1, \dots, s_w) \in (\mathcal{A}^\ast)^w(s 1​,…,s w​)∈(A∗)w, the traversal of the w w w-dimensional prefix tree is done by traversing the one-dimensional prefix tree on the input s 1 s_1s 1​ until no child is a prefix of the input, and then recursively traversing the w−1 w-1 w−1-dimensional prefix tree on (s 2,…,s w)(s_2, \dots, s_w)(s 2​,…,s w​). Similarly to the one-dimensional case, the list of matching patterns is stored at prefix tree nodes and reported during traversal. The traversal thus takes time O(∣s 1∣+⋯+∣s w∣)O(|s_1| + \cdots + |s_w|)O(∣s 1​∣+⋯+∣s w​∣), as every character of s ss is visited at most once.

For ℓ\ellℓ tuples of size w w w of words of maximum length L LL, we can bound the number of nodes of the w w w-dimensional prefix tree by 1+(ℓ⋅L)w 1 + (\ell \cdot L)^w1+(ℓ⋅L)w. The runtime and space complexity of the construction of the w w w-dimensional prefix tree is thus in O((ℓ⋅L)w)O((\ell \cdot L)^w)O((ℓ⋅L)w), summarised in the result:

Proposition 99.1Multi-dimensional string prefix matching
Let D⊆W\mathcal{D} \subseteq \mathcal{W}D⊆W be a set of string tuples and L LL the maximum length of a string in a tuple of D\mathcal{D}D. There is a prefix tree with at most (ℓ⋅L)w+1(\ell \cdot L)^w + 1(ℓ⋅L)w+1 nodes that encodes D\mathcal{D}D that can be used to solve the w w w-dimensional string prefix matching problem in time O(∣s 1∣+⋯+∣s w∣)O(|s_1| + \cdots + |s_w|)O(∣s 1​∣+⋯+∣s w​∣).

←Return to chapter