77
88
99class TrieNode :
10- def __init__ (self ):
11- self .nodes = dict () # Mapping from char to TrieNode
10+ def __init__ (self ) -> None :
11+ self .nodes : dict [ str , TrieNode ] = dict () # Mapping from char to TrieNode
1212 self .is_leaf = False
1313
14- def insert_many (self , words : list [str ]):
14+ def insert_many (self , words : list [str ]) -> None :
1515 """
1616 Inserts a list of words into the Trie
1717 :param words: list of string words
@@ -20,7 +20,7 @@ def insert_many(self, words: list[str]):
2020 for word in words :
2121 self .insert (word )
2222
23- def insert (self , word : str ):
23+ def insert (self , word : str ) -> None :
2424 """
2525 Inserts a word into the Trie
2626 :param word: word to be inserted
@@ -46,14 +46,14 @@ def find(self, word: str) -> bool:
4646 curr = curr .nodes [char ]
4747 return curr .is_leaf
4848
49- def delete (self , word : str ):
49+ def delete (self , word : str ) -> None :
5050 """
5151 Deletes a word in a Trie
5252 :param word: word to delete
5353 :return: None
5454 """
5555
56- def _delete (curr : TrieNode , word : str , index : int ):
56+ def _delete (curr : TrieNode , word : str , index : int ) -> bool :
5757 if index == len (word ):
5858 # If word does not exist
5959 if not curr .is_leaf :
@@ -75,7 +75,7 @@ def _delete(curr: TrieNode, word: str, index: int):
7575 _delete (self , word , 0 )
7676
7777
78- def print_words (node : TrieNode , word : str ):
78+ def print_words (node : TrieNode , word : str ) -> None :
7979 """
8080 Prints all the words in a Trie
8181 :param node: root node of Trie
@@ -89,7 +89,7 @@ def print_words(node: TrieNode, word: str):
8989 print_words (value , word + key )
9090
9191
92- def test_trie ():
92+ def test_trie () -> bool :
9393 words = "banana bananas bandana band apple all beast" .split ()
9494 root = TrieNode ()
9595 root .insert_many (words )
@@ -112,11 +112,11 @@ def print_results(msg: str, passes: bool) -> None:
112112 print (str (msg ), "works!" if passes else "doesn't work :(" )
113113
114114
115- def pytests ():
115+ def pytests () -> None :
116116 assert test_trie ()
117117
118118
119- def main ():
119+ def main () -> None :
120120 """
121121 >>> pytests()
122122 """
0 commit comments