-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrie.py
120 lines (97 loc) · 3.14 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
__author__ = 'yangbin1729'
class TrieByDict:
"""
用嵌套词典模拟字典树的树状结构
- 实现插入、搜索、及自动补全功能
>>> {'bad', 'bar'}
{'b':
{'a':
{'d': {'#': '#'},
'r': {'#': '#'}}}}
"""
def __init__(self):
self.lookup = {}
self.end_flag = '#'
def insert(self, word):
t = self.lookup
for char in word:
if char not in t:
t[char] = {}
t = t[char]
t[self.end_flag] = self.end_flag
def search(self, word):
t = self.lookup
for char in word:
if char not in t:
return False
t = t[char]
return self.end_flag in t
def startswith(self, prefix):
t = self.lookup
for char in prefix:
if char not in t:
return False
t = t[char]
return True
def autocomplete(self, prefix):
t = self.lookup
for char in prefix:
if char not in t:
return False
t = t[char]
res = []
def _helper(pre, d):
if "#" in d:
res.append(pre)
return
for char in d:
_helper(pre + char, d[char])
_helper(prefix, t)
return res
class Trie:
"""
字典树每一个节点的子节点,都用一个长为 26 的数组表示,每一位代表一个字母是否存在
"""
class TrieNode:
def __init__(self):
self.children = [None] * 26
self.isEndOfWord = False
def __init__(self):
self.root = self.getNode()
def getNode(self):
return self.TrieNode()
def _charToIndex(self, char):
return ord(char) - ord('a')
def insert(self, word):
root = self.root
length = len(word)
for level in range(length):
index = self._charToIndex(word[level])
if not root.children[index]:
root.children[index] = self.getNode()
root = root.children[index]
root.isEndOfWord = True
def search(self, word):
root = self.root
length = len(word)
for level in range(length):
index = self._charToIndex(word[level])
if not root.children[index]:
return False
root = root.children[index]
return root != None and root.isEndOfWord
if __name__ == '__main__':
words = ["the", "a", "there", "anaswe", "any", "by", "their"]
trie = Trie()
for word in words:
trie.insert(word)
output = ["Not present in trie", "Present in trie"]
print("{} ---- {}".format("the", output[trie.search("the")]))
print("{} ---- {}".format("these", output[trie.search("these")]))
print("{} ---- {}".format("their", output[trie.search("their")]))
print("{} ---- {}".format("thaw", output[trie.search("thaw")]))
words = {'bad', 'bed', 'bald', 'break'}
trie = TrieByDict()
for word in words:
trie.insert(word)
print(trie.autocomplete('ba'))