-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathTrie.py
More file actions
51 lines (38 loc) · 1.27 KB
/
Trie.py
File metadata and controls
51 lines (38 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Implementation of Trie data structure in python.
# Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_0 = obj.search(word)
# param_1 = obj.startsWith(prefix)
class TrieNode:
def __init__(self, val='', is_end=False):
''' Create a Trie node '''
self.val = val
self.children = {}
self.is_end = is_end
class Trie:
def __init__(self):
''' Create the root node '''
self.root = TrieNode()
def insert(self, word: str) -> None:
''' Insert word in Trie '''
node = self.root
for c in word:
if c not in node.children:
node.children[c] = TrieNode(c)
node = node.children[c]
node.is_end = True
def search(self, word: str) -> bool:
''' Search for word in Trie '''
node = self.root
for c in word:
if c not in node.children: return False
node = node.children[c]
return node.is_end
def startsWith(self, prefix: str) -> bool:
''' Check if a word in Trie starts with prefix '''
node = self.root
for c in prefix:
if c not in node.children: return False
node = node.children[c]
return True