Skip to content

Commit b1b4caf

Browse files
committed
Enhanced Trie with case insensitivity, word '
deletion, and improved readability.
1 parent bb83ee5 commit b1b4caf

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

strings/autocomplete_using_trie.py

+32-11
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,42 @@ def __init__(self) -> None:
88
self._trie: Dict[str, Union[Dict, bool]] = {}
99

1010
def insert_word(self, word: str) -> None:
11-
"""Inserts a word into the trie."""
11+
"""Inserts a word into the trie, case insensitive."""
1212
node = self._trie
13+
word = word.lower()
1314
for char in word:
1415
if char not in node:
1516
node[char] = {}
1617
node = node[char]
1718
node[END] = True
1819

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."""
2122
node = self._trie
23+
prefix = prefix.lower()
2224
for char in prefix:
2325
if char in node:
2426
node = node[char]
2527
else:
2628
return []
2729
return self._elements(node)
2830

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+
2947
def _elements(self, node: Dict[str, Union[Dict, bool]]) -> Tuple[str, ...]:
3048
"""Recursively collects all words from the current node."""
3149
result = []
@@ -37,20 +55,17 @@ def _elements(self, node: Dict[str, Union[Dict, bool]]) -> Tuple[str, ...]:
3755
result.extend(sub_result)
3856
return tuple(result)
3957

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, ...]:
4660
"""
4761
Autocompletes the given prefix using the trie.
4862
4963
>>> trie = Trie()
64+
>>> words = ("depart", "detergent", "daring", "dog", "deer", "deal")
5065
>>> for word in words:
5166
... trie.insert_word(word)
5267
...
53-
>>> matches = autocomplete_using_trie("de")
68+
>>> matches = autocomplete_using_trie("de", trie)
5469
>>> "detergent" in matches
5570
True
5671
>>> "dog" in matches
@@ -60,7 +75,13 @@ def autocomplete_using_trie(prefix: str) -> Tuple[str, ...]:
6075
return tuple(prefix + suffix for suffix in suffixes)
6176

6277
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))
6485

6586
if __name__ == "__main__":
6687
import doctest

0 commit comments

Comments
 (0)