Implement a Dictionary using Trie

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.

Examples:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

a diagrammatic representation of an area


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.