|
3 | 3 | trie (prefix tree) in whicheach node that is the only child is merged
|
4 | 4 | with its parent [https://en.wikipedia.org/wiki/Radix_tree]
|
5 | 5 | """
|
6 |
| - |
| 6 | +import unittest |
7 | 7 |
|
8 | 8 | class RadixNode:
|
9 | 9 | def __init__(self, prefix: str = "", is_leaf: bool = False) -> None:
|
@@ -62,6 +62,11 @@ def insert(self, word: str) -> None:
|
62 | 62 | -- A (leaf)
|
63 | 63 | --- A (leaf)
|
64 | 64 | """
|
| 65 | + ## Handle the Case where word is empty by using an if branch |
| 66 | + if word == "": |
| 67 | + self.is_leaf = True |
| 68 | + return |
| 69 | + |
65 | 70 | # Case 1: If the word is the prefix of the node
|
66 | 71 | # Solution: We set the current node as leaf
|
67 | 72 | if self.prefix == word and not self.is_leaf:
|
@@ -190,40 +195,39 @@ def print_tree(self, height: int = 0) -> None:
|
190 | 195 | for value in self.nodes.values():
|
191 | 196 | value.print_tree(height + 1)
|
192 | 197 |
|
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")) |
227 | 231 |
|
228 | 232 | if __name__ == "__main__":
|
229 |
| - main() |
| 233 | + unittest.main() |
0 commit comments