TRIE-P4

A compressed trie

Practice 4 of 5Tries8 tests

The problem

Write a compressed trie (lesson 4): each edge carries a string, the labels below a node start with different letters, and every node except the root is the end of a word or has at least two children. Store edges as the starter does: node.kids maps the first letter of a label to the pair (label, child). insert, search and starts_with work as in TRIE-P1.

The method labels() is given. It lists the edge labels depth first, in alphabetical order, so the tests can see the shape of your trie.

t = RadixTrie()
for w in ["bus", "tea", "team", "ten", "to", "toast"]:
    t.insert(w)
t.labels()
# ['bus', 't', 'e', 'a', 'm', 'n', 'o', 'ast']
t.starts_with("toa")   # True: ends inside the edge 'ast'
t.search("toa")        # False

Words and prefixes are non-empty and use the letters a to z. The same word may be inserted twice.

Limits

  • Up to 20,000 words of at most 8 letters. Each operation should take O(L) time, and the trie must have at most 2n nodes for n different words.

Builds on TRIE-4 Compressed tries.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

8 tests, not run yet
Test results
#TestResultReads · time
1The lesson's wordsNot run
2Split an edgeNot run
3Word ends inside an edgeNot run
4One wordNot run
5Order does not matterNot run
6Search inside an edgeNot run
7Inserted twiceNot run
820,000 wordsNot run