data-structure-typed 2.2.6 → 2.2.8

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 (72) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/CONTRIBUTING.md +47 -1
  3. package/README.md +19 -8
  4. package/README_CN.md +119 -275
  5. package/benchmark/report.html +1 -1
  6. package/benchmark/report.json +20 -324
  7. package/dist/cjs/index.cjs +109 -107
  8. package/dist/cjs/index.cjs.map +1 -1
  9. package/dist/cjs-legacy/index.cjs +109 -107
  10. package/dist/cjs-legacy/index.cjs.map +1 -1
  11. package/dist/esm/index.mjs +109 -107
  12. package/dist/esm/index.mjs.map +1 -1
  13. package/dist/esm-legacy/index.mjs +109 -107
  14. package/dist/esm-legacy/index.mjs.map +1 -1
  15. package/dist/leetcode/avl-tree-counter.mjs +2957 -0
  16. package/dist/leetcode/avl-tree-multi-map.mjs +2889 -0
  17. package/dist/leetcode/avl-tree.mjs +2720 -0
  18. package/dist/leetcode/binary-tree.mjs +1594 -0
  19. package/dist/leetcode/bst.mjs +2398 -0
  20. package/dist/leetcode/deque.mjs +683 -0
  21. package/dist/leetcode/directed-graph.mjs +1733 -0
  22. package/dist/leetcode/doubly-linked-list.mjs +709 -0
  23. package/dist/leetcode/hash-map.mjs +493 -0
  24. package/dist/leetcode/heap.mjs +542 -0
  25. package/dist/leetcode/max-heap.mjs +375 -0
  26. package/dist/leetcode/max-priority-queue.mjs +383 -0
  27. package/dist/leetcode/min-heap.mjs +363 -0
  28. package/dist/leetcode/min-priority-queue.mjs +371 -0
  29. package/dist/leetcode/priority-queue.mjs +363 -0
  30. package/dist/leetcode/queue.mjs +943 -0
  31. package/dist/leetcode/red-black-tree.mjs +2765 -0
  32. package/dist/leetcode/singly-linked-list.mjs +754 -0
  33. package/dist/leetcode/stack.mjs +217 -0
  34. package/dist/leetcode/tree-counter.mjs +3039 -0
  35. package/dist/leetcode/tree-multi-map.mjs +2913 -0
  36. package/dist/leetcode/trie.mjs +413 -0
  37. package/dist/leetcode/undirected-graph.mjs +1650 -0
  38. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
  39. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  40. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +10 -10
  41. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -23
  42. package/dist/types/data-structures/binary-tree/bst.d.ts +11 -11
  43. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
  44. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +1 -1
  45. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  46. package/dist/umd/data-structure-typed.js +105 -103
  47. package/dist/umd/data-structure-typed.js.map +1 -1
  48. package/dist/umd/data-structure-typed.min.js +2 -2
  49. package/dist/umd/data-structure-typed.min.js.map +1 -1
  50. package/package.json +48 -171
  51. package/src/data-structures/binary-tree/avl-tree-counter.ts +6 -6
  52. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -13
  53. package/src/data-structures/binary-tree/avl-tree.ts +15 -15
  54. package/src/data-structures/binary-tree/binary-tree.ts +53 -55
  55. package/src/data-structures/binary-tree/bst.ts +21 -22
  56. package/src/data-structures/binary-tree/red-black-tree.ts +3 -3
  57. package/src/data-structures/binary-tree/tree-counter.ts +4 -4
  58. package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
  59. package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +1 -2
  60. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +30 -30
  61. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +46 -46
  62. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +43 -43
  63. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +151 -151
  64. package/test/unit/data-structures/binary-tree/bst.test.ts +99 -99
  65. package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
  66. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +141 -141
  67. package/test/unit/data-structures/binary-tree/tree-counter.test.ts +37 -37
  68. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +145 -145
  69. package/tsup.config.js +50 -21
  70. package/tsup.leetcode.config.js +1 -1
  71. package/tsup.umd.config.js +29 -0
  72. package/tsup.node.config.js +0 -83
