-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.py
91 lines (75 loc) · 2.2 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class Node(object):
def __init__(self):
self._val = ''
self._children = []
self.parent = None
self._is_word = False
@property
def is_word(self):
return self._is_word
@is_word.setter
def is_word(self, value):
self._is_word = value
def contains(self, word):
if not word.startswith(self.val):
return None
if self.val == word:
return self
result = None
for child in self.children:
result = child.contains(word)
if result:
break
return result
def add_child(self, word):
child = Node()
child._val = word
self.children.append(child)
child.parent = self
return child
@property
def children(self):
return self._children
@property
def val(self):
return self._val
class Trie(object):
def __init__(self):
self._head = Node()
def add_word(self, word):
cur_node = self._head
i = 0
while i <= len(word):
if cur_node.val == word[:i]:
if i == len(word):
cur_node.is_word = True
break
i += 1
for child in cur_node.children:
if child.val == word[:i]:
cur_node = child
break
else:
cur_node = cur_node.add_child(word[:i])
# Assumes a word can be a prefix of itself. 'dog' is prefix of 'dog'
def get_prefix(self, prefix):
return self._head.contains(prefix)
def is_prefix(self, prefix):
return True if self._head.contains(prefix) else False
def is_word(self, word):
node = self.get_prefix(word)
return node and node.is_word
if __name__ == '__main__':
trie = Trie()
print(trie.is_word('cat'))
print(trie.is_word('dog'))
trie.add_word('cat')
trie.add_word('dog')
print(trie.is_word('cat'))
print(trie.is_word('dog'))
print(trie.is_word('d'))
print(trie.is_prefix('d'))
print(trie.is_prefix('do'))
print(trie.is_prefix('dog'))
print(trie.is_prefix('c'))
print(trie.is_word('c'))