heap-typed 1.48.1 → 1.48.2

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.
Files changed (49) hide show
  1. package/dist/data-structures/base/index.d.ts +1 -0
  2. package/dist/data-structures/base/index.js +17 -0
  3. package/dist/data-structures/base/iterable-base.d.ts +232 -0
  4. package/dist/data-structures/base/iterable-base.js +312 -0
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +36 -69
  6. package/dist/data-structures/binary-tree/binary-tree.js +78 -129
  7. package/dist/data-structures/graph/abstract-graph.d.ts +44 -6
  8. package/dist/data-structures/graph/abstract-graph.js +50 -27
  9. package/dist/data-structures/hash/hash-map.d.ts +59 -100
  10. package/dist/data-structures/hash/hash-map.js +69 -173
  11. package/dist/data-structures/heap/heap.d.ts +50 -7
  12. package/dist/data-structures/heap/heap.js +60 -30
  13. package/dist/data-structures/index.d.ts +1 -0
  14. package/dist/data-structures/index.js +1 -0
  15. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  16. package/dist/data-structures/linked-list/doubly-linked-list.js +46 -73
  17. package/dist/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  18. package/dist/data-structures/linked-list/singly-linked-list.js +40 -73
  19. package/dist/data-structures/queue/deque.d.ts +29 -51
  20. package/dist/data-structures/queue/deque.js +36 -71
  21. package/dist/data-structures/queue/queue.d.ts +49 -48
  22. package/dist/data-structures/queue/queue.js +69 -82
  23. package/dist/data-structures/stack/stack.d.ts +43 -10
  24. package/dist/data-structures/stack/stack.js +50 -31
  25. package/dist/data-structures/trie/trie.d.ts +41 -6
  26. package/dist/data-structures/trie/trie.js +53 -32
  27. package/dist/types/data-structures/base/base.d.ts +5 -0
  28. package/dist/types/data-structures/base/base.js +2 -0
  29. package/dist/types/data-structures/base/index.d.ts +1 -0
  30. package/dist/types/data-structures/base/index.js +17 -0
  31. package/dist/types/data-structures/index.d.ts +1 -0
  32. package/dist/types/data-structures/index.js +1 -0
  33. package/package.json +2 -2
  34. package/src/data-structures/base/index.ts +1 -0
  35. package/src/data-structures/base/iterable-base.ts +329 -0
  36. package/src/data-structures/binary-tree/binary-tree.ts +82 -138
  37. package/src/data-structures/graph/abstract-graph.ts +55 -28
  38. package/src/data-structures/hash/hash-map.ts +76 -185
  39. package/src/data-structures/heap/heap.ts +63 -36
  40. package/src/data-structures/index.ts +1 -0
  41. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
  42. package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
  43. package/src/data-structures/queue/deque.ts +40 -82
  44. package/src/data-structures/queue/queue.ts +72 -87
  45. package/src/data-structures/stack/stack.ts +53 -34
  46. package/src/data-structures/trie/trie.ts +58 -35
  47. package/src/types/data-structures/base/base.ts +6 -0
  48. package/src/types/data-structures/base/index.ts +1 -0
  49. package/src/types/data-structures/index.ts +1 -0
@@ -1,45 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Queue = exports.LinkedListQueue = void 0;
3
+ exports.LinkedListQueue = exports.Queue = void 0;
4
4
  /**
5
5
  * @license MIT
6
6
  * @copyright Tyler Zeng <zrwusa@gmail.com>
7
7
  * @class
8
8
  */
9
9
  const linked_list_1 = require("../linked-list");
