Implement trie using hashmaps

A trie is a tree-like data structure in which every node stores a character. A tire can store one or multiple words. After building a trie, strings or substrings can be retrieved by traversing down a path (a branch) of the trie. Tries are used to find substrings, autocomplete and many other string operations. In this post, we implement trie using hashmaps.

trie hashmap diagram

Note: In a trie, a trie node might have multiple branches. So there is a data structure used to point to next nodes in branches. The data structure can be an array, a linked list or a hashmap. Using a hashmap is the best of three implementations since its put and get operations take O(1) time.

Table of Content


Define classes

Like building a tree, you need to define a TrieNode class before a Trie class. A TrieNode class has three variables: data, children and isEnd. data stores a character. children is a data structure that points to child nodes (branches). Here the data structure is a hashmap. isEnd is to mark whether this is the last node in the branch.

In Trie class, there is one variable root. The operation of insertion, search or deletion always starts from root.

Java

Javascript

Python


Insert a word

To insert a word, first check whether the word is already stored in the trie so that you don’t insert a duplicate word. Then loop through each character in the word. Create a node to store the character. Use the character as the key and a new node as the value to create an entry. Store the new entry in the current node’s children. Then move to the child node. When the node is at the last character of the word, you mark the node‘s isEnd to be true.

Java

Javascript

Python


Delete a word

To delete a word from a trie, first check whether the word exists in the trie. If not, the method can return. Otherwise, continue. Loop through each character in the word. A pointer node starts from root. If the character is not in the node‘s children, the method returns directly. Otherwise, the pointer moves to the child node. When the pointer is at the last character of the word, mark the node‘s isEnd to be false.

Java

Javascript

Python

To search a word in a trie, loop through each character in the word. A pointer node starts from root. If the character is not in the node‘s children, the method returns false. Otherwise, the pointer moves to the child node. When the pointer is at the last character of the word, return the node‘s isEnd value, which can be true or false.

Java

Javascript

Python


Print all words in a trie

To print all words in a trie, recursion is used to traverse all nodes in the trie. This is similar to preorder (DFS, depth first search) of a tree. When visiting the node, the method concatenates characters from previously visited nodes with the character of the current node. When the node‘s isEnd is true, the recursion reaches the last character of the word, and add the word to the result list.

Java

Javascript

Python


Free download

Download TrieMap.java
Download TrieMap.js
Download TrieMap.py
Coding tutorial in YouTube
Implement trie using arrays

Comments are closed