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. Insert a word in trie, search a word and delete a word by key all takes O(n) time, at the expense of storage. Trie’s node can be implemented with array, linked list, HashMap. Each implementation has its pros and cons.

In array implementation, the character in the node will be used as index to find the pointer of the child. Array takes spaces. Linked list overcomes this problem by creating nodes only for the next character in the words. However a method is called to find the child by search each node in the list. So this implementation uses time to trade the space.

In this post, the trie is implemented in linked list. Please checkout the array and hash map if you are interested.

Table of Content


Define classes

The trie node class has tree variables: data stores a character, children is a data structure that points to the children (branches) nodes (here the data structure is linked list); 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

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

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.

Java

Javascript

Python


Search

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

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

Download Java, JavaScript and Python code
Trie (part 1) – array implementation
Trie (part 3) – hash map implementation
Trie animated visual
Data structures and Java collections