Skip to content
Related Articles

Related Articles

Improve Article
Implement a Dictionary using Trie
  • Difficulty Level : Medium
  • Last Updated : 16 Jul, 2019

Implement a dictionary using Trie such that if the input is a string representing a word, the program prints its meaning from the prebuilt dictionary.


Input: str = “map”
Output: a diagrammatic representation of an area

Input: str = “language”
Output: the method of human communication

Approach: We can use a Trie to efficiently store strings and search them. Here, an implementation of a dictionary using Trie (memory optimization using hash-map) is discussed. We add another field to Trie node, a string which will hold the meaning of a word. While searching for the meaning of the required word, we search for the word in Trie and if the word is present (i.e isEndOfWord = true) then we return its meaning otherwise we return an empty string.

Below is the implementation of the above approach:

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Structure for Trie
struct Trie {
    bool isEndOfWord;
    unordered_map<char, Trie*> map;
    string meaning;
// Function to create a new Trie node
Trie* getNewTrieNode()
    Trie* node = new Trie;
    node->isEndOfWord = false;
    return node;
// Function to insert a word with its meaning
// in the dictionary built using a Trie
void insert(Trie*& root, const string& str,
            const string& meaning)
    // If root is null
    if (root == NULL)
        root = getNewTrieNode();
    Trie* temp = root;
    for (int i = 0; i < str.length(); i++) {
        char x = str[i];
        // Make a new node if there is no path
        if (temp->map.find(x) == temp->map.end())
            temp->map[x] = getNewTrieNode();
        temp = temp->map[x];
    // Mark end of word and store the meaning
    temp->isEndOfWord = true;
    temp->meaning = meaning;
// Function to search a word in the Trie
// and return its meaning if the word exists
string getMeaning(Trie* root, const string& word)
    // If root is null i.e. the dictionary is empty
    if (root == NULL)
        return "";
    Trie* temp = root;
    // Search a word in the Trie
    for (int i = 0; i < word.length(); i++) {
        temp = temp->map[word[i]];
        if (temp == NULL)
            return "";
    // If it is the end of a valid word stored
    // before then return its meaning
    if (temp->isEndOfWord)
        return temp->meaning;
    return "";
// Driver code
int main()
    Trie* root = NULL;
    // Build the dictionary
    insert(root, "language", "the method of human communication");
    insert(root, "computer", "A computer is a machine that can be \
    instructed to carry out sequences of arithmetic or \
logical operations automatically via computer programming");
    insert(root, "map", "a diagrammatic representation \
of an area");
    insert(root, "book", "a written or printed work \
consisting of pages glued or sewn together along one \
side and bound in covers.");
    insert(root, "science", "the intellectual and \
practical activity encompassing the systematic study \
of the structure and behaviour of the physical and \
natural world through observation and experiment.");
    string str = "map";
    cout << getMeaning(root, str);
    return 0;
a diagrammatic representation of an area

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up
Recommended Articles
Page :