heap-typed 1.49.4 → 1.49.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 (69) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +1 -1
  2. package/dist/data-structures/binary-tree/avl-tree.d.ts +53 -48
  3. package/dist/data-structures/binary-tree/avl-tree.js +55 -49
  4. package/dist/data-structures/binary-tree/binary-tree.d.ts +154 -143
  5. package/dist/data-structures/binary-tree/binary-tree.js +211 -198
  6. package/dist/data-structures/binary-tree/bst.d.ts +83 -71
  7. package/dist/data-structures/binary-tree/bst.js +113 -89
  8. package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
  9. package/dist/data-structures/binary-tree/rb-tree.js +62 -59
  10. package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -55
  11. package/dist/data-structures/binary-tree/tree-multimap.js +59 -94
  12. package/dist/data-structures/graph/abstract-graph.d.ts +1 -1
  13. package/dist/data-structures/graph/abstract-graph.js +3 -2
  14. package/dist/data-structures/hash/hash-map.d.ts +1 -1
  15. package/dist/data-structures/hash/hash-map.js +2 -2
  16. package/dist/data-structures/heap/heap.js +2 -3
  17. package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  18. package/dist/data-structures/matrix/index.d.ts +0 -2
  19. package/dist/data-structures/matrix/index.js +0 -2
  20. package/dist/data-structures/matrix/matrix.d.ts +128 -10
  21. package/dist/data-structures/matrix/matrix.js +400 -15
  22. package/dist/data-structures/queue/deque.d.ts +2 -2
  23. package/dist/data-structures/queue/deque.js +5 -7
  24. package/dist/data-structures/queue/queue.d.ts +1 -1
  25. package/dist/interfaces/binary-tree.d.ts +3 -3
  26. package/dist/types/common.d.ts +3 -3
  27. package/dist/types/common.js +2 -2
  28. package/dist/types/data-structures/base/base.d.ts +1 -1
  29. package/dist/types/data-structures/heap/heap.d.ts +1 -1
  30. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  31. package/dist/utils/utils.d.ts +1 -0
  32. package/dist/utils/utils.js +6 -1
  33. package/package.json +2 -2
  34. package/src/data-structures/base/index.ts +1 -1
  35. package/src/data-structures/base/iterable-base.ts +7 -10
  36. package/src/data-structures/binary-tree/avl-tree.ts +73 -61
  37. package/src/data-structures/binary-tree/binary-tree.ts +301 -270
  38. package/src/data-structures/binary-tree/bst.ts +139 -115
  39. package/src/data-structures/binary-tree/rb-tree.ts +81 -73
  40. package/src/data-structures/binary-tree/tree-multimap.ts +72 -103
  41. package/src/data-structures/graph/abstract-graph.ts +13 -11
  42. package/src/data-structures/graph/directed-graph.ts +1 -3
  43. package/src/data-structures/graph/map-graph.ts +6 -1
  44. package/src/data-structures/graph/undirected-graph.ts +3 -6
  45. package/src/data-structures/hash/hash-map.ts +18 -16
  46. package/src/data-structures/heap/heap.ts +7 -10
  47. package/src/data-structures/heap/max-heap.ts +2 -1
  48. package/src/data-structures/heap/min-heap.ts +2 -1
  49. package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
  50. package/src/data-structures/matrix/index.ts +0 -2
  51. package/src/data-structures/matrix/matrix.ts +442 -13
  52. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
  53. package/src/data-structures/queue/deque.ts +18 -39
  54. package/src/data-structures/queue/queue.ts +1 -1
  55. package/src/interfaces/binary-tree.ts +9 -4
  56. package/src/types/common.ts +5 -5
  57. package/src/types/data-structures/base/base.ts +14 -3
  58. package/src/types/data-structures/base/index.ts +1 -1
  59. package/src/types/data-structures/graph/abstract-graph.ts +4 -2
  60. package/src/types/data-structures/hash/hash-map.ts +3 -3
  61. package/src/types/data-structures/heap/heap.ts +2 -2
  62. package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
  63. package/src/utils/utils.ts +7 -1
  64. package/dist/data-structures/matrix/matrix2d.d.ts +0 -107
  65. package/dist/data-structures/matrix/matrix2d.js +0 -199
  66. package/dist/data-structures/matrix/vector2d.d.ts +0 -200
  67. package/dist/data-structures/matrix/vector2d.js +0 -290
  68. package/src/data-structures/matrix/matrix2d.ts +0 -211
  69. package/src/data-structures/matrix/vector2d.ts +0 -315
