forked from HU-CS201-master/hw04-spring-18
/
trie.py
272 lines (225 loc) · 8.27 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
class TrieNode:
'''A node to be used in a generalized trie.
'''
def __init__(self):
'''Constructor.
'''
self.child = {}
def get_child(self, c):
'''N.get_child(str) -> TrieNode
Returns the child node correspodning to c. Returns None if no
such node exists.
'''
return self.child.get(c, None)
def get_children(self):
'''N.get_children() -> [(str, TrieNode)]
Returns a list containing a pair (l, c) for each child where c
is the child node and l is the label of the edge from N to n.
'''
return self.child.items()
def num_children(self):
'''N.num_children() -> int
Returns the number of children of N.
'''
return len(self.child)
def add_child(self, c):
'''N.add_child(str) -> TrieNode
Adds the child for c and returns it. Does not add but still
returns if the child is already present.
'''
if self.get_child(c):
return self.child[c]
node = TrieNode()
self.child[c] = node
return node
def remove_child(self, c):
'''N.remove_child(str) -> TrieNode
Removes the child for c and returns it. Returns None if no
such child existed at the time of calling this function.
'''
return self.child.pop(c, None)
class Trie:
'''A generalized trie (not space optimized).
'''
def __init__(self):
'''Constructor.
'''
self.root = TrieNode()
self.initiator = '#'
self.delimiter = '$'
self.count = 0
def _get_unique_delimiter(self):
'''T._get_unique_delimiter() -> str
Returns a unique delimiter. Unique delimiters are needed for
each distinct string added to T.
This function is for internal use only.
'''
d,m=divmod(self.count, 256)
return self.delimiter + chr(d) + chr(m)
def _get_branches(self, node):
'''T._get_branches(TrieNode) -> [str]
Returns a list of strings that begin at node in T. The
returned strings do not contain any special characters used
for T's internal purposes. If no string begins at node, the
returned list contains the empty string.
This function is for internal use only.
'''
children = node.get_children()
if not children:
return ['']
lst = []
for label, child in children:
if label == self.delimiter:
lst.append('')
else:
branches = self._get_branches(child)
lst.extend([label+b for b in branches])
return lst
def _get_num_branches(self, node):
'''T._get_num_branches(TrieNode) -> int
Returns the number of strings that begin at node in T. If no
string begins at node, then 1 is returned.
This function is for internal use only.
'''
children = node.get_children()
if not children:
return 1
return sum([self._get_num_branches(child) for _,child in children])
def add(self, q):
'''T.add(str) -> None
Adds q to T.
'''
def insert(s):
n = self.root
for c in s:
n = n.add_child(c)
s = self.initiator + q + self._get_unique_delimiter()
for i in range(len(q)+1):
insert(s[i:])
self.count += 1
def contains_substring(self, q):
'''T.contains_substring(str) -> bool
Returns whether q is a substring in T.
'''
n = self.root
for c in q:
n = n.get_child(c)
if not n:
return False
return True
def contains_string(self, q):
'''T.contains_string(str) -> bool
Returns whether the q has been added to T.
'''
return self.contains_substring(self.initiator + q + self.delimiter)
def count_substring(self, q):
'''T.count_substring(str) -> int
Returns the number of occurrences of q in T.
'''
n = self.root
for c in q:
n = n.get_child(c)
return self._get_num_branches(n)
def complete_substring(self, q):
'''T.complete_substring(str) -> [str]
Returns a list of completions for the substring q.
'''
n = self.root
for c in q:
n = n.get_child(c)
return sorted([q+w for w in self._get_branches(n)])
def complete_string(self, q):
'''T.complete_string(str) -> [str]
Returns a list of the strings added to T which have q as a
prefix.
'''
return [w[1:] for w in self.complete_substring(self.initiator+q)]
def most_frequent(self):
'''T.most_frequent() -> str
Returns the letter that appears the most often in the strings
added to T.
'''
return max([(self._get_num_branches(child), label)
for label, child in self.root.get_children()
if label != self.initiator])[1]
def least_frequent(self):
'''T.least_frequent() -> str
Returns the letter that appears the least often in the strings
added to T.
'''
return min([(self._get_num_branches(child), label)
for label, child in self.root.get_children()
if label != self.delimiter])[1]
def most_frequent_begin(self):
'''T.most_frequent_begin() -> str
Returns the letter with which the most number of strings added
to T begin.
'''
n = self.root.get_child(self.initiator)
children = n.get_children()
return max([(self._get_num_branches(child), label)
for label, child in children])[1]
def least_frequent_begin(self):
'''T.least_frequent_begin() -> str
Returns the letter with which the least number of strings added
to T begin.
'''
n = self.root.get_child(self.initiator)
children = n.get_children()
return min([(self._get_num_branches(child), label)
for label, child in children])[1]
def most_frequent_end(self):
'''T.most_frequent_end() -> str
Returns the letter with which the most number of strings added
to T end.
'''
n = self.root.get_child(self.initiator)
stack = [(l,c) for l,c in n.get_children()]
ends = {}
while stack:
label, node = stack.pop()
if node.get_child(self.delimiter):
ends[label] = ends.get(label,0) + 1
stack.extend([(l,c) for l,c in node.get_children()
if l != self.delimiter])
items = sorted([(count,label) for label,count in ends.items()])
return items[-1][1]
def least_frequent_end(self):
'''T.least_frequent_end() -> str
Returns the letter with which the least number of strings added
to T end.
'''
n = self.root.get_child(self.initiator)
stack = [(l,c) for l,c in n.get_children()]
ends = {}
while stack:
label, node = stack.pop()
if node.get_child(self.delimiter):
ends[label] = ends.get(label,0) + 1
stack.extend([(l,c) for l,c in node.get_children()
if l != self.delimiter])
items = sorted([(count,label) for label,count in ends.items()])
return items[0][1]
def longest_repeat(self):
'''T.longest_repeat() -> str
Returns the longest repeating substring in T.
'''
children = self.root.get_children()
depth = max_depth = 1
stack = [(l,c,depth) for l,c in children
if l != self.initiator
if l != self.delimiter
if c.num_children() > 1]
while stack:
label, node, depth = stack.pop()
if depth > max_depth:
max_depth, longest_label = depth, label
stack.extend([(label+l,c,depth+1)
for l,c in node.get_children()
if l != self.delimiter
if c.num_children() > 1])
return longest_label
trie = Trie()
for line in open('words.txt'):
line = line.strip()
trie.add(line)