-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefix_tree.py
More file actions
237 lines (190 loc) · 6.5 KB
/
prefix_tree.py
File metadata and controls
237 lines (190 loc) · 6.5 KB
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
"""
Write-only prefix tree (trie) for efficient string storage and retrieval.
Supports adding strings and finding all strings that are prefixes of a given string.
The tree structure allows for efficient storage of strings with common prefixes.
Time complexity: O(m) for add and find operations, where m is the length of the string.
Space complexity: O(ALPHABET_SIZE * N * M) in the worst case, where N is the number
of strings and M is the average length of strings.
"""
from __future__ import annotations
import bisect
class PrefixTree:
def __init__(self) -> None:
self.keys: list[str] = []
self.values: list[PrefixTree | None] = []
def pp(self, indent: int = 0) -> None:
"""Pretty-print tree structure for debugging."""
for key, value in zip(self.keys, self.values): # Note: add strict=False for Python 3.10+
print(" " * indent + key + ": " + ("-" if value is None else ""))
if value is not None:
value.pp(indent + 2)
def find_all(self, s: str, offset: int, append_to: list[int]) -> None:
"""Find all strings in tree that are prefixes of s[offset:]. Appends end positions."""
if self.keys and self.keys[0] == "":
append_to.append(offset)
index = bisect.bisect_left(self.keys, s[offset : offset + 1])
if index == len(self.keys):
return
if s[offset : offset + len(self.keys[index])] == self.keys[index]:
pt = self.values[index]
if pt is None:
append_to.append(offset + len(self.keys[index]))
else:
pt.find_all(s, offset + len(self.keys[index]), append_to)
def max_len(self) -> int:
"""Return length of longest string in tree."""
result = 0
for key, value in zip(self.keys, self.values): # Note: add strict=False for Python 3.10+
result = max(result, len(key) + (0 if value is None else value.max_len()))
return result
def add(self, s: str) -> None:
"""Add string to tree."""
if not s or not self.keys:
self.keys.insert(0, s)
self.values.insert(0, None)
return
pos = bisect.bisect_left(self.keys, s)
if pos and self.keys[pos - 1] and self.keys[pos - 1][0] == s[0]:
pos -= 1
if pos < len(self.keys) and self.keys[pos][:1] == s[:1]:
# Merge
if s.startswith(self.keys[pos]):
pt = self.values[pos]
if pt is None:
child = PrefixTree()
child.keys.append("")
child.values.append(None)
self.values[pos] = pt = child
pt.add(s[len(self.keys[pos]) :])
elif self.keys[pos].startswith(s):
child = PrefixTree()
child.keys.append("")
child.values.append(None)
child.keys.append(self.keys[pos][len(s) :])
child.values.append(self.values[pos])
self.keys[pos] = s
self.values[pos] = child
else:
prefix = 1
while s[prefix] == self.keys[pos][prefix]:
prefix += 1
child = PrefixTree()
if s < self.keys[pos]:
child.keys.append(s[prefix:])
child.values.append(None)
child.keys.append(self.keys[pos][prefix:])
child.values.append(self.values[pos])
if s >= self.keys[pos]:
child.keys.append(s[prefix:])
child.values.append(None)
self.keys[pos] = s[:prefix]
self.values[pos] = child
else:
self.keys.insert(pos, s)
self.values.insert(pos, None)
def test_main() -> None:
p = PrefixTree()
p.add("cat")
p.add("car")
p.add("card")
l: list[int] = []
p.find_all("card", 0, l)
assert l == [3, 4]
assert p.max_len() == 4
# Don't write tests below during competition.
def test_empty_tree() -> None:
p = PrefixTree()
l: list[int] = []
p.find_all("test", 0, l)
assert l == []
assert p.max_len() == 0
def test_single_string() -> None:
p = PrefixTree()
p.add("hello")
l: list[int] = []
p.find_all("hello world", 0, l)
assert l == [5]
assert p.max_len() == 5
def test_empty_string() -> None:
p = PrefixTree()
p.add("")
l: list[int] = []
p.find_all("anything", 0, l)
assert l == [0] # Empty string matches at position 0
def test_no_match() -> None:
p = PrefixTree()
p.add("cat")
p.add("car")
l: list[int] = []
p.find_all("dog", 0, l)
assert l == []
def test_partial_match() -> None:
p = PrefixTree()
p.add("catalog")
l: list[int] = []
p.find_all("cat", 0, l)
assert l == [] # "catalog" is not a prefix of "cat"
def test_overlapping_strings() -> None:
p = PrefixTree()
p.add("a")
p.add("ab")
p.add("abc")
l: list[int] = []
p.find_all("abcdef", 0, l)
assert l == [1, 2, 3]
def test_different_offsets() -> None:
p = PrefixTree()
p.add("test")
l: list[int] = []
p.find_all("xxtest", 2, l)
assert l == [6] # "test" found starting at offset 2, ends at 6
def test_multiple_words() -> None:
p = PrefixTree()
words = ["the", "then", "there", "answer", "any", "by", "bye", "their"]
for word in words:
p.add(word)
l: list[int] = []
p.find_all("their", 0, l)
# "the", "their" are prefixes of "their"
assert 3 in l
assert 5 in l
def test_common_prefix() -> None:
p = PrefixTree()
p.add("pre")
p.add("prefix")
p.add("prepare")
l: list[int] = []
p.find_all("prefix", 0, l)
assert l == [3, 6] # "pre" and "prefix"
def test_max_len() -> None:
p = PrefixTree()
assert p.max_len() == 0
p.add("a")
assert p.max_len() == 1
p.add("abc")
assert p.max_len() == 3
p.add("ab")
assert p.max_len() == 3
def test_duplicate_add() -> None:
p = PrefixTree()
p.add("test")
p.add("test") # Add same string again
l: list[int] = []
p.find_all("test", 0, l)
# Should still work correctly
assert 4 in l
def main() -> None:
test_empty_tree()
test_single_string()
test_empty_string()
test_no_match()
test_partial_match()
test_overlapping_strings()
test_different_offsets()
test_multiple_words()
test_common_prefix()
test_max_len()
test_duplicate_add()
test_main()
if __name__ == "__main__":
main()