@@ -1,7 +1,6 @@
1
- import { ElementCallback, EntryCallback, ReduceElementCallback, ReduceEntryCallback } from "../../types";
1
+ import { ElementCallback, EntryCallback, ReduceElementCallback, ReduceEntryCallback } from '../../types';
2
2
 
3
3
  export abstract class IterableEntryBase<K = any, V = any> {
4
-
5
4
  /**
6
5
  * Time Complexity: O(n)
7
6
  * Space Complexity: O(1)
@@ -147,7 +146,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
147
146
  let index = 0;
148
147
  for (const item of this) {
149
148
  const [key, value] = item;
150
- callbackfn.call(thisArg, value, key, index++, this)
149
+ callbackfn.call(thisArg, value, key, index++, this);
151
150
  }
152
151
  }
153
152
 
@@ -176,7 +175,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
176
175
  let index = 0;
177
176
  for (const item of this) {
178
177
  const [key, value] = item;
179
- accumulator = callbackfn(accumulator, value, key, index++, this)
178
+ accumulator = callbackfn(accumulator, value, key, index++, this);
180
179
  }
181
180
  return accumulator;
182
181
  }
@@ -193,14 +192,13 @@ export abstract class IterableEntryBase<K = any, V = any> {
193
192
  * Space Complexity: O(n)
194
193
  */
195
194
  print(): void {
196
- console.log([...this])
195
+ console.log([...this]);
197
196
  }
198
197
 
199
198
  protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
200
199
  }
201
200
 
