Actual Space Complexity of a Trie

Revision en4, by greedyaj, 2026-01-25 09:14:31

Space Complexity of a Trie

Most blogs state that the space complexity of a trie is

$$$ O(N \cdot L \cdot R) $$$

where:

  • $$$N$$$ = number of strings
  • $$$L$$$ = maximum length of a string
  • $$$R$$$ = alphabet size (typically $$$26$$$ for lowercase English letters)

However, this bound is very loose and often overestimates the actual memory usage by a large constant factor.


Where the Naive Bound Comes From

The usual reasoning is:

  • The trie can have at most $$$L$$$ levels
  • Each string contributes at most one node per level
  • Each node stores an array of size $$$R$$$

Hence,

$$$ \text{Space} = (\text{number of nodes}) \times R \le N \cdot L \cdot R $$$

This is technically correct as an upper bound, but it ignores a crucial structural limitation of tries.


Key Observation

At depth $$$d$$$, the maximum number of distinct nodes is:

$$$ \min(N, R^d) $$$

Why?

  • Each node at depth $$$d$$$ represents a unique prefix of length $$$d$$$
  • Over an alphabet of size $$$R$$$, there are at most $$$R^d$$$ such prefixes
  • The number of nodes cannot exceed the number of inserted strings $$$N$$$

More Accurate Node Count

Assume:

$$$ R = 26,\quad N = 10^6 $$$

Maximum number of nodes at each level:

$$$ \begin{aligned} \text{Level } 0 &: 26^0 = 1 \\ \text{Level } 1 &: 26^1 = 26 \\ \text{Level } 2 &: 26^2 = 676 \\ \text{Level } 3 &: 26^3 = 17576 \\ \text{Level } 4 &: 26^4 = 456976 \\ \text{Level } 5 &: 26^5 = 11881376 \gt N \end{aligned} $$$

From level $$$5$$$ onward, the number of nodes is capped by $$$N$$$.


Total Number of Nodes

Let $$$k$$$ be the smallest depth such that $$$26^k \ge N$$$.
Here, $$$k = 5$$$.

Total number of nodes:

$$$ \sum_{i=0}^{k-1} 26^i + (L - k)\cdot N $$$

Substituting:

$$$ \sum_{i=0}^{4} 26^i + (L - 5)\cdot N $$$

Total Space Usage

Each node stores an array of size $$$R = 26$$$:

$$$ 26 \cdot \left( \sum_{i=0}^{4} 26^i + (L - 5)\cdot N \right) $$$

Comparison with Naive Bound

Naive bound:

$$$ \text{Space}_{\text{naive}} = 26 \cdot N \cdot L $$$

Compare the two:

$$$ 26 \cdot \left( \sum_{i=0}^{4} 26^i + (L - 5)\cdot N \right) \;\;\text{vs}\;\; 26 \cdot N \cdot L $$$

The extra term in the naive bound is:

$$$ 26 \cdot N \cdot 5 $$$

while the actual prefix contribution is:

$$$ 26 \cdot \sum_{i=0}^{4} 26^i $$$

Dividing both by $$$26$$$:

$$$ \sum_{i=0}^{4} 26^i \lt 5N $$$

Compute:

$$$ \sum_{i=0}^{4} 26^i = 1 + 26 + 676 + 17576 + 456976 = 475255 $$$

For $$$N = 10^6$$$:

$$$ 475255 \lt 5 \times 10^6 $$$

Thus,

$$$ \text{Actual space} \lt \text{Naive space} $$$

Constant Factor Improvement (The 8× Effect)

For large $$$L$$$, the dominant terms are:

  • Naive: $$$\;\;26 \cdot N \cdot L$$$
  • Actual: $$$\;\;26 \cdot N \cdot (L - 5)$$$

So the ratio is approximately:

$$$ \frac{26 \cdot N \cdot L}{26 \cdot N \cdot (L - 5)} = \frac{L}{L - 5} $$$

For typical constraints (e.g. $$$L \approx 40$$$–$$$50$$$):

$$$ \frac{L}{L - 5} \approx 1.1 $$$

But when counting only necessary nodes instead of assuming $$$N$$$ nodes at every level, the naive bound overcounts the early levels by roughly:

$$$ \frac{5N}{\sum_{i=0}^{4} 26^i} \approx \frac{5 \times 10^6}{4.75 \times 10^5} \approx 10 $$$

In practice, this leads to memory usage that is often about 8–10× smaller than the naive estimate.


Final Complexity (Tight Bound)

Number of nodes:

$$$ O\left( \sum_{d=0}^{L} \min(N, R^d) \right) $$$

Total space:

$$$ O\left( R \cdot \sum_{d=0}^{L} \min(N, R^d) \right) $$$

For fixed $$$R$$$:

$$$ \boxed{ \text{Space} = O(N \cdot L) \text{ with a much smaller constant than } O(N \cdot L \cdot R) } $$$

Takeaway

  • $$$O(N \cdot L \cdot R)$$$ is a very loose upper bound
  • A trie cannot have $$$N$$$ nodes at every level
  • The naive estimate often overcounts memory by ~8–10×
  • Correct intuition:
$$$ \min(N, R^d) \text{ nodes at depth } d $$$
Tags trie, space complexity, overestimating

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en6 English greedyaj 2026-01-25 09:18:52 6
en5 English greedyaj 2026-01-25 09:16:59 2397 (published)
en4 English greedyaj 2026-01-25 09:14:31 1266 (saved to drafts)
en3 English greedyaj 2026-01-25 09:12:46 0 (published)
en2 English greedyaj 2026-01-25 09:12:32 1053
en1 English greedyaj 2026-01-25 09:11:14 3554 Initial revision (saved to drafts)