Skip to content

Instantly share code, notes, and snippets.

@DanielNill
Created July 18, 2019 21:48
Show Gist options
  • Select an option

  • Save DanielNill/b2511305a628e6686fecf5166b7182b7 to your computer and use it in GitHub Desktop.

Select an option

Save DanielNill/b2511305a628e6686fecf5166b7182b7 to your computer and use it in GitHub Desktop.
class Node():
def __init__(self):
self.children = {}
self.end = False
class Trie():
root = Node()
def insert(self, word):
cur = self.root
for char in word:
if char not in cur.children:
cur.children[char] = Node()
cur = cur.children[char]
cur.end = True
def search(self, word):
cur = self.root
for char in word:
if char not in cur.children:
return False
cur = cur.children[char]
return cur != None and cur.end
t = Trie()
t.insert("hello")
t.insert("fello")
t.insert("here")
t.insert("feel")
t.insert("feelings")
print(t.search("hello"))
print(t.search("goodbye"))
print(t.search("fello"))
print(t.search("here"))
print(t.search("feel"))
print(t.search("feelings"))
print(t.search("feeelings"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment