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
7
import unittest
7
8
9
+
8
10
class RadixNode :
9
11
def __init__ (self , prefix : str = "" , is_leaf : bool = False ) -> None :
10
12
# Mapping from the first character of the prefix of the node
@@ -195,9 +197,11 @@ def print_tree(self, height: int = 0) -> None:
195
197
for value in self .nodes .values ():
196
198
value .print_tree (height + 1 )
197
199
200
+
198
201
## write unit test for the code using unittest library with logic similar to test_trie() function
199
202
## and call it from main()
200
203
204
+
201
205
class TestRadixNode (unittest .TestCase ):
202
206
def test_trie (self ) -> None :
203
207
words = "banana bananas bandana band apple all beast" .split ()
@@ -212,13 +216,12 @@ def test_trie(self) -> None:
212
216
root .delete ("banana" )
213
217
self .assertFalse (root .find ("banana" ))
214
218
self .assertTrue (root .find ("bananas" ))
215
-
216
219
217
220
def test_trie_2 (self ) -> None :
218
- '''
219
- now add a new test case which inserts
221
+ """
222
+ now add a new test case which inserts
220
223
foobbb, fooaaa, foo in the given order and checks for different assertions
221
- '''
224
+ """
222
225
words = "foobbb fooaaa foo" .split ()
223
226
root = RadixNode ()
224
227
root .insert_many (words )
@@ -229,5 +232,6 @@ def test_trie_2(self) -> None:
229
232
self .assertTrue (root .find ("foobbb" ))
230
233
self .assertTrue (root .find ("fooaaa" ))
231
234
235
+
232
236
if __name__ == "__main__" :
233
237
unittest .main ()
0 commit comments