Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for What Is a Trie? The Data Structure Behind Autocomplete
OneDev
OneDev

Posted on

What Is a Trie? The Data Structure Behind Autocomplete

Ever wondered how search boxes suggest words as you type? Or how a spell checker knows what you probably meant to write?

Behind the scenes, there's a powerful but often underrated data structure making it all possible: theTrie (pronounced "try").

Let’s take a look 👇


🔠 What Is a Trie?

ATrie, also known as aprefix tree, is a tree-like data structure used to store a dynamic set of strings — especially useful forprefix-based lookups.

Each node in a trie represents a character, and by connecting nodes from top to bottom, you can represent entire words.


🧠 Real-World Use Cases

Tries shine when you’re dealing with words, prefixes, or partial matches. You’ll find them behind:

  • Autocomplete and search suggestions(e.g. Google search, VSCode IntelliSense)
  • Spell checkers and correctors
  • IP routing (longest prefix matching)
  • Word games like Boggle or Scrabble solvers
  • Dictionary or keyword matching systems

🔍 Why Not Just Use an Array or Hash Table?

Let’s say you have a list of 100,000 words. If you want to find all words that start with"cat", a trie can do thatfaster and more efficiently than scanning every word in an array or using a hash table.

Why? Because the structure of a trienaturally groups words by their prefixes, making lookups almost instantaneous.


⚙️ How It Works (At a High Level)

  • Each node represents a single character.
  • Children nodes represent possible next characters.
  • Words are built by walking down from the root node.
  • A special flag marks the end of a valid word.

Example: Storing "cat" and "car"

(root)/\c...|a/\tr
Enter fullscreen modeExit fullscreen mode

From the root,"c" leads to"a", which leads to"t" or"r" — forming "cat" and "car".


🚀 Benefits

  • Fast prefix searches (often O(k), wherek is the length of the word)
  • Efficient memory usage for shared prefixes
  • Flexible: can be extended to support wildcard matching, autocomplete ranking, etc.

🤔 When to Reach for a Trie

  • You’re implementing a feature based onprefix matching
  • You need to dolots of lookups from a large dictionary of words
  • You want to build something like autocomplete, spellcheck, or typeahead

🧠 TL;DR

  • ATrie is a tree-like data structure designed for efficientprefix-based string matching.
  • It powers real-world features likeautocomplete,search suggestions, andspellcheckers.
  • If you’re dealing with lots of strings and care about prefixes —Tries are worth knowing.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Just another developer that suffers when centering a div
  • Work
    Fullstack Developer
  • Joined

More fromOneDev

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp