MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / delete

Method delete

data_structures/trie/trie.py:49–75  ·  view source on GitHub ↗

Deletes a word in a Trie :param word: word to delete :return: None

(self, word: str)

Source from the content-addressed store, hash-verified

47 return curr.is_leaf
48
49 def delete(self, word: str) -> None:
50 """
51 Deletes a word in a Trie
52 :param word: word to delete
53 :return: None
54 """
55
56 def _delete(curr: TrieNode, word: str, index: int) -> bool:
57 if index == len(word):
58 # If word does not exist
59 if not curr.is_leaf:
60 return False
61 curr.is_leaf = False
62 return len(curr.nodes) == 0
63 char = word[index]
64 char_node = curr.nodes.get(char)
65 # If char not in current trie node
66 if not char_node:
67 return False
68 # Flag to check if node can be deleted
69 delete_curr = _delete(char_node, word, index + 1)
70 if delete_curr:
71 del curr.nodes[char]
72 return len(curr.nodes) == 0
73 return delete_curr
74
75 _delete(self, word, 0)
76
77
78def print_words(node: TrieNode, word: str) -> None:

Callers 2

test_trieFunction · 0.95
change_stageMethod · 0.45

Calls

no outgoing calls

Tested by 1

test_trieFunction · 0.76