Skip to content

Instantly share code, notes, and snippets.

@siddhantmedar
Created March 20, 2022 04:28
Show Gist options
  • Save siddhantmedar/90bbcc354ccaea34d35215a43ca967da to your computer and use it in GitHub Desktop.
Save siddhantmedar/90bbcc354ccaea34d35215a43ca967da to your computer and use it in GitHub Desktop.
Implementation of Prefix Tree (Trie) in Python along with insert and search operations
class Trie:
def __init__(self):
self.children = {}
self.isEnd = False
def insert(self, word):
curr = self
for w in word:
if w not in curr.children:
curr.children[w] = Trie()
curr = curr.children[w]
curr.isEnd = True
def search(self, word):
curr = self
for w in word:
if w not in curr.children:
return False
curr = curr.children[w]
return curr.isEnd == True
root = Trie()
root.insert("TRIE")
print(root.search("TRIE"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment