data-structure-typed 1.49.1 → 1.49.3

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 (106) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +16 -16
  3. package/README_zh-CN.md +2 -2
  4. package/benchmark/report.html +46 -1
  5. package/benchmark/report.json +457 -22
  6. package/dist/cjs/data-structures/base/iterable-base.d.ts +11 -0
  7. package/dist/cjs/data-structures/base/iterable-base.js +21 -0
  8. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  9. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +7 -7
  10. package/dist/cjs/data-structures/graph/abstract-graph.js +43 -12
  11. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  12. package/dist/cjs/data-structures/graph/directed-graph.d.ts +2 -2
  13. package/dist/cjs/data-structures/graph/directed-graph.js +2 -2
  14. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  15. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +1 -1
  16. package/dist/cjs/data-structures/graph/undirected-graph.js +1 -1
  17. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  18. package/dist/cjs/data-structures/hash/hash-map.d.ts +9 -9
  19. package/dist/cjs/data-structures/hash/hash-map.js +16 -15
  20. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  21. package/dist/cjs/data-structures/heap/heap.d.ts +6 -35
  22. package/dist/cjs/data-structures/heap/heap.js +10 -42
  23. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  24. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
  25. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +143 -146
  26. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  27. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +16 -21
  28. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +42 -42
  29. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  30. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +25 -25
  31. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +36 -36
  32. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  33. package/dist/cjs/data-structures/queue/deque.d.ts +70 -75
  34. package/dist/cjs/data-structures/queue/deque.js +100 -110
  35. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  36. package/dist/cjs/data-structures/queue/queue.d.ts +37 -38
  37. package/dist/cjs/data-structures/queue/queue.js +46 -49
  38. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  39. package/dist/cjs/data-structures/stack/stack.d.ts +2 -3
  40. package/dist/cjs/data-structures/stack/stack.js +2 -5
  41. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  42. package/dist/cjs/data-structures/trie/trie.d.ts +1 -2
  43. package/dist/cjs/data-structures/trie/trie.js +2 -5
  44. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  45. package/dist/mjs/data-structures/base/iterable-base.d.ts +11 -0
  46. package/dist/mjs/data-structures/base/iterable-base.js +21 -0
  47. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +7 -7
  48. package/dist/mjs/data-structures/graph/abstract-graph.js +43 -12
  49. package/dist/mjs/data-structures/graph/directed-graph.d.ts +2 -2
  50. package/dist/mjs/data-structures/graph/directed-graph.js +2 -2
  51. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +1 -1
  52. package/dist/mjs/data-structures/graph/undirected-graph.js +1 -1
  53. package/dist/mjs/data-structures/hash/hash-map.d.ts +9 -9
  54. package/dist/mjs/data-structures/hash/hash-map.js +16 -15
  55. package/dist/mjs/data-structures/heap/heap.d.ts +6 -35
  56. package/dist/mjs/data-structures/heap/heap.js +10 -42
  57. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
  58. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +142 -145
  59. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +16 -21
  60. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +43 -43
  61. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +25 -25
  62. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +36 -36
  63. package/dist/mjs/data-structures/queue/deque.d.ts +70 -75
  64. package/dist/mjs/data-structures/queue/deque.js +100 -110
  65. package/dist/mjs/data-structures/queue/queue.d.ts +37 -38
  66. package/dist/mjs/data-structures/queue/queue.js +45 -48
  67. package/dist/mjs/data-structures/stack/stack.d.ts +2 -3
  68. package/dist/mjs/data-structures/stack/stack.js +2 -5
  69. package/dist/mjs/data-structures/trie/trie.d.ts +1 -2
  70. package/dist/mjs/data-structures/trie/trie.js +2 -5
  71. package/dist/umd/data-structure-typed.js +531 -532
  72. package/dist/umd/data-structure-typed.min.js +2 -2
  73. package/dist/umd/data-structure-typed.min.js.map +1 -1
  74. package/package.json +1 -1
  75. package/src/data-structures/base/iterable-base.ts +24 -0
  76. package/src/data-structures/graph/abstract-graph.ts +55 -14
  77. package/src/data-structures/graph/directed-graph.ts +3 -2
  78. package/src/data-structures/graph/undirected-graph.ts +1 -1
  79. package/src/data-structures/hash/hash-map.ts +27 -28
  80. package/src/data-structures/heap/heap.ts +19 -57
  81. package/src/data-structures/linked-list/doubly-linked-list.ts +157 -161
  82. package/src/data-structures/linked-list/singly-linked-list.ts +49 -49
  83. package/src/data-structures/linked-list/skip-linked-list.ts +40 -40
  84. package/src/data-structures/queue/deque.ts +122 -135
  85. package/src/data-structures/queue/queue.ts +54 -58
  86. package/src/data-structures/stack/stack.ts +4 -8
  87. package/src/data-structures/trie/trie.ts +5 -9
  88. package/test/performance/data-structures/comparison/comparison.test.ts +6 -6
  89. package/test/performance/data-structures/heap/heap.test.ts +2 -2
  90. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
  91. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +2 -2
  92. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +2 -2
  93. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +2 -2
  94. package/test/unit/data-structures/graph/abstract-graph.test.ts +1 -1
  95. package/test/unit/data-structures/graph/directed-graph.test.ts +48 -3
  96. package/test/unit/data-structures/graph/undirected-graph.test.ts +48 -4
  97. package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
  98. package/test/unit/data-structures/heap/heap.test.ts +6 -1
  99. package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
  100. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -30
  101. package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
  102. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +21 -21
  103. package/test/unit/data-structures/linked-list/skip-list.test.ts +2 -2
  104. package/test/unit/data-structures/queue/deque.test.ts +5 -5
  105. package/test/unit/data-structures/queue/queue.test.ts +4 -4
  106. package/test/unit/data-structures/trie/trie.test.ts +1 -1
