Chapter 17

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]

  1. This trie stores the words: “tag,” “tan,” “tank,” “tap,” “today,” “total,” “we,” “well,” and “went.”

  2. Here is a trie that stores the words “get,” “go,” “got,” “gotten,” “hall,” “ham,” “hammer,” “hill,” and “zebra”:

    images/tries/solution_2.png
  3. 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:

     def​ ​traverse​(self, node=None):
      currentNode = node ...

Get A Common-Sense Guide to Data Structures and Algorithms, Second Edition, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.