Autocomplete using a trie
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 to Autocomplete
Autocomplete is a common feature in modern search engines, text editors, and much other software, improving user experience by predicting the completion of the current word or phrase the user is typing. One powerful data structure that can be used to implement autocomplete efficiently is the trie, pronounced as "try."
Understanding Trie
A trie, also known as a prefix tree, is a tree-like data structure that stores a dynamic set of strings, usually representing words. It structures words by their prefixes, which allows the retrieval of auto-completion suggestions at an optimal speed.
Structure of a Trie
A trie consists of the following components:
- Root Node: The starting point of the trie, representing an empty string.
- Edges: Connecting lines that represent each character of the string.
- Nodes (Trie Nodes): Points at the end of each edge holding references to subsequent characters/edges. Each node also contains a boolean flag indicating if it marks the end of a word.
Adding Words to a Trie
When adding a word to a trie:
- Start from the root node.
- For each character in the word:
- Check if an edge exists for the current character.
- If it doesn't exist, create a new node.
- Move to the next node via this edge.
- After adding all characters, mark the last node as the end of the word.
Searching for Words in a Trie
To search for a word or predict autocompletions:
- Start at the root and traverse down the trie following the characters of the prefix.
- If at any point, an edge for the next character does not exist, return no completions.
- If the prefix is fully traversed, retrieve all words starting from this point, which involves performing a depth-first search (DFS) from the last node of the prefix.
Implementing Trie for Autocomplete
The core functionality of implementing autocomplete using a trie relies on the efficient storage and retrieval of words based on prefixes.
- Speed: Trie enables O(N) time complexity for search and insertion, where N is the length of the word. This provides rapid prefix-based search and completion.
- Efficiency: Although it uses more memory compared to other data structures, it is efficient for storing words with a common prefix.
- Flexibility: Can handle various character sets, making it useful for implementing dictionaries across multiple languages.
Related reading
- Autogram Algorithms
- Average case algorithm analysis using Kolmogorov Incompressibility Method
- Average of 3 long integers
- Average of two strings in alphabetical/lexicographical order
- Average values of dictionaries
- awaitable Task based queue
- Average Runtime of Quickselect
- Avoiding overflow in integer multiplication followed by division

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.