heap-typed 1.54.2 → 2.0.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 (84) hide show
  1. package/dist/data-structures/base/iterable-element-base.d.ts +14 -40
  2. package/dist/data-structures/base/iterable-element-base.js +14 -11
  3. package/dist/data-structures/base/linear-base.d.ts +277 -0
  4. package/dist/data-structures/base/linear-base.js +552 -0
  5. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
  6. package/dist/data-structures/binary-tree/avl-tree-counter.js +8 -7
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +23 -19
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +51 -38
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +89 -21
  10. package/dist/data-structures/binary-tree/avl-tree.js +76 -8
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +173 -225
  12. package/dist/data-structures/binary-tree/binary-tree.js +244 -149
  13. package/dist/data-structures/binary-tree/bst.d.ts +62 -56
  14. package/dist/data-structures/binary-tree/bst.js +89 -133
  15. package/dist/data-structures/binary-tree/red-black-tree.d.ts +19 -25
  16. package/dist/data-structures/binary-tree/red-black-tree.js +7 -13
  17. package/dist/data-structures/binary-tree/tree-counter.d.ts +19 -19
  18. package/dist/data-structures/binary-tree/tree-counter.js +12 -12
  19. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +186 -25
  20. package/dist/data-structures/binary-tree/tree-multi-map.js +211 -41
  21. package/dist/data-structures/graph/abstract-graph.js +2 -2
  22. package/dist/data-structures/heap/heap.d.ts +3 -11
  23. package/dist/data-structures/heap/heap.js +0 -10
  24. package/dist/data-structures/heap/max-heap.d.ts +2 -2
  25. package/dist/data-structures/heap/min-heap.d.ts +2 -2
  26. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
  27. package/dist/data-structures/linked-list/doubly-linked-list.js +131 -146
  28. package/dist/data-structures/linked-list/singly-linked-list.d.ts +79 -75
  29. package/dist/data-structures/linked-list/singly-linked-list.js +217 -169
  30. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
  31. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
  32. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -2
  33. package/dist/data-structures/queue/deque.d.ts +130 -91
  34. package/dist/data-structures/queue/deque.js +269 -169
  35. package/dist/data-structures/queue/queue.d.ts +84 -40
  36. package/dist/data-structures/queue/queue.js +134 -50
  37. package/dist/data-structures/stack/stack.d.ts +3 -11
  38. package/dist/data-structures/stack/stack.js +0 -10
  39. package/dist/data-structures/trie/trie.d.ts +4 -3
  40. package/dist/data-structures/trie/trie.js +3 -0
  41. package/dist/types/data-structures/base/base.d.ts +9 -4
  42. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  43. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
  44. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  45. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  46. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  47. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  48. package/dist/types/data-structures/queue/deque.d.ts +2 -3
  49. package/dist/types/data-structures/queue/queue.d.ts +2 -2
  50. package/dist/utils/utils.d.ts +2 -2
  51. package/package.json +2 -2
  52. package/src/data-structures/base/iterable-element-base.ts +29 -20
  53. package/src/data-structures/base/linear-base.ts +649 -0
  54. package/src/data-structures/binary-tree/avl-tree-counter.ts +30 -23
  55. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +74 -49
  56. package/src/data-structures/binary-tree/avl-tree.ts +99 -29
  57. package/src/data-structures/binary-tree/binary-tree.ts +474 -257
  58. package/src/data-structures/binary-tree/bst.ts +150 -152
  59. package/src/data-structures/binary-tree/red-black-tree.ts +27 -35
  60. package/src/data-structures/binary-tree/tree-counter.ts +33 -27
  61. package/src/data-structures/binary-tree/tree-multi-map.ts +235 -53
  62. package/src/data-structures/graph/abstract-graph.ts +2 -2
  63. package/src/data-structures/heap/heap.ts +3 -14
  64. package/src/data-structures/heap/max-heap.ts +2 -2
  65. package/src/data-structures/heap/min-heap.ts +2 -2
  66. package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
  67. package/src/data-structures/linked-list/singly-linked-list.ts +241 -185
  68. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
  69. package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
  70. package/src/data-structures/priority-queue/priority-queue.ts +2 -2
  71. package/src/data-structures/queue/deque.ts +286 -183
  72. package/src/data-structures/queue/queue.ts +149 -63
  73. package/src/data-structures/stack/stack.ts +3 -18
  74. package/src/data-structures/trie/trie.ts +7 -3
  75. package/src/types/data-structures/base/base.ts +17 -8
  76. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  77. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -0
  78. package/src/types/data-structures/binary-tree/bst.ts +1 -1
  79. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
  80. package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
  81. package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
  82. package/src/types/data-structures/queue/deque.ts +2 -3
  83. package/src/types/data-structures/queue/queue.ts +2 -2
  84. package/src/utils/utils.ts +2 -2
