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.
Files changed (162) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/benchmark/report.html +2 -2
  3. package/benchmark/report.json +12 -18
  4. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
  5. package/dist/cjs/data-structures/binary-tree/avl-tree.js +23 -15
  6. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.js +66 -82
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  10. package/dist/cjs/data-structures/binary-tree/bst.d.ts +38 -37
  11. package/dist/cjs/data-structures/binary-tree/bst.js +56 -40
  12. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.js +26 -17
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
  17. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +31 -22
  18. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  19. package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
  20. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  21. package/dist/cjs/data-structures/hash/hash-map.d.ts +3 -3
  22. package/dist/cjs/data-structures/hash/hash-map.js +10 -4
  23. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  24. package/dist/cjs/data-structures/hash/hash-table.d.ts +9 -4
  25. package/dist/cjs/data-structures/hash/hash-table.js +50 -5
  26. package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
  27. package/dist/cjs/data-structures/heap/heap.d.ts +25 -22
  28. package/dist/cjs/data-structures/heap/heap.js +101 -41
  29. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  30. package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
  31. package/dist/cjs/data-structures/heap/max-heap.js +2 -2
  32. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  33. package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
  34. package/dist/cjs/data-structures/heap/min-heap.js +2 -2
  35. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  36. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
  37. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +125 -119
  38. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  39. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +38 -37
  40. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +65 -60
  41. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  42. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  43. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
  44. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  45. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  46. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
  47. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  48. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
  49. package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
  50. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  51. package/dist/cjs/data-structures/queue/deque.d.ts +50 -49
  52. package/dist/cjs/data-structures/queue/deque.js +81 -71
  53. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  54. package/dist/cjs/data-structures/queue/queue.d.ts +46 -0
  55. package/dist/cjs/data-structures/queue/queue.js +80 -0
  56. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  57. package/dist/cjs/data-structures/stack/stack.d.ts +20 -6
  58. package/dist/cjs/data-structures/stack/stack.js +65 -8
  59. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  60. package/dist/cjs/data-structures/trie/trie.d.ts +5 -0
  61. package/dist/cjs/data-structures/trie/trie.js +47 -0
  62. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  63. package/dist/cjs/interfaces/binary-tree.d.ts +3 -1
  64. package/dist/cjs/types/common.d.ts +2 -0
  65. package/dist/cjs/types/common.js.map +1 -1
  66. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  67. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  68. package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
  69. package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  70. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
  71. package/dist/mjs/data-structures/binary-tree/avl-tree.js +26 -16
  72. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
  73. package/dist/mjs/data-structures/binary-tree/binary-tree.js +66 -83
  74. package/dist/mjs/data-structures/binary-tree/bst.d.ts +38 -37
  75. package/dist/mjs/data-structures/binary-tree/bst.js +59 -41
  76. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
  77. package/dist/mjs/data-structures/binary-tree/rb-tree.js +30 -19
  78. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
  79. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +34 -23
  80. package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
  81. package/dist/mjs/data-structures/hash/hash-map.d.ts +3 -3
  82. package/dist/mjs/data-structures/hash/hash-map.js +10 -4
  83. package/dist/mjs/data-structures/hash/hash-table.d.ts +9 -4
  84. package/dist/mjs/data-structures/hash/hash-table.js +50 -5
  85. package/dist/mjs/data-structures/heap/heap.d.ts +25 -22
  86. package/dist/mjs/data-structures/heap/heap.js +102 -42
  87. package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
  88. package/dist/mjs/data-structures/heap/max-heap.js +2 -2
  89. package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
  90. package/dist/mjs/data-structures/heap/min-heap.js +2 -2
  91. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
  92. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +125 -119
  93. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +38 -37
  94. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +65 -60
  95. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  96. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
  97. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  98. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
  99. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
  100. package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
  101. package/dist/mjs/data-structures/queue/deque.d.ts +50 -49
  102. package/dist/mjs/data-structures/queue/deque.js +81 -71
  103. package/dist/mjs/data-structures/queue/queue.d.ts +46 -0
  104. package/dist/mjs/data-structures/queue/queue.js +80 -0
  105. package/dist/mjs/data-structures/stack/stack.d.ts +20 -6
  106. package/dist/mjs/data-structures/stack/stack.js +65 -8
  107. package/dist/mjs/data-structures/trie/trie.d.ts +5 -0
  108. package/dist/mjs/data-structures/trie/trie.js +47 -0
  109. package/dist/mjs/interfaces/binary-tree.d.ts +3 -1
  110. package/dist/mjs/types/common.d.ts +2 -0
  111. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  112. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  113. package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
  114. package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  115. package/dist/umd/data-structure-typed.js +856 -509
  116. package/dist/umd/data-structure-typed.min.js +2 -2
  117. package/dist/umd/data-structure-typed.min.js.map +1 -1
  118. package/package.json +1 -1
  119. package/src/data-structures/binary-tree/avl-tree.ts +27 -17
  120. package/src/data-structures/binary-tree/binary-tree.ts +114 -97
  121. package/src/data-structures/binary-tree/bst.ts +67 -47
  122. package/src/data-structures/binary-tree/rb-tree.ts +34 -20
  123. package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
  124. package/src/data-structures/graph/abstract-graph.ts +1 -1
  125. package/src/data-structures/hash/hash-map.ts +13 -7
  126. package/src/data-structures/hash/hash-table.ts +59 -9
  127. package/src/data-structures/heap/heap.ts +115 -46
  128. package/src/data-structures/heap/max-heap.ts +5 -5
  129. package/src/data-structures/heap/min-heap.ts +5 -5
  130. package/src/data-structures/linked-list/doubly-linked-list.ts +137 -128
  131. package/src/data-structures/linked-list/singly-linked-list.ts +72 -64
  132. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  133. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  134. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  135. package/src/data-structures/queue/deque.ts +86 -75
  136. package/src/data-structures/queue/queue.ts +88 -0
  137. package/src/data-structures/stack/stack.ts +75 -10
  138. package/src/data-structures/trie/trie.ts +53 -0
  139. package/src/interfaces/binary-tree.ts +13 -1
  140. package/src/types/common.ts +5 -1
  141. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  142. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  143. package/src/types/data-structures/heap/heap.ts +3 -1
  144. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
  145. package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
  146. package/test/performance/data-structures/heap/heap.test.ts +14 -5
  147. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
  148. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
  149. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -6
  150. package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
  151. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +1 -1
  152. package/test/unit/data-structures/hash/hash-table.test.ts +58 -2
  153. package/test/unit/data-structures/heap/heap.test.ts +2 -2
  154. package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
  155. package/test/unit/data-structures/heap/min-heap.test.ts +49 -1
  156. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
  157. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
  158. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
  159. package/test/unit/data-structures/queue/queue.test.ts +37 -0
  160. package/test/unit/data-structures/stack/stack.test.ts +55 -5
  161. package/test/unit/data-structures/trie/trie.test.ts +33 -0
  162. 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 = Array.isArray(elements) ? 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?: 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
- export type BSTComparator = (a: BTNKey, b: BTNKey) => number;
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?: BSTComparator;
7
+ comparator: Comparator<BTNKey>;
8
8
  };
@@ -1 +1,4 @@
1
- export {};
1
+ import { Comparator } from "../../common";
2
+ export type HeapOptions<T> = {
3
+ comparator: Comparator<T>;
4
+ };
@@ -1 +1,2 @@
1
- export {};
1
+ import { HeapOptions } from "../heap";
2
+ export type PriorityQueueOptions<T> = HeapOptions<T> & {};