Trie Search
The most classic trie operation is search—namely, determining whether a string is found in the trie. Search has two flavors: we can search to see whether the string is a complete word, or we can search to see whether the string is at least a word prefix (that is, the beginning of a word). These two versions are similar, but we’ll implement the latter one, where our search will look for prefixes. This search will end up finding complete words as well since a complete word is at least as good as a prefix.
The algorithm for prefix search performs the following steps (they’ll become clearer when we walk though an example that follows):
-
We establish a variable called currentNode. At the beginning of our algorithm, this points to the root ...
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