![]() |
VOOZH | about |
The trie data structure, also known as a prefix tree, is a tree-like data structure used for efficient retrieval of key-value pairs. It is commonly used for implementing dictionaries and autocomplete features, making it a fundamental component in many search algorithms.
Below are some important properties of the Trie data structure:
Below is a simple example of Trie data structure.
A Trie data structure is used for storing and retrieval of data and the same operations could be done using another data structure which is Hash Table but Trie data structure can perform these operations more efficiently. Moreover, a Trie data structure can be used for prefix-based searching and a sorted traversal of all words. So a Trie has advantages of both hash table and self balancing binary search trees.
Trie data structure can contain any number of characters including alphabets, numbers, and special characters. But for this article, we will discuss strings with characters a-z. Therefore, only 26 pointers need for every node, where the 0th index represents 'a' and the 25th index represents 'z' characters.
Let's see how a word "and" and "ant" is stored in the Trie data structure:
After storing the word "and" and "ant" the Trie will look like this:
👁 ImagePlease refer Trie Data Structure article for details.
This operation is used to delete strings from the Trie data structure. There are three cases when deleting a word from Trie.
As shown in the following figure, the deleted word "an" share a complete prefix with another word "and" and "ant".
An easy solution to perform a delete operation for this case is to just decrement the wordCount by 1 at the ending node of the word.
As shown in the following figure, the deleted word "and" has some common prefixes with other words ‘ant’. They share the prefix ‘an’.
The solution for this case is to delete all the nodes starting from the end of the prefix to the last character of the given word.
As shown in the following figure, the word "geek" does not share any common prefix with any other words.
The solution for this case is just to delete all the nodes.
Please refer Trie Delete Operation for implementation details.
| Operation | Time Complexity |
|---|---|
| Insertion | O(n) Here n is the length of string to be searched |
| Searching | O(n) |
| Deletion | O(n) |
Note: In the above complexity table 'n', 'm' represents the size of the string and the number of strings that are stored in the trie.
1. Autocomplete Feature: Autocomplete provides suggestions based on what you type in the search box. Trie data structure is used to implement autocomplete functionality.
2. Spell Checkers: If the word typed does not appear in the dictionary, then it shows suggestions based on what you typed.
It is a 3-step process that includes :
Trie stores the data dictionary and makes it easier to build an algorithm for searching the word from the dictionary and provides the list of valid words for the suggestion.
3. Longest Prefix Matching Algorithm(Maximum Prefix Length Match): This algorithm is used in networking by the routing devices in IP networking. Optimization of network routes requires contiguous masking that bound the complexity of lookup a time to O(n), where n is the length of the URL address in bits.
To speed up the lookup process, Multiple Bit trie schemes were developed that perform the lookups of multiple bits faster.