Data Structures19 sections · 729 units
Open in Course

Compressed Tries (Radix Trees)

Space optimization

Standard tries waste space when long strings share no common prefixes. Consider storing "algorithm" and "zebra". "algorithm" alone needs 99 nodes, each with only one child. Even "zebra" wastes 55 single-child nodes.

Compressed tries (radix trees) merge chains of single-child nodes: Standard trie for ["test", "testing"]:

root → t → e → s → t → [end]
                        ↓
                   i → n → g → [end]

Compressed trie:

root → "test" → [end]
              ↓
         "ing" → [end]

Edges store strings instead of single characters.

This reduces node count for sparse tries. Implementation is more complex. You need to handle splitting edges when inserting a word that shares a partial prefix. Time: O(L)O(L). Space: O(L)O(L).