queue-typed 1.47.5 → 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 (63) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +8 -8
  2. package/dist/data-structures/binary-tree/avl-tree.js +23 -15
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +65 -28
  4. package/dist/data-structures/binary-tree/binary-tree.js +66 -82
  5. package/dist/data-structures/binary-tree/bst.d.ts +38 -37
  6. package/dist/data-structures/binary-tree/bst.js +56 -40
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -7
  8. package/dist/data-structures/binary-tree/rb-tree.js +26 -17
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +16 -16
  10. package/dist/data-structures/binary-tree/tree-multimap.js +31 -22
  11. package/dist/data-structures/graph/abstract-graph.js +1 -1
  12. package/dist/data-structures/heap/heap.d.ts +19 -21
  13. package/dist/data-structures/heap/heap.js +52 -34
  14. package/dist/data-structures/heap/max-heap.d.ts +2 -5
  15. package/dist/data-structures/heap/max-heap.js +2 -2
  16. package/dist/data-structures/heap/min-heap.d.ts +2 -5
  17. package/dist/data-structures/heap/min-heap.js +2 -2
  18. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  19. package/dist/data-structures/linked-list/doubly-linked-list.js +9 -1
  20. package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  21. package/dist/data-structures/linked-list/singly-linked-list.js +8 -1
  22. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  23. package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
  24. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  25. package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
  26. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
  27. package/dist/data-structures/priority-queue/priority-queue.js +2 -2
  28. package/dist/data-structures/queue/deque.d.ts +1 -0
  29. package/dist/data-structures/queue/deque.js +3 -0
  30. package/dist/data-structures/queue/queue.d.ts +1 -0
  31. package/dist/data-structures/queue/queue.js +3 -0
  32. package/dist/data-structures/stack/stack.d.ts +2 -1
  33. package/dist/data-structures/stack/stack.js +10 -2
  34. package/dist/interfaces/binary-tree.d.ts +3 -1
  35. package/dist/types/common.d.ts +2 -0
  36. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  37. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
  38. package/dist/types/data-structures/heap/heap.d.ts +4 -1
  39. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  40. package/package.json +2 -2
  41. package/src/data-structures/binary-tree/avl-tree.ts +27 -17
  42. package/src/data-structures/binary-tree/binary-tree.ts +114 -97
  43. package/src/data-structures/binary-tree/bst.ts +67 -47
  44. package/src/data-structures/binary-tree/rb-tree.ts +34 -20
  45. package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
  46. package/src/data-structures/graph/abstract-graph.ts +1 -1
  47. package/src/data-structures/heap/heap.ts +57 -39
  48. package/src/data-structures/heap/max-heap.ts +5 -5
  49. package/src/data-structures/heap/min-heap.ts +5 -5
  50. package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
  51. package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
  52. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  53. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  54. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  55. package/src/data-structures/queue/deque.ts +4 -0
  56. package/src/data-structures/queue/queue.ts +4 -0
  57. package/src/data-structures/stack/stack.ts +12 -3
  58. package/src/interfaces/binary-tree.ts +13 -1
  59. package/src/types/common.ts +5 -1
  60. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  61. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  62. package/src/types/data-structures/heap/heap.ts +3 -1
  63. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
@@ -5,8 +5,8 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, BTNKey } from '../../types';
9
- import { CP, IterationType } from '../../types';
8
+ import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, BTNKey, Comparator } from '../../types';
9
+ import { CP, IterableEntriesOrKeys, IterationType } from '../../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBinaryTree } from '../../interfaces';
12
12
  import { Queue } from '../queue';
@@ -66,21 +66,23 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
66
66
  extends BinaryTree<V, N, TREE>
67
67
  implements IBinaryTree<V, N, TREE> {
68
68
 
69
- override options: BSTOptions;
70
-
71
69
  /**
72
70
  * The constructor function initializes a binary search tree with an optional comparator function.
73
71
  * @param {BSTOptions} [options] - An optional object that contains additional configuration options
74
72
  * for the binary search tree.
75
73
  */
76
- constructor(options?: BSTOptions) {
77
- super(options);
74
+ constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<BSTOptions>) {
75
+ super([], options);
76
+
78
77
  if (options) {
79
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b, ...options }
80
- } else {
81
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
78
+ const { comparator } = options;
79
+ if (comparator) {
80
+ this.comparator = comparator;
81
+ }
82
82
  }
83
+
83
84
  this._root = undefined;
85
+ if (elements) this.init(elements);
84
86
  }
