-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie.py
37 lines (27 loc) · 811 Bytes
/
Trie.py
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
class TrieNode:
def __init__(self, char):
self.char = char
self.is_word = False
self.children = {}
self.word_freq = None
class Trie:
def __init__(self):
self.root = TrieNode("")
def insert(self, word, freq):
node = self.root
for char in word:
if char in node.children:
node = node.children[char]
else:
new_node = TrieNode(char)
node.children[char] = new_node
node = new_node
node.is_word = True
node.word_freq = freq
# lazy way to do
def getFreq(self, word):
node = self.root
for char in word:
if char in node.children:
node = node.children[char]
return node.word_freq