Javatpoint Logo
Javatpoint Logo

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:

  1. The root node of the trie always represents the null node.
  2. Each child of nodes is sorted alphabetically.
  3. Each node can have a maximum of 26 children (A to Z).
  4. Each node (except the root) can store one letter of the alphabet.

The diagram below depicts a trie representation for the bell, bear, bore, bat, ball, stop, stock, and stack.

Trie Data Structure

Basic operations of Trie

There are three operations in the Trie:

  1. Insertion of a node
  2. Searching a node
  3. Deletion of a node

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:

  1. Every letter of the input key (word) is inserted as an individual in the Trie_node. Note that children point to the next level of Trie nodes.
  2. The key character array acts as an index of children.
  3. If the present node already has a reference to the present letter, set the present node to that referenced node. Otherwise, create a new node, set the letter to be equal to the present letter, and even start the present node with this new node.
  4. The character length determines the depth of the trie.

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:

  1. If the key is not found in the trie, the delete operation will stop and exit it.
  2. If the key is found in the trie, delete it from the trie.

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.

2. Auto-complete

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.

  • It provides an alphabetical filter of entries by the key of the node.
  • We trace pointers only to get the node that represents the string entered by the user.
  • As soon as you start typing, it tries to complete your input.

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

  1. It can be insert faster and search the string than hash tables and binary search trees.
  2. It provides an alphabetical filter of entries by the key of the node.

Disadvantages of Trie

  1. It requires more memory to store the strings.
  2. It is slower than the hash table.

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

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA