Created
July 18, 2019 21:48
-
-
Save DanielNill/b2511305a628e6686fecf5166b7182b7 to your computer and use it in GitHub Desktop.
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 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