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.

Array is the most intuitive implementation. The character is used as index to find the pointer to the child node. However, each node needs to declare a predefined length of (number of characters) array. If you use only alphabet, the size is 26. If you use special characters such as space, the size is 128 or more. This takes memory space. In this post, the trie is implemented in array. Please checkout the linked list and HashMap 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 array); 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 2) – linked list implementation
Trie (part 3) – hash map implementation
Trie animated visual
Data structures and Java collections