Web Analytics

šŸ“– Tries (Prefix Trees)

Intermediate ~30 min read

You're typing "app" in a search box. Instantly, suggestions appear: "apple," "application," "apply." How does it find all words starting with "app" so fast? Meet the Trie - a tree structure designed specifically for prefix-based operations!

The Dictionary Problem

Imagine building a dictionary. You need to:

  • Check if a word exists
  • Find all words starting with a prefix
  • Suggest corrections for misspellings

Why Not Use a Hash Table?

Hash table: Great for exact lookups, but can't efficiently find all words with prefix "app"

Trie: Designed for prefix operations - finds all matches in O(m + n) time!

What is a Trie?

Trie = Tree of Characters

A Trie (pronounced "try") is a tree where:

  • Each node represents a character
  • Each path from root to marked node = one word
  • Shared prefixes share nodes (space efficient!)

Example: "app", "apple", "apply" share nodes a→p→p

See the Structure

Basic Operations

1. Insert

How It Works

  1. Start at root
  2. For each character in word:
    • If child node exists, move to it
    • If not, create new node
  3. Mark last node as "end of word"

Time: O(m) where m = word length

2. Search

How It Works

  1. Follow path for each character
  2. If path breaks → word doesn't exist
  3. If reach end, check if marked as "end of word"

Key insight: "app" exists, but "appl" might not (even if path exists!)

Time: O(m)

3. Prefix Search (Autocomplete)

How It Works

  1. Navigate to prefix node
  2. Collect all words in that subtree
  3. Return as suggestions

Time: O(m + n) where m = prefix length, n = number of results

Real-World Applications

Where Tries Shine

  • Autocomplete: Google search, IDE code completion
  • Spell Checkers: Word processors, browsers
  • IP Routing: Longest prefix matching
  • Dictionary: Word games, text editors
  • Phone Contacts: T9 predictive text
  • DNA Sequencing: Pattern matching in genomes

The Complete Code

Output
Click Run to execute your code

Trie vs Hash Table

Comparison

Operation Trie Hash Table
Insert O(m) O(m)
Search O(m) O(m)
Prefix search O(m + n) āœ“ O(N) āœ—
Space O(ALPHABET Ɨ N Ɨ M) O(N Ɨ M)

Trie advantage: Prefix operations!

Trie disadvantage: More space (but shared prefixes help)

Space Optimization

Reducing Memory Usage

  • Compressed Trie: Merge single-child chains
  • Ternary Search Tree: Use 3-way branching
  • Hash Map Children: Instead of array (saves space for sparse alphabets)

Summary

What You've Learned

Tries are specialized trees for string operations:

  1. Structure: Each path = one word
  2. Insert: Create nodes for each character - O(m)
  3. Search: Follow path, check end marker - O(m)
  4. Prefix: Navigate to prefix, collect subtree - O(m + n)
  5. Space: Shared prefixes save memory
  6. Applications: Autocomplete, spell check, IP routing
  7. Trade-off: More space for faster prefix operations

What's Next?

You've completed Module 9! You now understand heaps, their applications, and tries. These are powerful tools for real-world systems. Next, we can explore more advanced data structures or move to graphs and algorithms!