Skip to content

Commit 275dcfd

Browse files
committed
Fixes The Bug In Radix Tree. Issue #11316
1 parent d016fda commit 275dcfd

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

data_structures/trie/radix_tree.py

+40-36
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
trie (prefix tree) in whicheach node that is the only child is merged
44
with its parent [https://en.wikipedia.org/wiki/Radix_tree]
55
"""
6-
6+
import unittest
77

88
class RadixNode:
99
def __init__(self, prefix: str = "", is_leaf: bool = False) -> None:
@@ -62,6 +62,11 @@ def insert(self, word: str) -> None:
6262
-- A (leaf)
6363
--- A (leaf)
6464
"""
65+
## Handle the Case where word is empty by using an if branch
66+
if word == "":
67+
self.is_leaf = True
68+
return
69+
6570
# Case 1: If the word is the prefix of the node
6671
# Solution: We set the current node as leaf
6772
if self.prefix == word and not self.is_leaf:
@@ -190,40 +195,39 @@ def print_tree(self, height: int = 0) -> None:
190195
for value in self.nodes.values():
191196
value.print_tree(height + 1)
192197

193-
194-
def test_trie() -> bool:
195-
words = "banana bananas bandana band apple all beast".split()
196-
root = RadixNode()
197-
root.insert_many(words)
198-
199-
assert all(root.find(word) for word in words)
200-
assert not root.find("bandanas")
201-
assert not root.find("apps")
202-
root.delete("all")
203-
assert not root.find("all")
204-
root.delete("banana")
205-
assert not root.find("banana")
206-
assert root.find("bananas")
207-
208-
return True
209-
210-
211-
def pytests() -> None:
212-
assert test_trie()
213-
214-
215-
def main() -> None:
216-
"""
217-
>>> pytests()
218-
"""
219-
root = RadixNode()
220-
words = "banana bananas bandanas bandana band apple all beast".split()
221-
root.insert_many(words)
222-
223-
print("Words:", words)
224-
print("Tree:")
225-
root.print_tree()
226-
198+
## write unit test for the code using unittest library with logic similar to test_trie() function
199+
## and call it from main()
200+
201+
class TestRadixNode(unittest.TestCase):
202+
def test_trie(self) -> None:
203+
words = "banana bananas bandana band apple all beast".split()
204+
root = RadixNode()
205+
root.insert_many(words)
206+
207+
self.assertTrue(all(root.find(word) for word in words))
208+
self.assertFalse(root.find("bandanas"))
209+
self.assertFalse(root.find("apps"))
210+
root.delete("all")
211+
self.assertFalse(root.find("all"))
212+
root.delete("banana")
213+
self.assertFalse(root.find("banana"))
214+
self.assertTrue(root.find("bananas"))
215+
216+
217+
def test_trie_2(self) -> None:
218+
'''
219+
now add a new test case which inserts
220+
foobbb, fooaaa, foo in the given order and checks for different assertions
221+
'''
222+
words = "foobbb fooaaa foo".split()
223+
root = RadixNode()
224+
root.insert_many(words)
225+
226+
self.assertTrue(all(root.find(word) for word in words))
227+
root.delete("foo")
228+
self.assertFalse(root.find("foo"))
229+
self.assertTrue(root.find("foobbb"))
230+
self.assertTrue(root.find("fooaaa"))
227231

228232
if __name__ == "__main__":
229-
main()
233+
unittest.main()

0 commit comments

Comments
 (0)