Trie Insertion
Inserting a new word into a trie is similar to searching for an existing word. We first search to see if the word already exists in the trie. If it doesn’t, we insert the new 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. Here, our search string represents the new word we’re inserting. We call it a search string since we’re also searching whether the string already exists in the trie.
-
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access