Back to DSA
CalculatorDSA

AutoComplete Understands Our Gibberish???

Learning Trie Trees with the analogy of YOU being lost in a library and finding a word!!!

2 min read
15 views
#Trie Trees#DSA#AutoComplete
AutoComplete Understands Our Gibberish???

One Parent >> Two Children?? đŸ€”

Nope, not a family rule
 that’s how a binary tree works 😅.

But today, let’s talk about another kind of tree: the Trie tree.
Where do we use it?
>> In autocomplete!


You’re typing something like:

“bfaiuosijkbk;v”

And your autocomplete is like: “What are you even trying to say??”
But somehow, it still helps you finish your words.
That happens because of Trie trees.


In this story:

  • You = User typing random letters

  • Autocomplete = Trie tree

  • Bookshelf = Storage of words

  • Path = Characters leading to the word

This is how Tries work in data structures. They store words by their prefixes instead of repeating them.
And today, in our DSA Basics series by TANICE, we’ll explore how Tries make autocomplete possible.


What is a Trie???

A Trie is a prefix tree.
It helps in autocomplete just like the LIKE operator in SQL does.

Normally, if you store words like:

  • app

  • appl

  • apple

You’d end up storing “app” thrice and "L" twice

But in a Trie:

  • No duplicates.

  • It creates a path through each character.

int main() {
    Trie trie;
    trie.add("app");
    trie.add("appl");
    trie.add("apple");
    trie.add("them");
    trie.add("they");
    return 0;
}

Above’s a simple example of how you can create and add words to a Trie
So if you search for words starting with app >> you instantly get {app, appl, apple}.
Below is a visual illustration of the above trie tree :D

Trie Trees.png

Analogy

Think of a Trie like a library of letters:

  • Each hallway = a character.

  • Each path = a word.

  • If you stop halfway, you still have a prefix.

Instead of storing three separate books for app, appl, and apple, the library just builds one hallway and lets you branch off when needed.


Why is it Fast?

Because you don’t have to check every single word.
You just follow the path:

  • Start at a

  • Then p

  • Then p again
    
and, you’re already at the shelf where all “app” words live.

This makes searching super quick >> about O(log N) time. That’s why Tries are used in autocomplete systems.

Something as simple as a tree of letters can power complex features like:

  • Autocomplete in search engines

  • Spell-checkers

  • Prefix-based queries (like SQL’s LIKE)


Final Bite

A Trie is like a tree of letters.
Each path from the root to a node represents a word or a prefix.
It’s super useful when working with lots of strings that share common beginnings.

That’s it for today’s DSA Basics lesson with TANICE 😋.
Stay tuned for more fun analogies that make data structures easy to digest!

Share this post