data-structure-typed 1.47.5 → 1.47.7

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 (169) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
  2. package/.github/workflows/ci.yml +1 -1
  3. package/.github/workflows/release-package.yml +1 -1
  4. package/CHANGELOG.md +1 -1
  5. package/CODE_OF_CONDUCT.md +32 -10
  6. package/COMMANDS.md +3 -1
  7. package/CONTRIBUTING.md +4 -3
  8. package/README.md +103 -28
  9. package/SECURITY.md +1 -1
  10. package/benchmark/report.html +46 -1
  11. package/benchmark/report.json +563 -8
  12. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +36 -18
  13. package/dist/cjs/data-structures/binary-tree/avl-tree.js +46 -29
  14. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +158 -129
  16. package/dist/cjs/data-structures/binary-tree/binary-tree.js +182 -184
  17. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/bst.d.ts +73 -63
  19. package/dist/cjs/data-structures/binary-tree/bst.js +168 -169
  20. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  21. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +54 -17
  22. package/dist/cjs/data-structures/binary-tree/rb-tree.js +77 -31
  23. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  24. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +29 -40
  25. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +66 -136
  26. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  27. package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
  28. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  29. package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
  30. package/dist/cjs/data-structures/hash/hash-map.js +5 -8
  31. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  32. package/dist/cjs/data-structures/heap/heap.d.ts +19 -21
  33. package/dist/cjs/data-structures/heap/heap.js +52 -34
  34. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  35. package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
  36. package/dist/cjs/data-structures/heap/max-heap.js +2 -2
  37. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  38. package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
  39. package/dist/cjs/data-structures/heap/min-heap.js +2 -2
  40. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  41. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  42. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +9 -1
  43. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  44. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  45. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +8 -1
  46. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  47. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  48. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
  49. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  50. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  51. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
  52. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  53. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
  54. package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
  55. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  56. package/dist/cjs/data-structures/queue/deque.d.ts +1 -0
  57. package/dist/cjs/data-structures/queue/deque.js +3 -0
  58. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  59. package/dist/cjs/data-structures/queue/queue.d.ts +1 -0
  60. package/dist/cjs/data-structures/queue/queue.js +3 -0
  61. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  62. package/dist/cjs/data-structures/stack/stack.d.ts +2 -1
  63. package/dist/cjs/data-structures/stack/stack.js +10 -2
  64. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  65. package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
  66. package/dist/cjs/data-structures/trie/trie.js +19 -4
  67. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  68. package/dist/cjs/interfaces/binary-tree.d.ts +4 -2
  69. package/dist/cjs/types/common.d.ts +7 -0
  70. package/dist/cjs/types/common.js.map +1 -1
  71. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  72. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  73. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
  74. package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
  75. package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  76. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +36 -18
  77. package/dist/mjs/data-structures/binary-tree/avl-tree.js +49 -30
  78. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +158 -129
  79. package/dist/mjs/data-structures/binary-tree/binary-tree.js +182 -194
  80. package/dist/mjs/data-structures/binary-tree/bst.d.ts +73 -63
  81. package/dist/mjs/data-structures/binary-tree/bst.js +171 -170
  82. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +54 -17
  83. package/dist/mjs/data-structures/binary-tree/rb-tree.js +81 -33
  84. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +29 -40
  85. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +70 -138
  86. package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
  87. package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
  88. package/dist/mjs/data-structures/hash/hash-map.js +5 -8
  89. package/dist/mjs/data-structures/heap/heap.d.ts +19 -21
  90. package/dist/mjs/data-structures/heap/heap.js +53 -35
  91. package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
  92. package/dist/mjs/data-structures/heap/max-heap.js +2 -2
  93. package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
  94. package/dist/mjs/data-structures/heap/min-heap.js +2 -2
  95. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  96. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +9 -1
  97. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  98. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +8 -1
  99. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  100. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
  101. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  102. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
  103. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
  104. package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
  105. package/dist/mjs/data-structures/queue/deque.d.ts +1 -0
  106. package/dist/mjs/data-structures/queue/deque.js +3 -0
  107. package/dist/mjs/data-structures/queue/queue.d.ts +1 -0
  108. package/dist/mjs/data-structures/queue/queue.js +3 -0
  109. package/dist/mjs/data-structures/stack/stack.d.ts +2 -1
  110. package/dist/mjs/data-structures/stack/stack.js +10 -2
  111. package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
  112. package/dist/mjs/data-structures/trie/trie.js +20 -4
  113. package/dist/mjs/interfaces/binary-tree.d.ts +4 -2
  114. package/dist/mjs/types/common.d.ts +7 -0
  115. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  116. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  117. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
  118. package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
  119. package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  120. package/dist/umd/data-structure-typed.js +629 -595
  121. package/dist/umd/data-structure-typed.min.js +2 -2
  122. package/dist/umd/data-structure-typed.min.js.map +1 -1
  123. package/package.json +1 -1
  124. package/src/data-structures/binary-tree/avl-tree.ts +61 -31
  125. package/src/data-structures/binary-tree/binary-tree.ts +283 -254
  126. package/src/data-structures/binary-tree/bst.ts +193 -170
  127. package/src/data-structures/binary-tree/rb-tree.ts +87 -32
  128. package/src/data-structures/binary-tree/tree-multimap.ts +76 -136
  129. package/src/data-structures/graph/abstract-graph.ts +1 -1
  130. package/src/data-structures/hash/hash-map.ts +8 -8
  131. package/src/data-structures/heap/heap.ts +57 -39
  132. package/src/data-structures/heap/max-heap.ts +5 -5
  133. package/src/data-structures/heap/min-heap.ts +5 -5
  134. package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
  135. package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
  136. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  137. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  138. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  139. package/src/data-structures/queue/deque.ts +4 -0
  140. package/src/data-structures/queue/queue.ts +4 -0
  141. package/src/data-structures/stack/stack.ts +12 -3
  142. package/src/data-structures/trie/trie.ts +23 -4
  143. package/src/interfaces/binary-tree.ts +14 -2
  144. package/src/types/common.ts +15 -1
  145. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  146. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  147. package/src/types/data-structures/hash/hash-map.ts +1 -2
  148. package/src/types/data-structures/heap/heap.ts +3 -1
  149. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
  150. package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
  151. package/test/integration/index.html +87 -0
  152. package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
  153. package/test/performance/data-structures/heap/heap.test.ts +2 -2
  154. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
  155. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +20 -20
  156. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -57
  157. package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
  158. package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
  159. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
  160. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
  161. package/test/unit/data-structures/heap/heap.test.ts +2 -2
  162. package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
  163. package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
  164. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
  165. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
  166. package/test/unit/data-structures/stack/stack.test.ts +2 -2
  167. package/test/unit/unrestricted-interconversion.test.ts +156 -0
  168. package/tsconfig-cjs.json +1 -1
  169. package/test/integration/conversion.test.ts +0 -0
