Related Articles

# Trie memory optimization using hash map

• Difficulty Level : Easy
• Last Updated : 26 Sep, 2017

We introduced and discussed an implementation in below post.

Trie | (Insert and Search) – GeeksforGeeks

The implementation used in above post uses an array of alphabet size with every node. It can be made memory efficient. One way to implementing Trie is linked set of nodes, where each node contains an array of child pointers, one for each symbol in the alphabet. This is not efficient in terms of time as we can’t quickly find a particular child.
The efficient way is an implementation where we use hash map to store children of a node. Now we allocate memory only for alphabets in use, and don’t waste space storing null pointers.

 `// A memory optimized CPP implementation of trie``// using unordered_map``#include ``#include ``using` `namespace` `std;`` ` `struct` `Trie {`` ` `    ``// isEndOfWord is true if the node``    ``// represents end of a word``    ``bool` `isEndOfWord;`` ` `    ``/* nodes store a map to child node */``    ``unordered_map<``char``, Trie*> map;``};`` ` `/*function to make a new trie*/``Trie* getNewTrieNode()``{``    ``Trie* node = ``new` `Trie;``    ``node->isEndOfWord = ``false``;``    ``return` `node;``}`` ` `/*function to insert in trie*/``void` `insert(Trie*& root, ``const` `string& str)``{``    ``if` `(root == nullptr)``        ``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];``    ``}`` ` `    ``temp->isEndOfWord = ``true``;``}`` ` `/*function to search in trie*/``bool` `search(Trie* root, ``const` `string& str)``{``    ``/*return false if Trie is empty*/``    ``if` `(root == nullptr)``        ``return` `false``;`` ` `    ``Trie* temp = root;``    ``for` `(``int` `i = 0; i < str.length(); i++) {`` ` `        ``/* go to next node*/``        ``temp = temp->map[str[i]];`` ` `        ``if` `(temp == nullptr)``            ``return` `false``;``    ``}`` ` `    ``return` `temp->isEndOfWord;``}`` ` `/*Driver function*/``int` `main()``{``    ``Trie* root = nullptr;`` ` `    ``insert(root, ``"geeks"``);``    ``cout << search(root, ``"geeks"``) << ``" "``;`` ` `    ``insert(root, ``"for"``);``    ``cout << search(root, ``"for"``) << ``" "``;`` ` `    ``cout << search(root, ``"geekk"``) << ``" "``;`` ` `    ``insert(root, ``"gee"``);``    ``cout << search(root, ``"gee"``) << ``" "``;`` ` `    ``insert(root, ``"science"``);``    ``cout << search(root, ``"science"``) << endl;`` ` `    ``return` `0;``}`

Output:

```1 1 0 1 1
```

Space used here with every node here is proportional to number of children which is much better than proportional to alphabet size, especially if alphabet is large.

This article is contributed by Pranav. 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.