Implement a Dictionary using Trie - GeeksforGeeks

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.
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:
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;  // Structure for Triestruct Trie {    bool isEndOfWord;    unordered_map<char, Trie*> map;    string meaning;};  // Function to create a new Trie nodeTrie* getNewTrieNode(){    Trie* node = new Trie;    node->isEndOfWord = false;    return node;}  // Function to insert a word with its meaning// in the dictionary built using a Trievoid 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 existsstring 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 codeint 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

Trie
Advanced Data Structure
Pattern Searching
Searching
Strings
Searching
Strings
Pattern Searching
Trie
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Ordered Set and GNU C++ PBDS
2-3 Trees | (Search, Insert and Deletion)
Extendible Hashing (Dynamic approach to DBMS)
Suffix Array | Set 1 (Introduction)
Difference between Backtracking and Branch-N-Bound technique
KMP Algorithm for Pattern Searching
Rabin-Karp Algorithm for Pattern Searching
Naive algorithm for Pattern Searching
Boyer Moore Algorithm for Pattern Searching
Check if a string is substring of another