We’re just about ready to build a real autocomplete feature. To make this a tad easier, let’s first build a slightly simpler function that we’ll use to help us with this feature.
Collecting All the Words
The next method we’re going to add to our Trie class is a method that returns an array of all the words in the trie. Now, it’s rare that we’d actually want to list the entire dictionary. However, we’re going to allow this method to accept any node of the trie as an argument so that it can list all the words that start from that node.
The following method, called collectAllWords , collects a list of all the trie’s words starting from a particular node:
| | def collectAllWords (self, node=None, word= "" , words=[]): |
| | # This method accepts three arguments. The first is the |
| | # node whose descendants we're collecting words from. |
| | # The second argument, word, begins as an empty string, |
| | # and we add characters to it as we move through the trie. |
| | # The third argument, words, begins as an empty array, |
| | # and by the end of the function will contain all the words |
| | # from the trie. |
| | # The current node is the node passed in as the first parameter, |
| | # or the root node if none is provided: |
| | currentNode = node or self.root |
| | |
| | # We iterate through all the current node's children: |
| | for key, childNode in currentNode.children.items(): |
| | # If the current key is *, it means we hit the end of a |
| | # complete word, so we can add it to our words array: |
| | if key == "*" : |
| | words.append(word) |
| | else : # If we're still in the middle of a word: |
| | # We recursively call this function on the child node. |
| | self.collectAllWords(childNode, word + key, words) |
| | |
| | return words |
This method relies heavily on recursion, so let’s break it down carefully.
The method accepts three primary arguments: node , word , and words . The node argument allows us to specify which node in the trie to start collecting the words from. If we don’t pass in this argument, our method will start from the root node, collecting every word in the entire trie.
The word and words arguments are used as part of the method’s recursion, so we aren’t expected to specify them at the outset. By default, the words argument is an empty array. As we discover complete words in our trie, we add those strings to this array, which is what we return at the end of our function.
The word argument defaults to an empty string. As we move through the trie, we add characters to this word . When we reach a "*" , the word is considered complete and we add it to the words array.
Let’s now break down each line of code.
The first thing we do is set the currentNode :
| | currentNode = node or self.root |
By default, the currentNode will be the root node, unless we passed in some other node as the method’s first parameter. Let’s assume for now that the currentNode is indeed the root node.
Next, we begin a loop that iterates over all the key-value pairs in the currentNode ’s children hash table:
| | for key, childNode in currentNode.children.items(): |
In each iteration of the loop, the key is always a single-character string, and the value, childNode , is another instance of TrieNode .
Let’s skip to the else clause, as this is where the magic happens:
| | self.collectAllWords(childNode, word + key, words) |
This line recursively calls the collectAllWords function, with the childNode passed in as the first argument. This is our way of traversing the trie and collecting the words as we go.
The second argument we pass in is word + key , making it so that as we move through each node of the trie, we add the key to the current word, building up the word as we go.
The third argument is the words array. By passing this array along in each recursive call, we’re able to collect the complete words into it and build this list as we traverse the trie.
The base case is when we reach a "*" key, indicating we’ve completed a word. At this point, we can add the word to the words array:
| | if key == "*" : |
| | words.append(word) |
At the end of the function, we return the words array. If we called this function without passing in a specific node, this will return the complete list of words in the trie.
Recursion Walk-Through
Let’s run through a quick visual example of this using a simple trie. This trie holds two words, “can” and “cat”:
Call 1: In the very first call of collectAllWords , the currentNode starts out at the root, word is an empty string, and the words array is empty:
We iterate over the root node’s children. The root node happens to only have one child key, the "c" , which points to a child node. Before we recursively call collectAllWords on this child node, we need to add the current call to the call stack.
We then recursively call collectAllWords on the "c" child node. We also pass in word + key as the word argument. word + key is the string "c" , since word was empty, and the key is "c" . And we also pass in the words array, which is still empty. The next image shows where we are once we make this recursive call:
Call 2: We iterate over the current node’s children. It has just one child key, the "a" . Before recursively calling collectAllWords on the respective child node, we will add the current call to the call stack. In the diagram that follows, we call this current node the "a" node, meaning it’s the node that has "a" as a child.
We then recursively call collectAllWords . We pass in the child node, "ca" (which is word + key ), and the still-empty words array:
Call 3: We iterate over the current node’s children, which are "n" and "t" . We’ll start with the "n" first. Before making any recursive calls, though, we need to first add the current call to our call stack. In the diagram that follows, we call this current node the "n/t" node, meaning it’s the node that has both "n" and "t" as children.
When we then call collectAllWords on the "n" child, and we also pass in "can" as the word argument, as well as the empty words array:
Call 4: We iterate over the current node’s children. In this case, it has only one child, which is the "*" . This is our base case. We add the current word , which is "can" , to the words array:
Call 5: We now pop the top call from our call stack, which was the call of collectAllWords on the node with the children keys of "n" and "t" , and where word was "ca" . This means we now return to that call (as we return to whatever call we pop from the call stack):
There’s a subtle but important point to make here. In the current call, word is back to "ca" , since that was the word argument when we first initiated this call. However, the words array now contains the word "can" , even though the array was empty when we originally made this call.
Here’s the reason why this works. In many programming languages, an array can be passed up and down a call stack because the array remains the same object in memory even when we add new values to it. (The same concept applies to hash tables as well, which was how we were able to pass it along as part of the memoization technique you learned about in Chapter 12, Dynamic Programming ).
When a string is modified, on the other hand, the computer actually creates a new string instead of truly modifying the original string object. Therefore, when we updated word by changing it from "ca" to "can" , the previous call still only has access to the original string, "ca" . (In some languages, this may work slightly differently. For our purposes, though, this is the general idea.)
In any case, we’re in the middle of a call where words contains the word "can" , and word is "ca" .
Call 6: At this point, we’ve already iterated over the "n" key, so the loop is now up to the "t" key. Before recursively calling collectAllWords on the "t" child node, we need to add the current call to the call stack again. (This actually will be the second time we add this call to the call stack. We previously popped it off, but now we’re going to add it again.)
When we call collectAllWords on the "t" child, we pass in "cat" as the word argument (since that is word + key ) , and the words array:
Call 7: We iterate over the current node’s children. The only child here is "*" , so we add the current word of "cat" to our words array:
At this point, we can unwind the call stack, popping off each call and completing their execution, which all end by returning the words array. The final call we complete—which was the first call to kick this all off— returns words as well. Because this contains the strings "can" and "cat" , we’ve successfully returned the trie’s entire word list.