Overview
A Trie (pronounced “try”), also called a prefix tree or digital tree, is a tree-like data structure used for efficiently storing and retrieving strings. Each node represents a single character, and paths from the root to special end markers represent complete words. The UCX DSA package provides a completeTrie implementation with TrieNode for building efficient string-based systems.
Use Cases
Autocomplete
Implement search suggestions and autocomplete features
Spell Checking
Validate words and suggest corrections efficiently
Prefix Matching
Find all strings with a common prefix quickly
Dictionary
Store and search large vocabularies with shared prefixes
TrieNode Class
TheTrieNode class represents a single node in the trie.
The trie uses a special end marker (
"*") to indicate that a path represents a complete word.Trie Class
TheTrie class provides the main interface for trie operations.
Creating a Trie
Core Operations
Insert Words
Add words to the trie. Each character becomes a node in the path.
Words that share prefixes will share nodes in the trie, making it space-efficient for storing related words.
Advanced Operations
Prefix Matching
Find all words that start with a given prefix:Autocomplete / Suggestions
Thesuggest() method provides fuzzy prefix matching by progressively removing characters:
How suggest() works
How suggest() works
The
suggest() method recursively removes the last character until it finds a valid prefix:- Try to find words with prefix “appx” → not found
- Try to find words with prefix “app” → found matches
- Return the matches
List All Words
Retrieve all words stored in the trie:Deep Copy
Create an independent copy of a trie:Complete Example
Visualization
Visualize the trie structure using theTrieDraw class:
The visualization shows each character as a node, with edges representing character transitions. End markers indicate complete words.
Comparison
Implementation Details
End Marker
End Marker
The trie uses a special character
"*" (stored in Trie.end_marker) to mark the end of words. This allows the trie to distinguish between complete words and prefixes.Storage Structure
Storage Structure
Each
TrieNode stores its children in a dictionary:Delete Implementation
Delete Implementation
The
delete() method uses post-order traversal to remove nodes safely:- Recursively traverse to the end of the word
- Remove the end marker
- On the way back, delete nodes that have no other children
- Preserve nodes that are part of other words
delete_preorder() method for demonstration purposes, but it should not be used in production.Time Complexity
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| insert(word) | O(m) | O(m) |
| search(word) | O(m) | O(1) |
| delete(word) | O(m) | O(1) |
| prefix(prefix) | O(m + n) | O(n) |
| list_words() | O(n) | O(n) |
m= length of the word/prefixn= total number of characters in all words with the given prefix
Tries excel at prefix-based operations, making them ideal for autocomplete systems where you need to find all words with a common prefix.