heap-typed 1.53.7 → 1.53.8

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 (45) hide show
  1. package/dist/common/index.js +5 -0
  2. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +2 -2
  4. package/dist/data-structures/binary-tree/binary-tree.d.ts +1 -1
  5. package/dist/data-structures/binary-tree/binary-tree.js +2 -2
  6. package/dist/data-structures/binary-tree/bst.d.ts +53 -23
  7. package/dist/data-structures/binary-tree/bst.js +59 -25
  8. package/dist/data-structures/binary-tree/index.d.ts +1 -1
  9. package/dist/data-structures/binary-tree/index.js +1 -1
  10. package/dist/data-structures/binary-tree/{rb-tree.d.ts → red-black-tree.d.ts} +49 -0
  11. package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +50 -1
  12. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  13. package/dist/data-structures/binary-tree/tree-multi-map.js +5 -5
  14. package/dist/data-structures/hash/hash-map.d.ts +30 -0
  15. package/dist/data-structures/hash/hash-map.js +30 -0
  16. package/dist/data-structures/heap/heap.d.ts +20 -3
  17. package/dist/data-structures/heap/heap.js +31 -11
  18. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +36 -1
  19. package/dist/data-structures/linked-list/doubly-linked-list.js +56 -9
  20. package/dist/data-structures/linked-list/singly-linked-list.d.ts +34 -1
  21. package/dist/data-structures/linked-list/singly-linked-list.js +54 -10
  22. package/dist/data-structures/queue/deque.d.ts +37 -8
  23. package/dist/data-structures/queue/deque.js +73 -29
  24. package/dist/data-structures/queue/queue.d.ts +41 -1
  25. package/dist/data-structures/queue/queue.js +51 -9
  26. package/dist/data-structures/stack/stack.d.ts +27 -10
  27. package/dist/data-structures/stack/stack.js +39 -20
  28. package/dist/data-structures/trie/trie.d.ts +8 -3
  29. package/dist/data-structures/trie/trie.js +8 -3
  30. package/package.json +2 -2
  31. package/src/common/index.ts +7 -1
  32. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
  33. package/src/data-structures/binary-tree/binary-tree.ts +2 -2
  34. package/src/data-structures/binary-tree/bst.ts +64 -25
  35. package/src/data-structures/binary-tree/index.ts +1 -1
  36. package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +50 -1
  37. package/src/data-structures/binary-tree/tree-multi-map.ts +3 -3
  38. package/src/data-structures/hash/hash-map.ts +30 -0
  39. package/src/data-structures/heap/heap.ts +33 -10
  40. package/src/data-structures/linked-list/doubly-linked-list.ts +62 -8
  41. package/src/data-structures/linked-list/singly-linked-list.ts +60 -10
  42. package/src/data-structures/queue/deque.ts +72 -28
  43. package/src/data-structures/queue/queue.ts +50 -7
  44. package/src/data-structures/stack/stack.ts +39 -20
  45. package/src/data-structures/trie/trie.ts +8 -3
@@ -53,14 +53,7 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
53
53
  const needBucketNum = calcMinUnitsRequired(_size, this._bucketSize);
54
54
  this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
55
55
  this._firstInBucket = this._lastInBucket = (this._bucketSize - (_size % this._bucketSize)) >> 1;
56
-
57
- for (const el of elements) {
58
- if (this.toElementFn) {
59
- this.push(this.toElementFn(el as R));
60
- } else {
61
- this.push(el as E);
62
- }
63
- }
56
+ this.pushMany(elements);
64
57
  }
65
58
 
66
59
  protected _bucketSize: number = 1 << 12;
@@ -230,6 +223,33 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
230
223
  return element;
231
224
  }
232
225
 
