Trie implementation is to implement a data structure trie. A trie is a tree-like data structure in which every node stores a character. After building the trie, strings or substrings can be retrieved by traversing down a path of the trie. Tries are used for find-substrings, auto-complete and many other string operations. Here trie is implemented in Java, Python and JavaScript.

trie diagram

Table of Content


Map of trie implementations

Array is the most intuitive implementation. In this post, This post is trie implemented using array.

you are here

Part 1 – Trie implementation using array.
Part 2 – Trie implementation using linked list
Part 3 – Trie implementation using hash map


Define classes in trie implementation

The trie node class has three variables: data stores a character, children is a data structure that points to the children (branches) nodes. The data structure is array. If you use only alphabet a-z, the length of array is 26.  Here we define the array length to be 128 so that it can hold most ASCII characters; isEnd is to mark whether this is the last node to compose a word. The trie class, there is one class level variable – root. The operation of insert, search or delete always starts from the root.

Java

Javascript

Python


Insert a word in trie implementation

To insert a word, first check whether the word is in the trie so that we don’t insert duplicate word. Then we loop through each character in the word. A pointer node starts from the root node. If the character is not in the node‘s children, a child node is created. Then the node moves to the child node. When the node is at the last character of the word, we mark the node‘s isEnd to be true.

Java

Javascript

Python

Doodle

trie insert array


Delete a word

To delete a word from the trie, first check whether the word exists in the trie. If not, the method can return. The same as insert, loop through each character in the word. A pointer node starts from the root node. If the character is not in the node‘s children, the method returns directly. Otherwise, the node moves to the child node. When the node is at the last character of the word, mark the node‘s isEnd to be false. The word is not actually deleted from the trie. It becomes invisible.

Java

Javascript

Python

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

Java

Javascript

Python


Print all words in trie

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

Java

Javascript

Python


Free download

Download trie implementation using Array in Java, JavaScript and Python
Download aggregate Data Structures implementations in Java
Download aggregate Data Structures implementations in JavaScript
Download aggregate Data Structures implementations in Python

What are the children in the trie?

implement trie with map

A trie normally holds many words. Some characters might have multiple characters following it . For example, “ant”, “add”, “amy” share the same parent node ‘a’. The node ‘a’ has children of ‘n’, ‘d’ and ‘m’. So a trie is a n-ary tree. In the trie node, there is an attribute children, which holds the references to the nodes following it. The children can beĀ  stored in a data structure such as array, linked list or hashmap.

How many children a trie node can have?

If you use only alphabet a-z, the size is 26. If you use A-Z and other special characters such as space, the size is 128 or more.

What is time complexity of trie?

Insert a word in trie, search a word and delete a word by key all takes O(s) time, s is the length of the word.