10
- class LinkedListQueue extends linked_list_1.SinglyLinkedList {
11
- /**
12
- * The enqueue function adds a value to the end of an array.
13
- * @param {E} value - The value parameter represents the value that you want to add to the queue.
14
- */
15
- enqueue(value) {
16
- this.push(value);
17
- }
18
- /**
19
- * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
20
- * @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
21
- */
22
- dequeue() {
23
- return this.shift();
24
- }
25
- /**
26
- * The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
27
- * @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
28
- */
29
- getFirst() {
30
- var _a;
31
- return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
32
- }
33
- /**
34
- * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
35
- * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
36
- */
37
- peek() {
38
- return this.getFirst();
39
- }
40
- }
41
- exports.LinkedListQueue = LinkedListQueue;
42
- class Queue {
10
+ const base_1 = require("../base");
11
+ class Queue extends base_1.IterableElementBase {
43
12
  /**
44
13
  * The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
45
14
  * @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
@@ -47,6 +16,7 @@ class Queue {
47
16
  * initialized as an empty array.
48
17
  */
49
18
  constructor(elements) {
19
+ super();
50
20
  this._nodes = elements || [];
51
21
  this._offset = 0;
52
22
  }
@@ -268,31 +238,6 @@ class Queue {
268
238
  print() {
269
239
  console.log([...this]);
270
240
  }
271
- *[Symbol.iterator]() {
272
- for (const item of this.nodes) {
273
- yield item;
274
- }
275
- }
276
- /**
277
- * Time Complexity: O(n)
278
- * Space Complexity: O(1)
279
- */
280
- /**
281
- * Time Complexity: O(n)
282
- * Space Complexity: O(1)
283
- *
284
- * The `forEach` function iterates over each element in a deque and applies a callback function to
285
- * each element.
286
- * @param callback - The callback parameter is a function that will be called for each element in the
287
- * deque. It takes three parameters:
288
- */
289
- forEach(callback) {
290
- let index = 0;
291
- for (const el of this) {
292
- callback(el, index, this);
293
- index++;
294
- }
295
- }
296
241
  /**
297
242
  * Time Complexity: O(n)
298
243
  * Space Complexity: O(n)
@@ -301,18 +246,23 @@ class Queue {
301
246
  * Time Complexity: O(n)
302
247
  * Space Complexity: O(n)
303
248
  *
304
- * The `filter` function creates a new deque containing only the elements that satisfy the given
305
- * predicate function.
306
- * @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
307
- * `index`, and `deque`.
308
- * @returns The `filter` method is returning a new `Queue` object that contains only the elements
309
- * that satisfy the given `predicate` function.
310
- */
311
- filter(predicate) {
249
+ * The `filter` function creates a new `Queue` object containing elements from the original `Queue`
250
+ * that satisfy a given predicate function.
251
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
252
+ * the current element being iterated over, the index of the current element, and the queue itself.
253
+ * It should return a boolean value indicating whether the element should be included in the filtered
254
+ * queue or not.
255
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
256
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
257
+ * passed as the `this` value to the `predicate` function. If `thisArg` is
258
+ * @returns The `filter` method is returning a new `Queue` object that contains the elements that
259
+ * satisfy the given predicate function.
260
+ */
261
+ filter(predicate, thisArg) {
312
262
  const newDeque = new Queue([]);
313
263
  let index = 0;
314
264
  for (const el of this) {
315
- if (predicate(el, index, this)) {
265
+ if (predicate.call(thisArg, el, index, this)) {
316
266
  newDeque.push(el);
317
267
  }
318
268
  index++;
@@ -327,28 +277,65 @@ class Queue {
327
277
  * Time Complexity: O(n)
328
278
  * Space Complexity: O(n)
329
279
  *
330
- * The `map` function takes a callback function and applies it to each element in the deque,
331
- * returning a new deque with the results.
332
- * @param callback - The `callback` parameter is a function that takes three arguments:
333
- * @returns The `map` method is returning a new `Queue` object with the transformed elements.
334
- */
335
- map(callback) {
280
+ * The `map` function takes a callback function and applies it to each element in the queue,
281
+ * returning a new queue with the results.
282
+ * @param callback - The callback parameter is a function that will be called for each element in the
283
+ * queue. It takes three arguments: the current element, the index of the current element, and the
284
+ * queue itself. The callback function should return a new value that will be added to the new queue.
285
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
286
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
287
+ * passed as the `this` value to the `callback` function. If `thisArg` is
288
+ * @returns The `map` function is returning a new `Queue` object with the transformed elements.
289
+ */
290
+ map(callback, thisArg) {
336
291
  const newDeque = new Queue([]);
337
292
  let index = 0;
338
293
  for (const el of this) {
339
- newDeque.push(callback(el, index, this));
294
+ newDeque.push(callback.call(thisArg, el, index, this));
340
295
  index++;
341
296
  }
342
297
  return newDeque;
343
298
  }
344
- reduce(callback, initialValue) {
345
- let accumulator = initialValue;
346
- let index = 0;
347
- for (const el of this) {
348
- accumulator = callback(accumulator, el, index, this);
349
- index++;
299
+ /**
300
+ * Time Complexity: O(n)
301
+ * Space Complexity: O(n)
302
+ */
303
+ *_getIterator() {
304
+ for (const item of this.nodes) {
305
+ yield item;
350
306
  }
351
- return accumulator;
352
307
  }
353
308
  }
354
309
  exports.Queue = Queue;
310
+ class LinkedListQueue extends linked_list_1.SinglyLinkedList {
311
+ /**
312
+ * The enqueue function adds a value to the end of an array.
313
+ * @param {E} value - The value parameter represents the value that you want to add to the queue.
314
+ */
315
+ enqueue(value) {
316
+ this.push(value);
317
+ }
318
+ /**
319
+ * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
320
+ * @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
321
+ */
322
+ dequeue() {
323
+ return this.shift();
324
+ }
325
+ /**
326
+ * The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
327
+ * @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
328
+ */
329
+ getFirst() {
330
+ var _a;
331
+ return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
332
+ }
333
+ /**
334
+ * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
335
+ * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
336
+ */
337
+ peek() {
338
+ return this.getFirst();
339
+ }
340
+ }
341
+ exports.LinkedListQueue = LinkedListQueue;
@@ -1,9 +1,11 @@
1
+ import { IterableElementBase } from "../base";
2
+ import { ElementCallback } from "../../types";
1
3
  /**
2
4
  * @license MIT
3
5
  * @copyright Tyler Zeng <zrwusa@gmail.com>
4
6
  * @class
5
7
  */
6
- export declare class Stack<E = any> {
8
+ export declare class Stack<E = any> extends IterableElementBase<E> {
7
9
  /**
8
10
  * The constructor initializes an array of elements, which can be provided as an optional parameter.
9
11
  * @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array
@@ -104,17 +106,48 @@ export declare class Stack<E = any> {
104
106
  */
105
107
  clone(): Stack<E>;
106
108
  /**
107
- * Custom iterator for the Stack class.
108
- * @returns An iterator object.
109
+ * Time Complexity: O(n)
110
+ * Space Complexity: O(n)
109
111
  */
110
- [Symbol.iterator](): Generator<E, void, unknown>;
111
112
  /**
112
- * Applies a function to each element of the stack.
113
- * @param {function(E): void} callback - A function to apply to each element.
113
+ * Time Complexity: O(n)
114
+ * Space Complexity: O(n)
115
+ *
116
+ * The `filter` function creates a new stack containing elements from the original stack that satisfy
117
+ * a given predicate function.
118
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
119
+ * the current element being iterated over, the index of the current element, and the stack itself.
120
+ * It should return a boolean value indicating whether the element should be included in the filtered
121
+ * stack or not.
122
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
123
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
124
+ * passed as the `this` value to the `predicate` function. If `thisArg` is
125
+ * @returns The `filter` method is returning a new `Stack` object that contains the elements that
126
+ * satisfy the given predicate function.
127
+ */
128
+ filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Stack<E>;
129
+ /**
130
+ * Time Complexity: O(n)
131
+ * Space Complexity: O(n)
114
132
  */
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;
133
+ /**
134
+ * Time Complexity: O(n)
135
+ * Space Complexity: O(n)
136
+ *
137
+ * The `map` function takes a callback function and applies it to each element in the stack,
138
+ * returning a new stack with the results.
139
+ * @param callback - The `callback` parameter is a function that will be called for each element in
140
+ * the stack. It takes three arguments:
141
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
142
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
143
+ * passed as the `this` value to the `callback` function. If `thisArg` is
144
+ * @returns The `map` method is returning a new `Stack` object.
145
+ */
146
+ map<T>(callback: ElementCallback<E, T>, thisArg?: any): Stack<T>;
119
147
  print(): void;
148
+ /**
149
+ * Custom iterator for the Stack class.
150
+ * @returns An iterator object.
151
+ */
152
+ protected _getIterator(): Generator<E, void, unknown>;
120
153
  }
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Stack = void 0;
4
+ const base_1 = require("../base");
4
5
  /**
5
6
  * @license MIT
6
7
  * @copyright Tyler Zeng <zrwusa@gmail.com>
7
8
  * @class
8
9
  */
9
- class Stack {
10
+ class Stack extends base_1.IterableElementBase {
10
11
  /**
11
12
  * The constructor initializes an array of elements, which can be provided as an optional parameter.
12
13
  * @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array
@@ -14,6 +15,7 @@ class Stack {
14
15
  * is provided and is an array, it is assigned to the `_elements
15
16
  */
16
17
  constructor(elements) {
18
+ super();
17
19
  this._elements = [];
18
20
  if (elements) {
19
21
  for (const el of elements) {
@@ -138,56 +140,73 @@ class Stack {
138
140
  return new Stack(this.elements.slice());
139
141
  }
140
142
  /**
141
- * Custom iterator for the Stack class.
142
- * @returns An iterator object.
143
+ * Time Complexity: O(n)
144
+ * Space Complexity: O(n)
143
145
  */
144
- *[Symbol.iterator]() {
145
- for (let i = 0; i < this.elements.length; i++) {
146
- yield this.elements[i];
147
- }
148
- }
149
146
  /**
150
- * Applies a function to each element of the stack.
151
- * @param {function(E): void} callback - A function to apply to each element.
152
- */
153
- forEach(callback) {
154
- let index = 0;
155
- for (const el of this) {
156
- callback(el, index, this);
157
- index++;
158
- }
159
- }
160
- filter(predicate) {
147
+ * Time Complexity: O(n)
148
+ * Space Complexity: O(n)
149
+ *
150
+ * The `filter` function creates a new stack containing elements from the original stack that satisfy
151
+ * a given predicate function.
152
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
153
+ * the current element being iterated over, the index of the current element, and the stack itself.
154
+ * It should return a boolean value indicating whether the element should be included in the filtered
155
+ * stack or not.
156
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
157
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
158
+ * passed as the `this` value to the `predicate` function. If `thisArg` is
159
+ * @returns The `filter` method is returning a new `Stack` object that contains the elements that
160
+ * satisfy the given predicate function.
161
+ */
162
+ filter(predicate, thisArg) {
161
163
  const newStack = new Stack();
162
164
  let index = 0;
163
165
  for (const el of this) {
164
- if (predicate(el, index, this)) {
166
+ if (predicate.call(thisArg, el, index, this)) {
165
167
  newStack.push(el);
166
168
  }
167
169
  index++;
168
170
  }
169
171
  return newStack;
170
172
  }
171
- map(callback) {
173
+ /**
174
+ * Time Complexity: O(n)
175
+ * Space Complexity: O(n)
176
+ */
177
+ /**
178
+ * Time Complexity: O(n)
179
+ * Space Complexity: O(n)
180
+ *
181
+ * The `map` function takes a callback function and applies it to each element in the stack,
182
+ * returning a new stack with the results.
183
+ * @param callback - The `callback` parameter is a function that will be called for each element in
184
+ * the stack. It takes three arguments:
185
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
186
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
187
+ * passed as the `this` value to the `callback` function. If `thisArg` is
188
+ * @returns The `map` method is returning a new `Stack` object.
189
+ */
190
+ map(callback, thisArg) {
172
191
  const newStack = new Stack();
173
192
  let index = 0;
174
193
  for (const el of this) {
175
- newStack.push(callback(el, index, this));
194
+ newStack.push(callback.call(thisArg, el, index, this));
176
195
  index++;
177
196
  }
178
197
  return newStack;
179
198
  }
180
- reduce(callback, initialValue) {
181
- let accumulator = initialValue;
182
- let index = 0;
183
- for (const el of this) {
184
- accumulator = callback(accumulator, el, index, this);
185
- index++;
186
- }
187
- return accumulator;
188
- }
189
199
  print() {
190
200
  console.log([...this]);
191
201
  }
202
+ /**
203
+ * Custom iterator for the Stack class.
204
+ * @returns An iterator object.
205
+ */
206
+ *_getIterator() {
207
+ for (let i = 0; i < this.elements.length; i++) {
208
+ yield this.elements[i];
209
+ }
210
+ }
192
211
  }
193
212
  exports.Stack = Stack;
@@ -5,6 +5,8 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
+ import { IterableElementBase } from "../base";
9
+ import { ElementCallback } from "../../types";
8
10
  /**
9
11
  * TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes,
10
12
  * and a flag indicating whether it's the end of a word.
@@ -18,7 +20,7 @@ export declare class TrieNode {
18
20
  /**
19
21
  * Trie represents a Trie data structure. It provides basic Trie operations and additional methods.
20
22
  */
21
- export declare class Trie {
23
+ export declare class Trie extends IterableElementBase<string> {
22
24
  constructor(words?: string[], caseSensitive?: boolean);
23
25
  protected _size: number;
24
26
  get size(): number;
@@ -142,12 +144,45 @@ export declare class Trie {
142
144
  * @returns {string[]} an array of strings.
143
145
  */
144
146
  getWords(prefix?: string, max?: number, isAllWhenEmptyPrefix?: boolean): string[];
145
- [Symbol.iterator](): IterableIterator<string>;
146
- forEach(callback: (word: string, index: number, trie: this) => void): void;
147
- filter(predicate: (word: string, index: number, trie: this) => boolean): string[];
148
- map(callback: (word: string, index: number, trie: this) => string): Trie;
149
- reduce<T>(callback: (accumulator: T, word: string, index: number, trie: this) => T, initialValue: T): T;
147
+ /**
148
+ * Time Complexity: O(n)
149
+ * Space Complexity: O(n)
150
+ */
151
+ /**
152
+ * Time Complexity: O(n)
153
+ * Space Complexity: O(n)
154
+ *
155
+ * The `filter` function takes a predicate function and returns a new array containing all the
156
+ * elements for which the predicate function returns true.
157
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
158
+ * `word`, `index`, and `this`. It should return a boolean value indicating whether the current
159
+ * element should be included in the filtered results or not.
160
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
161
+ * specify the value of `this` within the `predicate` function. It is used when you want to bind a
162
+ * specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
163
+ * @returns The `filter` method is returning an array of strings (`string[]`).
164
+ */
165
+ filter(predicate: ElementCallback<string, boolean>, thisArg?: any): string[];
166
+ /**
167
+ * Time Complexity: O(n)
168
+ * Space Complexity: O(n)
169
+ */
170
+ /**
171
+ * Time Complexity: O(n)
172
+ * Space Complexity: O(n)
173
+ *
174
+ * The `map` function creates a new Trie by applying a callback function to each element in the Trie.
175
+ * @param callback - The callback parameter is a function that will be called for each element in the
176
+ * Trie. It takes three arguments: the current element in the Trie, the index of the current element,
177
+ * and the Trie itself. The callback function should return a new value for the element.
178
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
179
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
180
+ * passed as the `this` value to the `callback` function. If `thisArg` is
181
+ * @returns The `map` function is returning a new Trie object.
182
+ */
183
+ map(callback: ElementCallback<string, string>, thisArg?: any): Trie;
150
184
  print(): void;
185
+ protected _getIterator(): IterableIterator<string>;
151
186
  /**
152
187
  * Time Complexity: O(M), where M is the length of the input string.
153
188
  * Space Complexity: O(1) - Constant space.
@@ -8,6 +8,7 @@
8
8
  */
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
10
  exports.Trie = exports.TrieNode = void 0;
11
+ const base_1 = require("../base");
11
12
  /**
12
13
  * TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes,
13
14
  * and a flag indicating whether it's the end of a word.
@@ -23,8 +24,9 @@ exports.TrieNode = TrieNode;
23
24
  /**
24
25
  * Trie represents a Trie data structure. It provides basic Trie operations and additional methods.
25
26
  */
26
- class Trie {
27
+ class Trie extends base_1.IterableElementBase {
27
28
  constructor(words, caseSensitive = true) {
29
+ super();
28
30
  this._root = new TrieNode('');
29
31
  this._caseSensitive = caseSensitive;
30
32
  this._size = 0;
@@ -317,56 +319,75 @@ class Trie {
317
319
  dfs(startNode, prefix);
318
320
  return words;
319
321
  }
320
- *[Symbol.iterator]() {
321
- function* _dfs(node, path) {
322
- if (node.isEnd) {
323
- yield path;
324
- }
325
- for (const [char, childNode] of node.children) {
326
- yield* _dfs(childNode, path + char);
327
- }
328
- }
329
- yield* _dfs(this.root, '');
330
- }
331
- forEach(callback) {
332
- let index = 0;
333
- for (const word of this) {
334
- callback(word, index, this);
335
- index++;
336
- }
337
- }
338
- filter(predicate) {
322
+ /**
323
+ * Time Complexity: O(n)
324
+ * Space Complexity: O(n)
325
+ */
326
+ /**
327
+ * Time Complexity: O(n)
328
+ * Space Complexity: O(n)
329
+ *
330
+ * The `filter` function takes a predicate function and returns a new array containing all the
331
+ * elements for which the predicate function returns true.
332
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
333
+ * `word`, `index`, and `this`. It should return a boolean value indicating whether the current
334
+ * element should be included in the filtered results or not.
335
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
336
+ * specify the value of `this` within the `predicate` function. It is used when you want to bind a
337
+ * specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
338
+ * @returns The `filter` method is returning an array of strings (`string[]`).
339
+ */
340
+ filter(predicate, thisArg) {
339
341
  const results = [];
340
342
  let index = 0;
341
343
  for (const word of this) {
342
- if (predicate(word, index, this)) {
344
+ if (predicate.call(thisArg, word, index, this)) {
343
345
  results.push(word);
344
346
  }
345
347
  index++;
346
348
  }
347
349
  return results;
348
350
  }
349
- map(callback) {
351
+ /**
352
+ * Time Complexity: O(n)
353
+ * Space Complexity: O(n)
354
+ */
355
+ /**
356
+ * Time Complexity: O(n)
357
+ * Space Complexity: O(n)
358
+ *
359
+ * The `map` function creates a new Trie by applying a callback function to each element in the Trie.
360
+ * @param callback - The callback parameter is a function that will be called for each element in the
361
+ * Trie. It takes three arguments: the current element in the Trie, the index of the current element,
362
+ * and the Trie itself. The callback function should return a new value for the element.
363
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
364
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
365
+ * passed as the `this` value to the `callback` function. If `thisArg` is
366
+ * @returns The `map` function is returning a new Trie object.
367
+ */
368
+ map(callback, thisArg) {
350
369
  const newTrie = new Trie();
351
370
  let index = 0;
352
371
  for (const word of this) {
353
- newTrie.add(callback(word, index, this));
372
+ newTrie.add(callback.call(thisArg, word, index, this));
354
373
  index++;
355
374
  }
356
375
  return newTrie;
357
376
  }
358
- reduce(callback, initialValue) {
359
- let accumulator = initialValue;
360
- let index = 0;
361
- for (const word of this) {
362
- accumulator = callback(accumulator, word, index, this);
363
- index++;
364
- }
365
- return accumulator;
366
- }
367
377
  print() {
368
378
  console.log([...this]);
369
379
  }
380
+ *_getIterator() {
381
+ function* _dfs(node, path) {
382
+ if (node.isEnd) {
383
+ yield path;
384
+ }
385
+ for (const [char, childNode] of node.children) {
386
+ yield* _dfs(childNode, path + char);
387
+ }
388
+ }
389
+ yield* _dfs(this.root, '');
390
+ }
370
391
  /**
371
392
  * Time Complexity: O(M), where M is the length of the input string.
372
393
  * Space Complexity: O(1) - Constant space.
@@ -0,0 +1,5 @@
1
+ import { IterableElementBase, IterablePairBase } from "../../../data-structures";
2
+ export type PairCallback<K, V, R> = (value: V, key: K, index: number, container: IterablePairBase<K, V>) => R;
3
+ export type ElementCallback<V, R> = (element: V, index: number, container: IterableElementBase<V>) => R;
4
+ export type ReducePairCallback<K, V, R> = (accumulator: R, value: V, key: K, index: number, container: IterablePairBase<K, V>) => R;
5
+ export type ReduceElementCallback<V, R> = (accumulator: R, element: V, index: number, container: IterableElementBase<V>) => R;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1 @@
1
+ export * from './base';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./base"), exports);
@@ -9,3 +9,4 @@ export * from './queue';
9
9
  export * from './stack';
10
10
  export * from './tree';
11
11
  export * from './trie';
12
+ export * from './base';
@@ -25,3 +25,4 @@ __exportStar(require("./queue"), exports);
25
25
  __exportStar(require("./stack"), exports);
26
26
  __exportStar(require("./tree"), exports);
27
27
  __exportStar(require("./trie"), exports);
28
+ __exportStar(require("./base"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "heap-typed",
3
- "version": "1.48.1",
3
+ "version": "1.48.2",
4
4
  "description": "Heap. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -131,6 +131,6 @@
131
131
  "typescript": "^4.9.5"
132
132
  },
133
133
  "dependencies": {
134
- "data-structure-typed": "^1.48.1"
134
+ "data-structure-typed": "^1.48.2"
135
135
  }
136
136
  }
@@ -0,0 +1 @@
1
+ export * from './iterable-base';