Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion data_structures/trie/trie.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ def delete(self, word: str) -> None:
Deletes a word in a Trie
:param word: word to delete
:return: None

>>> root = TrieNode()
>>> root.insert_many(["a", "ab"])
>>> root.delete("ab")
>>> root.find("a"), root.find("ab")
(True, False)
>>> root.delete("a")
>>> root.find("a"), root.nodes
(False, {})
"""

def _delete(curr: TrieNode, word: str, index: int) -> bool:
Expand All @@ -69,7 +78,7 @@ def _delete(curr: TrieNode, word: str, index: int) -> bool:
delete_curr = _delete(char_node, word, index + 1)
if delete_curr:
del curr.nodes[char]
return len(curr.nodes) == 0
return len(curr.nodes) == 0 and not curr.is_leaf
return delete_curr

_delete(self, word, 0)
Expand Down
Loading