85
87
 
86
88
  protected override _root?: N;
@@ -92,6 +94,8 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
92
94
  return this._root;
93
95
  }
94
96
 
97
+ comparator: Comparator<BTNKey> = (a, b) => a - b
98
+
95
99
  /**
96
100
  * The function creates a new binary search tree node with the given key and value.
97
101
  * @param {BTNKey} key - The key parameter is the key value that will be associated with
@@ -104,15 +108,13 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
104
108
  return new BSTNode<V, N>(key, value) as N;
105
109
  }
106
110
 
107
- override createTree(options?: BSTOptions): TREE {
108
- return new BST<V, N, TREE>({ ...this.options, ...options }) as TREE;
111
+ override createTree(options?: Partial<BSTOptions>): TREE {
112
+ return new BST<V, N, TREE>([], {
113
+ iterationType: this.iterationType,
114
+ comparator: this.comparator, ...options
115
+ }) as TREE;
109
116
  }
110
117
 
111
- /**
112
- * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
113
- * Space Complexity: O(1) - Constant space is used.
114
- */
115
-
116
118
  /**
117
119
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
118
120
  * Space Complexity: O(1) - Constant space is used.
@@ -193,8 +195,8 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
193
195
  }
194
196
 
195
197
  /**
196
- * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
197
- * Space Complexity: O(n) - Additional space is required for the sorted array.
198
+ * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
199
+ * Space Complexity: O(1) - Constant space is used.
198
200
  */
199
201
 
200
202
  /**
@@ -221,7 +223,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
221
223
  keysOrNodes: (BTNKey | N | undefined)[],
222
224
  data?: (V | undefined)[],
223
225
  isBalanceAdd = true,
224
- iterationType = this.options.iterationType
226
+ iterationType = this.iterationType
225
227
  ): (N | undefined)[] {
226
228
  // TODO this addMany function is inefficient, it should be optimized
227
229
  function hasNoUndefined(arr: (BTNKey | N | undefined)[]): arr is (BTNKey | N)[] {
@@ -297,8 +299,8 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
297
299
  }
298
300
 
299
301
  /**
300
- * Time Complexity: O(log n) - Average case for a balanced tree.
301
- * Space Complexity: O(1) - Constant space is used.
302
+ * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
303
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
302
304
  */
303
305
 
304
306
  /**
@@ -316,7 +318,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
316
318
  * the key of the leftmost node if the comparison result is greater than, and the key of the
317
319
  * rightmost node otherwise. If no node is found, it returns 0.
318
320
  */