@@ -71,7 +71,7 @@ export class BinaryTreeNode<K = any, V = any> {
71
71
  */
72
72
  set left(v: BinaryTreeNode<K, V> | null | undefined) {
73
73
  if (v) {
74
- v.parent = this as unknown as BinaryTreeNode<K, V>;
74
+ v.parent = this;
75
75
  }
76
76
  this._left = v;
77
77
  }
@@ -193,7 +193,7 @@ export class BinaryTreeNode<K = any, V = any> {
193
193
  *
194
194
  * @remarks
195
195
  * This class implements a basic Binary Tree, not a Binary Search Tree.
196
- * The `add` operation inserts nodes level-by-level (BFS) into the first available slot.
196
+ * The `set` operation inserts nodes level-by-level (BFS) into the first available slot.
197
197
  *
198
198
  * @template K - The type of the key.
199
199
  * @template V - The type of the value.
@@ -225,7 +225,7 @@ export class BinaryTreeNode<K = any, V = any> {
225
225
  * console.log(tree.size); // 9;
226
226
  *
227
227
  * // Add new element
228
- * tree.add(10, 'ten');
228
+ * tree.set(10, 'ten');
229
229
  * console.log(tree.size); // 10;
230
230
  * @example
231
231
  * // BinaryTree get and has operations
@@ -353,9 +353,9 @@ export class BinaryTree<K = any, V = any, R = any>
353
353
 
354
354
  /**
355
355
  * Creates an instance of BinaryTree.
356
- * @remarks Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `add` operation). Space O(N) for storing the nodes.
356
+ * @remarks Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `set` operation). Space O(N) for storing the nodes.
357
357
  *
358
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
358
+ * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
359
359
  * @param [options] - Configuration options for the tree.
360
360
  */
361
361
  constructor(
@@ -374,7 +374,7 @@ export class BinaryTree<K = any, V = any, R = any>
374
374
  else if (toEntryFn) throw TypeError('toEntryFn must be a function type');
375
375
  }
376
376
 
377
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
377
+ if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
378
378
  }
379
379
 
380
380
  protected _isMapMode = true;
@@ -640,13 +640,27 @@ export class BinaryTree<K = any, V = any, R = any>
640
640
  * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
641
641
  *
642
642
  * @param keyNodeOrEntry - The key, node, or entry to add.
643
- * @param [value] - The value, if providing just a key.
644
643
  * @returns True if the addition was successful, false otherwise.
645
644
  */
646
645
  add(
646
+ keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
647
+ ): boolean {
648
+ return this.set(keyNodeOrEntry);
649
+ }
650
+
651
+ /**
652
+ * Adds or updates a new node to the tree.
653
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation sets the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
654
+ *
655
+ * @param keyNodeOrEntry - The key, node, or entry to set or update.
656
+ * @param [value] - The value, if providing just a key.
657
+ * @returns True if the addition was successful, false otherwise.
658
+ */
659
+ set(
647
660
  keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
648
661
  value?: V
649
662
  ): boolean {
663
+
650
664
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
651
665
  if (newNode === undefined) return false;
652
666
 
@@ -699,29 +713,30 @@ export class BinaryTree<K = any, V = any, R = any>
699
713
  }
700
714
 
701
715
  /**
702
- * Adds or updates a new node to the tree.
703
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
716
+ * Adds multiple items to the tree.
717
+ * @remarks Time O(N * M), where N is the number of items to set and M is the size of the tree at insertion (due to O(M) `set` operation). Space O(M) (from `set`) + O(N) (for the `inserted` array).
704
718
  *
705
- * @param keyNodeOrEntry - The key, node, or entry to add or update.
706
- * @param [value] - The value, if providing just a key.
707
- * @returns True if the addition was successful, false otherwise.
719
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
720
+ * @param [values] - An optional parallel iterable of values.
721
+ * @returns An array of booleans indicating the success of each individual `set` operation.
708
722
  */
709
- set(
710
- keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
711
- value?: V
712
- ): boolean {
713
- return this.add(keyNodeOrEntry, value);
723
+ addMany(
724
+ keysNodesEntriesOrRaws: Iterable<
725
+ K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
726
+ >
727
+ ): boolean[] {
728
+ return this.setMany(keysNodesEntriesOrRaws);
714
729
  }
715
730
 
716
731
  /**
717
- * Adds multiple items to the tree.
718
- * @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
732
+ * Adds or updates multiple items to the tree.
733
+ * @remarks Time O(N * M), where N is the number of items to set and M is the size of the tree at insertion (due to O(M) `set` operation). Space O(M) (from `set`) + O(N) (for the `inserted` array).
719
734
  *
720
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
735
+ * @param keysNodesEntriesOrRaws - An iterable of items to set or update.
721
736
  * @param [values] - An optional parallel iterable of values.
722
- * @returns An array of booleans indicating the success of each individual `add` operation.
737
+ * @returns An array of booleans indicating the success of each individual `set` operation.
723
738
  */
724
- addMany(
739
+ setMany(
725
740
  keysNodesEntriesOrRaws: Iterable<
726
741
  K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
727
742
  >,
@@ -744,44 +759,27 @@ export class BinaryTree<K = any, V = any, R = any>
744
759
  }
745
760
  }
746
761
  if (this.isRaw(keyNodeEntryOrRaw)) keyNodeEntryOrRaw = this._toEntryFn!(keyNodeEntryOrRaw);
747
- inserted.push(this.add(keyNodeEntryOrRaw, value));
762
+ inserted.push(this.set(keyNodeEntryOrRaw, value));
748
763
  }
749
764
 
750
765
  return inserted;
751
766
  }
752
767
 
753
768
  /**
754
- * Adds or updates multiple items to the tree.
755
- * @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
756
- *
757
- * @param keysNodesEntriesOrRaws - An iterable of items to add or update.
758
- * @param [values] - An optional parallel iterable of values.
759
- * @returns An array of booleans indicating the success of each individual `add` operation.
760
- */
761
- setMany(
762
- keysNodesEntriesOrRaws: Iterable<
763
- K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
764
- >,
765
- values?: Iterable<V | undefined>
766
- ): boolean[] {
767
- return this.addMany(keysNodesEntriesOrRaws, values);
768
- }
769
-
770
- /**
771
- * Merges another tree into this one by adding all its nodes.
772
- * @remarks Time O(N * M), same as `addMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `add`).
769
+ * Merges another tree into this one by seting all its nodes.
770
+ * @remarks Time O(N * M), same as `setMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `set`).
773
771
  *
774
772
  * @param anotherTree - The tree to merge.
775
773
  */
776
774
  merge(anotherTree: BinaryTree<K, V, R>) {
777
- this.addMany(anotherTree, []);
775
+ this.setMany(anotherTree, []);
778
776
  }
779
777
 
780
778
  /**
781
779
  * Clears the tree and refills it with new items.
782
- * @remarks Time O(N) (for `clear`) + O(N * M) (for `addMany`) = O(N * M). Space O(M) (from `addMany`).
780
+ * @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
783
781
  *
784
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
782
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
785
783
  * @param [values] - An optional parallel iterable of values.
786
784
  */
787
785
  refill(
@@ -791,7 +789,7 @@ export class BinaryTree<K = any, V = any, R = any>
791
789
  values?: Iterable<V | undefined>
792
790
  ): void {
793
791
  this.clear();
794
- this.addMany(keysNodesEntriesOrRaws, values);
792
+ this.setMany(keysNodesEntriesOrRaws, values);
795
793
  }
796
794
 
797
795
  /**
@@ -1830,7 +1828,7 @@ export class BinaryTree<K = any, V = any, R = any>
1830
1828
 
1831
1829
  /**
1832
1830
  * Clones the tree.
1833
- * @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `add`, and `add` is O(M)). Space O(N) for the new tree and the BFS queue.
1831
+ * @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `set`, and `set` is O(M)). Space O(N) for the new tree and the BFS queue.
1834
1832
  *
1835
1833
  * @returns A new, cloned instance of the tree.
1836
1834
  */
@@ -1842,7 +1840,7 @@ export class BinaryTree<K = any, V = any, R = any>
1842
1840
 
1843
1841
  /**
1844
1842
  * Creates a new tree containing only the entries that satisfy the predicate.
1845
- * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion (O(N) iteration + O(M) `add` for each item). Space O(N) for the new tree.
1843
+ * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion (O(N) iteration + O(M) `set` for each item). Space O(N) for the new tree.
1846
1844
  *
1847
1845
  * @param predicate - A function to test each [key, value] pair.
1848
1846
  * @param [thisArg] - `this` context for the predicate.
@@ -1851,7 +1849,7 @@ export class BinaryTree<K = any, V = any, R = any>
1851
1849
  filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): this {
1852
1850
  const out = this._createInstance<K, V, R>();
1853
1851
  let i = 0;
1854
- for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.add([k, v]);
1852
+ for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.set([k, v]);
1855
1853
  return out;
1856
1854
  }
1857
1855
 
@@ -1874,7 +1872,7 @@ export class BinaryTree<K = any, V = any, R = any>
1874
1872
  ): BinaryTree<MK, MV, MR> {
1875
1873
  const out = this._createLike<MK, MV, MR>([], options);
1876
1874
  let i = 0;
1877
- for (const [k, v] of this) out.add(cb.call(thisArg, v, k, i++, this));
1875
+ for (const [k, v] of this) out.set(cb.call(thisArg, v, k, i++, this));
1878
1876
  return out;
1879
1877
  }
1880
1878
 
@@ -2204,8 +2202,8 @@ export class BinaryTree<K = any, V = any, R = any>
2204
2202
  }
2205
2203
 
2206
2204
  /**
2207
- * (Protected) Helper for cloning. Performs a BFS and adds all nodes to the new tree.
2208
- * @remarks Time O(N * M) (O(N) BFS + O(M) `add` for each node).
2205
+ * (Protected) Helper for cloning. Performs a BFS and sets all nodes to the new tree.
2206
+ * @remarks Time O(N * M) (O(N) BFS + O(M) `set` for each node).
2209
2207
  *
2210
2208
  * @param cloned - The new, empty tree instance to populate.
2211
2209
  */
@@ -2213,10 +2211,10 @@ export class BinaryTree<K = any, V = any, R = any>
2213
2211
  // Use BFS with nulls to preserve the tree structure
2214
2212
  this.bfs(
2215
2213
  node => {
2216
- if (node === null) cloned.add(null);
2214
+ if (node === null) cloned.set(null);
2217
2215
  else {
2218
- if (this._isMapMode) cloned.add([node.key, this._store.get(node.key)]);
2219
- else cloned.add([node.key, node.value]);
2216
+ if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
2217
+ else cloned.set([node.key, node.value]);
2220
2218
  }
2221
2219
  },
2222
2220
  this._root,
@@ -219,8 +219,8 @@ export class BSTNode<K = any, V = any> {
219
219
  * console.log(bst.size); // 16;
220
220
  *
221
221
  * // Add new elements
222
- * bst.add(17);
223
- * bst.add(0);
222
+ * bst.set(17);
223
+ * bst.set(0);
224
224
  * console.log(bst.size); // 18;
225
225
  *
226
226
  * // Verify keys are searchable
@@ -266,7 +266,7 @@ export class BSTNode<K = any, V = any> {
266
266
  *
267
267
  * // Merge datasets into a single BinarySearchTree
268
268
  * const merged = new BST<number, string>(dataset1);
269
- * merged.addMany(dataset2);
269
+ * merged.setMany(dataset2);
270
270
  * merged.merge(dataset3);
271
271
  *
272
272
  * // Verify merged dataset is in sorted order
@@ -350,7 +350,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
350
350
  * Creates an instance of BST.
351
351
  * @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
352
352
  *
353
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
353
+ * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
354
354
  * @param [options] - Configuration options for the BST, including comparator.
355
355
  */
356
356
  constructor(
@@ -370,7 +370,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
370
370
  this._comparator = this._createDefaultComparator();
371
371
  }
372
372
 
373
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
373
+ if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
374
374
  }
375
375
 
376
376
  protected override _root?: BSTNode<K, V> = undefined;
@@ -625,7 +625,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
625
625
  if (isRange) {
626
626
  predicate = node => {
627
627
  if (!node) return false;
628
- return (keyNodeEntryOrPredicate as Range<K>).isInRange(node.key, this._comparator);
628
+ return (keyNodeEntryOrPredicate).isInRange(node.key, this._comparator);
629
629
  };
630
630
  } else {
631
631
  predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
@@ -637,7 +637,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
637
637
  if (!this.isRealNode(cur.left)) return false;
638
638
  if (isRange) {
639
639
  // Range search: Only go left if the current key is >= the lower bound
640
- const range = keyNodeEntryOrPredicate as Range<K>;
640
+ const range = keyNodeEntryOrPredicate;
641
641
  const leftS = range.low;
642
642
  const leftI = range.includeLow;
643
643
  return (leftI && this._compare(cur.key, leftS) >= 0) || (!leftI && this._compare(cur.key, leftS) > 0);
@@ -655,7 +655,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
655
655
  if (!this.isRealNode(cur.right)) return false;
656
656
  if (isRange) {
657
657
  // Range search: Only go right if current key <= upper bound
658
- const range = keyNodeEntryOrPredicate as Range<K>;
658
+ const range = keyNodeEntryOrPredicate;
659
659
  const rightS = range.high;
660
660
  const rightI = range.includeHigh;
661
661
  return (rightI && this._compare(cur.key, rightS) <= 0) || (!rightI && this._compare(cur.key, rightS) < 0);
@@ -716,11 +716,11 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
716
716
  * Adds a new node to the BST based on key comparison.
717
717
  * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
718
718
  *
719
- * @param keyNodeOrEntry - The key, node, or entry to add.
719
+ * @param keyNodeOrEntry - The key, node, or entry to set.
720
720
  * @param [value] - The value, if providing just a key.
721
721
  * @returns True if the addition was successful, false otherwise.
722
722
  */
723
- override add(
723
+ override set(
724
724
  keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
725
725
  value?: V
726
726
  ): boolean {
@@ -766,17 +766,17 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
766
766
 
767
767
  /**
768
768
  * Adds multiple items to the tree.
769
- * @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced add).
769
+ * @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced set).
770
770
  * If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
771
771
  * Space O(N) for sorting and recursion/iteration stack.
772
772
  *
773
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
773
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
774
774
  * @param [values] - An optional parallel iterable of values.
775
775
  * @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
776
- * @param [iterationType=this.iterationType] - The traversal method for balanced add (recursive or iterative).
777
- * @returns An array of booleans indicating the success of each individual `add` operation.
776
+ * @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
777
+ * @returns An array of booleans indicating the success of each individual `set` operation.
778
778
  */
779
- override addMany(
779
+ override setMany(
780
780
  keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>,
781
781
  values?: Iterable<V | undefined>,
782
782
  isBalanceAdd = true,
@@ -790,7 +790,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
790
790
  for (let kve of keysNodesEntriesOrRaws) {
791
791
  const val = valuesIterator?.next().value;
792
792
  if (this.isRaw(kve)) kve = this._toEntryFn!(kve);
793
- inserted.push(this.add(kve, val));
793
+ inserted.push(this.set(kve, val));
794
794
  }
795
795
  return inserted;
796
796
  }
@@ -831,9 +831,9 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
831
831
  const { key, value, orgIndex } = arr[mid];
832
832
  if (this.isRaw(key)) {
833
833
  const entry = this._toEntryFn!(key);
834
- inserted[orgIndex] = this.add(entry);
834
+ inserted[orgIndex] = this.set(entry);
835
835
  } else {
836
- inserted[orgIndex] = this.add(key, value);
836
+ inserted[orgIndex] = this.set(key, value);
837
837
  }
838
838
  _dfs(arr.slice(0, mid));
839
839
  _dfs(arr.slice(mid + 1));
@@ -852,9 +852,9 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
852
852
  const { key, value, orgIndex } = sorted[m];
853
853
  if (this.isRaw(key)) {
854
854
  const entry = this._toEntryFn!(key);
855
- inserted[orgIndex] = this.add(entry);
855
+ inserted[orgIndex] = this.set(entry);
856
856
  } else {
857
- inserted[orgIndex] = this.add(key, value);
857
+ inserted[orgIndex] = this.set(key, value);
858
858
  }
859
859
  stack.push([m + 1, r]);
860
860
  stack.push([l, m - 1]);
@@ -1369,7 +1369,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
1369
1369
  let index = 0;
1370
1370
  // Iterates in-order
1371
1371
  for (const [key, value] of this) {
1372
- out.add(callback.call(thisArg, value, key, index++, this));
1372
+ out.set(callback.call(thisArg, value, key, index++, this));
1373
1373
  }
1374
1374
  return out;
1375
1375
  }
@@ -1441,7 +1441,6 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
1441
1441
  */
1442
1442
  protected _createDefaultComparator(): Comparator<K> {
1443
1443
  return (a: K, b: K): number => {
1444
- debugger;
1445
1444
  // If both keys are comparable (primitive types), use direct comparison
1446
1445
  if (isComparable(a) && isComparable(b)) {
1447
1446
  if (a > b) return 1;
@@ -292,7 +292,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
292
292
  this._root = this.NIL;
293
293
 
294
294
  if (keysNodesEntriesOrRaws) {
295
- this.addMany(keysNodesEntriesOrRaws);
295
+ this.setMany(keysNodesEntriesOrRaws);
296
296
  }
297
297
  }
298
298
 
@@ -350,7 +350,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
350
350
  * @param [value]- See parameter type for details.
351
351
  * @returns True if inserted or updated; false if ignored.
352
352
  */
353
- override add(
353
+ override set(
354
354
  keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
355
355
  value?: V
356
356
  ): boolean {
@@ -469,7 +469,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
469
469
 
470
470
  let index = 0;
471
471
  for (const [key, value] of this) {
472
- out.add(callback.call(thisArg, value, key, index++, this));
472
+ out.set(callback.call(thisArg, value, key, index++, this));
473
473
  }
474
474
  return out;
475
475
  }
@@ -204,7 +204,7 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
204
204
  options?: TreeCounterOptions<K, V, R>
205
205
  ) {
206
206
  super([], options);
207
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
207
+ if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
208
208
  }
209
209
 
210
210
  protected _count = 0;
@@ -255,14 +255,14 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
255
255
  * @param [count] - How much to increase the node's count (default 1).
256
256
  * @returns True if inserted/updated; false if ignored.
257
257
  */
258
- override add(
258
+ override set(
259
259
  keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
260
260
  value?: V,
261
261
  count = 1
262
262
  ): boolean {
263
263
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
264
264
  const orgCount = newNode?.count || 0;
265
- const isSuccessAdded = super.add(newNode, newValue);
265
+ const isSuccessAdded = super.set(newNode, newValue);
266
266
  if (isSuccessAdded) {
267
267
  this._count += orgCount;
268
268
  return true;
@@ -437,7 +437,7 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
437
437
 
438
438
  let index = 0;
439
439
  for (const [key, value] of this) {
440
- out.add(callback.call(thisArg, value, key, index++, this));
440
+ out.set(callback.call(thisArg, value, key, index++, this));
441
441
  }
442
442
  return out;
443
443
  }
@@ -364,7 +364,7 @@ export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[]
364
364
  ) {
365
365
  super([], { ...options });
366
366
  if (keysNodesEntriesOrRaws) {
367
- this.addMany(keysNodesEntriesOrRaws);
367
+ this.setMany(keysNodesEntriesOrRaws);
368
368
  }
369
369
  }
370
370
 
@@ -385,29 +385,29 @@ export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[]
385
385
  return keyNodeOrEntry instanceof TreeMultiMapNode;
386
386
  }
387
387
 
388
- override add(
388
+ override set(
389
389
  keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined
390
390
  ): boolean;
391
391
 
392
- override add(key: K, value: V): boolean;
392
+ override set(key: K, value: V): boolean;
393
393
 
394
394
  /**
395
395
  * Insert a value or a list of values into the multimap. If the key exists, values are appended.
396
396
  * @remarks Time O(log N + M), Space O(1)
397
397
  * @param keyNodeOrEntry - Key, node, or [key, values] entry.
398
- * @param [value] - Single value to add when a bare key is provided.
398
+ * @param [value] - Single value to set when a bare key is provided.
399
399
  * @returns True if inserted or appended; false if ignored.
400
400
  */
401
- override add(
401
+ override set(
402
402
  keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined,
403
403
  value?: V
404
404
  ): boolean {
405
- if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
405
+ if (this.isRealNode(keyNodeOrEntry)) return super.set(keyNodeOrEntry);
406
406
 
407
407
  const _commonAdd = (key?: BTNOptKeyOrNull<K>, values?: V[]) => {
408
408
  if (key === undefined || key === null) return false;
409
409
 
410
- const _addToValues = () => {
410
+ const _setToValues = () => {
411
411
  const existingValues = this.get(key);
412
412
  if (existingValues !== undefined && values !== undefined) {
413
413
  for (const value of values) existingValues.push(value);
@@ -416,12 +416,12 @@ export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[]
416
416
  return false;
417
417
  };
418
418
 
419
- const _addByNode = () => {
419
+ const _setByNode = () => {
420
420
  const existingNode = this.getNode(key);
421
421
  if (this.isRealNode(existingNode)) {
422
422
  const existingValues = this.get(existingNode);
423
423
  if (existingValues === undefined) {
424
- super.add(key, values);
424
+ super.set(key, values);
425
425
  return true;
426
426
  }
427
427
  if (values !== undefined) {
@@ -431,14 +431,14 @@ export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[]
431
431
  return false;
432
432
  }
433
433
  } else {
434
- return super.add(key, values);
434
+ return super.set(key, values);
435
435
  }
436
436
  };
437
437
 
438
438
  if (this._isMapMode) {
439
- return _addByNode() || _addToValues();
439
+ return _setByNode() || _setToValues();
440
440
  }
441
- return _addToValues() || _addByNode();
441
+ return _setToValues() || _setByNode();
442
442
  };
443
443
 
444
444
  if (this.isEntry(keyNodeOrEntry)) {
@@ -503,7 +503,7 @@ export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[]
503
503
  ): RedBlackTree<MK, MV, MR> {
504
504
  const out = this._createLike<MK, MV, MR>([], options);
505
505
  let i = 0;
506
- for (const [k, v] of this) out.add(callback.call(thisArg, v, k, i++, this));
506
+ for (const [k, v] of this) out.set(callback.call(thisArg, v, k, i++, this));
507
507
  return out;
508
508
  }
509
509
 
@@ -17,8 +17,7 @@ suite
17
17
  // for (let i = 0; i < randomArray.length; i++) rbTree.set(randomArray[i]);
18
18
  // })
19
19
  .add(`${MILLION.toLocaleString()} set`, () => {
20
- rbTree.clear();
21
- for (let i = 0; i < randomArray.length; i++) rbTree.set(i);
20
+ for (let i = 0; i < randomArray.length; i++) rbTree.set(randomArray[i], randomArray[i]);
22
21
  })
23
22
  .add(`${MILLION.toLocaleString()} get`, () => {
24
23
  for (let i = 0; i < randomArray.length; i++) rbTree.get(randomArray[i]);