@@ -8,24 +8,42 @@ def __init__(self) -> None:
8
8
self ._trie : Dict [str , Union [Dict , bool ]] = {}
9
9
10
10
def insert_word (self , word : str ) -> None :
11
- """Inserts a word into the trie."""
11
+ """Inserts a word into the trie, case insensitive ."""
12
12
node = self ._trie
13
+ word = word .lower ()
13
14
for char in word :
14
15
if char not in node :
15
16
node [char ] = {}
16
17
node = node [char ]
17
18
node [END ] = True
18
19
19
- def find_word (self , prefix : str ) -> Union [List [str ], Tuple [str ]]:
20
- """Finds all suffixes in the trie that match the given prefix."""
20
+ def find_word (self , prefix : str ) -> Union [List [str ], Tuple [str , ... ]]:
21
+ """Finds all suffixes in the trie that match the given prefix, case insensitive ."""
21
22
node = self ._trie
23
+ prefix = prefix .lower ()
22
24
for char in prefix :
23
25
if char in node :
24
26
node = node [char ]
25
27
else :
26
28
return []
27
29
return self ._elements (node )
28
30
31
+ def delete_word (self , word : str ) -> None :
32
+ """Deletes a word from the trie if it exists, case insensitive."""
33
+ def _delete (node : Dict [str , Union [Dict , bool ]], word : str , depth : int ) -> bool :
34
+ if depth == len (word ):
35
+ if END in node :
36
+ del node [END ]
37
+ return len (node ) == 0
38
+ return False
39
+ char = word [depth ]
40
+ if char in node and _delete (node [char ], word , depth + 1 ):
41
+ del node [char ]
42
+ return len (node ) == 0
43
+ return False
44
+
45
+ _delete (self ._trie , word .lower (), 0 )
46
+
29
47
def _elements (self , node : Dict [str , Union [Dict , bool ]]) -> Tuple [str , ...]:
30
48
"""Recursively collects all words from the current node."""
31
49
result = []
@@ -37,20 +55,17 @@ def _elements(self, node: Dict[str, Union[Dict, bool]]) -> Tuple[str, ...]:
37
55
result .extend (sub_result )
38
56
return tuple (result )
39
57
40
- trie = Trie ()
41
- words = ("depart" , "detergent" , "daring" , "dog" , "deer" , "deal" )
42
- for word in words :
43
- trie .insert_word (word )
44
-
45
- def autocomplete_using_trie (prefix : str ) -> Tuple [str , ...]:
58
+ # Example usage of the enhanced Trie class
59
+ def autocomplete_using_trie (prefix : str , trie : Trie ) -> Tuple [str , ...]:
46
60
"""
47
61
Autocompletes the given prefix using the trie.
48
62
49
63
>>> trie = Trie()
64
+ >>> words = ("depart", "detergent", "daring", "dog", "deer", "deal")
50
65
>>> for word in words:
51
66
... trie.insert_word(word)
52
67
...
53
- >>> matches = autocomplete_using_trie("de")
68
+ >>> matches = autocomplete_using_trie("de", trie )
54
69
>>> "detergent" in matches
55
70
True
56
71
>>> "dog" in matches
@@ -60,7 +75,13 @@ def autocomplete_using_trie(prefix: str) -> Tuple[str, ...]:
60
75
return tuple (prefix + suffix for suffix in suffixes )
61
76
62
77
def main () -> None :
63
- print (autocomplete_using_trie ("de" ))
78
+ trie = Trie ()
79
+ words = ("depart" , "detergent" , "daring" , "dog" , "deer" , "deal" )
80
+ for word in words :
81
+ trie .insert_word (word )
82
+ print (autocomplete_using_trie ("de" , trie ))
83
+ trie .delete_word ("detergent" )
84
+ print (autocomplete_using_trie ("de" , trie ))
64
85
65
86
if __name__ == "__main__" :
66
87
import doctest
0 commit comments