File tree Expand file tree Collapse file tree 2 files changed +17
-27
lines changed Expand file tree Collapse file tree 2 files changed +17
-27
lines changed Original file line number Diff line number Diff line change @@ -55,12 +55,14 @@ def test_Trie():
5555 assert trie_1 .autocomplete ("dict" ) == ["dict" , "dicts" , "dicts_lists_tuples" ]
5656
5757 trie_2 = Trie ()
58- trie_2 .bulk_insert (["apple" , "app" , "apricot" , "banana" ])
58+ trie_2 .insert ("apple" )
59+ trie_2 .insert ("app" )
60+ trie_2 .insert ("apricot" )
61+ trie_2 .insert ("banana" )
5962 assert trie_2 .count_words () == 4
6063
6164 trie_2 .clear ()
6265 assert trie_2 .count_words () == 0
63- assert trie_2 .is_empty ()
6466
6567 assert trie_2 .is_empty ()
6668
@@ -70,8 +72,10 @@ def test_Trie():
7072 assert sorted (trie_3 .all_words ()) == ["hello" , "world" ]
7173
7274 trie_4 = Trie ()
73- trie_4 .bulk_insert (["zebra" , "dog" , "duck" , "dove" ])
74- print (trie_4 .shortest_unique_prefix ())
75+ trie_4 .insert ("zebra" )
76+ trie_4 .insert ("dog" )
77+ trie_4 .insert ("duck" )
78+ trie_4 .insert ("dove" )
7579 assert trie_4 .shortest_unique_prefix () == {
7680 "zebra" : "z" ,
7781 "dog" : "dog" ,
Original file line number Diff line number Diff line change @@ -180,10 +180,13 @@ def strings_with_prefix(self, string: str) -> list:
180180 """
181181
182182 def _collect (node : TrieNode , prefix : str , strings : list ):
183- if node .is_terminal :
184- strings .append (prefix )
185- for child in node ._children :
186- _collect (node .get_child (child ), prefix + child , strings )
183+ stack = [(node , prefix )]
184+ while stack :
185+ current_node , current_prefix = stack .pop ()
186+ if current_node .is_terminal :
187+ strings .append (current_prefix )
188+ for child in current_node ._children :
189+ stack .append ((current_node .get_child (child ), current_prefix + child ))
187190
188191 strings = []
189192 walk = self .root
@@ -249,27 +252,10 @@ def autocomplete(self, prefix: str) -> list:
249252 """
250253 return self .strings_with_prefix (prefix )
251254
252- def bulk_insert (self , words : list ) -> None :
253- """
254- Inserts multiple words into the trie.
255-
256- Parameters
257- ==========
258-
259- words: list
260- A list of words to be inserted.
261-
262- Returns
263- =======
264-
265- None
266- """
267- for word in words :
268- self .insert (word )
269-
270255 def clear (self ) -> None :
271256 """
272- Clears the trie, removing all words.
257+ Resets the Trie by replacing the root node with a new empty node.
258+ This effectively clears all words from the Trie.
273259
274260 Returns
275261 =======
You can’t perform that action at this time.
0 commit comments