Tries

What is a Trie?

A Trie (pronounced "try") is a special data structure that helps store and search for words efficiently


Why Use a Trie?

  • Fast Word Search: Instead of searching through an entire list, a Trie lets you find words based on their starting letters. Like following branches in a tree, you can quickly eliminate parts of the alphabet that don't match your search.

  • Prefix Power: Say you're typing a word on your phone. A Trie can suggest words based on the letters you've typed so far, making it super helpful for autocorrect and autocomplete features.

Benefits:

  • Speedy Searches: Finding words with a Trie is like taking shortcuts in a maze. You only explore relevant parts, making searches faster!

  • Prefix Magic: Need words that start with a specific combination of letters? A Trie can find them in a flash!


Where are Tries used?

  • Spell Checkers: Tries help identify misspelled words by suggesting corrections based on valid prefixes.

  • Search Engines: Tries can power autocomplete features, suggesting relevant searches as you type.

  • Social Media: When you tag someone on social media, Tries might be behind the scenes, helping you find the right person quickly.


Time and Space complexity

Operation
Time Complexity
Space Complexity
Notes

Search

O(k)

O(W*L)

k - length of search word, W - number of words, L - average word length

Insertion

O(k)

O(W*L)

Similar to search

Deletion

O(k)

O(W*L)

In worst case (deleting entire branch)


Code

Last updated