@@ -6,75 +6,36 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type { ElementCallback, SinglyLinkedListOptions } from '../../types';
9
- import { IterableElementBase } from '../base';
10
- export declare class SinglyLinkedListNode<E = any> {
9
+ import { LinearLinkedBase, LinkedListNode } from '../base/linear-base';
10
+ export declare class SinglyLinkedListNode<E = any> extends LinkedListNode<E> {
11
11
  /**
12
12
  * The constructor function initializes an instance of a class with a given value and sets the next property to undefined.
13
13
  * @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
14
14
  * will be stored in the node of a linked list.
15
15
  */
16
16
  constructor(value: E);
17
- protected _value: E;
18
- /**
19
- * The function returns the value of a protected variable.
20
- * @returns The value of the variable `_value` is being returned.
21
- */
22
- get value(): E;
23
- /**
24
- * The above function sets the value of a variable.
25
- * @param {E} value - The parameter "value" is of type E, which means it can be any type.
26
- */
27
- set value(value: E);
28
17
  protected _next: SinglyLinkedListNode<E> | undefined;
29
- /**
30
- * The `next` function returns the next node in a singly linked list.
31
- * @returns The `next` property is being returned. It can be either a `SinglyLinkedListNode<E>`
32
- * object or `undefined`.
33
- */
34
18
  get next(): SinglyLinkedListNode<E> | undefined;
35
- /**
36
- * The "next" property of a SinglyLinkedListNode is set to the provided value.
37
- * @param {SinglyLinkedListNode<E> | undefined} value - The `value` parameter is of type
38
- * `SinglyLinkedListNode<E> | undefined`. This means that it can accept either a
39
- * `SinglyLinkedListNode` object or `undefined` as its value.
40
- */
41
19
  set next(value: SinglyLinkedListNode<E> | undefined);
42
20
  }
43
21
  /**
22
+ * 1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
23
+ * 2. Bidirectional Traversal: Unlike doubly linked lists, singly linked lists can be easily traversed forwards but not backwards.
24
+ * 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
25
+ * 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
26
+ * Caution: Although our linked list classes provide methods such as at, setAt, addAt, and indexOf that are based on array indices, their time complexity, like that of the native Array.lastIndexOf, is 𝑂(𝑛). If you need to use these methods frequently, you might want to consider other data structures, such as Deque or Queue (designed for random access). Similarly, since the native Array.shift method has a time complexity of 𝑂(𝑛), using an array to simulate a queue can be inefficient. In such cases, you should use Queue or Deque, as these data structures leverage deferred array rearrangement, effectively reducing the average time complexity to 𝑂(1).
44
27
  *
45
28
  */
46
- export declare class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R, SinglyLinkedList<E, R>> {
29
+ export declare class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, SinglyLinkedListNode<E>> {
47
30
  constructor(elements?: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>, options?: SinglyLinkedListOptions<E, R>);
48
31
  protected _head: SinglyLinkedListNode<E> | undefined;
49
- /**
50
- * The `head` function returns the first node of a singly linked list.
51
- * @returns The method is returning either a SinglyLinkedListNode object or undefined.
52
- */
53
32
  get head(): SinglyLinkedListNode<E> | undefined;
54
33
  protected _tail: SinglyLinkedListNode<E> | undefined;
55
- /**
56
- * The `tail` function returns the last node of a singly linked list.
57
- * @returns The method is returning either a SinglyLinkedListNode object or undefined.
58
- */
59
34
  get tail(): SinglyLinkedListNode<E> | undefined;
60
- /**
61
- * The above function returns the value of the first element in a linked list, or undefined if the
62
- * list is empty.
63
- * @returns The value of the first node in the linked list, or undefined if the linked list is empty.
64
- */
65
35
  get first(): E | undefined;
66
- /**
67
- * The function returns the value of the last element in a linked list, or undefined if the list is
68
- * empty.
69
- * @returns The value of the last node in the linked list, or undefined if the linked list is empty.
70
- */
71
36
  get last(): E | undefined;
72
- protected _size: number;
73
- /**
74
- * The function returns the size of an object.
75
- * @returns The size of the object, which is a number.
76
- */
77
- get size(): number;
37
+ protected _length: number;
38
+ get length(): number;
78
39
  /**
79
40
  * Time Complexity: O(n)
80
41
  * Space Complexity: O(n)
@@ -210,7 +171,7 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
210
171
  * @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
211
172
  * bounds.
212
173
  */
213
- deleteAt(index: number): boolean;
174
+ deleteAt(index: number): E | undefined;
214
175
  /**
215
176
  * Time Complexity: O(n)
216
177
  * Space Complexity: O(1)
@@ -238,6 +199,21 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
238
199
  * successfully added at the specified index, and `false` if the index is out of bounds.
239
200
  */
240
201
  addAt(index: number, newElementOrNode: E | SinglyLinkedListNode<E>): boolean;
202
+ /**
203
+ * Time Complexity: O(n)
204
+ * Space Complexity: O(1)
205
+ *
206
+ * The function setAt(index, value) updates the value at a specified index in a data structure if the
207
+ * index exists.
208
+ * @param {number} index - The `index` parameter in the `setAt` method refers to the position in the
209
+ * data structure where you want to set a new value.
210
+ * @param {E} value - The `value` parameter in the `setAt` method represents the new value that you
211
+ * want to set at the specified index in the data structure.
212
+ * @returns The `setAt` method returns a boolean value - `true` if the value at the specified index
213
+ * is successfully updated, and `false` if the index is out of bounds (i.e., the node at that index
214
+ * does not exist).
215
+ */
216
+ setAt(index: number, value: E): boolean;
241
217
  /**
242
218
  * Time Complexity: O(1)
243
219
  * Space Complexity: O(1)
@@ -254,14 +230,6 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
254
230
  * The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
255
231
  */
256
232
  clear(): void;
257
- /**
258
- * Time Complexity: O(n)
259
- * Space Complexity: O(n)
260
- *
261
- * The `toArray` function converts a linked list into an array.
262
- * @returns The `toArray()` method is returning an array of type `E[]`.
263
- */
264
- toArray(): E[];
265
233
  /**
266
234
  * Time Complexity: O(n)
267
235
  * Space Complexity: O(1)
@@ -270,20 +238,6 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
270
238
  * @returns The reverse() method does not return anything. It has a return type of void.
271
239
  */
272
240
  reverse(): this;
273
- /**
274
- * Time Complexity: O(n)
275
- * Space Complexity: O(1)
276
- *
277
- * The `indexOf` function in TypeScript searches for a specific element or node in a singly linked
278
- * list and returns its index if found.
279
- * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
280
- * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `indexOf` method can be one
281
- * of the following types:
282
- * @returns The `indexOf` method returns the index of the first occurrence of the element that
283
- * matches the provided predicate in the singly linked list. If no matching element is found, it
284
- * returns -1.
285
- */
286
- indexOf(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): number;
287
241
  /**
288
242
  * Time Complexity: O(n)
289
243
  * Space Complexity: O(1)
@@ -332,6 +286,26 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
332
286
  * was not found.
333
287
  */
334
288
  addAfter(existingElementOrNode: E | SinglyLinkedListNode<E>, newElementOrNode: E | SinglyLinkedListNode<E>): boolean;
289
+ /**
290
+ * Time Complexity: O(n)
291
+ * Space Complexity: O(1)
292
+ *
293
+ * The function `splice` in TypeScript overrides the default behavior to remove and insert elements
294
+ * in a singly linked list while handling boundary cases.
295
+ * @param {number} start - The `start` parameter in the `splice` method indicates the index at which
296
+ * to start modifying the list. It specifies the position where elements will be added or removed.
297
+ * @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
298
+ * number of elements to remove from the array starting at the specified `start` index. If
299
+ * `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
300
+ * elements can still be inserted at
301
+ * @param {E[]} items - The `items` parameter in the `splice` method represents the elements to be
302
+ * inserted into the list at the specified `start` index. These elements will be inserted in place of
303
+ * the elements that are removed from the list. The `splice` method allows you to add new elements to
304
+ * the list while
305
+ * @returns The `splice` method is returning a `SinglyLinkedList` containing the elements that were
306
+ * removed from the original list during the splice operation.
307
+ */
308
+ splice(start: number, deleteCount?: number, ...items: E[]): this;
335
309
  /**
336
310
  * Time Complexity: O(n)
337
311
  * Space Complexity: O(1)
@@ -353,7 +327,7 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
353
327
  * @returns The `clone()` method is returning a new instance of the `SinglyLinkedList` class, which
354
328
  * is a clone of the original list.
355
329
  */
356
- clone(): SinglyLinkedList<E, R>;
330
+ clone(): this;
357
331
  /**
358
332
  * Time Complexity: O(n)
359
333
  * Space Complexity: O(n)
@@ -371,7 +345,7 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
371
345
  * @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
372
346
  * elements that pass the filter condition specified by the `callback` function.
373
347
  */
374
- filter(callback: ElementCallback<E, R, boolean, SinglyLinkedList<E, R>>, thisArg?: any): SinglyLinkedList<E, R>;
348
+ filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): SinglyLinkedList<E, R>;
375
349
  /**
376
350
  * Time Complexity: O(n)
377
351
  * Space Complexity: O(n)
@@ -392,11 +366,31 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
392
366
  * value of
393
367
  * @returns a new instance of the `SinglyLinkedList` class with the mapped elements.
394
368
  */
395
- map<EM, RM>(callback: ElementCallback<E, R, EM, SinglyLinkedList<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): SinglyLinkedList<EM, RM>;
369
+ map<EM, RM>(callback: ElementCallback<E, R, EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): SinglyLinkedList<EM, RM>;
370
+ /**
371
+ * The function `_createInstance` returns a new instance of `SinglyLinkedList` with the specified
372
+ * options.
373
+ * @param [options] - The `options` parameter in the `_createInstance` method is of type
374
+ * `SinglyLinkedListOptions<E, R>`, which is used to configure the behavior of the `SinglyLinkedList`
375
+ * instance being created. It is an optional parameter, meaning it can be omitted when calling the
376
+ * method.
377
+ * @returns An instance of the `SinglyLinkedList` class with an empty array and the provided options
378
+ * is being returned.
379
+ */
380
+ protected _createInstance(options?: SinglyLinkedListOptions<E, R>): this;
396
381
  /**
397
382
  * The function `_getIterator` returns an iterable iterator that yields the values of a linked list.
398
383
  */
399
384
  protected _getIterator(): IterableIterator<E>;
385
+ /**
386
+ * The function returns an iterator that iterates over the elements of a collection in reverse order.
387
+ */
388
+ protected _getReverseIterator(): IterableIterator<E>;
389
+ /**
390
+ * The function `_getNodeIterator` returns an iterator that iterates over the nodes of a singly
391
+ * linked list.
392
+ */
393
+ protected _getNodeIterator(): IterableIterator<SinglyLinkedListNode<E>>;
400
394
  /**
401
395
  * The _isPredicate function in TypeScript checks if the input is a function that takes a
402
396
  * SinglyLinkedListNode as an argument and returns a boolean.
@@ -427,4 +421,14 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
427
421
  * a function is returned that checks if a given node's value is equal to the input
428
422
  */
429
423
  protected _ensurePredicate(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): (node: SinglyLinkedListNode<E>) => boolean;
424
+ /**
425
+ * The function `_getPrevNode` returns the node before a given node in a singly linked list.
426
+ * @param node - The `node` parameter in the `_getPrevNode` method is a reference to a node in a
427
+ * singly linked list. The method is used to find the node that comes before the given node in the
428
+ * linked list.
429
+ * @returns The `_getPrevNode` method returns either the previous node of the input node in a singly
430
+ * linked list or `undefined` if the input node is the head of the list or if the input node is not
431
+ * found in the list.
432
+ */
433
+ protected _getPrevNode(node: SinglyLinkedListNode<E>): SinglyLinkedListNode<E> | undefined;
430
434
  }