These are the solutions to the exercises found in the section, Exercises . The solutions provided here are in Python, but you can find the solutions in JavaScript and Ruby in the code download. [16]
-
This trie stores the words: “tag,” “tan,” “tank,” “tap,” “today,” “total,” “we,” “well,” and “went.”
-
Here is a trie that stores the words “get,” “go,” “got,” “gotten,” “hall,” “ham,” “hammer,” “hill,” and “zebra”:
-
The following code starts at the trie’s node and iterates over each of its children. For each child, it prints the key and then recursively calls itself on the child node:
<table><tbody><tr><td> <span> </span></td><td> <strong>def</strong> <strong>traverse</strong> (self, node=None):</td></tr><tr><td> <span> </span></td><td>currentNode = node <strong>or</strong> self.root</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <strong>for</strong> key, childNode <strong>in</strong> currentNode.children.items():</td></tr><tr><td> <span> </span></td><td> <strong>print</strong> (key)</td></tr><tr><td> <span> </span></td><td> <strong>if</strong> key != <em>"*"</em> :</td></tr><tr><td> <span> </span></td><td>self.traverse(childNode)</td></tr></tbody></table>
-
Our autocorrect implementation is a combination of the search and collectAllWords functions:
<table><tbody><tr><td> <span> </span></td><td> <strong>def</strong> <strong>autocorrect</strong> (self, word):</td></tr><tr><td> <span> </span></td><td>currentNode = self.root</td></tr><tr><td> <span> </span></td><td> <em># Keep track of how much of the user's word we've found</em> </td></tr><tr><td> <span> </span></td><td> <em># in the trie so far. We'll need to concatenate this with</em> </td></tr><tr><td> <span> </span></td><td> <em># the best suffix we can find in the trie.</em> </td></tr><tr><td> <span> </span></td><td>wordFoundSoFar = <em>""</em> </td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <strong>for</strong> char <strong>in</strong> word:</td></tr><tr><td> <span> </span></td><td> <em># If the current node has child key with current character:</em> </td></tr><tr><td> <span> </span></td><td> <strong>if</strong> currentNode.children.get(char):</td></tr><tr><td> <span> </span></td><td>wordFoundSoFar += char</td></tr><tr><td> <span> </span></td><td> <em># Follow the child node:</em> </td></tr><tr><td> <span> </span></td><td>currentNode = currentNode.children.get(char)</td></tr><tr><td> <span> </span></td><td> <strong>else</strong> :</td></tr><tr><td> <span> </span></td><td> <em># If the current character isn't found among</em> </td></tr><tr><td> <span> </span></td><td> <em># the current node's children, collect all the suffixes that</em> </td></tr><tr><td> <span> </span></td><td> <em># descend from the current node and get the first one.</em> </td></tr><tr><td> <span> </span></td><td> <em># We concatenate the suffix with the prefix we've found so</em> </td></tr><tr><td> <span> </span></td><td> <em># far to suggest the word the user meant to type in:</em> </td></tr><tr><td> <span> </span></td><td> <strong>return</strong> wordFoundSoFar + self.collectAllWords(currentNode)[0]</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <em># If the user's word is found in the trie:</em> </td></tr><tr><td> <span> </span></td><td> <strong>return</strong> word</td></tr></tbody></table>
The basic approach is that we first search the trie to find as much of the prefix as we can. When we hit a dead end, instead of just returning None (as the search function does), we call collectAllWords on the current node to collect all the suffixes that stem from that node. We then use the first suffix of the array and concatenate it with the prefix to suggest a new word to the user.