Open In App
Related Articles

Trie memory optimization using hash map

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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. 

CPP

// A memory optimized CPP implementation of trie
// using unordered_map
#include <iostream>
#include <unordered_map>
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;
}

                    

Java

// A memory optimized Java implementation of trie
// using unordered_map
import java.util.HashMap;
 
public class Trie {
// isEndOfWord is true if the node
// represents end of a word
boolean isEndOfWord;
 
/* nodes store a map to child node */
HashMap<Character, Trie> map;
 
/* function to make a new trie */
static Trie getNewTrieNode() {
    Trie node = new Trie();
    node.isEndOfWord = false;
    node.map = new HashMap<>();
    return node;
}
 
/* function to insert in trie */
static void insert(Trie root, String str) {
    Trie temp = root;
    for (int i = 0; i < str.length(); i++) {
        char x = str.charAt(i);
 
        /* make a new node if there is no path */
        if (!temp.map.containsKey(x))
            temp.map.put(x, getNewTrieNode());
 
        temp = temp.map.get(x);
    }
    temp.isEndOfWord = true;
}
 
/* function to search in trie */
static boolean search(Trie root, String str) {
    /* return false if Trie is empty */
    if (root == null)
        return false;
 
    Trie temp = root;
    for (int i = 0; i < str.length(); i++) {
 
        /* go to next node */
        if (!temp.map.containsKey(str.charAt(i)))
            return false;
 
        temp = temp.map.get(str.charAt(i));
    }
 
    return temp.isEndOfWord;
}
 
/*Driver function*/
public static void main(String[] args) {
    Trie root = getNewTrieNode();
 
    insert(root, "geeks");
    System.out.print(search(root, "geeks") + " ");
 
    insert(root, "for");
    System.out.print(search(root, "for") + " ");
 
    System.out.print(search(root, "geekk") + " ");
 
    insert(root, "gee");
    System.out.print(search(root, "gee") + " ");
 
    insert(root, "science");
    System.out.println(search(root, "science"));
   
  insert(root, "scienc");
    System.out.println(search(root, "scienc"));
}
}
 
// This code is contributed by Aman Kumar.

                    

Python3

# A memory optimized Python implementation of trie
# using dictionary
 
from collections import defaultdict
 
class TrieNode:
    def __init__(self):
        # nodes store a map to child node
        self.map = defaultdict(TrieNode)
         
        # isEndOfWord is true if the node
        # represents end of a word
        self.is_end_of_word = False
 
class Trie:
    # function to make a new trie
    def __init__(self):
        self.root = TrieNode()
 
    # function to insert in trie
    def insert(self, word: str) -> None:
        node = self.root
        for char in word:
            # make a new node if there is no path
            node = node.map[char]
        node.is_end_of_word = True
 
    # function to search in trie
    def search(self, word: str) -> bool:
        node = self.root
        for char in word:
            if char not in node.map:
                return False
            node = node.map[char]
        return node.is_end_of_word
 
# Driver function
if __name__ == '__main__':
    # create a new Trie
    root = Trie()
 
    root.insert('geeks')
    print(root.search('geeks'), end=' ')
     
    root.insert('for')
    print(root.search('for'), end=' ')
     
    print(root.search('geekk'), end=' ')
     
    root.insert('gee')
    print(root.search('gee'), end=' ')
     
    root.insert('science')
    print(root.search('science'))
 
#  This code is contributed by Utkarsh Kumar

                    

C#

// A memory optimized C# implementation of trie
// using Dictionary
using System.Collections.Generic;
 
public class Trie {
 
  // isEndOfWord is true if the node
  // represents end of a word
  bool isEndOfWord;
 
  /* nodes store a dictionary to child node */
  Dictionary<char, Trie> map;
 
  /* function to make a new trie */
  static Trie getNewTrieNode() {
    Trie node = new Trie();
    node.isEndOfWord = false;
    node.map = new Dictionary<char, Trie>();
    return node;
  }
 
  /* function to insert in trie */
  static void insert(Trie root, string str) {
    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.ContainsKey(x))
        temp.map[x] = getNewTrieNode();
 
      temp = temp.map[x];
    }
    temp.isEndOfWord = true;
  }
 
  /* function to search in trie */
  static bool search(Trie root, string str) {
 
    /* return false if Trie is empty */
    if (root == null)
      return false;
 
    Trie temp = root;
    for (int i = 0; i < str.Length; i++) {
 
      /* go to next node */
      if (!temp.map.ContainsKey(str[i]))
        return false;
 
      temp = temp.map[str[i]];
    }
 
    return temp.isEndOfWord;
  }
 
  /*Driver function*/
  public static void Main() {
    Trie root = getNewTrieNode();
 
    insert(root, "geeks");
    System.Console.Write(search(root, "geeks") + " ");
 
    insert(root, "for");
    System.Console.Write(search(root, "for") + " ");
 
    System.Console.Write(search(root, "geekk") + " ");
 
    insert(root, "gee");
    System.Console.Write(search(root, "gee") + " ");
 
    insert(root, "science");
    System.Console.WriteLine(search(root, "science"));
  }
}
 
// This code is contributed by Pushpesh Raj.

                    

Javascript

// A memory optimized JS implementation of trie
// using unordered_map
 
class TrieNode {
    constructor() {
         // isEndOfWord is true if the node
    // represents end of a word
        this.isEndOfWord = false;
         /* nodes store a map to child node */
        this.map = new Map();
    }
}
 
class Trie {
    constructor() {
        this.root = new TrieNode();
    }
/*function to insert in trie*/
    insert(str) {
        let temp = this.root;
        for (let i = 0; i < str.length; i++) {
            const x = str[i];
             
        /* make a new node if there is no path */
            if (!temp.map.has(x)) {
                temp.map.set(x, new TrieNode());
            }
             /* go to next node*/
            temp = temp.map.get(x);
        }
        temp.isEndOfWord = true;
        return this.root;
    }
/*function to search in trie*/
    search(str) {
        let temp = this.root;
        for (let i = 0; i < str.length; i++) {
            const x = str[i];
            temp = temp.map.get(x);
            if (!temp) {
                return false;
            }
        }
        return temp.isEndOfWord;
    }
}
/*Driver function*/
const trie = new Trie();
let root = null;
 
root = trie.insert("geeks");
console.log(trie.search("geeks")); // output: true
 
root = trie.insert("for");
console.log(trie.search("for")); // output: true
 
console.log(trie.search("geekk")); // output: false
 
root = trie.insert("gee");
console.log(trie.search("gee")); // output: true
 
root = trie.insert("science");
console.log(trie.search("science")); // output: true

                    

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.



Last Updated : 16 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads