data-structure-typed 1.47.4 → 1.47.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/benchmark/report.html +2 -2
  3. package/benchmark/report.json +12 -18
  4. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
  5. package/dist/cjs/data-structures/binary-tree/avl-tree.js +23 -15
  6. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.js +66 -82
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  10. package/dist/cjs/data-structures/binary-tree/bst.d.ts +38 -37
  11. package/dist/cjs/data-structures/binary-tree/bst.js +56 -40
  12. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.js +26 -17
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
  17. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +31 -22
  18. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  19. package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
  20. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  21. package/dist/cjs/data-structures/hash/hash-map.d.ts +3 -3
  22. package/dist/cjs/data-structures/hash/hash-map.js +10 -4
  23. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  24. package/dist/cjs/data-structures/hash/hash-table.d.ts +9 -4
  25. package/dist/cjs/data-structures/hash/hash-table.js +50 -5
  26. package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
  27. package/dist/cjs/data-structures/heap/heap.d.ts +25 -22
  28. package/dist/cjs/data-structures/heap/heap.js +101 -41
  29. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  30. package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
  31. package/dist/cjs/data-structures/heap/max-heap.js +2 -2
  32. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  33. package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
  34. package/dist/cjs/data-structures/heap/min-heap.js +2 -2
  35. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  36. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
  37. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +125 -119
  38. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  39. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +38 -37
  40. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +65 -60
  41. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  42. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  43. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
  44. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  45. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  46. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
  47. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  48. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
  49. package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
  50. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  51. package/dist/cjs/data-structures/queue/deque.d.ts +50 -49
  52. package/dist/cjs/data-structures/queue/deque.js +81 -71
  53. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  54. package/dist/cjs/data-structures/queue/queue.d.ts +46 -0
  55. package/dist/cjs/data-structures/queue/queue.js +80 -0
  56. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  57. package/dist/cjs/data-structures/stack/stack.d.ts +20 -6
  58. package/dist/cjs/data-structures/stack/stack.js +65 -8
  59. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  60. package/dist/cjs/data-structures/trie/trie.d.ts +5 -0
  61. package/dist/cjs/data-structures/trie/trie.js +47 -0
  62. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  63. package/dist/cjs/interfaces/binary-tree.d.ts +3 -1
  64. package/dist/cjs/types/common.d.ts +2 -0
  65. package/dist/cjs/types/common.js.map +1 -1
  66. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  67. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  68. package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
  69. package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  70. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
  71. package/dist/mjs/data-structures/binary-tree/avl-tree.js +26 -16
  72. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
  73. package/dist/mjs/data-structures/binary-tree/binary-tree.js +66 -83
  74. package/dist/mjs/data-structures/binary-tree/bst.d.ts +38 -37
  75. package/dist/mjs/data-structures/binary-tree/bst.js +59 -41
  76. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
  77. package/dist/mjs/data-structures/binary-tree/rb-tree.js +30 -19
  78. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
  79. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +34 -23
  80. package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
  81. package/dist/mjs/data-structures/hash/hash-map.d.ts +3 -3
  82. package/dist/mjs/data-structures/hash/hash-map.js +10 -4
  83. package/dist/mjs/data-structures/hash/hash-table.d.ts +9 -4
  84. package/dist/mjs/data-structures/hash/hash-table.js +50 -5
  85. package/dist/mjs/data-structures/heap/heap.d.ts +25 -22
  86. package/dist/mjs/data-structures/heap/heap.js +102 -42
  87. package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
  88. package/dist/mjs/data-structures/heap/max-heap.js +2 -2
  89. package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
  90. package/dist/mjs/data-structures/heap/min-heap.js +2 -2
  91. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
  92. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +125 -119
  93. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +38 -37
  94. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +65 -60
  95. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  96. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
  97. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  98. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
  99. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
  100. package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
  101. package/dist/mjs/data-structures/queue/deque.d.ts +50 -49
  102. package/dist/mjs/data-structures/queue/deque.js +81 -71
  103. package/dist/mjs/data-structures/queue/queue.d.ts +46 -0
  104. package/dist/mjs/data-structures/queue/queue.js +80 -0
  105. package/dist/mjs/data-structures/stack/stack.d.ts +20 -6
  106. package/dist/mjs/data-structures/stack/stack.js +65 -8
  107. package/dist/mjs/data-structures/trie/trie.d.ts +5 -0
  108. package/dist/mjs/data-structures/trie/trie.js +47 -0
  109. package/dist/mjs/interfaces/binary-tree.d.ts +3 -1
  110. package/dist/mjs/types/common.d.ts +2 -0
  111. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  112. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  113. package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
  114. package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  115. package/dist/umd/data-structure-typed.js +856 -509
  116. package/dist/umd/data-structure-typed.min.js +2 -2
  117. package/dist/umd/data-structure-typed.min.js.map +1 -1
  118. package/package.json +1 -1
  119. package/src/data-structures/binary-tree/avl-tree.ts +27 -17
  120. package/src/data-structures/binary-tree/binary-tree.ts +114 -97
  121. package/src/data-structures/binary-tree/bst.ts +67 -47
  122. package/src/data-structures/binary-tree/rb-tree.ts +34 -20
  123. package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
  124. package/src/data-structures/graph/abstract-graph.ts +1 -1
  125. package/src/data-structures/hash/hash-map.ts +13 -7
  126. package/src/data-structures/hash/hash-table.ts +59 -9
  127. package/src/data-structures/heap/heap.ts +115 -46
  128. package/src/data-structures/heap/max-heap.ts +5 -5
  129. package/src/data-structures/heap/min-heap.ts +5 -5
  130. package/src/data-structures/linked-list/doubly-linked-list.ts +137 -128
  131. package/src/data-structures/linked-list/singly-linked-list.ts +72 -64
  132. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  133. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  134. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  135. package/src/data-structures/queue/deque.ts +86 -75
  136. package/src/data-structures/queue/queue.ts +88 -0
  137. package/src/data-structures/stack/stack.ts +75 -10
  138. package/src/data-structures/trie/trie.ts +53 -0
  139. package/src/interfaces/binary-tree.ts +13 -1
  140. package/src/types/common.ts +5 -1
  141. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  142. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  143. package/src/types/data-structures/heap/heap.ts +3 -1
  144. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
  145. package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
  146. package/test/performance/data-structures/heap/heap.test.ts +14 -5
  147. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
  148. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
  149. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -6
  150. package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
  151. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +1 -1
  152. package/test/unit/data-structures/hash/hash-table.test.ts +58 -2
  153. package/test/unit/data-structures/heap/heap.test.ts +2 -2
  154. package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
  155. package/test/unit/data-structures/heap/min-heap.test.ts +49 -1
  156. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
  157. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
  158. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
  159. package/test/unit/data-structures/queue/queue.test.ts +37 -0
  160. package/test/unit/data-structures/stack/stack.test.ts +55 -5
  161. package/test/unit/data-structures/trie/trie.test.ts +33 -0
  162. package/test/unit/unrestricted-interconversion.test.ts +100 -0
