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

Method find

data_structures/trie/radix_tree.py:102–129  ·  view source on GitHub ↗

Returns if the word is on the tree Args: word (str): word to check Returns: bool: True if the word appears on the tree >>> RadixNode("myprefix").find("mystring") False

(self, word: str)

Source from the content-addressed store, hash-verified

100 self.nodes[matching_string[0]].insert(remaining_word)
101
102 def find(self, word: str) -> bool:
103 """Returns if the word is on the tree
104
105 Args:
106 word (str): word to check
107
108 Returns:
109 bool: True if the word appears on the tree
110
111 >>> RadixNode("myprefix").find("mystring")
112 False
113 """
114 incoming_node = self.nodes.get(word[0], None)
115 if not incoming_node:
116 return False
117 else:
118 _matching_string, remaining_prefix, remaining_word = incoming_node.match(
119 word
120 )
121 # If there is remaining prefix, the word can't be on the tree
122 if remaining_prefix != "":
123 return False
124 # This applies when the word and the prefix are equal
125 elif remaining_word == "":
126 return incoming_node.is_leaf
127 # We have word remaining so we check the next node
128 else:
129 return incoming_node.find(remaining_word)
130
131 def delete(self, word: str) -> bool:
132 """Deletes a word from the tree if it exists

Callers 1

test_trieFunction · 0.95

Calls 2

matchMethod · 0.80
getMethod · 0.45

Tested by 1

test_trieFunction · 0.76