Implement trie using arrays

A trie is a tree-like data structure in which every node stores a character. A trie can store 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 Arrays in Java, Python and JavaScript.

trie array 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 hash map. Using an array is the most intuitive implementation.

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 (e.g a letter). children is a data structure that points to children (branches) nodes. The data structure is an array. If you use only alphabet a-z, the length of the array is 26.  Here we define the array length to be 128 so that it can hold most ASCII characters, such as ‘ ‘ or ‘#’. isEnd is to mark whether this is the last node in this branch (i.e. the last letter in a word).

In Trie class, there is one class 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. Start from root, loop through each character in the word. Use the character as the index to find the cell in the current node’s children. Create a child node to store the character and store the node in this cell. Then move to the child node. When it is 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. Start from root, loop through each character in the word. If the character is not in the node‘s children, the method returns directly. Otherwise, move to the child node. When it is the last character of the word, mark the node‘s isEnd to be false. The word is not actually deleted from the trie. It just becomes inaccessible.

Java

Javascript

Python

To search a word in a trie, start from root and loop through each character in the word. If the character is not in the node‘s children, the method returns false. Otherwise, move to the child node. When it is 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

This is to print all words in a trie. Similar to preorder (DFS, depth first search) of a tree, recursion is used to traverse all nodes in a trie. When visiting the node, concatenate 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. Add the word to the result list.

Java

Javascript

Python


Free download

Download TrieArray.java
Download TrieArray.js
Download TrieArray.py
Coding tutorial in YouTube
Implement trie using hashmaps

Comments are closed