226
+ /**
227
+ * Time Complexity: O(1)
228
+ * Space Complexity: O(1)
229
+ *
230
+ * The `shift()` function removes and returns the first element from a data structure, updating the
231
+ * internal state variables accordingly.
232
+ * @returns The element that is being removed from the beginning of the data structure is being
233
+ * returned.
234
+ */
235
+ shift(): E | undefined {
236
+ if (this._size === 0) return;
237
+ const element = this._buckets[this._bucketFirst][this._firstInBucket];
238
+ if (this._size !== 1) {
239
+ if (this._firstInBucket < this._bucketSize - 1) {
240
+ this._firstInBucket += 1;
241
+ } else if (this._bucketFirst < this._bucketCount - 1) {
242
+ this._bucketFirst += 1;
243
+ this._firstInBucket = 0;
244
+ } else {
245
+ this._bucketFirst = 0;
246
+ this._firstInBucket = 0;
247
+ }
248
+ }
249
+ this._size -= 1;
250
+ return element;
251
+ }
252
+
233
253
  /**
234
254
  * Time Complexity: Amortized O(1)
235
255
  * Space Complexity: O(n)
@@ -260,30 +280,54 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
260
280
  }
261
281
 
262
282
  /**
263
- * Time Complexity: O(1)
264
- * Space Complexity: O(1)
283
+ * Time Complexity: O(k)
284
+ * Space Complexity: O(k)
265
285
  *
266
- * The `shift()` function removes and returns the first element from a data structure, updating the
267
- * internal state variables accordingly.
268
- * @returns The element that is being removed from the beginning of the data structure is being
269
- * returned.
270
- */
271
- shift(): E | undefined {
272
- if (this._size === 0) return;
273
- const element = this._buckets[this._bucketFirst][this._firstInBucket];
274
- if (this._size !== 1) {
275
- if (this._firstInBucket < this._bucketSize - 1) {
276
- this._firstInBucket += 1;
277
- } else if (this._bucketFirst < this._bucketCount - 1) {
278
- this._bucketFirst += 1;
279
- this._firstInBucket = 0;
286
+ * The function `pushMany` iterates over elements and pushes them into an array after applying a
287
+ * transformation function if provided.
288
+ * @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
289
+ * parameter in the `pushMany` function is expected to be an iterable containing elements of type `E`
290
+ * or `R`. It can be either an `IterableWithSizeOrLength<E>` or an `IterableWithSizeOrLength<R>`. The
291
+ * function iterates over each element
292
+ * @returns The `pushMany` function is returning an array of boolean values, where each value
293
+ * represents the result of calling the `push` method on the current object instance with the
294
+ * corresponding element from the input `elements` iterable.
295
+ */
296
+ pushMany(elements: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>) {
297
+ const ans: boolean[] = [];
298
+ for (const el of elements) {
299
+ if (this.toElementFn) {
300
+ ans.push(this.push(this.toElementFn(el as R)));
280
301
  } else {
281
- this._bucketFirst = 0;
282
- this._firstInBucket = 0;
302
+ ans.push(this.push(el as E));
283
303
  }
284
304
  }
285
- this._size -= 1;
286
- return element;
305
+ return ans;
306
+ }
307
+
308
+ /**
309
+ * Time Complexity: O(k)
310
+ * Space Complexity: O(k)
311
+ *
312
+ * The `unshiftMany` function in TypeScript iterates over elements and adds them to the beginning of
313
+ * an array, optionally converting them using a provided function.
314
+ * @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
315
+ * parameter in the `unshiftMany` function is an iterable containing elements of type `E` or `R`. It
316
+ * can be an array or any other iterable data structure that has a known size or length. The function
317
+ * iterates over each element in the `elements` iterable and
318
+ * @returns The `unshiftMany` function returns an array of boolean values indicating whether each
319
+ * element was successfully added to the beginning of the array.
320
+ */
321
+ unshiftMany(elements: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R> = []) {
322
+ const ans: boolean[] = [];
323
+ for (const el of elements) {
324
+ if (this.toElementFn) {
325
+ ans.push(this.unshift(this.toElementFn(el as R)));
326
+ } else {
327
+ ans.push(this.unshift(el as E));
328
+ }
329
+ }
330
+ return ans;
287
331
  }
288
332
 
289
333
  /**
@@ -25,12 +25,7 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
25
25
  this._autoCompactRatio = autoCompactRatio;
26
26
  }
27
27
 
28
- if (elements) {
29
- for (const el of elements) {
30
- if (this.toElementFn) this.push(this.toElementFn(el as R));
31
- else this.push(el as E);
32
- }
33
- }
28
+ this.pushMany(elements);
34
29
  }
35
30
 
36
31
  protected _elements: E[] = [];
@@ -131,6 +126,26 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
131
126
  return true;
132
127
  }
133
128
 
129
+ /**
130
+ * Time Complexity: O(k)
131
+ * Space Complexity: O(k)
132
+ *
133
+ * The `pushMany` function iterates over elements and pushes them into an array after applying a
134
+ * transformation function if provided.
135
+ * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
136
+ * is an iterable containing elements of type `E` or `R`.
137
+ * @returns The `pushMany` function is returning an array of boolean values indicating whether each
138
+ * element was successfully pushed into the data structure.
139
+ */
140
+ pushMany(elements: Iterable<E> | Iterable<R>) {
141
+ const ans: boolean[] = [];
142
+ for (const el of elements) {
143
+ if (this.toElementFn) ans.push(this.push(this.toElementFn(el as R)));
144
+ else ans.push(this.push(el as E));
145
+ }
146
+ return ans;
147
+ }
148
+
134
149
  /**
135
150
  * Time Complexity: O(1)
136
151
  * Space Complexity: O(1)
@@ -150,6 +165,9 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
150
165
  }
151
166
 
152
167
  /**
168
+ * Time Complexity: O(n)
169
+ * Space Complexity: O(1)
170
+ *
153
171
  * The delete function removes an element from the list.
154
172
  * @param {E} element - Specify the element to be deleted
155
173
  * @return A boolean value indicating whether the element was successfully deleted or not
@@ -160,6 +178,9 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
160
178
  }
161
179
 
162
180
  /**
181
+ * Time Complexity: O(n)
182
+ * Space Complexity: O(1)
183
+ *
163
184
  * The deleteAt function deletes the element at a given index.
164
185
  * @param {number} index - Determine the index of the element to be deleted
165
186
  * @return A boolean value
@@ -173,7 +194,12 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
173
194
  * Time Complexity: O(1)
174
195
  * Space Complexity: O(1)
175
196
  *
176
- * @param index
197
+ * The `at` function returns the element at a specified index adjusted by an offset, or `undefined`
198
+ * if the index is out of bounds.
199
+ * @param {number} index - The `index` parameter represents the position of the element you want to
200
+ * retrieve from the data structure.
201
+ * @returns The `at` method is returning the element at the specified index adjusted by the offset
202
+ * `_offset`.
177
203
  */
178
204
  at(index: number): E | undefined {
179
205
  return this.elements[index + this._offset];
@@ -213,6 +239,9 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
213
239
  }
214
240
 
215
241
  /**
242
+ * Time Complexity: O(n)
243
+ * Space Complexity: O(1)
244
+ *
216
245
  * The `compact` function in TypeScript slices the elements array based on the offset and resets the
217
246
  * offset to zero.
218
247
  * @returns The `compact()` method is returning a boolean value of `true`.
@@ -265,6 +294,20 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
265
294
  /**
266
295
  * Time Complexity: O(n)
267
296
  * Space Complexity: O(n)
297
+ *
298
+ * The `map` function in TypeScript creates a new Queue by applying a callback function to each
299
+ * element in the original Queue.
300
+ * @param callback - The `callback` parameter is a function that will be applied to each element in
301
+ * the queue. It takes the current element, its index, and the queue itself as arguments, and returns
302
+ * a new element.
303
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be provided to
304
+ * convert a raw element of type `RM` to a new element of type `EM`. This function is used within the
305
+ * `map` method to transform each raw element before passing it to the `callback` function. If
306
+ * @param {any} [thisArg] - The `thisArg` parameter in the `map` function is used to specify the
307
+ * value of `this` when executing the `callback` function. It allows you to set the context (the
308
+ * value of `this`) within the callback function. If `thisArg` is provided, it will be
309
+ * @returns A new Queue object containing elements of type EM, which are the result of applying the
310
+ * callback function to each element in the original Queue object.
268
311
  */
269
312
  map<EM, RM>(
270
313
  callback: ElementCallback<E, R, EM, Queue<E, R>>,
@@ -19,15 +19,7 @@ import { IterableElementBase } from '../base';
19
19
  export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E, R>> {
20
20
  constructor(elements: Iterable<E> | Iterable<R> = [], options?: StackOptions<E, R>) {
21
21
  super(options);
22
- if (elements) {
23
- for (const el of elements) {
24
- if (this.toElementFn) {
25
- this.push(this.toElementFn(el as R));
26
- } else {
27
- this.push(el as E);
28
- }
29
- }
30
- }
22
+ this.pushMany(elements);
31
23
  }
32
24
 
33
25
  protected _elements: E[] = [];
@@ -48,11 +40,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
48
40
  return this.elements.length;
49
41
  }
50
42
 
51
- /**
52
- * Time Complexity: O(n)
53
- * Space Complexity: O(n)
54
- */
55
-
56
43
  /**
57
44
  * Time Complexity: O(n)
58
45
  * Space Complexity: O(n)
@@ -67,6 +54,9 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
67
54
  }
68
55
 
69
56
  /**
57
+ * Time Complexity: O(1)
58
+ * Space Complexity: O(1)
59
+ *
70
60
  * The function checks if an array is empty and returns a boolean value.
71
61
  * @returns A boolean value indicating whether the `_elements` array is empty or not.
72
62
  */
@@ -115,9 +105,36 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
115
105
  }
116
106
 
117
107
  /**
118
- * The delete function removes an element from the stack.
119
- * @param element: E Specify the element to be deleted
120
- * @return A boolean value indicating whether the element was successfully deleted or not
108
+ * Time Complexity: O(k)
109
+ * Space Complexity: O(1)
110
+ *
111
+ * The function `pushMany` iterates over elements and pushes them into an array after applying a
112
+ * transformation function if provided.
113
+ * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
114
+ * is an iterable containing elements of type `E` or `R`. The function iterates over each element in
115
+ * the iterable and pushes it into the data structure. If a transformation function `toElementFn` is
116
+ * provided, it is used to
117
+ * @returns The `pushMany` function is returning an array of boolean values indicating whether each
118
+ * element was successfully pushed into the data structure.
119
+ */
120
+ pushMany(elements: Iterable<E> | Iterable<R>) {
121
+ const ans: boolean[] = [];
122
+ for (const el of elements) {
123
+ if (this.toElementFn) {
124
+ ans.push(this.push(this.toElementFn(el as R)));
125
+ } else {
126
+ ans.push(this.push(el as E));
127
+ }
128
+ }
129
+ return ans;
130
+ }
131
+
132
+ /**
133
+ * Time Complexity: O(n)
134
+ * Space Complexity: O(1)
135
+ *
136
+ * The toArray function returns a copy of the elements in an array.
137
+ * @returns An array of type E.
121
138
  */
122
139
  delete(element: E): boolean {
123
140
  const index = this.elements.indexOf(element);
@@ -125,9 +142,11 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
125
142
  }
126
143
 
127
144
  /**
128
- * The deleteAt function deletes the element at a given index.
129
- * @param index: number Determine the index of the element to be deleted
130
- * @return A boolean value
145
+ * Time Complexity: O(n)
146
+ * Space Complexity: O(1)
147
+ *
148
+ * The toArray function returns a copy of the elements in an array.
149
+ * @returns An array of type E.
131
150
  */
132
151
  deleteAt(index: number): boolean {
133
152
  const spliced = this.elements.splice(index, 1);
@@ -268,7 +268,7 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
268
268
  * @returns The `addMany` method returns an array of boolean values indicating whether each word in
269
269
  * the input iterable was successfully added to the data structure.
270
270
  */
271
- addMany(words: Iterable<string> | Iterable<R> = []): boolean[] {
271
+ addMany(words: Iterable<string> | Iterable<R>): boolean[] {
272
272
  const ans: boolean[] = [];
273
273
  for (const word of words) {
274
274
  if (this.toElementFn) {
@@ -366,9 +366,14 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
366
366
  }
367
367
 
368
368
  /**
369
- * Time Complexity: O(n), where n is the total number of nodes in the trie.
370
- * Space Complexity: O(1) - Constant space.
369
+ * Time Complexity: O(n)
370
+ * Space Complexity: O(1)
371
371
  *
372
+ * The function `getHeight` calculates the height of a trie data structure starting from the root
373
+ * node.
374
+ * @returns The `getHeight` method returns the maximum depth or height of the trie tree starting from
375
+ * the root node. It calculates the depth using a breadth-first search (BFS) traversal of the trie
376
+ * tree and returns the maximum depth found.
372
377
  */
373
378
  getHeight(): number {
374
379
  const startNode = this.root;