data-structure-typed 2.0.1 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/package.json +1 -1
- package/src/data-structures/queue/queue.ts +1 -1
- package/test/unit/data-structures/queue/queue.test.ts +1 -1
- package/test/unit/utils/utils.test.ts +3 -2
- package/dist/individuals/binary-tree/avl-tree-counter.mjs +0 -4701
- package/dist/individuals/binary-tree/avl-tree-multi-map.mjs +0 -4514
- package/dist/individuals/binary-tree/avl-tree.mjs +0 -4321
- package/dist/individuals/binary-tree/binary-tree.mjs +0 -3097
- package/dist/individuals/binary-tree/bst.mjs +0 -3858
- package/dist/individuals/binary-tree/red-black-tree.mjs +0 -4391
- package/dist/individuals/binary-tree/tree-counter.mjs +0 -4806
- package/dist/individuals/binary-tree/tree-multi-map.mjs +0 -4582
- package/dist/individuals/graph/directed-graph.mjs +0 -2910
- package/dist/individuals/graph/undirected-graph.mjs +0 -2745
- package/dist/individuals/hash/hash-map.mjs +0 -1040
- package/dist/individuals/heap/heap.mjs +0 -909
- package/dist/individuals/heap/max-heap.mjs +0 -671
- package/dist/individuals/heap/min-heap.mjs +0 -659
- package/dist/individuals/linked-list/doubly-linked-list.mjs +0 -1495
- package/dist/individuals/linked-list/singly-linked-list.mjs +0 -1479
- package/dist/individuals/priority-queue/max-priority-queue.mjs +0 -768
- package/dist/individuals/priority-queue/min-priority-queue.mjs +0 -757
- package/dist/individuals/priority-queue/priority-queue.mjs +0 -670
- package/dist/individuals/queue/deque.mjs +0 -1262
- package/dist/individuals/queue/queue.mjs +0 -1865
- package/dist/individuals/stack/stack.mjs +0 -415
- package/dist/individuals/trie/trie.mjs +0 -687
|
@@ -1,687 +0,0 @@
|
|
|
1
|
-
// src/data-structures/base/iterable-element-base.ts
|
|
2
|
-
var IterableElementBase = class {
|
|
3
|
-
/**
|
|
4
|
-
* The protected constructor initializes the options for the IterableElementBase class, including the
|
|
5
|
-
* toElementFn function.
|
|
6
|
-
* @param [options] - An optional object that contains the following properties:
|
|
7
|
-
*/
|
|
8
|
-
constructor(options) {
|
|
9
|
-
if (options) {
|
|
10
|
-
const { toElementFn } = options;
|
|
11
|
-
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
12
|
-
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
_toElementFn;
|
|
16
|
-
get toElementFn() {
|
|
17
|
-
return this._toElementFn;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Time Complexity: O(n)
|
|
21
|
-
* Space Complexity: O(1)
|
|
22
|
-
*
|
|
23
|
-
* The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
|
|
24
|
-
* @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
|
|
25
|
-
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
26
|
-
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
27
|
-
*/
|
|
28
|
-
*[Symbol.iterator](...args) {
|
|
29
|
-
yield* this._getIterator(...args);
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Time Complexity: O(n)
|
|
33
|
-
* Space Complexity: O(n)
|
|
34
|
-
*
|
|
35
|
-
* The function returns an iterator that yields all the values in the object.
|
|
36
|
-
*/
|
|
37
|
-
*values() {
|
|
38
|
-
for (const item of this) {
|
|
39
|
-
yield item;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Time Complexity: O(n)
|
|
44
|
-
* Space Complexity: O(1)
|
|
45
|
-
*
|
|
46
|
-
* The `every` function checks if every element in the array satisfies a given predicate.
|
|
47
|
-
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
48
|
-
* the current element being processed, its index, and the array it belongs to. It should return a
|
|
49
|
-
* boolean value indicating whether the element satisfies a certain condition or not.
|
|
50
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
51
|
-
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
52
|
-
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
53
|
-
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
54
|
-
* the array satisfies the provided predicate function, and `false` otherwise.
|
|
55
|
-
*/
|
|
56
|
-
every(predicate, thisArg) {
|
|
57
|
-
let index = 0;
|
|
58
|
-
for (const item of this) {
|
|
59
|
-
if (!predicate.call(thisArg, item, index++, this)) {
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return true;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Time Complexity: O(n)
|
|
67
|
-
* Space Complexity: O(1)
|
|
68
|
-
*
|
|
69
|
-
* The "some" function checks if at least one element in a collection satisfies a given predicate.
|
|
70
|
-
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
71
|
-
* `value`, `index`, and `array`. It should return a boolean value indicating whether the current
|
|
72
|
-
* element satisfies the condition.
|
|
73
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
74
|
-
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
75
|
-
* it will be passed as the `this` value to the `predicate` function. If `thisArg
|
|
76
|
-
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
|
77
|
-
* in the collection, and false otherwise.
|
|
78
|
-
*/
|
|
79
|
-
some(predicate, thisArg) {
|
|
80
|
-
let index = 0;
|
|
81
|
-
for (const item of this) {
|
|
82
|
-
if (predicate.call(thisArg, item, index++, this)) {
|
|
83
|
-
return true;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Time Complexity: O(n)
|
|
90
|
-
* Space Complexity: O(1)
|
|
91
|
-
*
|
|
92
|
-
* The `forEach` function iterates over each element in an array-like object and calls a callback
|
|
93
|
-
* function for each element.
|
|
94
|
-
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
95
|
-
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
96
|
-
* element, and the array that forEach was called upon.
|
|
97
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
98
|
-
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
99
|
-
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
100
|
-
*/
|
|
101
|
-
forEach(callbackfn, thisArg) {
|
|
102
|
-
let index = 0;
|
|
103
|
-
for (const item of this) {
|
|
104
|
-
callbackfn.call(thisArg, item, index++, this);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Time Complexity: O(n)
|
|
109
|
-
* Space Complexity: O(1)
|
|
110
|
-
*
|
|
111
|
-
* The `find` function iterates over the elements of an array-like object and returns the first
|
|
112
|
-
* element that satisfies the provided callback function.
|
|
113
|
-
* @param predicate - The predicate parameter is a function that will be called for each element in
|
|
114
|
-
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
115
|
-
* element, and the array itself. The function should return a boolean value indicating whether the
|
|
116
|
-
* current element matches the desired condition.
|
|
117
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
118
|
-
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
119
|
-
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
120
|
-
* @returns The `find` method returns the first element in the array that satisfies the provided
|
|
121
|
-
* callback function. If no element satisfies the callback function, `undefined` is returned.
|
|
122
|
-
*/
|
|
123
|
-
find(predicate, thisArg) {
|
|
124
|
-
let index = 0;
|
|
125
|
-
for (const item of this) {
|
|
126
|
-
if (predicate.call(thisArg, item, index++, this)) return item;
|
|
127
|
-
}
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Time Complexity: O(n)
|
|
132
|
-
* Space Complexity: O(1)
|
|
133
|
-
*
|
|
134
|
-
* The function checks if a given element exists in a collection.
|
|
135
|
-
* @param {E} element - The parameter "element" is of type E, which means it can be any type. It
|
|
136
|
-
* represents the element that we want to check for existence in the collection.
|
|
137
|
-
* @returns a boolean value. It returns true if the element is found in the collection, and false
|
|
138
|
-
* otherwise.
|
|
139
|
-
*/
|
|
140
|
-
has(element) {
|
|
141
|
-
for (const ele of this) {
|
|
142
|
-
if (ele === element) return true;
|
|
143
|
-
}
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Time Complexity: O(n)
|
|
148
|
-
* Space Complexity: O(1)
|
|
149
|
-
*
|
|
150
|
-
* The `reduce` function iterates over the elements of an array-like object and applies a callback
|
|
151
|
-
* function to reduce them into a single value.
|
|
152
|
-
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
153
|
-
* the array. It takes four arguments:
|
|
154
|
-
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
155
|
-
* is the value that the accumulator starts with before the reduction operation begins.
|
|
156
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
157
|
-
* all the elements in the array and applying the callback function to each element.
|
|
158
|
-
*/
|
|
159
|
-
reduce(callbackfn, initialValue) {
|
|
160
|
-
let accumulator = initialValue ?? 0;
|
|
161
|
-
let index = 0;
|
|
162
|
-
for (const item of this) {
|
|
163
|
-
accumulator = callbackfn(accumulator, item, index++, this);
|
|
164
|
-
}
|
|
165
|
-
return accumulator;
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Time Complexity: O(n)
|
|
169
|
-
* Space Complexity: O(n)
|
|
170
|
-
*
|
|
171
|
-
* The `toArray` function converts a linked list into an array.
|
|
172
|
-
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
173
|
-
*/
|
|
174
|
-
toArray() {
|
|
175
|
-
return [...this];
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Time Complexity: O(n)
|
|
179
|
-
* Space Complexity: O(n)
|
|
180
|
-
*
|
|
181
|
-
* The print function logs the elements of an array to the console.
|
|
182
|
-
*/
|
|
183
|
-
toVisual() {
|
|
184
|
-
return [...this];
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Time Complexity: O(n)
|
|
188
|
-
* Space Complexity: O(n)
|
|
189
|
-
*
|
|
190
|
-
* The print function logs the elements of an array to the console.
|
|
191
|
-
*/
|
|
192
|
-
print() {
|
|
193
|
-
console.log(this.toVisual());
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
// src/data-structures/trie/trie.ts
|
|
198
|
-
var TrieNode = class {
|
|
199
|
-
constructor(key) {
|
|
200
|
-
this._key = key;
|
|
201
|
-
this._isEnd = false;
|
|
202
|
-
this._children = /* @__PURE__ */ new Map();
|
|
203
|
-
}
|
|
204
|
-
_key;
|
|
205
|
-
/**
|
|
206
|
-
* The function returns the value of the protected variable _key.
|
|
207
|
-
* @returns The value of the `_key` property, which is a string.
|
|
208
|
-
*/
|
|
209
|
-
get key() {
|
|
210
|
-
return this._key;
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* The above function sets the value of a protected variable called "key".
|
|
214
|
-
* @param {string} value - The value parameter is a string that represents the value to be assigned
|
|
215
|
-
* to the key.
|
|
216
|
-
*/
|
|
217
|
-
set key(value) {
|
|
218
|
-
this._key = value;
|
|
219
|
-
}
|
|
220
|
-
_children;
|
|
221
|
-
/**
|
|
222
|
-
* The function returns the children of a TrieNode as a Map.
|
|
223
|
-
* @returns The `children` property of the TrieNode object, which is a Map containing string keys and
|
|
224
|
-
* TrieNode values.
|
|
225
|
-
*/
|
|
226
|
-
get children() {
|
|
227
|
-
return this._children;
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* The function sets the value of the `_children` property of a TrieNode object.
|
|
231
|
-
* @param value - The value parameter is a Map object that represents the children of a TrieNode. The
|
|
232
|
-
* keys of the map are strings, which represent the characters that are associated with each child
|
|
233
|
-
* TrieNode. The values of the map are TrieNode objects, which represent the child nodes of the
|
|
234
|
-
* current TrieNode.
|
|
235
|
-
*/
|
|
236
|
-
set children(value) {
|
|
237
|
-
this._children = value;
|
|
238
|
-
}
|
|
239
|
-
_isEnd;
|
|
240
|
-
/**
|
|
241
|
-
* The function returns a boolean value indicating whether a certain condition is met.
|
|
242
|
-
* @returns The method is returning a boolean value, specifically the value of the variable `_isEnd`.
|
|
243
|
-
*/
|
|
244
|
-
get isEnd() {
|
|
245
|
-
return this._isEnd;
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* The function sets the value of the "_isEnd" property.
|
|
249
|
-
* @param {boolean} value - The value parameter is a boolean value that indicates whether the current
|
|
250
|
-
* state is the end state or not.
|
|
251
|
-
*/
|
|
252
|
-
set isEnd(value) {
|
|
253
|
-
this._isEnd = value;
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
var Trie = class _Trie extends IterableElementBase {
|
|
257
|
-
/**
|
|
258
|
-
* The constructor initializes a Trie data structure with optional options and words provided as
|
|
259
|
-
* input.
|
|
260
|
-
* @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the constructor is an
|
|
261
|
-
* iterable containing either strings or elements of type `R`. It is used to initialize the Trie with
|
|
262
|
-
* a list of words or elements. If no `words` are provided, an empty iterable is used as the default
|
|
263
|
-
* value.
|
|
264
|
-
* @param [options] - The `options` parameter in the constructor is an optional object that can
|
|
265
|
-
* contain configuration options for the Trie data structure. One of the options it can have is
|
|
266
|
-
* `caseSensitive`, which is a boolean value indicating whether the Trie should be case-sensitive or
|
|
267
|
-
* not. If `caseSensitive` is set to `
|
|
268
|
-
*/
|
|
269
|
-
constructor(words = [], options) {
|
|
270
|
-
super(options);
|
|
271
|
-
if (options) {
|
|
272
|
-
const { caseSensitive } = options;
|
|
273
|
-
if (caseSensitive !== void 0) this._caseSensitive = caseSensitive;
|
|
274
|
-
}
|
|
275
|
-
if (words) {
|
|
276
|
-
this.addMany(words);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
_size = 0;
|
|
280
|
-
/**
|
|
281
|
-
* The size function returns the size of the stack.
|
|
282
|
-
* @return The number of elements in the list
|
|
283
|
-
*/
|
|
284
|
-
get size() {
|
|
285
|
-
return this._size;
|
|
286
|
-
}
|
|
287
|
-
_caseSensitive = true;
|
|
288
|
-
/**
|
|
289
|
-
* The caseSensitive function is a getter that returns the value of the protected _caseSensitive property.
|
|
290
|
-
* @return The value of the _caseSensitive protected variable
|
|
291
|
-
*/
|
|
292
|
-
get caseSensitive() {
|
|
293
|
-
return this._caseSensitive;
|
|
294
|
-
}
|
|
295
|
-
_root = new TrieNode("");
|
|
296
|
-
/**
|
|
297
|
-
* The root function returns the root node of the tree.
|
|
298
|
-
* @return The root node
|
|
299
|
-
*/
|
|
300
|
-
get root() {
|
|
301
|
-
return this._root;
|
|
302
|
-
}
|
|
303
|
-
/**
|
|
304
|
-
* Time Complexity: O(l), where l is the length of the word being added.
|
|
305
|
-
* Space Complexity: O(l) - Each character in the word adds a TrieNode.
|
|
306
|
-
*
|
|
307
|
-
* Add a word to the Trie structure.
|
|
308
|
-
* @param {string} word - The word to add.
|
|
309
|
-
* @returns {boolean} True if the word was successfully added.
|
|
310
|
-
*/
|
|
311
|
-
add(word) {
|
|
312
|
-
word = this._caseProcess(word);
|
|
313
|
-
let cur = this.root;
|
|
314
|
-
let isNewWord = false;
|
|
315
|
-
for (const c of word) {
|
|
316
|
-
let nodeC = cur.children.get(c);
|
|
317
|
-
if (!nodeC) {
|
|
318
|
-
nodeC = new TrieNode(c);
|
|
319
|
-
cur.children.set(c, nodeC);
|
|
320
|
-
}
|
|
321
|
-
cur = nodeC;
|
|
322
|
-
}
|
|
323
|
-
if (!cur.isEnd) {
|
|
324
|
-
isNewWord = true;
|
|
325
|
-
cur.isEnd = true;
|
|
326
|
-
this._size++;
|
|
327
|
-
}
|
|
328
|
-
return isNewWord;
|
|
329
|
-
}
|
|
330
|
-
/**
|
|
331
|
-
* Time Complexity: O(n * l)
|
|
332
|
-
* Space Complexity: O(1)
|
|
333
|
-
*
|
|
334
|
-
* The `addMany` function in TypeScript takes an iterable of strings or elements of type R, converts
|
|
335
|
-
* them using a provided function if available, and adds them to a data structure while returning an
|
|
336
|
-
* array of boolean values indicating success.
|
|
337
|
-
* @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the `addMany` function is
|
|
338
|
-
* an iterable that contains either strings or elements of type `R`.
|
|
339
|
-
* @returns The `addMany` method returns an array of boolean values indicating whether each word in
|
|
340
|
-
* the input iterable was successfully added to the data structure.
|
|
341
|
-
*/
|
|
342
|
-
addMany(words) {
|
|
343
|
-
const ans = [];
|
|
344
|
-
for (const word of words) {
|
|
345
|
-
if (this.toElementFn) {
|
|
346
|
-
ans.push(this.add(this.toElementFn(word)));
|
|
347
|
-
} else {
|
|
348
|
-
ans.push(this.add(word));
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
return ans;
|
|
352
|
-
}
|
|
353
|
-
/**
|
|
354
|
-
* Time Complexity: O(l), where l is the length of the input word.
|
|
355
|
-
* Space Complexity: O(1) - Constant space.
|
|
356
|
-
*
|
|
357
|
-
* Check if the Trie contains a given word.
|
|
358
|
-
* @param {string} word - The word to check for.
|
|
359
|
-
* @returns {boolean} True if the word is present in the Trie.
|
|
360
|
-
*/
|
|
361
|
-
has(word) {
|
|
362
|
-
word = this._caseProcess(word);
|
|
363
|
-
let cur = this.root;
|
|
364
|
-
for (const c of word) {
|
|
365
|
-
const nodeC = cur.children.get(c);
|
|
366
|
-
if (!nodeC) return false;
|
|
367
|
-
cur = nodeC;
|
|
368
|
-
}
|
|
369
|
-
return cur.isEnd;
|
|
370
|
-
}
|
|
371
|
-
/**
|
|
372
|
-
* Time Complexity: O(1)
|
|
373
|
-
* Space Complexity: O(1)
|
|
374
|
-
*
|
|
375
|
-
* The isEmpty function checks if the size of the queue is 0.
|
|
376
|
-
* @return True if the size of the queue is 0
|
|
377
|
-
*/
|
|
378
|
-
isEmpty() {
|
|
379
|
-
return this._size === 0;
|
|
380
|
-
}
|
|
381
|
-
/**
|
|
382
|
-
* Time Complexity: O(1)
|
|
383
|
-
* Space Complexity: O(1)
|
|
384
|
-
*
|
|
385
|
-
* The clear function resets the size of the Trie to 0 and creates a new root TrieNode.
|
|
386
|
-
*/
|
|
387
|
-
clear() {
|
|
388
|
-
this._size = 0;
|
|
389
|
-
this._root = new TrieNode("");
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* Time Complexity: O(l), where l is the length of the word being deleted.
|
|
393
|
-
* Space Complexity: O(n) - Due to the recursive DFS approach.
|
|
394
|
-
*
|
|
395
|
-
* Remove a word from the Trie structure.
|
|
396
|
-
* @param{string} word - The word to delete.
|
|
397
|
-
* @returns {boolean} True if the word was successfully removed.
|
|
398
|
-
*/
|
|
399
|
-
delete(word) {
|
|
400
|
-
word = this._caseProcess(word);
|
|
401
|
-
let isDeleted = false;
|
|
402
|
-
const dfs = (cur, i) => {
|
|
403
|
-
const char = word[i];
|
|
404
|
-
const child = cur.children.get(char);
|
|
405
|
-
if (child) {
|
|
406
|
-
if (i === word.length - 1) {
|
|
407
|
-
if (child.isEnd) {
|
|
408
|
-
if (child.children.size > 0) {
|
|
409
|
-
child.isEnd = false;
|
|
410
|
-
} else {
|
|
411
|
-
cur.children.delete(char);
|
|
412
|
-
}
|
|
413
|
-
isDeleted = true;
|
|
414
|
-
return true;
|
|
415
|
-
}
|
|
416
|
-
return false;
|
|
417
|
-
}
|
|
418
|
-
const res = dfs(child, i + 1);
|
|
419
|
-
if (res && !cur.isEnd && child.children.size === 0) {
|
|
420
|
-
cur.children.delete(char);
|
|
421
|
-
return true;
|
|
422
|
-
}
|
|
423
|
-
return false;
|
|
424
|
-
}
|
|
425
|
-
return false;
|
|
426
|
-
};
|
|
427
|
-
dfs(this.root, 0);
|
|
428
|
-
if (isDeleted) {
|
|
429
|
-
this._size--;
|
|
430
|
-
}
|
|
431
|
-
return isDeleted;
|
|
432
|
-
}
|
|
433
|
-
/**
|
|
434
|
-
* Time Complexity: O(n)
|
|
435
|
-
* Space Complexity: O(1)
|
|
436
|
-
*
|
|
437
|
-
* The function `getHeight` calculates the height of a trie data structure starting from the root
|
|
438
|
-
* node.
|
|
439
|
-
* @returns The `getHeight` method returns the maximum depth or height of the trie tree starting from
|
|
440
|
-
* the root node. It calculates the depth using a breadth-first search (BFS) traversal of the trie
|
|
441
|
-
* tree and returns the maximum depth found.
|
|
442
|
-
*/
|
|
443
|
-
getHeight() {
|
|
444
|
-
const startNode = this.root;
|
|
445
|
-
let maxDepth = 0;
|
|
446
|
-
if (startNode) {
|
|
447
|
-
const bfs = (node, level) => {
|
|
448
|
-
if (level > maxDepth) {
|
|
449
|
-
maxDepth = level;
|
|
450
|
-
}
|
|
451
|
-
const { children } = node;
|
|
452
|
-
if (children) {
|
|
453
|
-
for (const child of children.entries()) {
|
|
454
|
-
bfs(child[1], level + 1);
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
};
|
|
458
|
-
bfs(startNode, 0);
|
|
459
|
-
}
|
|
460
|
-
return maxDepth;
|
|
461
|
-
}
|
|
462
|
-
/**
|
|
463
|
-
* Time Complexity: O(l), where l is the length of the input prefix.
|
|
464
|
-
* Space Complexity: O(1) - Constant space.
|
|
465
|
-
*
|
|
466
|
-
* Check if a given input string has an absolute prefix in the Trie, meaning it's not a complete word.
|
|
467
|
-
* @param {string} input - The input string to check.
|
|
468
|
-
* @returns {boolean} True if it's an absolute prefix in the Trie.
|
|
469
|
-
*/
|
|
470
|
-
hasPurePrefix(input) {
|
|
471
|
-
input = this._caseProcess(input);
|
|
472
|
-
let cur = this.root;
|
|
473
|
-
for (const c of input) {
|
|
474
|
-
const nodeC = cur.children.get(c);
|
|
475
|
-
if (!nodeC) return false;
|
|
476
|
-
cur = nodeC;
|
|
477
|
-
}
|
|
478
|
-
return !cur.isEnd;
|
|
479
|
-
}
|
|
480
|
-
/**
|
|
481
|
-
* Time Complexity: O(l), where l is the length of the input prefix.
|
|
482
|
-
* Space Complexity: O(1) - Constant space.
|
|
483
|
-
*
|
|
484
|
-
* Check if a given input string is a prefix of any existing word in the Trie, whether as an absolute prefix or a complete word.
|
|
485
|
-
* @param {string} input - The input string representing the prefix to check.
|
|
486
|
-
* @returns {boolean} True if it's a prefix in the Trie.
|
|
487
|
-
*/
|
|
488
|
-
hasPrefix(input) {
|
|
489
|
-
input = this._caseProcess(input);
|
|
490
|
-
let cur = this.root;
|
|
491
|
-
for (const c of input) {
|
|
492
|
-
const nodeC = cur.children.get(c);
|
|
493
|
-
if (!nodeC) return false;
|
|
494
|
-
cur = nodeC;
|
|
495
|
-
}
|
|
496
|
-
return true;
|
|
497
|
-
}
|
|
498
|
-
/**
|
|
499
|
-
* Time Complexity: O(n), where n is the total number of nodes in the trie.
|
|
500
|
-
* Space Complexity: O(l), where l is the length of the input prefix.
|
|
501
|
-
*
|
|
502
|
-
* Check if the input string is a common prefix in the Trie, meaning it's a prefix shared by all words in the Trie.
|
|
503
|
-
* @param {string} input - The input string representing the common prefix to check for.
|
|
504
|
-
* @returns {boolean} True if it's a common prefix in the Trie.
|
|
505
|
-
*/
|
|
506
|
-
hasCommonPrefix(input) {
|
|
507
|
-
input = this._caseProcess(input);
|
|
508
|
-
let commonPre = "";
|
|
509
|
-
const dfs = (cur) => {
|
|
510
|
-
commonPre += cur.key;
|
|
511
|
-
if (commonPre === input) return;
|
|
512
|
-
if (cur.isEnd) return;
|
|
513
|
-
if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
|
|
514
|
-
else return;
|
|
515
|
-
};
|
|
516
|
-
dfs(this.root);
|
|
517
|
-
return commonPre === input;
|
|
518
|
-
}
|
|
519
|
-
/**
|
|
520
|
-
* Time Complexity: O(n), where n is the total number of nodes in the trie.
|
|
521
|
-
* Space Complexity: O(l), where l is the length of the longest common prefix.
|
|
522
|
-
*
|
|
523
|
-
* Get the longest common prefix among all the words stored in the Trie.
|
|
524
|
-
* @returns {string} The longest common prefix found in the Trie.
|
|
525
|
-
*/
|
|
526
|
-
getLongestCommonPrefix() {
|
|
527
|
-
let commonPre = "";
|
|
528
|
-
const dfs = (cur) => {
|
|
529
|
-
commonPre += cur.key;
|
|
530
|
-
if (cur.isEnd) return;
|
|
531
|
-
if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
|
|
532
|
-
else return;
|
|
533
|
-
};
|
|
534
|
-
dfs(this.root);
|
|
535
|
-
return commonPre;
|
|
536
|
-
}
|
|
537
|
-
/**
|
|
538
|
-
* Time Complexity: O(w * l), where w is the number of words retrieved, and l is the average length of the words.
|
|
539
|
-
* Space Complexity: O(w * l) - The space required for the output array.
|
|
540
|
-
*
|
|
541
|
-
* The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
|
|
542
|
-
* @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
543
|
-
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
544
|
-
* @param {number} max - The max count of words will be found
|
|
545
|
-
* @param isAllWhenEmptyPrefix - If true, when the prefix provided as '', returns all the words in the trie.
|
|
546
|
-
* @returns {string[]} an array of strings.
|
|
547
|
-
*/
|
|
548
|
-
getWords(prefix = "", max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false) {
|
|
549
|
-
prefix = this._caseProcess(prefix);
|
|
550
|
-
const words = [];
|
|
551
|
-
let found = 0;
|
|
552
|
-
function dfs(node, word) {
|
|
553
|
-
for (const char of node.children.keys()) {
|
|
554
|
-
const charNode = node.children.get(char);
|
|
555
|
-
if (charNode !== void 0) {
|
|
556
|
-
dfs(charNode, word.concat(char));
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
if (node.isEnd) {
|
|
560
|
-
if (found > max - 1) return;
|
|
561
|
-
words.push(word);
|
|
562
|
-
found++;
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
let startNode = this.root;
|
|
566
|
-
if (prefix) {
|
|
567
|
-
for (const c of prefix) {
|
|
568
|
-
const nodeC = startNode.children.get(c);
|
|
569
|
-
if (nodeC) {
|
|
570
|
-
startNode = nodeC;
|
|
571
|
-
} else {
|
|
572
|
-
return [];
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
if (isAllWhenEmptyPrefix || startNode !== this.root) dfs(startNode, prefix);
|
|
577
|
-
return words;
|
|
578
|
-
}
|
|
579
|
-
/**
|
|
580
|
-
* Time Complexity: O(n)
|
|
581
|
-
* Space Complexity: O(n)
|
|
582
|
-
*
|
|
583
|
-
* The `clone` function returns a new instance of the Trie class with the same values and case
|
|
584
|
-
* sensitivity as the original Trie.
|
|
585
|
-
* @returns A new instance of the Trie class is being returned.
|
|
586
|
-
*/
|
|
587
|
-
clone() {
|
|
588
|
-
return new _Trie(this, { caseSensitive: this.caseSensitive, toElementFn: this.toElementFn });
|
|
589
|
-
}
|
|
590
|
-
/**
|
|
591
|
-
* Time Complexity: O(n)
|
|
592
|
-
* Space Complexity: O(n)
|
|
593
|
-
*
|
|
594
|
-
* The `filter` function takes a predicate function and returns a new array containing all the
|
|
595
|
-
* elements for which the predicate function returns true.
|
|
596
|
-
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
597
|
-
* `word`, `index`, and `this`. It should return a boolean value indicating whether the current
|
|
598
|
-
* element should be included in the filtered results or not.
|
|
599
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
600
|
-
* specify the value of `this` within the `predicate` function. It is used when you want to bind a
|
|
601
|
-
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
|
|
602
|
-
* @returns The `filter` method is returning an array of strings (`string[]`).
|
|
603
|
-
*/
|
|
604
|
-
filter(predicate, thisArg) {
|
|
605
|
-
const results = new _Trie([], { toElementFn: this.toElementFn, caseSensitive: this.caseSensitive });
|
|
606
|
-
let index = 0;
|
|
607
|
-
for (const word of this) {
|
|
608
|
-
if (predicate.call(thisArg, word, index, this)) {
|
|
609
|
-
results.add(word);
|
|
610
|
-
}
|
|
611
|
-
index++;
|
|
612
|
-
}
|
|
613
|
-
return results;
|
|
614
|
-
}
|
|
615
|
-
/**
|
|
616
|
-
* Time Complexity: O(n)
|
|
617
|
-
* Space Complexity: O(n)
|
|
618
|
-
*
|
|
619
|
-
* The `map` function creates a new Trie by applying a callback function to each element in the
|
|
620
|
-
* current Trie.
|
|
621
|
-
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
622
|
-
* Trie. It takes four arguments:
|
|
623
|
-
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
|
|
624
|
-
* convert the raw element (`RM`) into a string representation. This can be useful if the raw element
|
|
625
|
-
* is not already a string or if you want to customize how the element is converted into a string. If
|
|
626
|
-
* this parameter is
|
|
627
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
628
|
-
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
629
|
-
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
630
|
-
* value of
|
|
631
|
-
* @returns a new Trie object.
|
|
632
|
-
*/
|
|
633
|
-
map(callback, toElementFn, thisArg) {
|
|
634
|
-
const newTrie = new _Trie([], { toElementFn, caseSensitive: this.caseSensitive });
|
|
635
|
-
let index = 0;
|
|
636
|
-
for (const word of this) {
|
|
637
|
-
newTrie.add(callback.call(thisArg, word, index, this));
|
|
638
|
-
index++;
|
|
639
|
-
}
|
|
640
|
-
return newTrie;
|
|
641
|
-
}
|
|
642
|
-
/**
|
|
643
|
-
* Time Complexity: O(n)
|
|
644
|
-
* Space Complexity: O(n)
|
|
645
|
-
*
|
|
646
|
-
* The function `_getIterator` returns an iterable iterator that performs a depth-first search on a
|
|
647
|
-
* trie data structure and yields all the paths to the end nodes.
|
|
648
|
-
*/
|
|
649
|
-
*_getIterator() {
|
|
650
|
-
function* _dfs(node, path) {
|
|
651
|
-
if (node.isEnd) {
|
|
652
|
-
yield path;
|
|
653
|
-
}
|
|
654
|
-
for (const [char, childNode] of node.children) {
|
|
655
|
-
yield* _dfs(childNode, path + char);
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
yield* _dfs(this.root, "");
|
|
659
|
-
}
|
|
660
|
-
get _total() {
|
|
661
|
-
return this._size;
|
|
662
|
-
}
|
|
663
|
-
/**
|
|
664
|
-
* Time Complexity: O(l), where l is the length of the input string.
|
|
665
|
-
* Space Complexity: O(1) - Constant space.
|
|
666
|
-
*
|
|
667
|
-
* @param str
|
|
668
|
-
* @protected
|
|
669
|
-
*/
|
|
670
|
-
_caseProcess(str) {
|
|
671
|
-
if (!this._caseSensitive) {
|
|
672
|
-
str = str.toLowerCase();
|
|
673
|
-
}
|
|
674
|
-
return str;
|
|
675
|
-
}
|
|
676
|
-
};
|
|
677
|
-
export {
|
|
678
|
-
Trie,
|
|
679
|
-
TrieNode
|
|
680
|
-
};
|
|
681
|
-
/**
|
|
682
|
-
* data-structure-typed
|
|
683
|
-
*
|
|
684
|
-
* @author Pablo Zeng
|
|
685
|
-
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
686
|
-
* @license MIT License
|
|
687
|
-
*/
|