heap-typed 1.51.9 → 1.52.0

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 (94) hide show
  1. package/dist/data-structures/base/index.d.ts +2 -1
  2. package/dist/data-structures/base/index.js +2 -1
  3. package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
  4. package/dist/data-structures/base/iterable-element-base.js +225 -0
  5. package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
  6. package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -3
  8. package/dist/data-structures/binary-tree/avl-tree.d.ts +3 -3
  9. package/dist/data-structures/binary-tree/binary-tree.d.ts +4 -4
  10. package/dist/data-structures/binary-tree/binary-tree.js +5 -3
  11. package/dist/data-structures/binary-tree/bst.d.ts +3 -11
  12. package/dist/data-structures/binary-tree/bst.js +2 -10
  13. package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -3
  14. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
  15. package/dist/data-structures/hash/hash-map.d.ts +2 -2
  16. package/dist/data-structures/heap/heap.d.ts +43 -114
  17. package/dist/data-structures/heap/heap.js +59 -127
  18. package/dist/data-structures/heap/max-heap.d.ts +50 -4
  19. package/dist/data-structures/heap/max-heap.js +76 -10
  20. package/dist/data-structures/heap/min-heap.d.ts +51 -5
  21. package/dist/data-structures/heap/min-heap.js +68 -11
  22. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
  23. package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
  24. package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
  25. package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
  26. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
  27. package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
  28. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
  29. package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
  30. package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
  31. package/dist/data-structures/priority-queue/priority-queue.js +70 -1
  32. package/dist/data-structures/queue/deque.d.ts +20 -18
  33. package/dist/data-structures/queue/deque.js +27 -20
  34. package/dist/data-structures/queue/queue.d.ts +8 -28
  35. package/dist/data-structures/queue/queue.js +15 -31
  36. package/dist/data-structures/stack/stack.d.ts +17 -22
  37. package/dist/data-structures/stack/stack.js +25 -24
  38. package/dist/data-structures/trie/trie.d.ts +18 -13
  39. package/dist/data-structures/trie/trie.js +26 -15
  40. package/dist/interfaces/binary-tree.d.ts +2 -2
  41. package/dist/types/data-structures/base/base.d.ts +5 -2
  42. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -3
  43. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
  44. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -3
  45. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -3
  46. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -3
  47. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -3
  48. package/dist/types/data-structures/heap/heap.d.ts +3 -2
  49. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  50. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  51. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  52. package/dist/types/data-structures/queue/deque.d.ts +3 -2
  53. package/dist/types/data-structures/queue/queue.d.ts +2 -1
  54. package/dist/types/data-structures/stack/stack.d.ts +2 -1
  55. package/dist/types/data-structures/trie/trie.d.ts +3 -2
  56. package/package.json +2 -2
  57. package/src/data-structures/base/index.ts +2 -1
  58. package/src/data-structures/base/iterable-element-base.ts +250 -0
  59. package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
  60. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +2 -3
  61. package/src/data-structures/binary-tree/avl-tree.ts +2 -3
  62. package/src/data-structures/binary-tree/binary-tree.ts +6 -6
  63. package/src/data-structures/binary-tree/bst.ts +8 -19
  64. package/src/data-structures/binary-tree/rb-tree.ts +2 -3
  65. package/src/data-structures/binary-tree/tree-multi-map.ts +2 -3
  66. package/src/data-structures/hash/hash-map.ts +4 -4
  67. package/src/data-structures/heap/heap.ts +71 -152
  68. package/src/data-structures/heap/max-heap.ts +88 -13
  69. package/src/data-structures/heap/min-heap.ts +78 -15
  70. package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
  71. package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
  72. package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
  73. package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
  74. package/src/data-structures/priority-queue/priority-queue.ts +81 -4
  75. package/src/data-structures/queue/deque.ts +35 -24
  76. package/src/data-structures/queue/queue.ts +23 -36
  77. package/src/data-structures/stack/stack.ts +31 -26
  78. package/src/data-structures/trie/trie.ts +33 -18
  79. package/src/interfaces/binary-tree.ts +1 -2
  80. package/src/types/data-structures/base/base.ts +14 -6
  81. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -3
  82. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -3
  83. package/src/types/data-structures/binary-tree/binary-tree.ts +3 -4
  84. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  85. package/src/types/data-structures/binary-tree/rb-tree.ts +2 -3
  86. package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -3
  87. package/src/types/data-structures/heap/heap.ts +4 -1
  88. package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
  89. package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
  90. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  91. package/src/types/data-structures/queue/deque.ts +3 -1
  92. package/src/types/data-structures/queue/queue.ts +3 -1
  93. package/src/types/data-structures/stack/stack.ts +3 -1
  94. package/src/types/data-structures/trie/trie.ts +3 -1