@@ -86,11 +86,11 @@ export class AbstractGraph extends IterableEntryBase {
86
86
  */
87
87
  addVertex(keyOrVertex, value) {
88
88
  if (keyOrVertex instanceof AbstractVertex) {
89
- return this._addVertexOnly(keyOrVertex);
89
+ return this._addVertex(keyOrVertex);
90
90
  }
91
91
  else {
92
92
  const newVertex = this.createVertex(keyOrVertex, value);
93
- return this._addVertexOnly(newVertex);
93
+ return this._addVertex(newVertex);
94
94
  }
95
95
  }
96
96
  isVertexKey(potentialKey) {
@@ -160,7 +160,7 @@ export class AbstractGraph extends IterableEntryBase {
160
160
  */
161
161
  addEdge(srcOrEdge, dest, weight, value) {
162
162
  if (srcOrEdge instanceof AbstractEdge) {
163
- return this._addEdgeOnly(srcOrEdge);
163
+ return this._addEdge(srcOrEdge);
164
164
  }
165
165
  else {
166
166
  if (dest instanceof AbstractVertex || typeof dest === 'string' || typeof dest === 'number') {
@@ -171,7 +171,7 @@ export class AbstractGraph extends IterableEntryBase {
171
171
  if (dest instanceof AbstractVertex)
172
172
  dest = dest.key;
173
173
  const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
174
- return this._addEdgeOnly(newEdge);
174
+ return this._addEdge(newEdge);
175
175
  }
176
176
  else {
177
177
  throw new Error('dest must be a Vertex or vertex key while srcOrEdge is an Edge');
@@ -996,13 +996,6 @@ export class AbstractGraph extends IterableEntryBase {
996
996
  getLowMap() {
997
997
  return this.tarjan(false, false, false, false).lowMap;
998
998
  }
999
- /**
1000
- * The function `getCycles` returns a map of cycles found using the Tarjan algorithm.
1001
- * @returns The function `getCycles()` is returning a `Map<number, VO[]>`.
1002
- */
1003
- getCycles() {
1004
- return this.tarjan(false, false, false, true).cycles;
1005
- }
1006
999
  /**
1007
1000
  * The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
1008
1001
  * @returns an array of VO objects, specifically the cut vertexes.
@@ -1025,6 +1018,44 @@ export class AbstractGraph extends IterableEntryBase {
1025
1018
  getBridges() {
1026
1019
  return this.tarjan(false, true, false, false).bridges;
1027
1020
  }
1021
+ /**
1022
+ * O(V+E+C)
1023
+ * O(V+C)
1024
+ */
1025
+ getCycles(isInclude2Cycle = false) {
1026
+ const cycles = [];
1027
+ const visited = new Set();
1028
+ const dfs = (vertex, currentPath, visited) => {
1029
+ if (visited.has(vertex)) {
1030
+ if ((!isInclude2Cycle && currentPath.length > 2 || isInclude2Cycle && currentPath.length >= 2) && currentPath[0] === vertex.key) {
1031
+ cycles.push([...currentPath]);
1032
+ }
1033
+ return;
1034
+ }
1035
+ visited.add(vertex);
1036
+ currentPath.push(vertex.key);
1037
+ for (const neighbor of this.getNeighbors(vertex)) {
1038
+ neighbor && dfs(neighbor, currentPath, visited);
1039
+ }
1040
+ visited.delete(vertex);
1041
+ currentPath.pop();
1042
+ };
1043
+ for (const vertex of this.vertexMap.values()) {
1044
+ dfs(vertex, [], visited);
1045
+ }
1046
+ // Use a set to eliminate duplicate cycles
1047
+ const uniqueCycles = new Map();
1048
+ for (const cycle of cycles) {
1049
+ const sorted = [...cycle].sort().toString();
1050
+ if (uniqueCycles.has(sorted))
1051
+ continue;
1052
+ else {
1053
+ uniqueCycles.set(sorted, cycle);
1054
+ }
1055
+ }
1056
+ // Convert the unique cycles back to an array
1057
+ return [...uniqueCycles].map(cycleString => cycleString[1]);
1058
+ }
1028
1059
  /**
1029
1060
  * Time Complexity: O(n)
1030
1061
  * Space Complexity: O(n)
@@ -1087,7 +1118,7 @@ export class AbstractGraph extends IterableEntryBase {
1087
1118
  yield [vertex.key, vertex.value];
1088
1119
  }
1089
1120
  }
1090
- _addVertexOnly(newVertex) {
1121
+ _addVertex(newVertex) {
1091
1122
  if (this.hasVertex(newVertex)) {
1092
1123
  return false;
1093
1124
  // throw (new Error('Duplicated vertex key is not allowed'));
@@ -335,11 +335,11 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
335
335
  * Time Complexity: O(1)
336
336
  * Space Complexity: O(1)
337
337
  *
338
- * The function `_addEdgeOnly` adds an edge to a graph if the source and destination vertexMap exist.
338
+ * The function `_addEdge` adds an edge to a graph if the source and destination vertexMap exist.
339
339
  * @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph. It is the edge that
340
340
  * needs to be added to the graph.
341
341
  * @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
342
342
  * source or destination vertex does not exist in the graph.
343
343
  */
344
- protected _addEdgeOnly(edge: EO): boolean;
344
+ protected _addEdge(edge: EO): boolean;
345
345
  }
@@ -528,13 +528,13 @@ export class DirectedGraph extends AbstractGraph {
528
528
  * Time Complexity: O(1)
529
529
  * Space Complexity: O(1)
530
530
  *
531
- * The function `_addEdgeOnly` adds an edge to a graph if the source and destination vertexMap exist.
531
+ * The function `_addEdge` adds an edge to a graph if the source and destination vertexMap exist.
532
532
  * @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph. It is the edge that
533
533
  * needs to be added to the graph.
534
534
  * @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
535
535
  * source or destination vertex does not exist in the graph.
536
536
  */
537
- _addEdgeOnly(edge) {
537
+ _addEdge(edge) {
538
538
  if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
539
539
  return false;
540
540
  }
@@ -205,5 +205,5 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
205
205
  * @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
206
206
  * @returns a boolean value.
207
207
  */
208
- protected _addEdgeOnly(edge: EO): boolean;
208
+ protected _addEdge(edge: EO): boolean;
209
209
  }
@@ -332,7 +332,7 @@ export class UndirectedGraph extends AbstractGraph {
332
332
  * @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
333
333
  * @returns a boolean value.
334
334
  */
335
- _addEdgeOnly(edge) {
335
+ _addEdge(edge) {
336
336
  for (const end of edge.vertexMap) {
337
337
  const endVertex = this._getVertex(end);
338
338
  if (endVertex === undefined)
@@ -42,13 +42,13 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
42
42
  * @param {V} value - The value parameter represents the value that you want to associate with the
43
43
  * key in the data structure.
44
44
  */
45
- set(key: K, value: V): void;
45
+ set(key: K, value: V): boolean;
46
46
  /**
47
47
  * The function "setMany" sets multiple key-value pairs in a map.
48
48
  * @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
49
49
  * key-value pair is represented as an array with two elements: the key and the value.
50
50
  */
51
- setMany(elements: Iterable<[K, V]>): void;
51
+ setMany(elements: Iterable<[K, V]>): boolean[];
52
52
  /**
53
53
  * The `get` function retrieves a value from a map based on a given key, either from an object map or
54
54
  * a string map.
@@ -114,6 +114,7 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
114
114
  */
115
115
  filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): HashMap<K, V>;
116
116
  print(): void;
117
+ put(key: K, value: V): boolean;
117
118
  /**
118
119
  * The function returns an iterator that yields key-value pairs from both an object store and an
119
120
  * object map.
@@ -178,10 +179,9 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
178
179
  * value associated with the key being set in the data structure.
179
180
  * @returns the size of the data structure after the key-value pair has been set.
180
181
  */
181
- set(key: K, value?: V): number;
182
+ set(key: K, value?: V): boolean;
182
183
  has(key: K): boolean;
183
- hasValue(value: V): boolean;
184
- setMany(entries: Iterable<[K, V]>): void;
184
+ setMany(entries: Iterable<[K, V]>): boolean[];
185
185
  /**
186
186
  * Time Complexity: O(1)
187
187
  * Space Complexity: O(1)
@@ -207,7 +207,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
207
207
  * the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
208
208
  * where `K` is the key and `V` is the value.
209
209
  */
210
- getAt(index: number): [K, V];
210
+ getAt(index: number): V | undefined;
211
211
  /**
212
212
  * Time Complexity: O(1)
213
213
  * Space Complexity: O(1)
@@ -228,7 +228,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
228
228
  * deleted in the linked list.
229
229
  * @returns The size of the list after deleting the element at the specified index.
230
230
  */
231
- deleteAt(index: number): number;
231
+ deleteAt(index: number): boolean;
232
232
  /**
233
233
  * Time Complexity: O(1)
234
234
  * Space Complexity: O(1)
@@ -288,7 +288,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
288
288
  * function.
289
289
  */
290
290
  map<NV>(callback: EntryCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV>;
291
- print(): void;
291
+ put(key: K, value: V): boolean;
292
292
  /**
293
293
  * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
294
294
  * Space Complexity: O(1)
@@ -306,5 +306,5 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
306
306
  * represents a node in a linked list. It contains a key-value pair and references to the previous
307
307
  * and next nodes in the list.
308
308
  */
309
- protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>): void;
309
+ protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>): boolean;
310
310
  }
@@ -64,6 +64,7 @@ export class HashMap extends IterableEntryBase {
64
64
  }
65
65
  this._store[strKey] = { key, value };
66
66
  }
67
+ return true;
67
68
  }
68
69
  /**
69
70
  * The function "setMany" sets multiple key-value pairs in a map.
@@ -71,8 +72,10 @@ export class HashMap extends IterableEntryBase {
71
72
  * key-value pair is represented as an array with two elements: the key and the value.
72
73
  */
73
74
  setMany(elements) {
75
+ const results = [];
74
76
  for (const [key, value] of elements)
75
- this.set(key, value);
77
+ results.push(this.set(key, value));
78
+ return results;
76
79
  }
77
80
  /**
78
81
  * The `get` function retrieves a value from a map based on a given key, either from an object map or
@@ -189,6 +192,9 @@ export class HashMap extends IterableEntryBase {
189
192
  print() {
190
193
  console.log([...this.entries()]);
191
194
  }
195
+ put(key, value) {
196
+ return this.set(key, value);
197
+ }
192
198
  /**
193
199
  * The function returns an iterator that yields key-value pairs from both an object store and an
194
200
  * object map.
@@ -357,7 +363,7 @@ export class LinkedHashMap extends IterableEntryBase {
357
363
  this._sentinel.prev = node;
358
364
  this._size++;
359
365
  }
360
- return this._size;
366
+ return true;
361
367
  }
362
368
  has(key) {
363
369
  if (isWeakKey(key)) {
@@ -369,18 +375,13 @@ export class LinkedHashMap extends IterableEntryBase {
369
375
  return hash in this._noObjMap;
370
376
  }
371
377
  }
372
- hasValue(value) {
373
- for (const [, elementValue] of this) {
374
- if (elementValue === value)
375
- return true;
376
- }
377
- return false;
378
- }
379
378
  setMany(entries) {
379
+ const results = [];
380
380
  for (const entry of entries) {
381
381
  const [key, value] = entry;
382
- this.set(key, value);
382
+ results.push(this.set(key, value));
383
383
  }
384
+ return results;
384
385
  }
385
386
  /**
386
387
  * Time Complexity: O(1)
@@ -424,7 +425,7 @@ export class LinkedHashMap extends IterableEntryBase {
424
425
  while (index--) {
425
426
  node = node.next;
426
427
  }
427
- return [node.key, node.value];
428
+ return node.value;
428
429
  }
429
430
  /**
430
431
  * Time Complexity: O(1)
@@ -477,8 +478,7 @@ export class LinkedHashMap extends IterableEntryBase {
477
478
  while (index--) {
478
479
  node = node.next;
479
480
  }
480
- this._deleteNode(node);
481
- return this._size;
481
+ return this._deleteNode(node);
482
482
  }
483
483
  /**
484
484
  * Time Complexity: O(1)
@@ -571,8 +571,8 @@ export class LinkedHashMap extends IterableEntryBase {
571
571
  }
572
572
  return mappedMap;
573
573
  }
574
- print() {
575
- console.log([...this]);
574
+ put(key, value) {
575
+ return this.set(key, value);
576
576
  }
577
577
  /**
578
578
  * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
@@ -608,5 +608,6 @@ export class LinkedHashMap extends IterableEntryBase {
608
608
  this._tail = prev;
609
609
  }
610
610
  this._size -= 1;
611
+ return true;
611
612
  }
612
613
  }
@@ -52,19 +52,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
52
52
  * Insert an element into the heap and maintain the heap properties.
53
53
  * @param element - The element to be inserted.
54
54
  */
55
- add(element: E): Heap<E>;
56
- /**
57
- * Time Complexity: O(log n), where n is the number of elements in the heap.
58
- * Space Complexity: O(1)
59
- */
60
- /**
61
- * Time Complexity: O(log n), where n is the number of elements in the heap.
62
- * Space Complexity: O(1)
63
- *
64
- * Insert an element into the heap and maintain the heap properties.
65
- * @param element - The element to be inserted.
66
- */
67
- push(element: E): Heap<E>;
55
+ add(element: E): boolean;
68
56
  /**
69
57
  * Time Complexity: O(log n), where n is the number of elements in the heap.
70
58
  * Space Complexity: O(1)
@@ -77,18 +65,6 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
77
65
  * @returns The top element or undefined if the heap is empty.
78
66
  */
79
67
  poll(): E | undefined;
80
- /**
81
- * Time Complexity: O(log n), where n is the number of elements in the heap.
82
- * Space Complexity: O(1)
83
- */
84
- /**
85
- * Time Complexity: O(log n), where n is the number of elements in the heap.
86
- * Space Complexity: O(1)
87
- *
88
- * Remove and return the top element (smallest or largest element) from the heap.
89
- * @returns The top element or undefined if the heap is empty.
90
- */
91
- pop(): E | undefined;
92
68
  /**
93
69
  * Peek at the top element of the heap without removing it.
94
70
  * @returns The top element or undefined if the heap is empty.
@@ -114,7 +90,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
114
90
  * Clear and add elements of the heap
115
91
  * @param elements
116
92
  */
117
- refill(elements: E[]): void;
93
+ refill(elements: E[]): boolean[];
118
94
  /**
119
95
  * Time Complexity: O(n), where n is the number of elements in the heap.
120
96
  * Space Complexity: O(1)
@@ -203,7 +179,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
203
179
  *
204
180
  * Fix the entire heap to maintain heap properties.
205
181
  */
206
- fix(): void;
182
+ fix(): boolean[];
207
183
  /**
208
184
  * Time Complexity: O(n)
209
185
  * Space Complexity: O(n)
@@ -250,12 +226,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
250
226
  * original Heap.
251
227
  */
252
228
  map<T>(callback: ElementCallback<E, T>, comparator: Comparator<T>, thisArg?: any): Heap<T>;
253
- /**
254
- * Time Complexity: O(log n)
255
- * Space Complexity: O(1)
256
- */
257
- print(): void;
258
- protected _getIterator(): Generator<E, void, unknown>;
229
+ protected _getIterator(): IterableIterator<E>;
259
230
  /**
260
231
  * Time Complexity: O(n)
261
232
  * Space Complexity: O(1)
@@ -267,7 +238,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
267
238
  * Float operation to maintain heap properties after adding an element.
268
239
  * @param index - The index of the newly added element.
269
240
  */
270
- protected _bubbleUp(index: number): void;
241
+ protected _bubbleUp(index: number): boolean;
271
242
  /**
272
243
  * Time Complexity: O(log n)
273
244
  * Space Complexity: O(1)
@@ -276,7 +247,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
276
247
  * @param index - The index from which to start sinking.
277
248
  * @param halfLength
278
249
  */
279
- protected _sinkDown(index: number, halfLength: number): void;
250
+ protected _sinkDown(index: number, halfLength: number): boolean;
280
251
  }
281
252
  export declare class FibonacciHeapNode<E> {
282
253
  element: E;
@@ -39,7 +39,7 @@ export class Heap extends IterableElementBase {
39
39
  }
40
40
  if (elements) {
41
41
  for (const el of elements) {
42
- this.push(el);
42
+ this.add(el);
43
43
  }
44
44
  // this.fix();
45
45
  }
@@ -82,23 +82,8 @@ export class Heap extends IterableElementBase {
82
82
  * @param element - The element to be inserted.
83
83
  */
84
84
  add(element) {
85
- return this.push(element);
86
- }
87
- /**
88
- * Time Complexity: O(log n), where n is the number of elements in the heap.
89
- * Space Complexity: O(1)
90
- */
91
- /**
92
- * Time Complexity: O(log n), where n is the number of elements in the heap.
93
- * Space Complexity: O(1)
94
- *
95
- * Insert an element into the heap and maintain the heap properties.
96
- * @param element - The element to be inserted.
97
- */
98
- push(element) {
99
85
  this._elements.push(element);
100
- this._bubbleUp(this.elements.length - 1);
101
- return this;
86
+ return this._bubbleUp(this.elements.length - 1);
102
87
  }
103
88
  /**
104
89
  * Time Complexity: O(log n), where n is the number of elements in the heap.
@@ -122,20 +107,6 @@ export class Heap extends IterableElementBase {
122
107
  }
123
108
  return value;
124
109
  }
125
- /**
126
- * Time Complexity: O(log n), where n is the number of elements in the heap.
127
- * Space Complexity: O(1)
128
- */
129
- /**
130
- * Time Complexity: O(log n), where n is the number of elements in the heap.
131
- * Space Complexity: O(1)
132
- *
133
- * Remove and return the top element (smallest or largest element) from the heap.
134
- * @returns The top element or undefined if the heap is empty.
135
- */
136
- pop() {
137
- return this.poll();
138
- }
139
110
  /**
140
111
  * Peek at the top element of the heap without removing it.
141
112
  * @returns The top element or undefined if the heap is empty.
@@ -169,7 +140,7 @@ export class Heap extends IterableElementBase {
169
140
  */
170
141
  refill(elements) {
171
142
  this._elements = elements;
172
- this.fix();
143
+ return this.fix();
173
144
  }
174
145
  /**
175
146
  * Time Complexity: O(n), where n is the number of elements in the heap.
@@ -206,7 +177,7 @@ export class Heap extends IterableElementBase {
206
177
  if (index < 0)
207
178
  return false;
208
179
  if (index === 0) {
209
- this.pop();
180
+ this.poll();
210
181
  }
211
182
  else if (index === this.elements.length - 1) {
212
183
  this.elements.pop();
@@ -318,8 +289,10 @@ export class Heap extends IterableElementBase {
318
289
  * Fix the entire heap to maintain heap properties.
319
290
  */
320
291
  fix() {
292
+ const results = [];
321
293
  for (let i = Math.floor(this.size / 2); i >= 0; i--)
322
- this._sinkDown(i, this.elements.length >> 1);
294
+ results.push(this._sinkDown(i, this.elements.length >> 1));
295
+ return results;
323
296
  }
324
297
  /**
325
298
  * Time Complexity: O(n)
@@ -346,7 +319,7 @@ export class Heap extends IterableElementBase {
346
319
  let index = 0;
347
320
  for (const current of this) {
348
321
  if (callback.call(thisArg, current, index, this)) {
349
- filteredList.push(current);
322
+ filteredList.add(current);
350
323
  }
351
324
  index++;
352
325
  }
@@ -385,13 +358,6 @@ export class Heap extends IterableElementBase {
385
358
  }
386
359
  return mappedHeap;
387
360
  }
388
- /**
389
- * Time Complexity: O(log n)
390
- * Space Complexity: O(1)
391
- */
392
- print() {
393
- console.log([...this]);
394
- }
395
361
  *_getIterator() {
396
362
  for (const element of this.elements) {
397
363
  yield element;
@@ -419,6 +385,7 @@ export class Heap extends IterableElementBase {
419
385
  index = parent;
420
386
  }
421
387
  this.elements[index] = element;
388
+ return true;
422
389
  }
423
390
  /**
424
391
  * Time Complexity: O(log n)
@@ -445,6 +412,7 @@ export class Heap extends IterableElementBase {
445
412
  index = left;
446
413
  }
447
414
  this.elements[index] = element;
415
+ return true;
448
416
  }
449
417
  }
450
418
  export class FibonacciHeapNode {