Trie is a tree-based data structure, which is used for efficient retrieval of a key in a large dataset of strings. In this post, we will cover memory-efficient implementation of Trie data structure in C++ using the map data structure.

There are several ways to represent tries, corresponding to different trade-offs between memory use and operations speed. The basic form is that of a linked set of nodes, where each node contains an array of child pointers, one for each symbol in the alphabet (so for the English alphabet, one would store 26 child pointers and for the alphabet of bytes, 256 pointers).

This representation is simple but wasteful in terms of memory. Each Trie node requires a kilobyte of storage, with the alphabet of bytes (size 256) and 4–byte pointers, and when there is little overlap in the strings’ prefixes, the total number of required nodes is roughly the combined length of the stored strings. Also, nodes near the bottom of the tree tend to have few children, and there are many of them, so the structure waste space storing null pointers.

 
The storage problem can be alleviated by using a map to store a node’s children. The idea is to allocate memory only for alphabets in use and don’t waste space storing null pointers, as demonstrated below in C++.

Download  Run Code

Output:

1 1 0 1 1
0 1 1
0 1 1
0 1 0
Trie empty!!
0

 
The time complexity of a Trie data structure for insertion, deletion, and search operation is O(n), where n is the key length.

 
Also see:

Java Implementation of Trie Data Structure

 
References: https://en.wikipedia.org/wiki/Trie