I am setting up a trie
, here is my code:
class Trie:
def __init__(self):
self.root = {}
self.endSymbol = "*"
def add(self, word):
node = self.root
for letter in word:
if letter not in node:
node[letter] = {}
node = node[letter]
node[self.endSymbol] = word
def multiStringSearch(smallStrings):
trie = Trie()
for word in smallStrings:
trie.add(word)
return trie
print(multiStringSearch(["abc", "mnopqr", "wyz", "no", "e", "tuuv","abb"])
Two questions:
print out the trie
?"mnopqr"
and "no"
should be under the same root
for the 'n'
, but they appear separately by the above construction.Thanks John and Ervin for your contributions, can I just check then, which of the following would the trie set-up produce?:
trie
:class Trie:
def __init__(self):
self.root = {}
self.endSymbol = "*"
def add(self, word):
node = self.root
for letter in word:
if letter not in node:
node[letter] = {}
node = node[letter]
node[self.endSymbol] = word
def __str__(self):
return str(self.root)
def multiStringSearch(smallStrings):
trie = Trie()
for word in smallStrings:
trie.add(word)
return trie
if __name__ == "__main__":
trie = multiStringSearch(["abc", "mnopqr", "wyz", "no", "e", "tuuv","abb"])
print(trie)
abb
and abc
should be under the same root, and they are if you print out the `trie.