Reading — step 1 of 5
Read
~1 min readSearch & Prefix
Deletion
Deleting a word from a trie:
- Walk to the word (return early if not found).
- Unset
is_wordon the leaf. - 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…