26/35
Part 7 of 7 · Follow-upsLearn and do
Delete a word by lowering the counts, and drop a branch when its count reaches 0
With counts, deleting is the insert run backwards, plus one decision: drop a branch as soon as no word uses it.
Sedgewick and Wayne name the two styles. A lazy delete only removes the marker. An eager delete also cleans up the links to parts of the trie that no word uses any more.
shop = {}
for word in CATALOG:
insert_counted(shop, word)
print(remove(shop, "tents"), has_word(shop, "tent"), count_prefix(shop, "te"))
print(remove(shop, "lantern"), has_word(shop, "lamp"), count_prefix(shop, "la"))
print(remove(shop, "ten"))
remove(shop, "tents") runs on the six items. What is count_prefix(shop, "te") after it?
Pick one to see what it prints.
Pick an answer above to see the rest