Created
March 20, 2022 04:28
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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