@@ -289,10 +289,12 @@ export class HashMap {
289
289
  */
290
290
  filter(predicate) {
291
291
  const filteredMap = new HashMap();
292
+ let index = 0;
292
293
  for (const [key, value] of this) {
293
- if (predicate([key, value], this)) {
294
+ if (predicate([key, value], index, this)) {
294
295
  filteredMap.set(key, value);
295
296
  }
297
+ index++;
296
298
  }
297
299
  return filteredMap;
298
300
  }
@@ -305,9 +307,11 @@ export class HashMap {
305
307
  */
306
308
  map(callback) {
307
309
  const mappedMap = new HashMap();
310
+ let index = 0;
308
311
  for (const [key, value] of this) {
309
- const newValue = callback([key, value], this);
312
+ const newValue = callback([key, value], index, this);
310
313
  mappedMap.set(key, newValue);
314
+ index++;
311
315
  }
312
316
  return mappedMap;
313
317
  }
@@ -325,8 +329,10 @@ export class HashMap {
325
329
  */
326
330
  reduce(callback, initialValue) {
327
331
  let accumulator = initialValue;
328
- for (const element of this) {
329
- accumulator = callback(accumulator, element, this);
332
+ let index = 0;
333
+ for (const entry of this) {
334
+ accumulator = callback(accumulator, entry, index, this);
335
+ index++;
330
336
  }
331
337
  return accumulator;
332
338
  }
@@ -8,11 +8,11 @@
8
8
  export declare class HashTableNode<K, V> {
9
9
  key: K;
10
10
  value: V;
11
- next: HashTableNode<K, V> | null;
11
+ next: HashTableNode<K, V> | undefined;
12
12
  constructor(key: K, value: V);
13
13
  }
14
14
  import { HashFunction } from '../../types';
15
- export declare class HashTable<K, V> {
15
+ export declare class HashTable<K = any, V = any> {
16
16
  protected static readonly DEFAULT_CAPACITY = 16;
17
17
  protected static readonly LOAD_FACTOR = 0.75;
18
18
  constructor(capacity?: number, hashFn?: HashFunction<K>);
@@ -20,8 +20,8 @@ export declare class HashTable<K, V> {
20
20
  get capacity(): number;
21
21
  protected _size: number;
22
22
  get size(): number;
23
- protected _buckets: Array<HashTableNode<K, V> | null>;
24
- get buckets(): Array<HashTableNode<K, V> | null>;
23
+ protected _buckets: Array<HashTableNode<K, V> | undefined>;
24
+ get buckets(): Array<HashTableNode<K, V> | undefined>;
25
25
  protected _hashFn: HashFunction<K>;
26
26
  get hashFn(): HashFunction<K>;
27
27
  /**
@@ -50,6 +50,11 @@ export declare class HashTable<K, V> {
50
50
  * any value.
51
51
  */
52
52
  delete(key: K): void;
53
+ [Symbol.iterator](): Generator<[K, V], void, undefined>;
54
+ forEach(callback: (entry: [K, V], index: number, table: HashTable<K, V>) => void): void;
55
+ filter(predicate: (entry: [K, V], index: number, table: HashTable<K, V>) => boolean): HashTable<K, V>;
56
+ map<T>(callback: (entry: [K, V], index: number, table: HashTable<K, V>) => T): HashTable<K, T>;
57
+ reduce<T>(callback: (accumulator: T, entry: [K, V], index: number, table: HashTable<K, V>) => T, initialValue: T): T;
53
58
  /**
54
59
  * The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
55
60
  * capacity of the data structure.
@@ -12,7 +12,7 @@ export class HashTableNode {
12
12
  constructor(key, value) {
13
13
  this.key = key;
14
14
  this.value = value;
15
- this.next = null;
15
+ this.next = undefined;
16
16
  }
17
17
  }
18
18
  export class HashTable {
@@ -22,7 +22,7 @@ export class HashTable {
22
22
  this._hashFn = hashFn || this._defaultHashFn;
23
23
  this._capacity = Math.max(capacity, HashTable.DEFAULT_CAPACITY);
24
24
  this._size = 0;
25
- this._buckets = new Array(this._capacity).fill(null);
25
+ this._buckets = new Array(this._capacity).fill(undefined);
26
26
  }
27
27
  _capacity;
28
28
  get capacity() {
@@ -106,7 +106,7 @@ export class HashTable {
106
106
  delete(key) {
107
107
  const index = this._hash(key);
108
108
  let currentNode = this._buckets[index];
109
- let prevNode = null;
109
+ let prevNode = undefined;
110
110
  while (currentNode) {
111
111
  if (currentNode.key === key) {
112
112
  if (prevNode) {
@@ -116,13 +116,58 @@ export class HashTable {
116
116
  this._buckets[index] = currentNode.next;
117
117
  }
118
118
  this._size--;
119
- currentNode.next = null; // Release memory
119
+ currentNode.next = undefined; // Release memory
120
120
  return;
121
121
  }
122
122
  prevNode = currentNode;
123
123
  currentNode = currentNode.next;
124
124
  }
125
125
  }
126
+ *[Symbol.iterator]() {
127
+ for (const bucket of this._buckets) {
128
+ let currentNode = bucket;
129
+ while (currentNode) {
130
+ yield [currentNode.key, currentNode.value];
131
+ currentNode = currentNode.next;
132
+ }
133
+ }
134
+ }
135
+ forEach(callback) {
136
+ let index = 0;
137
+ for (const entry of this) {
138
+ callback(entry, index, this);
139
+ index++;
140
+ }
141
+ }
142
+ filter(predicate) {
143
+ const newTable = new HashTable();
144
+ let index = 0;
145
+ for (const [key, value] of this) {
146
+ if (predicate([key, value], index, this)) {
147
+ newTable.set(key, value);
148
+ }
149
+ index++;
150
+ }
151
+ return newTable;
152
+ }
153
+ map(callback) {
154
+ const newTable = new HashTable();
155
+ let index = 0;
156
+ for (const [key, value] of this) {
157
+ newTable.set(key, callback([key, value], index, this));
158
+ index++;
159
+ }
160
+ return newTable;
161
+ }
162
+ reduce(callback, initialValue) {
163
+ let accumulator = initialValue;
164
+ let index = 0;
165
+ for (const entry of this) {
166
+ accumulator = callback(accumulator, entry, index, this);
167
+ index++;
168
+ }
169
+ return accumulator;
170
+ }
126
171
  /**
127
172
  * The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
128
173
  * capacity of the data structure.
@@ -213,7 +258,7 @@ export class HashTable {
213
258
  */
214
259
  _expand() {
215
260
  const newCapacity = this._capacity * 2;
216
- const newBuckets = new Array(newCapacity).fill(null);
261
+ const newBuckets = new Array(newCapacity).fill(undefined);
217
262
  for (const bucket of this._buckets) {
218
263
  let currentNode = bucket;
219
264
  while (currentNode) {
@@ -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
  /**
@@ -147,7 +144,7 @@ export declare class Heap<E = any> {
147
144
  * @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
148
145
  * @returns An array containing elements traversed in the specified order.
149
146
  */
150
- dfs(order: DFSOrderPattern): E[];
147
+ dfs(order?: DFSOrderPattern): E[];
151
148
  /**
152
149
  * Time Complexity: O(n)
153
150
  * Space Complexity: O(n)
@@ -195,10 +192,20 @@ export declare class Heap<E = any> {
195
192
  * Fix the entire heap to maintain heap properties.
196
193
  */
197
194
  fix(): void;
195
+ [Symbol.iterator](): Generator<E, void, unknown>;
196
+ forEach(callback: (element: E, index: number, heap: this) => void): void;
197
+ filter(predicate: (element: E, index: number, heap: Heap<E>) => boolean): Heap<E>;
198
+ map<T>(callback: (element: E, index: number, heap: Heap<E>) => T, comparator: Comparator<T>): Heap<T>;
199
+ reduce<T>(callback: (accumulator: T, currentValue: E, currentIndex: number, heap: Heap<E>) => T, initialValue: T): T;
198
200
  /**
199
201
  * Time Complexity: O(log n)
200
202
  * Space Complexity: O(1)
201
203
  */
204
+ print(): void;
205
+ /**
206
+ * Time Complexity: O(n)
207
+ * Space Complexity: O(1)
208
+ */
202
209
  /**
203
210
  * Time Complexity: O(log n)
204
211
  * Space Complexity: O(1)
@@ -207,10 +214,6 @@ export declare class Heap<E = any> {
207
214
  * @param index - The index of the newly added element.
208
215
  */
209
216
  protected _bubbleUp(index: number): void;
210
- /**
211
- * Time Complexity: O(n)
212
- * Space Complexity: O(1)
213
- */
214
217
  /**
215
218
  * Time Complexity: O(log n)
216
219
  * Space Complexity: O(1)
@@ -344,18 +347,18 @@ export declare class FibonacciHeap<E> {
344
347
  */
345
348
  merge(heapToMerge: FibonacciHeap<E>): void;
346
349
  /**
347
- * Default comparator function used by the heap.
348
- * @param {E} a
349
- * @param {E} b
350
+ * Create a new node.
351
+ * @param element
350
352
  * @protected
351
353
  */
352
- protected defaultComparator(a: E, b: E): number;
354
+ createNode(element: E): FibonacciHeapNode<E>;
353
355
  /**
354
- * Create a new node.
355
- * @param element
356
+ * Default comparator function used by the heap.
357
+ * @param {E} a
358
+ * @param {E} b
356
359
  * @protected
357
360
  */
358
- protected createNode(element: E): FibonacciHeapNode<E>;
361
+ protected _defaultComparator(a: E, b: E): number;
359
362
  /**
360
363
  * Time Complexity: O(1)
361
364
  * Space Complexity: O(1)
@@ -394,7 +397,7 @@ export declare class FibonacciHeap<E> {
394
397
  * @param x
395
398
  * @protected
396
399
  */
397
- protected link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
400
+ protected _link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
398
401
  /**
399
402
  * Time Complexity: O(n log n), where n is the number of elements in the heap.
400
403
  * Space Complexity: O(n)
@@ -406,5 +409,5 @@ export declare class FibonacciHeap<E> {
406
409
  * Remove and return the top element (smallest or largest element) from the heap.
407
410
  * @protected
408
411
  */
409
- protected consolidate(): void;
412
+ protected _consolidate(): void;
410
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.
@@ -201,29 +216,30 @@ export class Heap {
201
216
  * @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
202
217
  * @returns An array containing elements traversed in the specified order.
203
218
  */
204
- dfs(order) {
219
+ dfs(order = 'pre') {
205
220
  const result = [];
206
221
  // Auxiliary recursive function, traverses the binary heap according to the traversal order
207
- const dfsHelper = (index) => {
222
+ const _dfs = (index) => {
223
+ const left = 2 * index + 1, right = left + 1;
208
224
  if (index < this.size) {
209
225
  if (order === 'in') {
210
- dfsHelper(2 * index + 1);
226
+ _dfs(left);
211
227
  result.push(this.elements[index]);
212
- dfsHelper(2 * index + 2);
228
+ _dfs(right);
213
229
  }
214
230
  else if (order === 'pre') {
215
231
  result.push(this.elements[index]);
216
- dfsHelper(2 * index + 1);
217
- dfsHelper(2 * index + 2);
232
+ _dfs(left);
233
+ _dfs(right);
218
234
  }
219
235
  else if (order === 'post') {
220
- dfsHelper(2 * index + 1);
221
- dfsHelper(2 * index + 2);
236
+ _dfs(left);
237
+ _dfs(right);
222
238
  result.push(this.elements[index]);
223
239
  }
224
240
  }
225
241
  };
226
- dfsHelper(0); // Traverse starting from the root node
242
+ _dfs(0); // Traverse starting from the root node
227
243
  return result;
228
244
  }
229
245
  /**
@@ -252,7 +268,7 @@ export class Heap {
252
268
  * @returns A new Heap instance containing the same elements.
253
269
  */
254
270
  clone() {
255
- const clonedHeap = new Heap({ comparator: this.comparator });
271
+ const clonedHeap = new Heap([], this.options);
256
272
  clonedHeap._elements = [...this.elements];
257
273
  return clonedHeap;
258
274
  }
@@ -291,10 +307,58 @@ export class Heap {
291
307
  for (let i = Math.floor(this.size / 2); i >= 0; i--)
292
308
  this._sinkDown(i, this.elements.length >> 1);
293
309
  }
310
+ *[Symbol.iterator]() {
311
+ for (const element of this.elements) {
312
+ yield element;
313
+ }
314
+ }
315
+ forEach(callback) {
316
+ let index = 0;
317
+ for (const el of this) {
318
+ callback(el, index, this);
319
+ index++;
320
+ }
321
+ }
322
+ filter(predicate) {
323
+ const filteredHeap = new Heap([], this.options);
324
+ let index = 0;
325
+ for (const el of this) {
326
+ if (predicate(el, index, this)) {
327
+ filteredHeap.push(el);
328
+ }
329
+ index++;
330
+ }
331
+ return filteredHeap;
332
+ }
333
+ map(callback, comparator) {
334
+ const mappedHeap = new Heap([], { comparator: comparator });
335
+ let index = 0;
336
+ for (const el of this) {
337
+ mappedHeap.add(callback(el, index, this));
338
+ index++;
339
+ }
340
+ return mappedHeap;
341
+ }
342
+ reduce(callback, initialValue) {
343
+ let accumulator = initialValue;
344
+ let index = 0;
345
+ for (const el of this) {
346
+ accumulator = callback(accumulator, el, index, this);
347
+ index++;
348
+ }
349
+ return accumulator;
350
+ }
294
351
  /**
295
352
  * Time Complexity: O(log n)
296
353
  * Space Complexity: O(1)
297
354
  */
355
+ print() {
356
+ console.log([...this]);
357
+ }
358
+ /**
359
+ * Time Complexity: O(n)
360
+ * Space Complexity: O(1)
361
+ */
298
362
  /**
299
363
  * Time Complexity: O(log n)
300
364
  * Space Complexity: O(1)
@@ -307,17 +371,13 @@ export class Heap {
307
371
  while (index > 0) {
308
372
  const parent = (index - 1) >> 1;
309
373
  const parentItem = this.elements[parent];
310
- if (this._comparator(parentItem, element) <= 0)
374
+ if (this.options.comparator(parentItem, element) <= 0)
311
375
  break;
312
376
  this.elements[index] = parentItem;
313
377
  index = parent;
314
378
  }
315
379
  this.elements[index] = element;
316
380
  }
317
- /**
318
- * Time Complexity: O(n)
319
- * Space Complexity: O(1)
320
- */
321
381
  /**
322
382
  * Time Complexity: O(log n)
323
383
  * Space Complexity: O(1)
@@ -333,11 +393,11 @@ export class Heap {
333
393
  const right = left + 1;
334
394
  let minItem = this.elements[left];
335
395
  if (right < this.elements.length &&
336
- this._comparator(minItem, this.elements[right]) > 0) {
396
+ this.options.comparator(minItem, this.elements[right]) > 0) {
337
397
  left = right;
338
398
  minItem = this.elements[right];
339
399
  }
340
- if (this._comparator(minItem, element) >= 0)
400
+ if (this.options.comparator(minItem, element) >= 0)
341
401
  break;
342
402
  this.elements[index] = minItem;
343
403
  index = left;
@@ -362,7 +422,7 @@ export class FibonacciHeapNode {
362
422
  export class FibonacciHeap {
363
423
  constructor(comparator) {
364
424
  this.clear();
365
- this._comparator = comparator || this.defaultComparator;
425
+ this._comparator = comparator || this._defaultComparator;
366
426
  if (typeof this.comparator !== 'function') {
367
427
  throw new Error('FibonacciHeap constructor: given comparator should be a function.');
368
428
  }
@@ -537,7 +597,7 @@ export class FibonacciHeap {
537
597
  }
538
598
  else {
539
599
  this._min = z.right;
540
- this.consolidate();
600
+ this._consolidate();
541
601
  }
542
602
  this._size--;
543
603
  return z.element;
@@ -577,27 +637,27 @@ export class FibonacciHeap {
577
637
  // Clear the heap that was merged
578
638
  heapToMerge.clear();
579
639
  }
640
+ /**
641
+ * Create a new node.
642
+ * @param element
643
+ * @protected
644
+ */
645
+ createNode(element) {
646
+ return new FibonacciHeapNode(element);
647
+ }
580
648
  /**
581
649
  * Default comparator function used by the heap.
582
650
  * @param {E} a
583
651
  * @param {E} b
584
652
  * @protected
585
653
  */
586
- defaultComparator(a, b) {
654
+ _defaultComparator(a, b) {
587
655
  if (a < b)
588
656
  return -1;
589
657
  if (a > b)
590
658
  return 1;
591
659
  return 0;
592
660
  }
593
- /**
594
- * Create a new node.
595
- * @param element
596
- * @protected
597
- */
598
- createNode(element) {
599
- return new FibonacciHeapNode(element);
600
- }
601
661
  /**
602
662
  * Time Complexity: O(1)
603
663
  * Space Complexity: O(1)
@@ -653,7 +713,7 @@ export class FibonacciHeap {
653
713
  * @param x
654
714
  * @protected
655
715
  */
656
- link(y, x) {
716
+ _link(y, x) {
657
717
  this.removeFromRoot(y);
658
718
  y.left = y;
659
719
  y.right = y;
@@ -672,7 +732,7 @@ export class FibonacciHeap {
672
732
  * Remove and return the top element (smallest or largest element) from the heap.
673
733
  * @protected
674
734
  */
675
- consolidate() {
735
+ _consolidate() {
676
736
  const A = new Array(this.size);
677
737
  const elements = this.consumeLinkedList(this.root);
678
738
  let x, y, d, t;
@@ -686,7 +746,7 @@ export class FibonacciHeap {
686
746
  x = y;
687
747
  y = t;
688
748
  }
689
- this.link(y, x);
749
+ this._link(y, x);
690
750
  A[d] = undefined;
691
751
  d++;
692
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
  }