319
- lastKey(beginRoot: BTNKey | N | undefined = this.root, iterationType = this.options.iterationType): BTNKey {
321
+ lastKey(beginRoot: BTNKey | N | undefined = this.root, iterationType = this.iterationType): BTNKey {
320
322
  if (this._compare(0, 1) === CP.lt) return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
321
323
  else if (this._compare(0, 1) === CP.gt) return this.getLeftMost(beginRoot, iterationType)?.key ?? 0;
322
324
  else return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
@@ -324,7 +326,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
324
326
 
325
327
  /**
326
328
  * Time Complexity: O(log n) - Average case for a balanced tree.
327
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
329
+ * Space Complexity: O(1) - Constant space is used.
328
330
  */
329
331
 
330
332
  /**
@@ -366,6 +368,11 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
366
368
  }
367
369
  }
368
370
 
371
+ /**
372
+ * Time Complexity: O(log n) - Average case for a balanced tree.
373
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
374
+ */
375
+
369
376
  /**
370
377
  * The function `ensureNotKey` returns the node corresponding to the given key if it is a node key,
371
378
  * otherwise it returns the key itself.
@@ -379,11 +386,6 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
379
386
  return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
380
387
  }
381
388
 
382
- /**
383
- * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
384
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
385
- */
386
-
387
389
  /**
388
390
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
389
391
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -412,7 +414,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
412
414
  callback: C = this._defaultOneParamCallback as C,
413
415
  onlyOne = false,
414
416
  beginRoot: BTNKey | N | undefined = this.root,
415
- iterationType = this.options.iterationType
417
+ iterationType = this.iterationType
416
418
  ): N[] {
417
419
  beginRoot = this.ensureNotKey(beginRoot);
418
420
  if (!beginRoot) return [];
@@ -493,7 +495,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
493
495
  callback: C = this._defaultOneParamCallback as C,
494
496
  lesserOrGreater: CP = CP.lt,
495
497
  targetNode: BTNKey | N | undefined = this.root,
496
- iterationType = this.options.iterationType
498
+ iterationType = this.iterationType
497
499
  ): ReturnType<C>[] {
498
500
  targetNode = this.ensureNotKey(targetNode);
499
501
  const ans: ReturnType<BTNCallback<N>>[] = [];
@@ -531,18 +533,8 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
531
533
  }
532
534
 
533
535
  /**
534
- * Balancing Adjustment:
535
- * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
536
- * AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
537
- *
538
- * Use Cases and Efficiency:
539
- * Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
540
- * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
541
- */
542
-
543
- /**
544
- * Time Complexity: O(n) - Building a balanced tree from a sorted array.
545
- * Space Complexity: O(n) - Additional space is required for the sorted array.
536
+ * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
537
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
546
538
  */
547
539
 
548
540
  /**
@@ -556,7 +548,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
556
548
  * values:
557
549
  * @returns The function `perfectlyBalance` returns a boolean value.
558
550
  */
559
- perfectlyBalance(iterationType = this.options.iterationType): boolean {
551
+ perfectlyBalance(iterationType = this.iterationType): boolean {
560
552
  const sorted = this.dfs(node => node, 'in'),
561
553
  n = sorted.length;
562
554
  this.clear();
@@ -595,8 +587,18 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
595
587
  }
596
588
 
597
589
  /**
598
- * Time Complexity: O(n) - Visiting each node once.
599
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
590
+ * Balancing Adjustment:
591
+ * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
592
+ * AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
593
+ *
594
+ * Use Cases and Efficiency:
595
+ * Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
596
+ * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
597
+ */
598
+
599
+ /**
600
+ * Time Complexity: O(n) - Building a balanced tree from a sorted array.
601
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
600
602
  */
601
603
 
602
604
  /**
@@ -608,7 +610,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
608
610
  * to check if the AVL tree is balanced. It can have two possible values:
609
611
  * @returns a boolean value.
610
612
  */
611
- isAVLBalanced(iterationType = this.options.iterationType): boolean {
613
+ isAVLBalanced(iterationType = this.iterationType): boolean {
612
614
  if (!this.root) return true;
613
615
 
614
616
  let balanced = true;
@@ -652,6 +654,24 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
652
654
  return balanced;
653
655
  }
654
656
 
657
+ /**
658
+ * Time Complexity: O(n) - Visiting each node once.
659
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
660
+ */
661
+
662
+ init(elements: IterableEntriesOrKeys<V>): void {
663
+ if (elements) {
664
+ for (const entryOrKey of elements) {
665
+ if (Array.isArray(entryOrKey)) {
666
+ const [key, value] = entryOrKey;
667
+ this.add(key, value);
668
+ } else {
669
+ this.add(entryOrKey);
670
+ }
671
+ }
672
+ }
673
+ }
674
+
655
675
  protected _setRoot(v: N | undefined) {
656
676
  if (v) {
657
677
  v.parent = undefined;
@@ -668,7 +688,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
668
688
  * than), CP.lt (less than), or CP.eq (equal).
669
689
  */
670
690
  protected _compare(a: BTNKey, b: BTNKey): CP {
671
- const compared = this.options.comparator!(a, b);
691
+ const compared = this.comparator(a, b);
672
692
  if (compared > 0) return CP.gt;
673
693
  else if (compared < 0) return CP.lt;
674
694
  else return CP.eq;
@@ -10,6 +10,7 @@ import {
10
10
  BiTreeDeleteResult,
11
11
  BTNCallback,
12
12
  BTNKey,
13
+ IterableEntriesOrKeys,
13
14
  IterationType,
14
15
  RBTNColor,
15
16
  RBTreeOptions,
@@ -43,21 +44,18 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
43
44
  extends BST<V, N, TREE>
44
45
  implements IBinaryTree<V, N, TREE> {
45
46
  Sentinel: N = new RedBlackTreeNode<V>(NaN) as unknown as N;
46
- override options: RBTreeOptions;
47
+
47
48
 
48
49
  /**
49
50
  * The constructor function initializes a Red-Black Tree with an optional set of options.
50
51
  * @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
51
52
  * passed to the constructor. It is used to configure the RBTree object with specific options.
52
53
  */
53
- constructor(options?: RBTreeOptions) {
54
- super(options);
55
- if (options) {
56
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b, ...options }
57
- } else {
58
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
59
- }
54
+ constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<RBTreeOptions>) {
55
+ super([], options);
56
+
60
57
  this._root = this.Sentinel;
58
+ if (elements) this.init(elements);
61
59
  }
62
60
 
63
61
  protected _root: N;
@@ -77,14 +75,12 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
77
75
  }
78
76
 
79
77
  override createTree(options?: RBTreeOptions): TREE {
80
- return new RedBlackTree<V, N, TREE>({ ...this.options, ...options }) as TREE;
78
+ return new RedBlackTree<V, N, TREE>([], {
79
+ iterationType: this.iterationType,
80
+ comparator: this.comparator, ...options
81
+ }) as TREE;
81
82
  }
82
83
 
83
- /**
84
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
85
- * Space Complexity: O(1)
86
- */
87
-
88
84
  /**
89
85
  * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
90
86
  * Space Complexity: O(1)
@@ -235,6 +231,11 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
235
231
  return ans;
236
232
  }
237
233
 
234
+ /**
235
+ * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
236
+ * Space Complexity: O(1)
237
+ */
238
+
238
239
  override isRealNode(node: N | undefined): node is N {
239
240
  return node !== this.Sentinel && node !== undefined;
240
241
  }
@@ -260,11 +261,6 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
260
261
  iterationType?: IterationType
261
262
  ): N | undefined;
262
263
 
263
- /**
264
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
265
- * Space Complexity: O(1)
266
- */
267
-
268
264
  /**
269
265
  * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
270
266
  * Space Complexity: O(1)
@@ -290,7 +286,7 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
290
286
  identifier: ReturnType<C> | undefined,
291
287
  callback: C = this._defaultOneParamCallback as C,
292
288
  beginRoot: BTNKey | N | undefined = this.root,
293
- iterationType = this.options.iterationType
289
+ iterationType = this.iterationType
294
290
  ): N | null | undefined {
295
291
  if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
296
292
  beginRoot = this.ensureNotKey(beginRoot);
@@ -351,11 +347,29 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
351
347
  return y!;
352
348
  }
353
349
 
350
+ /**
351
+ * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
352
+ * Space Complexity: O(1)
353
+ */
354
+
354
355
  override clear() {
355
356
  this._root = this.Sentinel;
356
357
  this._size = 0;
357
358
  }
358
359
 
360
+ init(elements: IterableEntriesOrKeys<V>): void {
361
+ if (elements) {
362
+ for (const entryOrKey of elements) {
363
+ if (Array.isArray(entryOrKey)) {
364
+ const [key, value] = entryOrKey;
365
+ this.add(key, value);
366
+ } else {
367
+ this.add(entryOrKey);
368
+ }
369
+ }
370
+ }
371
+ }
372
+
359
373
  protected override _setRoot(v: N) {
360
374
  if (v) {
361
375
  v.parent = undefined;
@@ -6,7 +6,15 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type { BTNKey, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
9
- import { BiTreeDeleteResult, BTNCallback, CP, FamilyPosition, IterationType, TreeMultimapNested } from '../../types';
9
+ import {
10
+ BiTreeDeleteResult,
11
+ BTNCallback,
12
+ CP,
13
+ FamilyPosition,
14
+ IterableEntriesOrKeys,
15
+ IterationType,
16
+ TreeMultimapNested
17
+ } from '../../types';
10
18
  import { IBinaryTree } from '../../interfaces';
11
19
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
20
 
@@ -40,21 +48,15 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
40
48
  extends AVLTree<V, N, TREE>
41
49
  implements IBinaryTree<V, N, TREE> {
42
50
 
43
- override options: TreeMultimapOptions;
44
-
45
51
  /**
46
52
  * The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
47
53
  * merge duplicated values.
48
54
  * @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
49
55
  * TreeMultimap.
50
56
  */
51
- constructor(options: TreeMultimapOptions = { iterationType: IterationType.ITERATIVE }) {
52
- super(options);
53
- if (options) {
54
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b, ...options }
55
- } else {
56
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
57
- }
57
+ constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<TreeMultimapOptions>) {
58
+ super([], options);
59
+ if (elements) this.init(elements);
58
60
  }
59
61
 
60
62
  private _count = 0;
@@ -77,14 +79,12 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
77
79
  }
78
80
 
79
81
  override createTree(options?: TreeMultimapOptions): TREE {
80
- return new TreeMultimap<V, N, TREE>({ ...this.options, ...options }) as TREE;
82
+ return new TreeMultimap<V, N, TREE>([], {
83
+ iterationType: this.iterationType,
84
+ comparator: this.comparator, ...options
85
+ }) as TREE;
81
86
  }
82
87
 
83
- /**
84
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
85
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
86
- */
87
-
88
88
  /**
89
89
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
90
90
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -169,8 +169,8 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
169
169
  }
170
170
 
171
171
  /**
172
- * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
173
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
172
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
173
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
174
174
  */
175
175
 
176
176
  /**
@@ -208,8 +208,8 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
208
208
  }
209
209
 
210
210
  /**
211
- * Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
212
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
211
+ * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
212
+ * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
213
213
  */
214
214
 
215
215
  /**
@@ -223,7 +223,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
223
223
  * values:
224
224
  * @returns a boolean value.
225
225
  */
226
- override perfectlyBalance(iterationType = this.options.iterationType): boolean {
226
+ override perfectlyBalance(iterationType = this.iterationType): boolean {
227
227
  const sorted = this.dfs(node => node, 'in'),
228
228
  n = sorted.length;
229
229
  if (sorted.length < 1) return false;
@@ -262,8 +262,8 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
262
262
  }
263
263
 
264
264
  /**
265
- * Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
266
- * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
265
+ * Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
266
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
267
267
  */
268
268
 
269
269
  /**
@@ -346,8 +346,8 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
346
346
  }
347
347
 
348
348
  /**
349
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
350
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
349
+ * Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
350
+ * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
351
351
  */
352
352
 
353
353
  /**
@@ -358,6 +358,24 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
358
358
  this._count = 0;
359
359
  }
360
360
 
361
+ /**
362
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
363
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
364
+ */
365
+
366
+ init(elements: IterableEntriesOrKeys<V>): void {
367
+ if (elements) {
368
+ for (const entryOrKey of elements) {
369
+ if (Array.isArray(entryOrKey)) {
370
+ const [key, value] = entryOrKey;
371
+ this.add(key, value);
372
+ } else {
373
+ this.add(entryOrKey);
374
+ }
375
+ }
376
+ }
377
+ }
378
+
361
379
  /**
362
380
  * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
363
381
  * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
@@ -691,7 +691,7 @@ export abstract class AbstractGraph<
691
691
  if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
692
692
  }
693
693
 
694
- const heap = new PriorityQueue<{ key: number; value: VO }>({ comparator: (a, b) => a.key - b.key });
694
+ const heap = new PriorityQueue<{ key: number; value: VO }>([], { comparator: (a, b) => a.key - b.key });
695
695
  heap.add({ key: 0, value: srcVertex });
696
696
 
697
697
  distMap.set(srcVertex, 0);