Inserting a new word into a trie is similar to searching for an existing word.
Here’s how the algorithm goes:
-
We establish a variable called currentNode . At the beginning of our algorithm, this points to the root node.
-
We iterate over each character of our search string.
-
As we point to each character of our search string, we look to see if the currentNode has a child with that character as a key.
-
If it does, we update the currentNode to become that child node and we go back to Step 2, moving on to the next character of our search string.
-
If the currentNode does not have a child node that matches the current character, we create such a child node and update the currentNode to be this new node. We then go back to Step 2, moving on to the next character of our search string.
-
After we insert the final character of our new word, we add a "*" child to the last node to indicate the word is complete.
Let’s see this in action by inserting the word “can” into our earlier example trie.
Setup: We set the currentNode to be the root node. We also point to the first character of our string, which is the "c" as shown in the top diagram .
Step 1: The root node has a "c" child key, so we turn that key’s value into the currentNode . We also point to the next character of our new word, the "a" as shown in the bottom diagram .
Step 2: We inspect the currentNode for a child with the key of "a" . There is one, so we make that the currentNode and point to the next character of our string, which is the "n" as shown in the top diagram .
Step 3: The currentNode does not have an "n" , so we need to create that child as shown in the bottom diagram .
Step 4: We’re done inserting "can" into our trie, so we cap it off with a child of "*" :
And we’re done!
Code Implementation: Trie Insertion
Here is the insert method for our Trie class. You’ll note that most of it looks the same as the earlier search method:
| | def insert (self, word): |
| | currentNode = self.root |
| | |
| | for char in word: |
| | # If the current node has child key with current character: |
| | if currentNode.children.get(char): |
| | # Follow the child node: |
| | currentNode = currentNode.children[char] |
| | else : |
| | # If the current character isn't found among |
| | # the current node's children, we add |
| | # the character as a new child node: |
| | newNode = TrieNode() |
| | currentNode.children[char] = newNode |
| | |
| | # Follow this new node: |
| | currentNode = newNode |
| | |
| | # After inserting the entire word into the trie, |
| | # we add a * key at the end: |
| | currentNode.children[ "*" ] = None |
The first part of this method is the same as search . It diverges when the current Node doesn’t have the child that matches the current character. When this is the case, we add a new key-value pair to the currentNode ’s hash table, with the key being the current character, and the value being a new TrieNode :
| | newNode = TrieNode() |
| | currentNode.children[char] = newNode |
We then update the currentNode to be this new node:
| | currentNode = newNode |
We then repeat the loop until we’re done inserting our new word. Once we’re done, we add a "*" key to the final node’s hash table, with the value as None :
| | currentNode.children[ "*" ] = None |
Like search, trie insertion takes about O(K) steps. If we count the adding of the "*" at the end, it’s technically K + 1 steps, but because we drop the constants, we express the speed as O(K).