-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriev4.py
More file actions
33 lines (23 loc) · 675 Bytes
/
triev4.py
File metadata and controls
33 lines (23 loc) · 675 Bytes
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
class Trie(object):
def __init__(self):
self.root_node = {}
def add_word(self, word):
current_node = self.root_node
is_new_word = False
for char in word:
if char not in current_node:
is_new_word = True
current_node[char] = {}
current_node = current_node[char]
if 'EOW' not in current_node:
is_new_word = True
current_node['EOW'] = {}
return is_new_word
def main():
t = Trie()
print(t.add_word('foo'))
print(t.add_word('ood'))
print(t.add_word('food'))
print(t.root_node)
if __name__ == '__main__':
main()