Trie complexity and searching
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A trie, or prefix tree, is a data structure built for storing strings by shared prefixes instead of storing each string independently. It is especially useful when you need fast prefix queries such as autocomplete, dictionary lookup, or routing-table style matching. The main complexity result is that operations depend on key length rather than the total number of stored words.
How a Trie Stores Data
Each edge in a trie represents one character, and each path from the root corresponds to a prefix. A node often contains:
- a mapping from character to child node
- a flag indicating whether a full word ends at that node
Here is a simple Python implementation:
This is the standard shape behind most trie discussions.
Search Complexity
Searching for a word of length m takes O(m) time because the algorithm follows at most one edge per character.
That is the key trie idea: lookup does not depend directly on how many total words are stored, only on how long the query is.
Example:
If the searched word is not present, failure can happen before processing all characters, but worst-case complexity is still O(m).
Insert and Delete Complexity
Insertion is also O(m) for a word of length m, because each character is processed once.
Deletion is slightly more involved because nodes may need to be cleaned up only if they are no longer shared by other words.
Deletion still has O(m) traversal cost, though cleanup logic adds implementation complexity.
Prefix Queries Are Where Tries Shine
A trie is most compelling when you care about prefixes, not just exact words. Once you reach the node for a prefix, every descendant represents a completion.
Simple prefix collection:
Exact search and prefix search both start with the same O(m) traversal, which is why tries are popular for autocomplete engines.
Space Complexity and Tradeoffs
The main downside of a trie is memory usage. If you store many sparse branches, the child maps at each node can consume much more memory than a hash set of complete words.
Approximate considerations:
- time for search and insert:
O(m) - memory: proportional to total number of characters stored across all nodes
In practice, space usage depends heavily on alphabet size and representation. An array of fixed child pointers is fast but wasteful for sparse alphabets. A dictionary of children is more memory-efficient but slightly slower.
When a Trie Is Better Than a Hash Set
Use a trie when you need:
- prefix search
- autocomplete
- lexicographic traversal
- longest-prefix matching
Use a hash set when you need only exact membership checks and want simpler code with lower memory overhead.
That distinction matters more than theoretical lookup complexity alone.
Common Pitfalls
- Assuming trie lookup is
O(1)just because it feels tree-like. - Ignoring memory cost when storing large sparse alphabets.
- Forgetting the end-of-word marker and treating prefixes as full words.
- Using a trie when a hash set would be simpler for exact-only lookup.
- Overlooking cleanup logic when implementing delete.
Summary
- Trie operations scale with key length, usually
O(m). - Search, insert, and prefix lookup all follow one edge per character.
- Prefix queries are the main reason to choose a trie.
- The tradeoff is increased memory usage compared with simpler structures.
- Choose node representation based on alphabet size and workload.
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.