-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursive_tries.py
353 lines (294 loc) · 12.2 KB
/
recursive_tries.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from __future__ import annotations
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Any
# import numpy as np # to create actual arrays for fixed size tries
# import numpy.typing as npt
@dataclass
class AbstractTrie(ABC):
"""
Abstract class for trie data structures.
"""
children: Any # all implementations use a form of group of children
def is_leaf(self):
"""
Check if the node is a leaf, by checking if it has no children.
"""
return not bool(self.children) # leafs are nodes without children
def contains(self, word: str) -> bool:
"""
Performs contains query on trie. Calls `_contains`.
Each implementation of `_contains` is based on recursion.
First it checks if the first character of the word is contained in the children of the current node.
If it is, the method is called recursively on the child node with the rest of the word.
If the character is not found in the children, the method returns False.
"""
if len(word) == 0: return True # Empty word is always contained
else: return self._contains(word)
@abstractmethod
def _contains(self, word: str) -> bool:
pass
def delete(self, word: str) -> bool:
"""
Performs delete query on trie. Calls `_delete`.
Each implementation of `_delete` is based on recursion.
First it checks if the first character of the word is contained in the children of the current node.
If it is, the method is called recursively on the child node with the rest of the word.
If the deletion (of the recursive call) was successful and the child is a leaf, the child node is removed.
"""
if len(word) == 0: return True # Empty word means nothing to delete
else: return self._delete(word)
@abstractmethod
def _delete(self, word: str) -> bool:
pass
def insert(self, word: str) -> bool:
"""
Performs insert query on trie. Calls `_insert`.
Each implementation of `_insert` is based on recursion.
First it checks if the first character of the word is contained in the children of the current node.
If it is, the method is called recursively on the child node with the rest of the word.
If the character is not found in the children, a new trie is created for the rest of the word and added to the other children.
"""
if len(word) == 0: return False # Empty word means nothing to insert
else: return self._insert(word)
@abstractmethod
def _insert(self, word: str) -> bool:
pass
@staticmethod
@abstractmethod
def create_trie(words: str) -> AbstractTrie:
"""
Constructor method for trie data structures. Creates a trie from a list of words.
"""
pass
@abstractmethod
def print_trie(self):
"""
Prints the trie in a human-readable format.
"""
pass
@dataclass
class VarSizeTrie(AbstractTrie):
"""
Class for variable size trie data structures.
For each node, its character is stored as a string and the children are stored in a list.
"""
children: list[VarSizeTrie]
char: str
def _contains(self, word: str) -> bool:
"""
Since the children are stored in a list, the method iterates over `children` to find the correct one.
"""
for child in self.children:
if word[0] == child.char: return child.contains(word[1:])
return False
def _delete(self, word: str) -> bool:
"""
One has to iterate over the `children` list to find a child.
Deletion afterward is done with the default `remove` method from lists.
"""
success = False
child_to_delete = None
for child in self.children:
if word[0] == child.char:
success = child.delete(word[1:])
child_to_delete = child
break # only one child can have the correct character
if success:
if child_to_delete.is_leaf():
self.children.remove(child_to_delete)
return success
def _insert(self, word: str) -> bool:
"""
One has to iterate over the `children` list to find the correct child.
Adding new children is done with the `append` method from lists.
"""
found = False
success = True
for child in self.children:
if word[0] == child.char:
found = True
success = child.insert(word[1:])
break # only one child can have the correct character
if not found:
new_trie = self.__create_trie_for_rest(word)
self.children.append(new_trie)
return success
@staticmethod
def __create_trie_for_rest(rest_word: str) -> VarSizeTrie:
if len(rest_word) == 1:
return VarSizeTrie(char=rest_word, children=[])
else:
lower_node = VarSizeTrie.__create_trie_for_rest(rest_word[1:])
return VarSizeTrie(char=rest_word[0], children=[lower_node])
@staticmethod
def create_trie(words: list[str]) -> VarSizeTrie:
trie = VarSizeTrie(char="root", children=[])
for word in words: trie.insert(word)
return trie
def print_trie(self):
self.__recursive_print()
def __recursive_print(self, is_root=True, depth=0, is_last=False, was_last=False):
prefix = " "
for _ in range(depth-2):
prefix += "│ "
if depth > 1: prefix += "│ " if not was_last else " "
prefix += "├──" if not is_last else "└──"
if is_root:
print(self.char)
else:
char = self.char if self.char != "\0" else "$"
print(prefix + char)
for child in self.children:
child.__recursive_print(False, depth+1, id(child) == id(self.children[-1]), is_last)
@dataclass
class FixedSizeTrie(AbstractTrie):
"""
Class for fixed size trie data structures.
For each node, the children are stored in a fixed size list and the character to index mapping is stored in a list.
"""
# children: npt.NDArray[Any]
# char_to_idx: npt.NDArray[np.int_]
children: list[FixedSizeTrie]
char_to_idx: list[int]
alphabet: str
def is_leaf(self):
if self.children is None or not any(self.children): return True
return False
def _contains(self, word: str) -> bool:
"""
Instead of iterating over `children`, the method uses the character to index mapping to find the correct child.
In theory this should reduce the time complexity to O(1) for each character.
"""
child: FixedSizeTrie = self.children[self.char_to_idx[ord(word[0])]]
if child: return child.contains(word[1:])
else: return False
def _delete(self, word: str) -> bool:
"""
The character to index mapping is used to find the correct child. In theory, this is in O(1).
Deletion is done afterward by setting the corresponding position in the array to `None`.
"""
success = False
child_idx = self.char_to_idx[ord(word[0])]
child: FixedSizeTrie = self.children[child_idx]
if child:
success = child.delete(word[1:])
if success:
if child.is_leaf():
self.children[child_idx] = None
return success
def _insert(self, word: str) -> bool:
"""
The character to index mapping is used to find the correct child. In theory, this is in O(1).
A new `children` array (empty and of fixed size) is created if the child is `None`.
"""
success = True
child_idx = self.char_to_idx[ord(word[0])]
child: FixedSizeTrie = self.children[child_idx]
if child:
success = child.insert(word[1:])
else:
# new_children = np.empty(len(self.alphabet), dtype=FixedSizeTrie)
new_children = [None] * len(self.children)
if len(word) == 1:
new_child = FixedSizeTrie(char_to_idx=None, alphabet=self.alphabet, children=None)
else:
new_child = FixedSizeTrie(char_to_idx=self.char_to_idx, alphabet=self.alphabet, children=new_children)
new_child.insert(word[1:])
self.children[child_idx] = new_child
return success
@staticmethod
def create_trie(alphabet: str, words: list[str]) -> FixedSizeTrie:
highest_index = max([ord(char) for char in alphabet.strip('\x00')])
# char_to_idx = np.zeros(highest_index+1, dtype=int)
# children = np.empty(len(alphabet), dtype=FixedSizeTrie)
char_to_idx: list[int] = [None] * (highest_index+1)
children: list[FixedSizeTrie] = [None] * len(alphabet)
for i, char in enumerate(alphabet):
char_to_idx[ord(char)] = i
trie = FixedSizeTrie(char_to_idx=char_to_idx, alphabet=alphabet, children=children)
for word in words: trie.insert(word)
return trie
def print_trie(self):
self.__recursive_print()
def __recursive_print(self, name="root", is_root=True, depth=0):
prefix = " "
for _ in range(depth-2):
prefix += "│ "
if depth > 1: prefix += "│ "
prefix += "└──"
if is_root:
print(name)
else:
char = name if name != "\0" else "$"
print(prefix + char)
if self.children:
for i, child in enumerate(self.children):
if child:
child.__recursive_print(self.alphabet[i], False, depth+1)
@dataclass
class HashTrie(AbstractTrie):
"""
Class for hash trie data structures.
For each node, the children are stored in a dictionary.
"""
children: dict[str, HashTrie]
def _contains(self, word: str) -> bool:
"""
`children` being a dictionary allows for quick access to the correct child.
"""
if word[0] in self.children:
return self.children[word[0]].contains(word[1:])
return False
def _delete(self, word: str) -> bool:
"""
`children` being a dictionary allows for quick access to the correct child.
To remove a child, the default `pop` method from dictionaries is used.
"""
success = False
if word[0] in self.children:
child = self.children[word[0]]
success = child.delete(word[1:])
if success:
if child.is_leaf():
self.children.pop(word[0])
return success
def _insert(self, word: str) -> bool:
"""
`children` being a dictionary allows for quick access to the correct child.
Adding new children is done with the default `[]` notation for dictionaries.
"""
success = True
if word[0] in self.children:
success = self.children[word[0]].insert(word[1:])
else:
new_trie = self.__create_trie_for_rest(word[1:])
self.children[word[0]] = new_trie
return success
@staticmethod
def __create_trie_for_rest(rest_word: str) -> HashTrie:
if len(rest_word) == 1:
return HashTrie({rest_word: HashTrie({})})
else:
lower_node = HashTrie.__create_trie_for_rest(rest_word[1:])
return HashTrie({rest_word[0]: lower_node})
@staticmethod
def create_trie(words: list[str]) -> HashTrie:
trie = HashTrie({})
for word in words: trie.insert(word)
return trie
def print_trie(self):
self.__recursive_print()
def __recursive_print(self, char="", is_root=True, depth=0, is_last=False, was_last=False):
prefix = " "
for _ in range(depth-2):
prefix += "│ "
if depth > 1: prefix += "│ " if not was_last else " "
prefix += "├──" if not is_last else "└──"
if is_root:
print("root")
else:
char = char if char != "\0" else "$"
print(prefix + char)
for child in self.children:
self.children[child].__recursive_print(child, False, depth+1, id(child) == id(list(self.children.keys())[-1]), is_last)