August 2024
Intermediate to advanced
516 pages
11h 47m
English
We’re finally ready to implement our autocomplete feature. In fact, we’ve pretty much done all the legwork already. All we need to do is put the pieces together.
Here’s a basic autocomplete method that we can drop into our Trie class:
| | autocomplete(prefix) { |
| | const currentNode = this.search(prefix); |
| | |
| | if (!currentNode) { return null; } |
| | |
| | return this.collectAllWords([], currentNode); |
| | } |
Yes, that’s it. By using our search method and collectAllWords method together, we can autocomplete any prefix. Here’s how this works.
The autocomplete method accepts the prefix parameter, which is the string of characters the user begins typing in.
First, we search the trie for the existence of the prefix. If the search ...
Read now
Unlock full access