@@ -589,7 +589,7 @@ export class AbstractGraph {
589
589
  if (vertexOrKey instanceof AbstractVertex)
590
590
  distMap.set(vertexOrKey, Infinity);
591
591
  }
592
- const heap = new PriorityQueue({ comparator: (a, b) => a.key - b.key });
592
+ const heap = new PriorityQueue([], { comparator: (a, b) => a.key - b.key });
593
593
  heap.add({ key: 0, value: srcVertex });
594
594
  distMap.set(srcVertex, 0);
595
595
  preMap.set(srcVertex, null);
@@ -14,12 +14,7 @@ export declare class HashMap<K = any, V = any> {
14
14
  protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
15
15
  protected _hashFn: (key: K) => string;
16
16
  protected _objHashFn: (key: K) => object;
17
- /**
18
- * The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
19
- * @param options - The `options` parameter is an object that contains the `elements` property. The
20
- * `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
21
- */
22
- constructor(options?: HashMapOptions<K, V>);
17
+ constructor(elements?: Iterable<[K, V]>, options?: HashMapOptions<K>);
23
18
  protected _size: number;
24
19
  get size(): number;
25
20
  /**
@@ -172,6 +167,7 @@ export declare class HashMap<K = any, V = any> {
172
167
  * The above function is an iterator that yields key-value pairs from a linked list.
173
168
  */
174
169
  [Symbol.iterator](): Generator<[K, V], void, unknown>;
170
+ print(): void;
175
171
  /**
176
172
  * Time Complexity: O(1)
177
173
  * Space Complexity: O(1)
@@ -14,19 +14,13 @@ export class HashMap {
14
14
  _sentinel;
15
15
  _hashFn;
16
16
  _objHashFn;
17
- /**
18
- * The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
19
- * @param options - The `options` parameter is an object that contains the `elements` property. The
20
- * `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
21
- */
22
- constructor(options = {
23
- elements: [],
17
+ constructor(elements, options = {
24
18
  hashFn: (key) => String(key),
25
19
  objHashFn: (key) => key
26
20
  }) {
27
21
  this._sentinel = {};
28
22
  this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
29
- const { elements, hashFn, objHashFn } = options;
23
+ const { hashFn, objHashFn } = options;
30
24
  this._hashFn = hashFn;
31
25
  this._objHashFn = objHashFn;
32
26
  if (elements) {
@@ -349,6 +343,9 @@ export class HashMap {
349
343
  node = node.next;
350
344
  }
351
345
  }
346
+ print() {
347
+ console.log([...this]);
348
+ }
352
349
  /**
353
350
  * Time Complexity: O(1)
354
351
  * Space Complexity: O(1)
@@ -5,15 +5,12 @@
5
5
  * @license MIT License
6
6
  */
7
7
  import type { Comparator, DFSOrderPattern } from '../../types';
8
+ import { HeapOptions } from "../../types";
8
9
  export declare class Heap<E = any> {
9
- constructor(options: {
10
- comparator: Comparator<E>;
11
- elements?: E[];
12
- });
10
+ options: HeapOptions<E>;
11
+ constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
13
12
  protected _elements: E[];
14
13
  get elements(): E[];
15
- protected _comparator: Comparator<E>;
16
- get comparator(): Comparator<E>;
17
14
  /**
18
15
  * Get the size (number of elements) of the heap.
19
16
  */
@@ -26,10 +23,10 @@ export declare class Heap<E = any> {
26
23
  /**
27
24
  * Static method that creates a binary heap from an array of elements and a comparison function.
28
25
  * @returns A new Heap instance.
26
+ * @param elements
29
27
  * @param options
30
28
  */
31
- static heapify<E>(options: {
32
- elements: E[];
29
+ static heapify<E>(elements: Iterable<E>, options: {
33
30
  comparator: Comparator<E>;
34
31
  }): Heap<E>;
35
32
  /**
@@ -204,6 +201,11 @@ export declare class Heap<E = any> {
204
201
  * Time Complexity: O(log n)
205
202
  * Space Complexity: O(1)
206
203
  */
204
+ print(): void;
205
+ /**
206
+ * Time Complexity: O(n)
207
+ * Space Complexity: O(1)
208
+ */
207
209
  /**
208
210
  * Time Complexity: O(log n)
209
211
  * Space Complexity: O(1)
@@ -212,10 +214,6 @@ export declare class Heap<E = any> {
212
214
  * @param index - The index of the newly added element.
213
215
  */
214
216
  protected _bubbleUp(index: number): void;
215
- /**
216
- * Time Complexity: O(n)
217
- * Space Complexity: O(1)
218
- */
219
217
  /**
220
218
  * Time Complexity: O(log n)
221
219
  * Space Complexity: O(1)
@@ -349,18 +347,18 @@ export declare class FibonacciHeap<E> {
349
347
  */
350
348
  merge(heapToMerge: FibonacciHeap<E>): void;
351
349
  /**
352
- * Default comparator function used by the heap.
353
- * @param {E} a
354
- * @param {E} b
350
+ * Create a new node.
351
+ * @param element
355
352
  * @protected
356
353
  */
357
- protected defaultComparator(a: E, b: E): number;
354
+ createNode(element: E): FibonacciHeapNode<E>;
358
355
  /**
359
- * Create a new node.
360
- * @param element
356
+ * Default comparator function used by the heap.
357
+ * @param {E} a
358
+ * @param {E} b
361
359
  * @protected
362
360
  */
363
- protected createNode(element: E): FibonacciHeapNode<E>;
361
+ protected _defaultComparator(a: E, b: E): number;
364
362
  /**
365
363
  * Time Complexity: O(1)
366
364
  * Space Complexity: O(1)
@@ -399,7 +397,7 @@ export declare class FibonacciHeap<E> {
399
397
  * @param x
400
398
  * @protected
401
399
  */
402
- protected link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
400
+ protected _link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
403
401
  /**
404
402
  * Time Complexity: O(n log n), where n is the number of elements in the heap.
405
403
  * Space Complexity: O(n)
@@ -411,5 +409,5 @@ export declare class FibonacciHeap<E> {
411
409
  * Remove and return the top element (smallest or largest element) from the heap.
412
410
  * @protected
413
411
  */
414
- protected consolidate(): void;
412
+ protected _consolidate(): void;
415
413
  }
@@ -5,21 +5,35 @@
5
5
  * @license MIT License
6
6
  */
7
7
  export class Heap {
8
- constructor(options) {
9
- this._comparator = options.comparator;
10
- if (options.elements && options.elements.length > 0) {
11
- this._elements = options.elements;
12
- this.fix();
8
+ options;
9
+ constructor(elements, options) {
10
+ const defaultComparator = (a, b) => {
11
+ if (!(typeof a === 'number' && typeof b === 'number')) {
12
+ throw new Error('The a, b params of compare function must be number');
13
+ }
14
+ else {
15
+ return a - b;
16
+ }
17
+ };
18
+ if (options) {
19
+ this.options = options;
20
+ }
21
+ else {
22
+ this.options = {
23
+ comparator: defaultComparator
24
+ };
25
+ }
26
+ if (elements) {
27
+ for (const el of elements) {
28
+ this.push(el);
29
+ }
30
+ // this.fix();
13
31
  }
14
32
  }
15
33
  _elements = [];
16
34
  get elements() {
17
35
  return this._elements;
18
36
  }
19
- _comparator;
20
- get comparator() {
21
- return this._comparator;
22
- }
23
37
  /**
24
38
  * Get the size (number of elements) of the heap.
25
39
  */
@@ -36,10 +50,11 @@ export class Heap {
36
50
  /**
37
51
  * Static method that creates a binary heap from an array of elements and a comparison function.
38
52
  * @returns A new Heap instance.
53
+ * @param elements
39
54
  * @param options
40
55
  */
41
- static heapify(options) {
42
- return new Heap(options);
56
+ static heapify(elements, options) {
57
+ return new Heap(elements, options);
43
58
  }
44
59
  /**
45
60
  * Time Complexity: O(log n), where n is the number of elements in the heap.
@@ -253,7 +268,7 @@ export class Heap {
253
268
  * @returns A new Heap instance containing the same elements.
254
269
  */
255
270
  clone() {
256
- const clonedHeap = new Heap({ comparator: this.comparator });
271
+ const clonedHeap = new Heap([], this.options);
257
272
  clonedHeap._elements = [...this.elements];
258
273
  return clonedHeap;
259
274
  }
@@ -305,7 +320,7 @@ export class Heap {
305
320
  }
306
321
  }
307
322
  filter(predicate) {
308
- const filteredHeap = new Heap({ comparator: this.comparator });
323
+ const filteredHeap = new Heap([], this.options);
309
324
  let index = 0;
310
325
  for (const el of this) {
311
326
  if (predicate(el, index, this)) {
@@ -316,7 +331,7 @@ export class Heap {
316
331
  return filteredHeap;
317
332
  }
318
333
  map(callback, comparator) {
319
- const mappedHeap = new Heap({ comparator: comparator });
334
+ const mappedHeap = new Heap([], { comparator: comparator });
320
335
  let index = 0;
321
336
  for (const el of this) {
322
337
  mappedHeap.add(callback(el, index, this));
@@ -337,6 +352,13 @@ export class Heap {
337
352
  * Time Complexity: O(log n)
338
353
  * Space Complexity: O(1)
339
354
  */
355
+ print() {
356
+ console.log([...this]);
357
+ }
358
+ /**
359
+ * Time Complexity: O(n)
360
+ * Space Complexity: O(1)
361
+ */
340
362
  /**
341
363
  * Time Complexity: O(log n)
342
364
  * Space Complexity: O(1)
@@ -349,17 +371,13 @@ export class Heap {
349
371
  while (index > 0) {
350
372
  const parent = (index - 1) >> 1;
351
373
  const parentItem = this.elements[parent];
352
- if (this._comparator(parentItem, element) <= 0)
374
+ if (this.options.comparator(parentItem, element) <= 0)
353
375
  break;
354
376
  this.elements[index] = parentItem;
355
377
  index = parent;
356
378
  }
357
379
  this.elements[index] = element;
358
380
  }
359
- /**
360
- * Time Complexity: O(n)
361
- * Space Complexity: O(1)
362
- */
363
381
  /**
364
382
  * Time Complexity: O(log n)
365
383
  * Space Complexity: O(1)
@@ -375,11 +393,11 @@ export class Heap {
375
393
  const right = left + 1;
376
394
  let minItem = this.elements[left];
377
395
  if (right < this.elements.length &&
378
- this._comparator(minItem, this.elements[right]) > 0) {
396
+ this.options.comparator(minItem, this.elements[right]) > 0) {
379
397
  left = right;
380
398
  minItem = this.elements[right];
381
399
  }
382
- if (this._comparator(minItem, element) >= 0)
400
+ if (this.options.comparator(minItem, element) >= 0)
383
401
  break;
384
402
  this.elements[index] = minItem;
385
403
  index = left;
@@ -404,7 +422,7 @@ export class FibonacciHeapNode {
404
422
  export class FibonacciHeap {
405
423
  constructor(comparator) {
406
424
  this.clear();
407
- this._comparator = comparator || this.defaultComparator;
425
+ this._comparator = comparator || this._defaultComparator;
408
426
  if (typeof this.comparator !== 'function') {
409
427
  throw new Error('FibonacciHeap constructor: given comparator should be a function.');
410
428
  }
@@ -579,7 +597,7 @@ export class FibonacciHeap {
579
597
  }
580
598
  else {
581
599
  this._min = z.right;
582
- this.consolidate();
600
+ this._consolidate();
583
601
  }
584
602
  this._size--;
585
603
  return z.element;
@@ -619,27 +637,27 @@ export class FibonacciHeap {
619
637
  // Clear the heap that was merged
620
638
  heapToMerge.clear();
621
639
  }
640
+ /**
641
+ * Create a new node.
642
+ * @param element
643
+ * @protected
644
+ */
645
+ createNode(element) {
646
+ return new FibonacciHeapNode(element);
647
+ }
622
648
  /**
623
649
  * Default comparator function used by the heap.
624
650
  * @param {E} a
625
651
  * @param {E} b
626
652
  * @protected
627
653
  */
628
- defaultComparator(a, b) {
654
+ _defaultComparator(a, b) {
629
655
  if (a < b)
630
656
  return -1;
631
657
  if (a > b)
632
658
  return 1;
633
659
  return 0;
634
660
  }
635
- /**
636
- * Create a new node.
637
- * @param element
638
- * @protected
639
- */
640
- createNode(element) {
641
- return new FibonacciHeapNode(element);
642
- }
643
661
  /**
644
662
  * Time Complexity: O(1)
645
663
  * Space Complexity: O(1)
@@ -695,7 +713,7 @@ export class FibonacciHeap {
695
713
  * @param x
696
714
  * @protected
697
715
  */
698
- link(y, x) {
716
+ _link(y, x) {
699
717
  this.removeFromRoot(y);
700
718
  y.left = y;
701
719
  y.right = y;
@@ -714,7 +732,7 @@ export class FibonacciHeap {
714
732
  * Remove and return the top element (smallest or largest element) from the heap.
715
733
  * @protected
716
734
  */
717
- consolidate() {
735
+ _consolidate() {
718
736
  const A = new Array(this.size);
719
737
  const elements = this.consumeLinkedList(this.root);
720
738
  let x, y, d, t;
@@ -728,7 +746,7 @@ export class FibonacciHeap {
728
746
  x = y;
729
747
  y = t;
730
748
  }
731
- this.link(y, x);
749
+ this._link(y, x);
732
750
  A[d] = undefined;
733
751
  d++;
734
752
  }
@@ -6,10 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { Heap } from './heap';
9
- import type { Comparator } from '../../types';
9
+ import type { HeapOptions } from '../../types';
10
10
  export declare class MaxHeap<E = any> extends Heap<E> {
11
- constructor(options?: {
12
- comparator: Comparator<E>;
13
- nodes?: E[];
14
- });
11
+ constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
15
12
  }
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import { Heap } from './heap';
9
9
  export class MaxHeap extends Heap {
10
- constructor(options = {
10
+ constructor(elements, options = {
11
11
  comparator: (a, b) => {
12
12
  if (!(typeof a === 'number' && typeof b === 'number')) {
13
13
  throw new Error('The a, b params of compare function must be number');
@@ -17,6 +17,6 @@ export class MaxHeap extends Heap {
17
17
  }
18
18
  }
19
19
  }) {
20
- super(options);
20
+ super(elements, options);
21
21
  }
22
22
  }
@@ -6,10 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { Heap } from './heap';
9
- import type { Comparator } from '../../types';
9
+ import type { HeapOptions } from '../../types';
10
10
  export declare class MinHeap<E = any> extends Heap<E> {
11
- constructor(options?: {
12
- comparator: Comparator<E>;
13
- nodes?: E[];
14
- });
11
+ constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
15
12
  }
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import { Heap } from './heap';
9
9
  export class MinHeap extends Heap {
10
- constructor(options = {
10
+ constructor(elements, options = {
11
11
  comparator: (a, b) => {
12
12
  if (!(typeof a === 'number' && typeof b === 'number')) {
13
13
  throw new Error('The a, b params of compare function must be number');
@@ -17,6 +17,6 @@ export class MinHeap extends Heap {
17
17
  }
18
18
  }
19
19
  }) {
20
- super(options);
20
+ super(elements, options);
21
21
  }
22
22
  }
@@ -20,7 +20,7 @@ export declare class DoublyLinkedList<E = any> {
20
20
  /**
21
21
  * The constructor initializes the linked list with an empty head, tail, and length.
22
22
  */
23
- constructor();
23
+ constructor(elements?: Iterable<E>);
24
24
  protected _head: DoublyLinkedListNode<E> | null;
25
25
  get head(): DoublyLinkedListNode<E> | null;
26
26
  protected _tail: DoublyLinkedListNode<E> | null;
@@ -453,4 +453,5 @@ export declare class DoublyLinkedList<E = any> {
453
453
  * elements in the linked list.
454
454
  */
455
455
  reduce<T>(callback: (accumulator: T, value: E, index: number, list: DoublyLinkedList<E>) => T, initialValue: T): T;
456
+ print(): void;
456
457
  }
@@ -24,10 +24,15 @@ export class DoublyLinkedList {
24
24
  /**
25
25
  * The constructor initializes the linked list with an empty head, tail, and length.
26
26
  */
27
- constructor() {
27
+ constructor(elements) {
28
28
  this._head = null;
29
29
  this._tail = null;
30
30
  this._length = 0;
31
+ if (elements) {
32
+ for (const el of elements) {
33
+ this.push(el);
34
+ }
35
+ }
31
36
  }
32
37
  _head;
33
38
  get head() {
@@ -766,4 +771,7 @@ export class DoublyLinkedList {
766
771
  }
767
772
  return accumulator;
768
773
  }
774
+ print() {
775
+ console.log([...this]);
776
+ }
769
777
  }
@@ -19,7 +19,7 @@ export declare class SinglyLinkedList<E = any> {
19
19
  /**
20
20
  * The constructor initializes the linked list with an empty head, tail, and length.
21
21
  */
22
- constructor();
22
+ constructor(elements?: Iterable<E>);
23
23
  protected _head: SinglyLinkedListNode<E> | null;
24
24
  get head(): SinglyLinkedListNode<E> | null;
25
25
  protected _tail: SinglyLinkedListNode<E> | null;
@@ -411,4 +411,5 @@ export declare class SinglyLinkedList<E = any> {
411
411
  * elements in the linked list.
412
412
  */
413
413
  reduce<T>(callback: (accumulator: T, value: E, index: number, list: SinglyLinkedList<E>) => T, initialValue: T): T;
414
+ print(): void;
414
415
  }
@@ -22,10 +22,14 @@ export class SinglyLinkedList {
22
22
  /**
23
23
  * The constructor initializes the linked list with an empty head, tail, and length.
24
24
  */
25
- constructor() {
25
+ constructor(elements) {
26
26
  this._head = null;
27
27
  this._tail = null;
28
28
  this._length = 0;
29
+ if (elements) {
30
+ for (const el of elements)
31
+ this.push(el);
32
+ }
29
33
  }
30
34
  _head;
31
35
  get head() {
@@ -710,4 +714,7 @@ export class SinglyLinkedList {
710
714
  }
711
715
  return accumulator;
712
716
  }
717
+ print() {
718
+ console.log([...this]);
719
+ }
713
720
  }
@@ -6,10 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { PriorityQueue } from './priority-queue';
9
- import type { Comparator } from '../../types';
9
+ import type { PriorityQueueOptions } from '../../types';
10
10
  export declare class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
11
- constructor(options?: {
12
- comparator: Comparator<E>;
13
- nodes?: E[];
14
- });
11
+ constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
15
12
  }
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import { PriorityQueue } from './priority-queue';
9
9
  export class MaxPriorityQueue extends PriorityQueue {
10
- constructor(options = {
10
+ constructor(elements, options = {
11
11
  comparator: (a, b) => {
12
12
  if (!(typeof a === 'number' && typeof b === 'number')) {
13
13
  throw new Error('The a, b params of compare function must be number');
@@ -17,6 +17,6 @@ export class MaxPriorityQueue extends PriorityQueue {
17
17
  }
18
18
  }
19
19
  }) {
20
- super(options);
20
+ super(elements, options);
21
21
  }
22
22
  }
@@ -6,10 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { PriorityQueue } from './priority-queue';
9
- import type { Comparator } from '../../types';
9
+ import type { PriorityQueueOptions } from '../../types';
10
10
  export declare class MinPriorityQueue<E = any> extends PriorityQueue<E> {
11
- constructor(options?: {
12
- comparator: Comparator<E>;
13
- nodes?: E[];
14
- });
11
+ constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
15
12
  }
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import { PriorityQueue } from './priority-queue';
9
9
  export class MinPriorityQueue extends PriorityQueue {
10
- constructor(options = {
10
+ constructor(elements, options = {
11
11
  comparator: (a, b) => {
12
12
  if (!(typeof a === 'number' && typeof b === 'number')) {
13
13
  throw new Error('The a, b params of compare function must be number');
@@ -17,6 +17,6 @@ export class MinPriorityQueue extends PriorityQueue {
17
17
  }
18
18
  }
19
19
  }) {
20
- super(options);
20
+ super(elements, options);
21
21
  }
22
22
  }
@@ -6,10 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { Heap } from '../heap';
9
- import { Comparator } from '../../types';
9
+ import { PriorityQueueOptions } from '../../types';
10
10
  export declare class PriorityQueue<E = any> extends Heap<E> {
11
- constructor(options: {
12
- comparator: Comparator<E>;
13
- nodes?: E[];
14
- });
11
+ constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
15
12
  }
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import { Heap } from '../heap';
9
9
  export class PriorityQueue extends Heap {
10
- constructor(options) {
11
- super(options);
10
+ constructor(elements, options) {
11
+ super(elements, options);
12
12
  }
13
13
  }
@@ -428,6 +428,7 @@ export declare class Deque<E> {
428
428
  * applying the callback function to each element.
429
429
  */
430
430
  reduce<T>(callback: (accumulator: T, element: E, index: number, deque: this) => T, initialValue: T): T;
431
+ print(): void;
431
432
  /**
432
433
  * Time Complexity: O(n)
433
434
  * Space Complexity: O(n)
@@ -759,6 +759,9 @@ export class Deque {
759
759
  }
760
760
  return accumulator;
761
761
  }
762
+ print() {
763
+ console.log([...this]);
764
+ }
762
765
  /**
763
766
  * Time Complexity: O(n)
764
767
  * Space Complexity: O(n)
@@ -205,6 +205,7 @@ 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>;
209
210
  /**
210
211
  * Time Complexity: O(n)
@@ -262,6 +262,9 @@ 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;
@@ -10,7 +10,7 @@ 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
  /**
@@ -116,4 +116,5 @@ export declare class Stack<E = any> {
116
116
  filter(predicate: (element: E, index: number, stack: this) => boolean): Stack<E>;
117
117
  map<T>(callback: (element: E, index: number, stack: this) => T): Stack<T>;
118
118
  reduce<T>(callback: (accumulator: T, element: E, index: number, stack: this) => T, initialValue: T): T;
119
+ print(): void;
119
120
  }