A trie is a tree-like data structure that can store multiple strings. A node in a trie stores a character. After building a trie, strings or substrings can be retrieved by traversing down a path (a branch) of the trie. Trie list is a trie using a linked list in the trie node to access child nodes.

trie list

Why is a singly linked list used in a trie node?

In a trie, each trie node might have multiple branches. So a trie node has a data structure to store the references to the next nodes in the branches. This data structure can be an array, a linked list, or a hash map. Each implementation has its pros and cons. In the array implementation, you have to initialize the array size, which leads to a large amount of unused space. A linked list overcomes this problem by storing references to the following nodes in a chain. However, this requires time to search for the character’s corresponding reference in the list. So this implementation uses time to trade space.

Table of Content

Map of trie implementations

Part 1 – Trie implementation using an array

you are here

Part 2 – Trie implementation using a linked list
Part 3 – Trie implementation using a hash map


Define classes in trie implementation

Like building a tree, you need to define a TrieNode class before a Trie class. The trie node class has tree variables: data, children, and isEnd. data stores a character. children is a linked list of children nodes. isEnd is to mark whether this is the last node of the branch. In the TrieNode class, the getChild() method returns the reference of the node by the input character.

In the Trie class, there is a variable root. The operation of insertion, search, or deletion always starts from root. The data of the root can be any character, which is not associated with any strings. Its children hold references to the first characters in strings.

Java

Javascript

Python


Insert in trie implementation

To insert a string, first check whether the string is in the trie so that you don’t insert a duplicate string You start from root, and loop through each character in the string. 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 has the last character of the string, you mark the node’s isEnd to be true.

Java

Javascript

Python

Doodle

trie insert array


Delete

To delete a string from a trie, first check whether the string exists in the trie. If not, the method can return. You start from root and loop through each character in the string. If the next character is in the node’s children, the node moves to the child node. When the node has the last character of the string, mark the node’s isEnd to be false. The string is still in the trie, but unsearchable.

Java

Javascript

Python

To search for a string in a trie, you start from root and loop through each character in the string. 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 has the last character of the string, return the node’s isEnd value. If the value is false, it indicates the string has been deleted.

Java

Javascript

Python


Print

To print all strings in a trie, recursion is used to traverse all nodes in the trie. This is similar to the preorder (depth-first search) traversal 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 string. It indicates the completion of concatenating one string. You can go ahead to add the string to the result list.

Java

Javascript

Python


Free download

Download trie implementation using linked list in Java, JavaScript and Python
Data structures introduction PDF