What is a Trie data structure?
The word "Trie" is an excerpt from the word "retrieval". Trie is a sorted tree-based data-structure that stores the set of strings. It has the number of pointers equal to the number of characters of the alphabet in each node. It can search a word in the dictionary with the help of the word's prefix. For example, if we assume that all strings are formed from the letters 'a' to 'z' in the English alphabet, each trie node can have a maximum of 26 points.
Trie is also known as the digital tree or prefix tree. The position of a node in the Trie determines the key with which that node is connected.
Properties of the Trie for a set of the string:
The diagram below depicts a trie representation for the bell, bear, bore, bat, ball, stop, stock, and stack.
Basic operations of Trie
There are three operations in the Trie:
Insert of a node in the Trie
The first operation is to insert a new node into the trie. Before we start the implementation, it is important to understand some points:
Implementation of insert a new node in the Trie
Searching a node in Trie
The second operation is to search for a node in a Trie. The searching operation is similar to the insertion operation. The search operation is used to search a key in the trie. The implementation of the searching operation is shown below.
Implementation of search a node in the Trie
Deletion of a node in the Trie
The Third operation is the deletion of a node in the Trie. Before we begin the implementation, it is important to understand some points:
Implementation of delete a node in the Trie
Applications of Trie
1. Spell Checker
Spell checking is a three-step process. First, look for that word in a dictionary, generate possible suggestions, and then sort the suggestion words with the desired word at the top.
Trie is used to store the word in dictionaries. The spell checker can easily be applied in the most efficient way by searching for words on a data structure. Using trie not only makes it easy to see the word in the dictionary, but it is also simple to build an algorithm to include a collection of relevant words or suggestions.
Auto-complete functionality is widely used on text editors, mobile applications, and the Internet. It provides a simple way to find an alternative word to complete the word for the following reasons.
3. Browser history
It is also used to complete the URL in the browser. The browser keeps a history of the URLs of the websites you've visited.
Advantages of Trie
Disadvantages of Trie
Complete program in C++
Search the word ohh: Not Found Search the word bag: Found! Search the word can: Found! Search the word ways: Not Found Search the word way: Found! → h → e → l → l → o → w → a → y → i → t → e → a → b → a → g → c → a → n deleting the word 'hello'... → w → a → y → h → i → t → e → a → b → a → g → c → a → n deleting the word 'can'... → w → a → y → h → i → t → e → a → b → a → g
Next TopicHeap Data Structure