VOOZH about

URL: https://www.geeksforgeeks.org/dsa/trie-delete/

⇱ Trie | (Delete) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Trie | (Delete)

Last Updated : 23 May, 2026

In the previous post on trie we have described how to insert and search a node in trie. Here is an algorithm how to delete a node from trie.
During delete operation we delete the key in bottom up manner using recursion. The following are possible conditions when deleting key from trie, 

  1. Key may not be there in trie. Delete operation should not modify trie.
  2. Key present as unique key (no part of key contains another key (prefix), nor the key itself is prefix of another key in trie). Delete all the nodes.
  3. Key is prefix key of another long key in trie. Unmark the leaf node.
  4. Key present in trie, having atleast one other key as prefix key. Delete nodes from end of key until first leaf node of longest prefix key.

The below code presents algorithm to implement above conditions. 


Output
Yes
No
Yes

Time Complexity: The time complexity of the deletion operation is O(n) where n is the key length.
Auxiliary Space: O(n) due to recursive call stack, where n is the key length.

Comment
Article Tags: