Here is some code that I found online to create and search a trie:
class Trie:
head = {}
def add(self, word):
cur = self.head
for ch in word:
if ch not in cur:
cur[ch] = {}
cur = cur[ch]
cur['*'] = True
def search(self, word):
cur = self.head
for ch in word:
if ch not in cur:
return False
cur = cur[ch]
if '*' in cur:
return True
else:
return False
Let’s add a few nodes:
t=Trie()
t.add('hi')
t.add('hello')
Now, let’s search the trie and see the result:
print(t.search('hi'))
print(t.search('hello'))
print(t.search('hel'))
#returns
True
True
False
Is this what you are looking for? If not, please provide an example trie and the expected result.