@@ -1 +1,2 @@
1
- export {};
1
+ import { IterableElementBaseOptions } from '../base';
2
+ export type QueueOptions<E, R> = IterableElementBaseOptions<E, R> & {};
@@ -1 +1,2 @@
1
- export {};
1
+ import { IterableElementBaseOptions } from '../base';
2
+ export type StackOptions<E, R> = IterableElementBaseOptions<E, R> & {};
@@ -1,3 +1,4 @@
1
- export type TrieOptions = {
1
+ import { IterableElementBaseOptions } from '../base';
2
+ export type TrieOptions<R> = {
2
3
  caseSensitive?: boolean;
3
- };
4
+ } & IterableElementBaseOptions<string, R>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "heap-typed",
3
- "version": "1.51.9",
3
+ "version": "1.52.0",
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.51.9"
134
+ "data-structure-typed": "^1.52.0"
135
135
  }
136
136
  }
@@ -1 +1,2 @@
1
- export * from './iterable-base';
1
+ export * from './iterable-entry-base';
2
+ export * from './iterable-element-base';
@@ -0,0 +1,250 @@
1
+ import { ElementCallback, IterableElementBaseOptions, ReduceElementCallback } from '../../types';
2
+
3
+ export abstract class IterableElementBase<E, R, C> {
4
+ /**
5
+ * The protected constructor initializes the options for the IterableElementBase class, including the
6
+ * toElementFn function.
7
+ * @param [options] - An optional object that contains the following properties:
8
+ */
9
+ protected constructor(options?: IterableElementBaseOptions<E, R>) {
10
+ if (options) {
11
+ const { toElementFn } = options;
12
+ if (typeof toElementFn === 'function') this._toElementFn = toElementFn;
13
+ else if (toElementFn) throw new TypeError('toElementFn must be a function type');
14
+ }
15
+ }
16
+
17
+ abstract get size(): number;
18
+
19
+ protected _toElementFn?: (rawElement: R) => E;
20
+
21
+ /**
22
+ * The function returns the _toElementFn property, which is a function that converts a raw element to
23
+ * a specific type.
24
+ * @returns The function `get toElementFn()` is returning either a function that takes a raw element
25
+ * `rawElement` of type `R` and returns an element `E`, or `undefined` if no function is assigned to
26
+ * `_toElementFn`.
27
+ */
28
+ get toElementFn(): ((rawElement: R) => E) | undefined {
29
+ return this._toElementFn;
30
+ }
31
+
32
+ /**
33
+ * Time Complexity: O(n)
34
+ * Space Complexity: O(1)
35
+ */
36
+ /**
37
+ * Time Complexity: O(n)
38
+ * Space Complexity: O(1)
39
+ *
40
+ * The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
41
+ * @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
42
+ * allows the function to accept any number of arguments as an array. In this case, the `args`
43
+ * parameter is used to pass any number of arguments to the `_getIterator` method.
44
+ */
45
+ * [Symbol.iterator](...args: any[]): IterableIterator<E> {
46
+ yield* this._getIterator(...args);
47
+ }
48
+
49
+ /**
50
+ * Time Complexity: O(n)
51
+ * Space Complexity: O(n)
52
+ */
53
+ /**
54
+ * Time Complexity: O(n)
55
+ * Space Complexity: O(n)
56
+ *
57
+ * The function returns an iterator that yields all the values in the object.
58
+ */
59
+ * values(): IterableIterator<E> {
60
+ for (const item of this) {
61
+ yield item;
62
+ }
63
+ }
64
+
65
+ /**
66
+ * Time Complexity: O(n)
67
+ * Space Complexity: O(1)
68
+ */
69
+ /**
70
+ * Time Complexity: O(n)
71
+ * Space Complexity: O(1)
72
+ *
73
+ * The `every` function checks if every element in the array satisfies a given predicate.
74
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
75
+ * the current element being processed, its index, and the array it belongs to. It should return a
76
+ * boolean value indicating whether the element satisfies a certain condition or not.
77
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
78
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
79
+ * passed as the `this` value to the `predicate` function. If `thisArg` is
80
+ * @returns The `every` method is returning a boolean value. It returns `true` if every element in
81
+ * the array satisfies the provided predicate function, and `false` otherwise.
82
+ */
83
+ every(predicate: ElementCallback<E, R, boolean, C>, thisArg?: any): boolean {
84
+ let index = 0;
85
+ for (const item of this) {
86
+ if (!predicate.call(thisArg, item, index++, this)) {
87
+ return false;
88
+ }
89
+ }
90
+ return true;
91
+ }
92
+
93
+ /**
94
+ * Time Complexity: O(n)
95
+ * Space Complexity: O(1)
96
+ */
97
+
98
+ /**
99
+ * Time Complexity: O(n)
100
+ * Space Complexity: O(1)
101
+ */
102
+ /**
103
+ * Time Complexity: O(n)
104
+ * Space Complexity: O(1)
105
+ *
106
+ * The "some" function checks if at least one element in a collection satisfies a given predicate.
107
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
108
+ * `value`, `index`, and `array`. It should return a boolean value indicating whether the current
109
+ * element satisfies the condition.
110
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
111
+ * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
112
+ * it will be passed as the `this` value to the `predicate` function. If `thisArg
113
+ * @returns a boolean value. It returns true if the predicate function returns true for any element
114
+ * in the collection, and false otherwise.
115
+ */
116
+ some(predicate: ElementCallback<E, R, boolean, C>, thisArg?: any): boolean {
117
+ let index = 0;
118
+ for (const item of this) {
119
+ if (predicate.call(thisArg, item, index++, this)) {
120
+ return true;
121
+ }
122
+ }
123
+ return false;
124
+ }
125
+
126
+ /**
127
+ * Time Complexity: O(n)
128
+ * Space Complexity: O(1)
129
+ */
130
+
131
+ /**
132
+ * Time Complexity: O(n)
133
+ * Space Complexity: O(1)
134
+ *
135
+ * The `forEach` function iterates over each element in an array-like object and calls a callback
136
+ * function for each element.
137
+ * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
138
+ * the array. It takes three arguments: the current element being processed, the index of the current
139
+ * element, and the array that forEach was called upon.
140
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
141
+ * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
142
+ * be passed as the `this` value to the `callbackfn` function. If `thisArg
143
+ */
144
+ forEach(callbackfn: ElementCallback<E, R, void, C>, thisArg?: any): void {
145
+ let index = 0;
146
+ for (const item of this) {
147
+ callbackfn.call(thisArg, item, index++, this);
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Time Complexity: O(n)
153
+ * Space Complexity: O(1)
154
+ */
155
+
156
+ /**
157
+ * Time Complexity: O(n)
158
+ * Space Complexity: O(1)
159
+ *
160
+ * The `find` function iterates over the elements of an array-like object and returns the first
161
+ * element that satisfies the provided callback function.
162
+ * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
163
+ * the array. It takes three arguments: the current element being processed, the index of the current
164
+ * element, and the array itself. The function should return a boolean value indicating whether the
165
+ * current element matches the desired condition.
166
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
167
+ * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
168
+ * be passed as the `this` value to the `callbackfn` function. If `thisArg
169
+ * @returns The `find` method returns the first element in the array that satisfies the provided
170
+ * callback function. If no element satisfies the callback function, `undefined` is returned.
171
+ */
172
+ find(callbackfn: ElementCallback<E, R, boolean, C>, thisArg?: any): E | undefined {
173
+ let index = 0;
174
+ for (const item of this) {
175
+ if (callbackfn.call(thisArg, item, index++, this)) return item;
176
+ }
177
+
178
+ return;
179
+ }
180
+
181
+ /**
182
+ * Time Complexity: O(n)
183
+ * Space Complexity: O(1)
184
+ *
185
+ * The function checks if a given element exists in a collection.
186
+ * @param {E} element - The parameter "element" is of type E, which means it can be any type. It
187
+ * represents the element that we want to check for existence in the collection.
188
+ * @returns a boolean value. It returns true if the element is found in the collection, and false
189
+ * otherwise.
190
+ */
191
+ has(element: E): boolean {
192
+ for (const ele of this) {
193
+ if (ele === element) return true;
194
+ }
195
+ return false;
196
+ }
197
+
198
+ /**
199
+ * Time Complexity: O(n)
200
+ * Space Complexity: O(1)
201
+ */
202
+ /**
203
+ * Time Complexity: O(n)
204
+ * Space Complexity: O(1)
205
+ *
206
+ * The `reduce` function iterates over the elements of an array-like object and applies a callback
207
+ * function to reduce them into a single value.
208
+ * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
209
+ * the array. It takes four arguments:
210
+ * @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
211
+ * is the value that the accumulator starts with before the reduction operation begins.
212
+ * @returns The `reduce` method is returning the final value of the accumulator after iterating over
213
+ * all the elements in the array and applying the callback function to each element.
214
+ */
215
+ reduce<U>(callbackfn: ReduceElementCallback<E, R, U, C>, initialValue: U): U {
216
+ let accumulator = initialValue;
217
+ let index = 0;
218
+ for (const item of this) {
219
+ accumulator = callbackfn(accumulator, item as E, index++, this);
220
+ }
221
+ return accumulator;
222
+ }
223
+
224
+ /**
225
+ * Time Complexity: O(n)
226
+ * Space Complexity: O(n)
227
+ */
228
+
229
+ /**
230
+ * Time Complexity: O(n)
231
+ * Space Complexity: O(n)
232
+ *
233
+ * The print function logs the elements of an array to the console.
234
+ */
235
+ print(): void {
236
+ console.log([...this]);
237
+ }
238
+
239
+ abstract isEmpty(): boolean;
240
+
241
+ abstract clear(): void;
242
+
243
+ abstract clone(): C;
244
+
245
+ abstract map(...args: any[]): any;
246
+
247
+ abstract filter(...args: any[]): any;
248
+
249
+ protected abstract _getIterator(...args: any[]): IterableIterator<E>;
250
+ }
@@ -1,6 +1,14 @@
1
- import { ElementCallback, EntryCallback, ReduceElementCallback, ReduceEntryCallback } from '../../types';
1
+ import { EntryCallback, ReduceEntryCallback } from '../../types';
2
2
 
3
3
  export abstract class IterableEntryBase<K = any, V = any> {
4
+ // protected constructor(options?: IterableEntryBaseOptions<K, V, R>) {
5
+ // if (options) {
6
+ // const { toEntryFn } = options;
7
+ // if (typeof toEntryFn === 'function') this._toEntryFn = toEntryFn
8
+ // else throw new TypeError('toEntryFn must be a function type');
9
+ // }
10
+ // }
11
+
4
12
  /**
5
13
  * Time Complexity: O(n)
6
14
  * Space Complexity: O(1)
@@ -8,6 +16,16 @@ export abstract class IterableEntryBase<K = any, V = any> {
8
16
 
9
17
  abstract get size(): number;
10
18
 
19
+ // protected _toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
20
+ //
21
+ // /**
22
+ // * The function returns the value of the _toEntryFn property.
23
+ // * @returns The function being returned is `this._toEntryFn`.
24
+ // */
25
+ // get toEntryFn() {
26
+ // return this._toEntryFn;
27
+ // }
28
+
11
29
  /**
12
30
  * Time Complexity: O(n)
13
31
  * Space Complexity: O(1)
@@ -285,221 +303,12 @@ export abstract class IterableEntryBase<K = any, V = any> {
285
303
  * Time Complexity: O(n)
286
304
  * Space Complexity: O(n)
287
305
  */
288
- print(): void {
289
- console.log([...this]);
290
- }
291
-
292
- abstract isEmpty(): boolean;
293
-
294
- abstract clear(): void;
295
-
296
- abstract clone(): any;
297
-
298
- abstract map(...args: any[]): any;
299
-
300
- abstract filter(...args: any[]): any;
301
-
302
- protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
303
- }
304
-
305
- export abstract class IterableElementBase<E = any, C = any> {
306
- abstract get size(): number;
307
-
308
- /**
309
- * Time Complexity: O(n)
310
- * Space Complexity: O(1)
311
- */
312
- /**
313
- * Time Complexity: O(n)
314
- * Space Complexity: O(1)
315
- *
316
- * The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
317
- * @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
318
- * allows the function to accept any number of arguments as an array. In this case, the `args`
319
- * parameter is used to pass any number of arguments to the `_getIterator` method.
320
- */
321
- * [Symbol.iterator](...args: any[]): IterableIterator<E> {
322
- yield* this._getIterator(...args);
323
- }
324
306
 
325
307
  /**
326
308
  * Time Complexity: O(n)
327
309
  * Space Complexity: O(n)
328
- */
329
- /**
330
- * Time Complexity: O(n)
331
- * Space Complexity: O(n)
332
- *
333
- * The function returns an iterator that yields all the values in the object.
334
- */
335
- * values(): IterableIterator<E> {
336
- for (const item of this) {
337
- yield item;
338
- }
339
- }
340
-
341
- /**
342
- * Time Complexity: O(n)
343
- * Space Complexity: O(1)
344
- */
345
- /**
346
- * Time Complexity: O(n)
347
- * Space Complexity: O(1)
348
- *
349
- * The `every` function checks if every element in the array satisfies a given predicate.
350
- * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
351
- * the current element being processed, its index, and the array it belongs to. It should return a
352
- * boolean value indicating whether the element satisfies a certain condition or not.
353
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
354
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
355
- * passed as the `this` value to the `predicate` function. If `thisArg` is
356
- * @returns The `every` method is returning a boolean value. It returns `true` if every element in
357
- * the array satisfies the provided predicate function, and `false` otherwise.
358
- */
359
- every(predicate: ElementCallback<E, boolean>, thisArg?: any): boolean {
360
- let index = 0;
361
- for (const item of this) {
362
- if (!predicate.call(thisArg, item, index++, this)) {
363
- return false;
364
- }
365
- }
366
- return true;
367
- }
368
-
369
- /**
370
- * Time Complexity: O(n)
371
- * Space Complexity: O(1)
372
- */
373
-
374
- /**
375
- * Time Complexity: O(n)
376
- * Space Complexity: O(1)
377
- */
378
- /**
379
- * Time Complexity: O(n)
380
- * Space Complexity: O(1)
381
- *
382
- * The "some" function checks if at least one element in a collection satisfies a given predicate.
383
- * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
384
- * `value`, `index`, and `array`. It should return a boolean value indicating whether the current
385
- * element satisfies the condition.
386
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
387
- * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
388
- * it will be passed as the `this` value to the `predicate` function. If `thisArg
389
- * @returns a boolean value. It returns true if the predicate function returns true for any element
390
- * in the collection, and false otherwise.
391
- */
392
- some(predicate: ElementCallback<E, boolean>, thisArg?: any): boolean {
393
- let index = 0;
394
- for (const item of this) {
395
- if (predicate.call(thisArg, item, index++, this)) {
396
- return true;
397
- }
398
- }
399
- return false;
400
- }
401
-
402
- /**
403
- * Time Complexity: O(n)
404
- * Space Complexity: O(1)
405
- */
406
-
407
- /**
408
- * Time Complexity: O(n)
409
- * Space Complexity: O(1)
410
- *
411
- * The `forEach` function iterates over each element in an array-like object and calls a callback
412
- * function for each element.
413
- * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
414
- * the array. It takes three arguments: the current element being processed, the index of the current
415
- * element, and the array that forEach was called upon.
416
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
417
- * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
418
- * be passed as the `this` value to the `callbackfn` function. If `thisArg
419
- */
420
- forEach(callbackfn: ElementCallback<E, void>, thisArg?: any): void {
421
- let index = 0;
422
- for (const item of this) {
423
- callbackfn.call(thisArg, item, index++, this);
424
- }
425
- }
426
-
427
- /**
428
- * Time Complexity: O(n)
429
- * Space Complexity: O(1)
430
- */
431
-
432
- /**
433
- * Time Complexity: O(n)
434
- * Space Complexity: O(1)
435
- *
436
- * The `find` function iterates over the elements of an array-like object and returns the first
437
- * element that satisfies the provided callback function.
438
- * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
439
- * the array. It takes three arguments: the current element being processed, the index of the current
440
- * element, and the array itself. The function should return a boolean value indicating whether the
441
- * current element matches the desired condition.
442
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
443
- * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
444
- * be passed as the `this` value to the `callbackfn` function. If `thisArg
445
- * @returns The `find` method returns the first element in the array that satisfies the provided
446
- * callback function. If no element satisfies the callback function, `undefined` is returned.
447
- */
448
- find(callbackfn: ElementCallback<E, boolean>, thisArg?: any): E | undefined {
449
- let index = 0;
450
- for (const item of this) {
451
- if (callbackfn.call(thisArg, item, index++, this)) return item;
452
- }
453
-
454
- return;
455
- }
456
-
457
- /**
458
- * Time Complexity: O(n)
459
- * Space Complexity: O(1)
460
- *
461
- * The function checks if a given element exists in a collection.
462
- * @param {E} element - The parameter "element" is of type E, which means it can be any type. It
463
- * represents the element that we want to check for existence in the collection.
464
- * @returns a boolean value. It returns true if the element is found in the collection, and false
465
- * otherwise.
466
- */
467
- has(element: E): boolean {
468
- for (const ele of this) {
469
- if (ele === element) return true;
470
- }
471
- return false;
472
- }
473
-
474
- /**
475
- * Time Complexity: O(n)
476
- * Space Complexity: O(1)
477
- */
478
- /**
479
- * Time Complexity: O(n)
480
- * Space Complexity: O(1)
481
310
  *
482
- * The `reduce` function iterates over the elements of an array-like object and applies a callback
483
- * function to reduce them into a single value.
484
- * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
485
- * the array. It takes four arguments:
486
- * @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
487
- * is the value that the accumulator starts with before the reduction operation begins.
488
- * @returns The `reduce` method is returning the final value of the accumulator after iterating over
489
- * all the elements in the array and applying the callback function to each element.
490
- */
491
- reduce<U>(callbackfn: ReduceElementCallback<E, U>, initialValue: U): U {
492
- let accumulator = initialValue;
493
- let index = 0;
494
- for (const item of this) {
495
- accumulator = callbackfn(accumulator, item as E, index++, this);
496
- }
497
- return accumulator;
498
- }
499
-
500
- /**
501
- * Time Complexity: O(n)
502
- * Space Complexity: O(n)
311
+ * The print function logs the elements of an array to the console.
503
312
  */
504
313
  print(): void {
505
314
  console.log([...this]);
@@ -509,11 +318,11 @@ export abstract class IterableElementBase<E = any, C = any> {
509
318
 
510
319
  abstract clear(): void;
511
320
 
512
- abstract clone(): C;
321
+ abstract clone(): any;
513
322
 
514
323
  abstract map(...args: any[]): any;
515
324
 
516
325
  abstract filter(...args: any[]): any;
517
326
 
518
- protected abstract _getIterator(...args: any[]): IterableIterator<E>;
327
+ protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
519
328
  }
@@ -12,7 +12,6 @@ import type {
12
12
  BinaryTreeDeleteResult,
13
13
  BSTNKeyOrNode,
14
14
  BTNCallback,
15
- Comparable,
16
15
  IterationType,
17
16
  KeyOrNodeOrEntry
18
17
  } from '../../types';
@@ -21,7 +20,7 @@ import { IBinaryTree } from '../../interfaces';
21
20
  import { AVLTree, AVLTreeNode } from './avl-tree';
22
21
 
23
22
  export class AVLTreeMultiMapNode<
24
- K extends Comparable,
23
+ K = any,
25
24
  V = any,
26
25
  NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>
27
26
  > extends AVLTreeNode<K, V, NODE> {
@@ -64,7 +63,7 @@ export class AVLTreeMultiMapNode<
64
63
  * The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
65
64
  */
66
65
  export class AVLTreeMultiMap<
67
- K extends Comparable,
66
+ K = any,
68
67
  V = any,
69
68
  R = BTNEntry<K, V>,
70
69
  NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
@@ -13,14 +13,13 @@ import type {
13
13
  BinaryTreeDeleteResult,
14
14
  BSTNKeyOrNode,
15
15
  BTNCallback,
16
- Comparable,
17
16
  KeyOrNodeOrEntry
18
17
  } from '../../types';
19
18
  import { BTNEntry } from '../../types';
20
19
  import { IBinaryTree } from '../../interfaces';
21
20
 
22
21
  export class AVLTreeNode<
23
- K extends Comparable,
22
+ K = any,
24
23
  V = any,
25
24
  NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>
26
25
  > extends BSTNode<K, V, NODE> {
@@ -67,7 +66,7 @@ export class AVLTreeNode<
67
66
  * 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
68
67
  */
69
68
  export class AVLTree<
70
- K extends Comparable,
69
+ K = any,
71
70
  V = any,
72
71
  R = BTNEntry<K, V>,
73
72
  NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
@@ -14,7 +14,6 @@ import type {
14
14
  BinaryTreePrintOptions,
15
15
  BTNCallback,
16
16
  BTNEntry,
17
- Comparable,
18
17
  DFSOrderPattern,
19
18
  EntryCallback,
20
19
  FamilyPosition,
@@ -33,7 +32,7 @@ import { IterableEntryBase } from '../base';
33
32
  * @template NODE - The type of the family relationship in the binary tree.
34
33
  */
35
34
  export class BinaryTreeNode<
36
- K extends Comparable,
35
+ K = any,
37
36
  V = any,
38
37
  NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>
39
38
  > {
@@ -131,7 +130,7 @@ export class BinaryTreeNode<
131
130
  */
132
131
 
133
132
  export class BinaryTree<
134
- K extends Comparable,
133
+ K = any,
135
134
  V = any,
136
135
  R = BTNEntry<K, V>,
137
136
  NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>,
@@ -143,7 +142,7 @@ export class BinaryTree<
143
142
 
144
143
  /**
145
144
  * The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
146
- * @param [keysOrNodesOrEntriesOrRawElements] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
145
+ * @param [keysOrNodesOrEntriesOrRawElements] - Optional iterable of KeyOrNodeOrEntry objects. These objects represent the
147
146
  * nodes to be added to the binary tree.
148
147
  * @param [options] - The `options` parameter is an optional object that can contain additional
149
148
  * configuration options for the binary tree. In this case, it is of type
@@ -159,6 +158,7 @@ export class BinaryTree<
159
158
  const { iterationType, toEntryFn } = options;
160
159
  if (iterationType) this.iterationType = iterationType;
161
160
  if (typeof toEntryFn === 'function') this._toEntryFn = toEntryFn;
161
+ else if (toEntryFn) throw TypeError('toEntryFn must be a function type');
162
162
  }
163
163
 
164
164
  if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
@@ -2001,7 +2001,7 @@ export class BinaryTree<
2001
2001
  // Display logic of normal nodes
2002
2002
 
2003
2003
  const key = node.key,
2004
- line = this.isNIL(node) ? 'S' : key.toString(),
2004
+ line = this.isNIL(node) ? 'S' : String(key),
2005
2005
  width = line.length;
2006
2006
 
2007
2007
  return _buildNodeDisplay(
@@ -2012,7 +2012,7 @@ export class BinaryTree<
2012
2012
  );
2013
2013
  } else {
2014
2014
  // For cases where none of the conditions are met, null, undefined, and NaN nodes are not displayed
2015
- const line = node === undefined ? 'U' : 'NODE',
2015
+ const line = node === undefined ? 'U' : 'N',
2016
2016
  width = line.length;
2017
2017
 
2018
2018
  return _buildNodeDisplay(line, width, [[''], 1, 0, 0], [[''], 1, 0, 0]);