Skip to Content
Course content

403: Implementing a Trie for String Search

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Imagine we're building an autocomplete feature for a travel app. We have a list of 50,000 global cities, and as the user types "New", we want to instantly suggest "New York", "New Delhi", and "New Orleans". It seems simple enough, but the way you store those strings determines whether your app feels snappy or like it's wading through molasses.

The hidden cost of the list comprehension

When I first saw developers tackle this, the go-to move was usually a list comprehension. It looks clean: [city for city in cities if city.startswith(prefix)]. On a small dataset, it's perfectly fine. But here's the problem: every single time the user presses a key, Python has to iterate through all 50,000 strings. If the average city name is 15 characters, you're doing a massive amount of redundant work. You're checking "New" against "London", "Tokyo", and "Paris" over and over again, even though you know they don't start with 'N'.

You might think, "I'll just use a set for O(1) lookup." That works if you're looking for an exact match, but sets are useless for prefix searches. They don't store the relationship between "New" and "New York". You're stuck with a linear scan, which means as your city list grows, your latency grows right along with it.

Structuring data for the prefix

To fix this, we need to stop thinking of strings as monolithic blocks and start thinking of them as paths. This is where the Trie (or prefix tree) comes in. Instead of storing "New York" as one string in a list, we store it as a sequence of nodes. The root node has a child 'N', which has a child 'e', which has a child 'w', and so on.

The magic here is that "New York" and "New Delhi" share the exact same path for the first three characters. When the user types "New", we traverse three nodes and suddenly we're standing at the root of a tiny subtree that contains only the cities that start with "New". We've effectively pruned 99% of the search space in three steps.

class TrieNode:
    def __init__(self):
        self.children = {}
        self.is_end_of_word = False

class Trie:
    def __init__(self):
        self.root = TrieNode()

    def insert(self, word):
        node = self.root
        for char in word:
            if char not in node.children:
                node.children[char] = TrieNode()
            node = node.children[char]
        node.is_end_of_word = True

    def search_prefix(self, prefix):
        node = self.root
        for char in prefix:
            if char not in node.children:
                return [] # Prefix not found
            node = node.children[char]
        
        # Now we find all words stemming from this node
        results = []
        self._dfs(node, prefix, results)
        return results

    def _dfs(self, node, prefix, results):
        if node.is_end_of_word:
            results.append(prefix)
        
        for char, next_node in node.children.items():
            self._dfs(next_node, prefix + char, results)

Memory overhead vs. search velocity

Now, I'll be honest with you: Tries aren't free. If you're worried about RAM, a Trie is significantly "heavier" than a simple list of strings. Each character becomes an object (a TrieNode) with its own dictionary. In a language like C++, you could optimize this with arrays, but in Python, the overhead is real.

However, the trade-off is almost always worth it for search functionality. The time complexity for searching a prefix drops from O(N * M) (where N is the number of words and M is the length) to O(L), where L is just the length of the prefix you're searching for. Whether you have 50,000 cities or 50 million, looking up "New" always takes exactly three steps to reach the prefix node. That's the kind of scaling that keeps an application from crashing under its own weight.




📋 Practical Task

Build a Python Library Autocomplete Engine

Using the Trie implementation discussed in the lesson, build a specialized autocomplete engine for Python library names.

  • Create a LibraryTrie class that can insert a list of library names (e.g., ["pandas", "numpy", "matplotlib", "scikit-learn", "scipy", "selenium"]).
  • Implement a suggest method that takes a partial string and returns a list of all libraries that start with that prefix.
  • Requirement: Your suggest method must handle cases where the prefix doesn't exist by returning an empty list.
  • Test Case: If you insert the list above and call suggest("sci"), your code should return ["scikit-learn", "scipy"].
Rating
0 0

There are no comments for now.

to be the first to leave a comment.