Skip to content
Deletion
step 1/5

Reading — step 1 of 5

Read

~1 min readSearch & Prefix

Deletion

Deleting a word from a trie:

  1. Walk to the word (return early if not found).
  2. Unset is_word on the leaf.
  3. Walk back up; for each node, if it has no children AND is not a word, delete it.
python

The recursion bubbles up "should_prune" booleans. Each node decides whether to delete the child based on whether the child is now useless (no descendants, not a word).

Edge cases:

  • Word not in trie: no-op.
  • Other words share a prefix: don't delete those nodes.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…