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:
- How can I
print out the trie
? - For some reason, the above doesn't produce the correct trie, for example,
"mnopqr"
and"no"
should be under thesame 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?: