Skip to content

Commit 3cf34fe

Browse files
committed
Fix empty-string lookup and deletion in the radix tree
1 parent 6e03a48 commit 3cf34fe

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

‎data_structures/trie/radix_tree.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ def find(self, word: str) -> bool:
118118
>>> RadixNode("myprefix").find("mystring")
119119
False
120120
"""
121+
if word == "":
122+
return self.is_leaf
123+
121124
incoming_node = self.nodes.get(word[0], None)
122125
if not incoming_node:
123126
return False
@@ -147,6 +150,11 @@ def delete(self, word: str) -> bool:
147150
>>> RadixNode("myprefix").delete("mystring")
148151
False
149152
"""
153+
if word == "":
154+
was_present = self.is_leaf
155+
self.is_leaf = False
156+
return was_present
157+
150158
incoming_node = self.nodes.get(word[0], None)
151159
if not incoming_node:
152160
return False

0 commit comments

Comments
 (0)