Data Structures · Visualization

Trie: Delete

Trie

Deleting a word from a trie unmarks its 'end of word' flag, and then cleans up any nodes along its path that are no longer needed — that is, nodes that have no children and aren't the end of any other word. Time: O(L), where L is the word's length. Space: O(L) for the recursive call stack.

Big-O: Lookup O(word length), independent of the number of words.

Trie: Delete

Trie

Deleting a word from a trie unmarks its 'end of word' flag, and then cleans up any nodes along its path that are no longer needed — that is, nodes that have no children and aren't the end of any other word. This cleanup is usually done recursively from the last character back toward the root, stopping as soon as a node is found that is still needed for another word.

How Delete Works

  1. Search down the trie following the word's characters, keeping track of the path taken.
  2. If the word isn't found, there's nothing to delete.
  3. If found, unmark the final node's 'end of word' flag.
  4. Walking back up the path, remove any node that has no children and is not itself the end of another word.
  5. Stop removing as soon as a node is still needed (has children or marks another word's end).

Delete Worked Example

Input: Delete 'cat' from a trie containing only 'cat'

  1. Follow path: c -> a -> t, found and marked end of word
  2. Unmark 't' as end of word
  3. 't' has no children and isn't an end of word anymore → remove it
  4. 'a' now has no children and isn't an end of word → remove it
  5. 'c' now has no children and isn't an end of word → remove it

Output: The trie becomes empty (just the root remains)

Try your own input
Trie: Introduction
A tree where each path from the root spells out a word one character per edge — words sharing a prefix share the same starting path.
root
root
c
a
t
r
t
d
o
g
n
e
Active path End of word
Trie: Introduction
Press ▶ Run to watch it step by step.
Python · Tries in Code
def delete(root, word):
    def rec(node, i):
        if node is None:
            return None
        if i == len(word):
            node.is_end = False
        else:
            ch = word[i]
            node.children[ch] = rec(node.children.get(ch), i + 1)
        # prune node if empty and not an end
        if not node.is_end and not node.children:
            return None
        return node
    return rec(root, 0)
# example: delete "cat"
Input: Delete 'cat' from a trie containing only 'cat'

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Delete Time & Space Complexity

TimeO(L), where L is the word's length
SpaceO(L) for the recursive call stack

When to Use Delete

  • Removing an outdated word from a dictionary or spell-check list
  • Removing a discontinued product name from a search index
  • Keeping an autocomplete index up to date as data changes

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.