202
201
  export abstract class IterableElementBase<V> {
203
-
204
202
  /**
205
203
  * Time Complexity: O(n)
206
204
  * Space Complexity: O(1)
@@ -310,7 +308,7 @@ export abstract class IterableElementBase<V> {
310
308
  forEach(callbackfn: ElementCallback<V, void>, thisArg?: any): void {
311
309
  let index = 0;
312
310
  for (const item of this) {
313
- callbackfn.call(thisArg, item as V, index++, this)
311
+ callbackfn.call(thisArg, item as V, index++, this);
314
312
  }
315
313
  }
316
314
 
@@ -335,18 +333,17 @@ export abstract class IterableElementBase<V> {
335
333
  let accumulator = initialValue;
336
334
  let index = 0;
337
335
  for (const item of this) {
338
- accumulator = callbackfn(accumulator, item as V, index++, this)
336
+ accumulator = callbackfn(accumulator, item as V, index++, this);
339
337
  }
340
338
  return accumulator;
341
339
  }
342
340
 
343
-
344
341
  /**
345
342
  * Time Complexity: O(n)
346
343
  * Space Complexity: O(n)
347
344
  */
348
345
  print(): void {
349
- console.log([...this])
346
+ console.log([...this]);
350
347
  }
351
348
 
352
349
  protected abstract _getIterator(...args: any[]): IterableIterator<V>;
@@ -13,12 +13,15 @@ import type {
13
13
  BinaryTreeDeleteResult,
14
14
  BSTNKeyOrNode,
15
15
  BTNCallback,
16
- BTNExemplar,
17
- BTNKeyOrNode
16
+ KeyOrNodeOrEntry
18
17
  } from '../../types';
19
18
  import { IBinaryTree } from '../../interfaces';
20
19
 
21
- export class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
20
+ export class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNodeNested<K, V>> extends BSTNode<
21
+ K,
22
+ V,
23
+ N
24
+ > {
22
25
  height: number;
23
26
 
24
27
  constructor(key: K, value?: V) {
@@ -36,22 +39,26 @@ export class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLT
36
39
  * 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
37
40
  * 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
38
41
  */
39
- export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, N, TREE> = AVLTree<K, V, N, AVLTreeNested<K, V, N>>>
42
+ export class AVLTree<
43
+ K = any,
44
+ V = any,
45
+ N extends AVLTreeNode<K, V, N> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
46
+ TREE extends AVLTree<K, V, N, TREE> = AVLTree<K, V, N, AVLTreeNested<K, V, N>>
47
+ >
40
48
  extends BST<K, V, N, TREE>
41
49
  implements IBinaryTree<K, V, N, TREE> {
42
-
43
50
  /**
44
- * The constructor function initializes an AVLTree object with optional elements and options.
45
- * @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>`
46
- * objects. It represents a collection of elements that will be added to the AVL tree during
51
+ * The constructor function initializes an AVLTree object with optional nodes and options.
52
+ * @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
53
+ * objects. It represents a collection of nodes that will be added to the AVL tree during
47
54
  * initialization.
48
55
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
49
56
  * behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
50
57
  * provide only a subset of the properties defined in the `AVLTreeOptions` interface.
51
58
  */
52
- constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<AVLTreeOptions<K>>) {
59
+ constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<AVLTreeOptions<K>>) {
53
60
  super([], options);
54
- if (elements) super.addMany(elements);
61
+ if (nodes) super.addMany(nodes);
55
62
  }
56
63
 
57
64
  /**
@@ -77,17 +84,18 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
77
84
  override createTree(options?: AVLTreeOptions<K>): TREE {
78
85
  return new AVLTree<K, V, N, TREE>([], {
79
86
  iterationType: this.iterationType,
80
- variant: this.variant, ...options
87
+ variant: this.variant,
88
+ ...options
81
89
  }) as TREE;
82
90
  }
83
91
 
84
92
  /**
85
- * The function checks if an exemplar is an instance of AVLTreeNode.
86
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
87
- * @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
93
+ * The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
94
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
95
+ * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
88
96
  */
89
- override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
90
- return exemplar instanceof AVLTreeNode;
97
+ override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N {
98
+ return keyOrNodeOrEntry instanceof AVLTreeNode;
91
99
  }
92
100
 
93
101
  /**
@@ -96,19 +104,19 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
96
104
  * data type.
97
105
  * @returns a boolean value indicating whether the potentialKey is of type number or not.
98
106
  */
99
- override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
100
- return !(potentialKey instanceof AVLTreeNode)
107
+ override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
108
+ return !(potentialKey instanceof AVLTreeNode);
101
109
  }
102
110
 
103
111
  /**
104
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
105
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
112
+ * Time Complexity: O(log n)
113
+ * Space Complexity: O(1)
114
+ * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
106
115
  */
107
116
 
108
-
109
117
  /**
110
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
111
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
118
+ * Time Complexity: O(log n)
119
+ * Space Complexity: O(1)
112
120
  *
113
121
  * The function overrides the add method of a binary tree node and balances the tree after inserting
114
122
  * a new node.
@@ -118,21 +126,21 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
118
126
  * being added to the binary tree.
119
127
  * @returns The method is returning either the inserted node or undefined.
120
128
  */
121
- override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined {
122
- if (keyOrNodeOrEntry === null) return undefined;
129
+ override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean {
130
+ if (keyOrNodeOrEntry === null) return false;
123
131
  const inserted = super.add(keyOrNodeOrEntry, value);
124
- if (inserted) this._balancePath(inserted);
132
+ if (inserted) this._balancePath(keyOrNodeOrEntry);
125
133
  return inserted;
126
134
  }
127
135
 
128
136
  /**
129
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
130
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
137
+ * Time Complexity: O(log n)
138
+ * Space Complexity: O(1)
131
139
  */
132
140
 
133
141
  /**
134
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (BST) has logarithmic time complexity.
135
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
142
+ * Time Complexity: O(log n)
143
+ * Space Complexity: O(1)
136
144
  *
137
145
  * The function overrides the delete method of a binary tree, performs the deletion, and then
138
146
  * balances the tree if necessary.
@@ -159,7 +167,6 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
159
167
  return deletedResults;
160
168
  }
161
169
 
162
-
163
170
  /**
164
171
  * The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
165
172
  * tree.
@@ -196,13 +203,14 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
196
203
  }
197
204
 
198
205
  /**
199
- * Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
200
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
206
+ * Time Complexity: O(1)
207
+ * Space Complexity: O(1)
208
+ * constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
201
209
  */
202
210
 
203
211
  /**
204
- * Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
205
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
212
+ * Time Complexity: O(1)
213
+ * Space Complexity: O(1)
206
214
  *
207
215
  * The function calculates the balance factor of a node in a binary tree.
208
216
  * @param {N} node - The parameter "node" represents a node in a binary tree data structure.
@@ -220,13 +228,14 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
220
228
  }
221
229
 
222
230
  /**
223
- * Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
224
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
231
+ * Time Complexity: O(1)
232
+ * Space Complexity: O(1)
233
+ * constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
225
234
  */
226
235
 
227
236
  /**
228
- * Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
229
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
237
+ * Time Complexity: O(1)
238
+ * Space Complexity: O(1)
230
239
  *
231
240
  * The function updates the height of a node in a binary tree based on the heights of its left and
232
241
  * right children.
@@ -242,20 +251,22 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
242
251
  }
243
252
 
244
253
  /**
245
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root.
246
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
254
+ * Time Complexity: O(log n)
255
+ * Space Complexity: O(1)
256
+ * logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size.
247
257
  */
248
258
 
249
259
  /**
250
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root.
251
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
260
+ * Time Complexity: O(log n)
261
+ * Space Complexity: O(1)
252
262
  *
253
263
  * The `_balancePath` function is used to update the heights of nodes and perform rotation operations
254
264
  * to restore balance in an AVL tree after inserting a node.
255
265
  * @param {N} node - The `node` parameter in the `_balancePath` function represents the node in the
256
266
  * AVL tree that needs to be balanced.
257
267
  */
258
- protected _balancePath(node: N): void {
268
+ protected _balancePath(node: KeyOrNodeOrEntry<K, V, N>): void {
269
+ node = this.ensureNode(node);
259
270
  const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
260
271
  for (let i = 0; i < path.length; i++) {
261
272
  // second O(log n)
@@ -295,13 +306,14 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
295
306
  }
296
307
 
297
308
  /**
298
- * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
299
- * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
309
+ * Time Complexity: O(1)
310
+ * Space Complexity: O(1)
311
+ * constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory.
300
312
  */
301
313
 
302
314
  /**
303
- * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
304
- * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
315
+ * Time Complexity: O(1)
316
+ * Space Complexity: O(1)
305
317
  *
306
318
  * The function `_balanceLL` performs a left-left rotation to balance a binary tree.
307
319
  * @param {N} A - A is a node in a binary tree.
@@ -333,13 +345,13 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
333
345
  }
334
346
 
335
347
  /**
336
- * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
337
- * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
348
+ * Time Complexity: O(1)
349
+ * Space Complexity: O(1)
338
350
  */
339
351
 
340
352
  /**
341
- * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
342
- * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
353
+ * Time Complexity: O(1)
354
+ * Space Complexity: O(1)
343
355
  *
344
356
  * The `_balanceLR` function performs a left-right rotation to balance a binary tree.
345
357
  * @param {N} A - A is a node in a binary tree.
@@ -389,13 +401,13 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
389
401
  }
390
402
 
391
403
  /**
392
- * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
393
- * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
404
+ * Time Complexity: O(1)
405
+ * Space Complexity: O(1)
394
406
  */
395
407
 
396
408
  /**
397
- * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
398
- * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
409
+ * Time Complexity: O(1)
410
+ * Space Complexity: O(1)
399
411
  *
400
412
  * The function `_balanceRR` performs a right-right rotation to balance a binary tree.
401
413
  * @param {N} A - A is a node in a binary tree.
@@ -432,13 +444,13 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
432
444
  }
433
445
 
434
446
  /**
435
- * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
436
- * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
447
+ * Time Complexity: O(1)
448
+ * Space Complexity: O(1)
437
449
  */
438
450
 
439
451
  /**
440
- * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
441
- * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
452
+ * Time Complexity: O(1)
453
+ * Space Complexity: O(1)
442
454
  *
443
455
  * The function `_balanceRL` performs a right-left rotation to balance a binary tree.
444
456
  * @param {N} A - A is a node in a binary tree.
@@ -489,6 +501,6 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
489
501
  protected _replaceNode(oldNode: N, newNode: N): N {
490
502
  newNode.height = oldNode.height;
491
503
 
492
- return super._replaceNode(oldNode, newNode)
504
+ return super._replaceNode(oldNode, newNode);
493
505
  }
494
506
  }