Implement insert, search, and delete operations on Trie data structure. Assume that the input consists of only lowercase letters a–z.

Overview of Trie

Trie is a tree-based data structure, which is used for efficient retrieval of a key in a large dataset of strings. Unlike a binary search tree, where a node stores the key associated with that node, the Trie node’s position in the tree defines the key with which it is associated, and the key is only associated with the leaves. It is also known as a prefix tree as all descendants of a node have a common prefix of the string associated with that node, and the root is associated with the empty string.

Representation of Trie

There are several ways to represent a Trie, corresponding to different trade-offs between memory use and operations speed. The basic form is that of a linked set of nodes, where each node contains an array of child pointers, one for each symbol in the alphabet (so for the English alphabet, one would store 26 child pointers and for the alphabet of bytes, 256 pointers). The Trie node also maintains a flag that specifies whether it corresponds to the key’s end or not.

As illustrated in the following figure, each key is represented in the Trie as a path from the root to the internal node or a leaf:

 
Trie Implementation

Implementation of Trie

Insertion proceeds by walking the Trie according to the string to be inserted, then appending new nodes for the suffix of the string that is not contained in the Trie. Searching also proceeds the similar way by walking the Trie according to the string to be searched, returning false if the string is not found. Deletion is a bit complicated. The idea is to delete the key in a bottom-up manner using recursion. Special care has to be taken while deleting the key as it can be the prefix of another key, or its prefix can be another key in Trie.

Following is the C implementation of the Trie data structure, which supports insertion, deletion, and search operations. The implementation currently supports only lowercase English characters (a – z), but it can be easily extended to support any set of characters.

Download  Run Code

Output:

1 1 0 1 1
0 1 1
0 1 1
0 1 0
Trie empty!!
0

Performance of Trie

The time complexity of a Trie data structure for insertion/deletion/search operation is just O(n), where n is key length.

The space complexity of a Trie data structure is O(N × M × C), where N is the total number of strings, M is the maximum length of the string, and C is the alphabet’s size. Please refer to the following post for a memory-efficient implementation of the Trie:

Memory Efficient C++ Implementation of Trie – Insert, Search, and Delete

Applications of Trie

Numerous Trie data structure applications take advantage of a Trie’s ability to quickly search, insert, and delete entries.

 

Trie has several advantages over binary search trees. It can also replace a hash table as lookup is generally faster in the Trie, even in the worst case. Also, there are no collisions of different keys in a Trie, and a Trie can provide an alphabetical ordering of the entries by key.

 

A Trie’s common application is storing a predictive text or autocomplete dictionaries, such as found on a mobile telephone or search engines. Autocomplete (or word completion) is a feature in which an application predicts the rest of a word the user is typing.

 

The spell checker flags words in a document that may not be spelled correctly. Spell checkers are commonly used in word processors (like MS Word), email clients, search engines, etc.

 

Lexicographic sorting of a set of keys can be accomplished with a simple Trie-based algorithm. We initially insert all keys into a Trie and then print all keys in the Trie by performing preorder traversal (depth–first traversal), resulting in a lexicographically increasing order.

 

Routers use the longest prefix match algorithm in Internet Protocol (IP) networking to select an entry from a forwarding table.

 
Also see:

C++ Implementation of Trie Data Structure

Java Implementation of Trie Data Structure

Trie Data Structure – Python Implementation

 
References: https://en.wikipedia.org/wiki/Trie