data-structure-typed 1.47.4 → 1.47.6
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/benchmark/report.html +2 -2
- package/benchmark/report.json +12 -18
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +23 -15
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +66 -82
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/cjs/data-structures/binary-tree/bst.js +56 -40
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +26 -17
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +31 -22
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/cjs/data-structures/hash/hash-map.js +10 -4
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/cjs/data-structures/hash/hash-table.js +50 -5
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +25 -22
- package/dist/cjs/data-structures/heap/heap.js +101 -41
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/max-heap.js +2 -2
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/min-heap.js +2 -2
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +125 -119
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +38 -37
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +65 -60
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +50 -49
- package/dist/cjs/data-structures/queue/deque.js +81 -71
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +46 -0
- package/dist/cjs/data-structures/queue/queue.js +80 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +20 -6
- package/dist/cjs/data-structures/stack/stack.js +65 -8
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +5 -0
- package/dist/cjs/data-structures/trie/trie.js +47 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -1
- package/dist/cjs/types/common.d.ts +2 -0
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +26 -16
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +66 -83
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/mjs/data-structures/binary-tree/bst.js +59 -41
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +30 -19
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +34 -23
- package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/mjs/data-structures/hash/hash-map.js +10 -4
- package/dist/mjs/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/mjs/data-structures/hash/hash-table.js +50 -5
- package/dist/mjs/data-structures/heap/heap.d.ts +25 -22
- package/dist/mjs/data-structures/heap/heap.js +102 -42
- package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/max-heap.js +2 -2
- package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/min-heap.js +2 -2
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +125 -119
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +38 -37
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +65 -60
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/mjs/data-structures/queue/deque.d.ts +50 -49
- package/dist/mjs/data-structures/queue/deque.js +81 -71
- package/dist/mjs/data-structures/queue/queue.d.ts +46 -0
- package/dist/mjs/data-structures/queue/queue.js +80 -0
- package/dist/mjs/data-structures/stack/stack.d.ts +20 -6
- package/dist/mjs/data-structures/stack/stack.js +65 -8
- package/dist/mjs/data-structures/trie/trie.d.ts +5 -0
- package/dist/mjs/data-structures/trie/trie.js +47 -0
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -1
- package/dist/mjs/types/common.d.ts +2 -0
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/umd/data-structure-typed.js +856 -509
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +27 -17
- package/src/data-structures/binary-tree/binary-tree.ts +114 -97
- package/src/data-structures/binary-tree/bst.ts +67 -47
- package/src/data-structures/binary-tree/rb-tree.ts +34 -20
- package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +13 -7
- package/src/data-structures/hash/hash-table.ts +59 -9
- package/src/data-structures/heap/heap.ts +115 -46
- package/src/data-structures/heap/max-heap.ts +5 -5
- package/src/data-structures/heap/min-heap.ts +5 -5
- package/src/data-structures/linked-list/doubly-linked-list.ts +137 -128
- package/src/data-structures/linked-list/singly-linked-list.ts +72 -64
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +86 -75
- package/src/data-structures/queue/queue.ts +88 -0
- package/src/data-structures/stack/stack.ts +75 -10
- package/src/data-structures/trie/trie.ts +53 -0
- package/src/interfaces/binary-tree.ts +13 -1
- package/src/types/common.ts +5 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
- package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
- package/test/performance/data-structures/heap/heap.test.ts +14 -5
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +1 -1
- package/test/unit/data-structures/hash/hash-table.test.ts +58 -2
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +49 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
- package/test/unit/data-structures/queue/queue.test.ts +37 -0
- package/test/unit/data-structures/stack/stack.test.ts +55 -5
- package/test/unit/data-structures/trie/trie.test.ts +33 -0
- package/test/unit/unrestricted-interconversion.test.ts +100 -0
|
@@ -205,5 +205,51 @@ export declare class Queue<E = any> {
|
|
|
205
205
|
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
206
206
|
*/
|
|
207
207
|
clone(): Queue<E>;
|
|
208
|
+
print(): void;
|
|
208
209
|
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
210
|
+
/**
|
|
211
|
+
* Time Complexity: O(n)
|
|
212
|
+
* Space Complexity: O(1)
|
|
213
|
+
*/
|
|
214
|
+
/**
|
|
215
|
+
* Time Complexity: O(n)
|
|
216
|
+
* Space Complexity: O(1)
|
|
217
|
+
*
|
|
218
|
+
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
219
|
+
* each element.
|
|
220
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
221
|
+
* deque. It takes three parameters:
|
|
222
|
+
*/
|
|
223
|
+
forEach(callback: (element: E, index: number, queue: this) => void): void;
|
|
224
|
+
/**
|
|
225
|
+
* Time Complexity: O(n)
|
|
226
|
+
* Space Complexity: O(n)
|
|
227
|
+
*/
|
|
228
|
+
/**
|
|
229
|
+
* Time Complexity: O(n)
|
|
230
|
+
* Space Complexity: O(n)
|
|
231
|
+
*
|
|
232
|
+
* The `filter` function creates a new deque containing only the elements that satisfy the given
|
|
233
|
+
* predicate function.
|
|
234
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
|
|
235
|
+
* `index`, and `deque`.
|
|
236
|
+
* @returns The `filter` method is returning a new `Queue` object that contains only the elements
|
|
237
|
+
* that satisfy the given `predicate` function.
|
|
238
|
+
*/
|
|
239
|
+
filter(predicate: (element: E, index: number, queue: this) => boolean): Queue<E>;
|
|
240
|
+
/**
|
|
241
|
+
* Time Complexity: O(n)
|
|
242
|
+
* Space Complexity: O(n)
|
|
243
|
+
*/
|
|
244
|
+
/**
|
|
245
|
+
* Time Complexity: O(n)
|
|
246
|
+
* Space Complexity: O(n)
|
|
247
|
+
*
|
|
248
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
249
|
+
* returning a new deque with the results.
|
|
250
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
251
|
+
* @returns The `map` method is returning a new `Queue` object with the transformed elements.
|
|
252
|
+
*/
|
|
253
|
+
map<T>(callback: (element: E, index: number, queue: this) => T): Queue<T>;
|
|
254
|
+
reduce<T>(callback: (accumulator: T, element: E, index: number, queue: this) => T, initialValue: T): T;
|
|
209
255
|
}
|
|
@@ -262,9 +262,89 @@ export class Queue {
|
|
|
262
262
|
clone() {
|
|
263
263
|
return new Queue(this.nodes.slice(this.offset));
|
|
264
264
|
}
|
|
265
|
+
print() {
|
|
266
|
+
console.log([...this]);
|
|
267
|
+
}
|
|
265
268
|
*[Symbol.iterator]() {
|
|
266
269
|
for (const item of this.nodes) {
|
|
267
270
|
yield item;
|
|
268
271
|
}
|
|
269
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Time Complexity: O(n)
|
|
275
|
+
* Space Complexity: O(1)
|
|
276
|
+
*/
|
|
277
|
+
/**
|
|
278
|
+
* Time Complexity: O(n)
|
|
279
|
+
* Space Complexity: O(1)
|
|
280
|
+
*
|
|
281
|
+
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
282
|
+
* each element.
|
|
283
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
284
|
+
* deque. It takes three parameters:
|
|
285
|
+
*/
|
|
286
|
+
forEach(callback) {
|
|
287
|
+
let index = 0;
|
|
288
|
+
for (const el of this) {
|
|
289
|
+
callback(el, index, this);
|
|
290
|
+
index++;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Time Complexity: O(n)
|
|
295
|
+
* Space Complexity: O(n)
|
|
296
|
+
*/
|
|
297
|
+
/**
|
|
298
|
+
* Time Complexity: O(n)
|
|
299
|
+
* Space Complexity: O(n)
|
|
300
|
+
*
|
|
301
|
+
* The `filter` function creates a new deque containing only the elements that satisfy the given
|
|
302
|
+
* predicate function.
|
|
303
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
|
|
304
|
+
* `index`, and `deque`.
|
|
305
|
+
* @returns The `filter` method is returning a new `Queue` object that contains only the elements
|
|
306
|
+
* that satisfy the given `predicate` function.
|
|
307
|
+
*/
|
|
308
|
+
filter(predicate) {
|
|
309
|
+
const newDeque = new Queue([]);
|
|
310
|
+
let index = 0;
|
|
311
|
+
for (const el of this) {
|
|
312
|
+
if (predicate(el, index, this)) {
|
|
313
|
+
newDeque.push(el);
|
|
314
|
+
}
|
|
315
|
+
index++;
|
|
316
|
+
}
|
|
317
|
+
return newDeque;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Time Complexity: O(n)
|
|
321
|
+
* Space Complexity: O(n)
|
|
322
|
+
*/
|
|
323
|
+
/**
|
|
324
|
+
* Time Complexity: O(n)
|
|
325
|
+
* Space Complexity: O(n)
|
|
326
|
+
*
|
|
327
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
328
|
+
* returning a new deque with the results.
|
|
329
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
330
|
+
* @returns The `map` method is returning a new `Queue` object with the transformed elements.
|
|
331
|
+
*/
|
|
332
|
+
map(callback) {
|
|
333
|
+
const newDeque = new Queue([]);
|
|
334
|
+
let index = 0;
|
|
335
|
+
for (const el of this) {
|
|
336
|
+
newDeque.push(callback(el, index, this));
|
|
337
|
+
index++;
|
|
338
|
+
}
|
|
339
|
+
return newDeque;
|
|
340
|
+
}
|
|
341
|
+
reduce(callback, initialValue) {
|
|
342
|
+
let accumulator = initialValue;
|
|
343
|
+
let index = 0;
|
|
344
|
+
for (const el of this) {
|
|
345
|
+
accumulator = callback(accumulator, el, index, this);
|
|
346
|
+
index++;
|
|
347
|
+
}
|
|
348
|
+
return accumulator;
|
|
349
|
+
}
|
|
270
350
|
}
|
|
@@ -10,13 +10,18 @@ export declare class Stack<E = any> {
|
|
|
10
10
|
* of elements of type `E`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
|
|
11
11
|
* is provided and is an array, it is assigned to the `_elements
|
|
12
12
|
*/
|
|
13
|
-
constructor(elements?: E
|
|
13
|
+
constructor(elements?: Iterable<E>);
|
|
14
14
|
protected _elements: E[];
|
|
15
15
|
get elements(): E[];
|
|
16
16
|
/**
|
|
17
17
|
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
|
|
18
18
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
19
19
|
*/
|
|
20
|
+
/**
|
|
21
|
+
* The size() function returns the number of elements in an array.
|
|
22
|
+
* @returns The size of the elements array.
|
|
23
|
+
*/
|
|
24
|
+
get size(): number;
|
|
20
25
|
/**
|
|
21
26
|
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
|
|
22
27
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
@@ -32,11 +37,6 @@ export declare class Stack<E = any> {
|
|
|
32
37
|
* @returns A boolean value indicating whether the `_elements` array is empty or not.
|
|
33
38
|
*/
|
|
34
39
|
isEmpty(): boolean;
|
|
35
|
-
/**
|
|
36
|
-
* The size() function returns the number of elements in an array.
|
|
37
|
-
* @returns The size of the elements array.
|
|
38
|
-
*/
|
|
39
|
-
size(): number;
|
|
40
40
|
/**
|
|
41
41
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
42
42
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
@@ -103,4 +103,18 @@ export declare class Stack<E = any> {
|
|
|
103
103
|
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
104
104
|
*/
|
|
105
105
|
clone(): Stack<E>;
|
|
106
|
+
/**
|
|
107
|
+
* Custom iterator for the Stack class.
|
|
108
|
+
* @returns An iterator object.
|
|
109
|
+
*/
|
|
110
|
+
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
111
|
+
/**
|
|
112
|
+
* Applies a function to each element of the stack.
|
|
113
|
+
* @param {function(E): void} callback - A function to apply to each element.
|
|
114
|
+
*/
|
|
115
|
+
forEach(callback: (element: E, index: number, stack: this) => void): void;
|
|
116
|
+
filter(predicate: (element: E, index: number, stack: this) => boolean): Stack<E>;
|
|
117
|
+
map<T>(callback: (element: E, index: number, stack: this) => T): Stack<T>;
|
|
118
|
+
reduce<T>(callback: (accumulator: T, element: E, index: number, stack: this) => T, initialValue: T): T;
|
|
119
|
+
print(): void;
|
|
106
120
|
}
|
|
@@ -11,7 +11,12 @@ export class Stack {
|
|
|
11
11
|
* is provided and is an array, it is assigned to the `_elements
|
|
12
12
|
*/
|
|
13
13
|
constructor(elements) {
|
|
14
|
-
this._elements =
|
|
14
|
+
this._elements = [];
|
|
15
|
+
if (elements) {
|
|
16
|
+
for (const el of elements) {
|
|
17
|
+
this.push(el);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
15
20
|
}
|
|
16
21
|
_elements;
|
|
17
22
|
get elements() {
|
|
@@ -21,6 +26,13 @@ export class Stack {
|
|
|
21
26
|
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
|
|
22
27
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
23
28
|
*/
|
|
29
|
+
/**
|
|
30
|
+
* The size() function returns the number of elements in an array.
|
|
31
|
+
* @returns The size of the elements array.
|
|
32
|
+
*/
|
|
33
|
+
get size() {
|
|
34
|
+
return this.elements.length;
|
|
35
|
+
}
|
|
24
36
|
/**
|
|
25
37
|
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
|
|
26
38
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
@@ -40,13 +52,6 @@ export class Stack {
|
|
|
40
52
|
isEmpty() {
|
|
41
53
|
return this.elements.length === 0;
|
|
42
54
|
}
|
|
43
|
-
/**
|
|
44
|
-
* The size() function returns the number of elements in an array.
|
|
45
|
-
* @returns The size of the elements array.
|
|
46
|
-
*/
|
|
47
|
-
size() {
|
|
48
|
-
return this.elements.length;
|
|
49
|
-
}
|
|
50
55
|
/**
|
|
51
56
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
52
57
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
@@ -130,4 +135,56 @@ export class Stack {
|
|
|
130
135
|
clone() {
|
|
131
136
|
return new Stack(this.elements.slice());
|
|
132
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* Custom iterator for the Stack class.
|
|
140
|
+
* @returns An iterator object.
|
|
141
|
+
*/
|
|
142
|
+
*[Symbol.iterator]() {
|
|
143
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
144
|
+
yield this.elements[i];
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Applies a function to each element of the stack.
|
|
149
|
+
* @param {function(E): void} callback - A function to apply to each element.
|
|
150
|
+
*/
|
|
151
|
+
forEach(callback) {
|
|
152
|
+
let index = 0;
|
|
153
|
+
for (const el of this) {
|
|
154
|
+
callback(el, index, this);
|
|
155
|
+
index++;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
filter(predicate) {
|
|
159
|
+
const newStack = new Stack();
|
|
160
|
+
let index = 0;
|
|
161
|
+
for (const el of this) {
|
|
162
|
+
if (predicate(el, index, this)) {
|
|
163
|
+
newStack.push(el);
|
|
164
|
+
}
|
|
165
|
+
index++;
|
|
166
|
+
}
|
|
167
|
+
return newStack;
|
|
168
|
+
}
|
|
169
|
+
map(callback) {
|
|
170
|
+
const newStack = new Stack();
|
|
171
|
+
let index = 0;
|
|
172
|
+
for (const el of this) {
|
|
173
|
+
newStack.push(callback(el, index, this));
|
|
174
|
+
index++;
|
|
175
|
+
}
|
|
176
|
+
return newStack;
|
|
177
|
+
}
|
|
178
|
+
reduce(callback, initialValue) {
|
|
179
|
+
let accumulator = initialValue;
|
|
180
|
+
let index = 0;
|
|
181
|
+
for (const el of this) {
|
|
182
|
+
accumulator = callback(accumulator, el, index, this);
|
|
183
|
+
index++;
|
|
184
|
+
}
|
|
185
|
+
return accumulator;
|
|
186
|
+
}
|
|
187
|
+
print() {
|
|
188
|
+
console.log([...this]);
|
|
189
|
+
}
|
|
133
190
|
}
|
|
@@ -140,6 +140,11 @@ export declare class Trie {
|
|
|
140
140
|
* @returns {string[]} an array of strings.
|
|
141
141
|
*/
|
|
142
142
|
getWords(prefix?: string, max?: number, isAllWhenEmptyPrefix?: boolean): string[];
|
|
143
|
+
[Symbol.iterator](): IterableIterator<string>;
|
|
144
|
+
forEach(callback: (word: string, index: number, trie: this) => void): void;
|
|
145
|
+
filter(predicate: (word: string, index: number, trie: this) => boolean): string[];
|
|
146
|
+
map(callback: (word: string, index: number, trie: this) => string): Trie;
|
|
147
|
+
reduce<T>(callback: (accumulator: T, word: string, index: number, trie: this) => T, initialValue: T): T;
|
|
143
148
|
/**
|
|
144
149
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
145
150
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -306,6 +306,53 @@ export class Trie {
|
|
|
306
306
|
dfs(startNode, prefix);
|
|
307
307
|
return words;
|
|
308
308
|
}
|
|
309
|
+
*[Symbol.iterator]() {
|
|
310
|
+
function* _dfs(node, path) {
|
|
311
|
+
if (node.isEnd) {
|
|
312
|
+
yield path;
|
|
313
|
+
}
|
|
314
|
+
for (const [char, childNode] of node.children) {
|
|
315
|
+
yield* _dfs(childNode, path + char);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
yield* _dfs(this.root, '');
|
|
319
|
+
}
|
|
320
|
+
forEach(callback) {
|
|
321
|
+
let index = 0;
|
|
322
|
+
for (const word of this) {
|
|
323
|
+
callback(word, index, this);
|
|
324
|
+
index++;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
filter(predicate) {
|
|
328
|
+
const results = [];
|
|
329
|
+
let index = 0;
|
|
330
|
+
for (const word of this) {
|
|
331
|
+
if (predicate(word, index, this)) {
|
|
332
|
+
results.push(word);
|
|
333
|
+
}
|
|
334
|
+
index++;
|
|
335
|
+
}
|
|
336
|
+
return results;
|
|
337
|
+
}
|
|
338
|
+
map(callback) {
|
|
339
|
+
const newTrie = new Trie();
|
|
340
|
+
let index = 0;
|
|
341
|
+
for (const word of this) {
|
|
342
|
+
newTrie.add(callback(word, index, this));
|
|
343
|
+
index++;
|
|
344
|
+
}
|
|
345
|
+
return newTrie;
|
|
346
|
+
}
|
|
347
|
+
reduce(callback, initialValue) {
|
|
348
|
+
let accumulator = initialValue;
|
|
349
|
+
let index = 0;
|
|
350
|
+
for (const word of this) {
|
|
351
|
+
accumulator = callback(accumulator, word, index, this);
|
|
352
|
+
index++;
|
|
353
|
+
}
|
|
354
|
+
return accumulator;
|
|
355
|
+
}
|
|
309
356
|
/**
|
|
310
357
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
311
358
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeNested, BinaryTreeNodeNested, BiTreeDeleteResult, BTNCallback, BTNKey } from '../types';
|
|
2
|
+
import { BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BiTreeDeleteResult, BTNCallback, BTNKey, IterableEntriesOrKeys } from '../types';
|
|
3
3
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>, TREE extends BinaryTree<V, N, TREE> = BinaryTreeNested<V, N>> {
|
|
4
4
|
createNode(key: BTNKey, value?: N['value']): N;
|
|
5
|
+
createTree(options?: Partial<BinaryTreeOptions>): TREE;
|
|
6
|
+
init(elements: IterableEntriesOrKeys<V>): void;
|
|
5
7
|
add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
|
|
6
8
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
|
|
7
9
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { BTNKey } from "./data-structures";
|
|
1
2
|
export type Comparator<T> = (a: T, b: T) => number;
|
|
2
3
|
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
3
4
|
export type BTNCallback<N, D = any> = (node: N) => D;
|
|
@@ -18,3 +19,4 @@ export type BinaryTreePrintOptions = {
|
|
|
18
19
|
isShowNull?: boolean;
|
|
19
20
|
isShowRedBlackNIL?: boolean;
|
|
20
21
|
};
|
|
22
|
+
export type IterableEntriesOrKeys<T> = Iterable<[BTNKey, T | undefined] | BTNKey>;
|
|
@@ -26,6 +26,6 @@ export type BiTreeDeleteResult<N> = {
|
|
|
26
26
|
export type BinaryTreeNodeNested<T> = BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
27
27
|
export type BinaryTreeNested<T, N extends BinaryTreeNode<T, N>> = BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
28
28
|
export type BinaryTreeOptions = {
|
|
29
|
-
iterationType
|
|
29
|
+
iterationType: IterationType;
|
|
30
30
|
};
|
|
31
31
|
export type NodeDisplayLayout = [string[], number, number, number];
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { BST, BSTNode } from '../../../data-structures';
|
|
2
2
|
import type { BinaryTreeOptions, BTNKey } from './binary-tree';
|
|
3
|
-
|
|
3
|
+
import { Comparator } from "../../common";
|
|
4
4
|
export type BSTNodeNested<T> = BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
5
5
|
export type BSTNested<T, N extends BSTNode<T, N>> = BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
6
6
|
export type BSTOptions = BinaryTreeOptions & {
|
|
7
|
-
comparator
|
|
7
|
+
comparator: Comparator<BTNKey>;
|
|
8
8
|
};
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { HeapOptions } from "../heap";
|
|
2
|
+
export type PriorityQueueOptions<T> = HeapOptions<T> & {};
|