binary-tree-typed 2.2.7 → 2.3.0

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 (30) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/index.cjs +41 -43
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +41 -43
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +41 -43
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +41 -43
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
  11. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  12. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +10 -10
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -24
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +11 -11
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
  16. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +1 -1
  17. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  18. package/dist/umd/binary-tree-typed.js +41 -43
  19. package/dist/umd/binary-tree-typed.js.map +1 -1
  20. package/dist/umd/binary-tree-typed.min.js +2 -2
  21. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  22. package/package.json +2 -2
  23. package/src/data-structures/binary-tree/avl-tree-counter.ts +6 -6
  24. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -13
  25. package/src/data-structures/binary-tree/avl-tree.ts +15 -15
  26. package/src/data-structures/binary-tree/binary-tree.ts +52 -55
  27. package/src/data-structures/binary-tree/bst.ts +21 -22
  28. package/src/data-structures/binary-tree/red-black-tree.ts +3 -3
  29. package/src/data-structures/binary-tree/tree-counter.ts +4 -4
  30. package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
package/README.md CHANGED
@@ -55,7 +55,7 @@ yarn add binary-tree-typed
55
55
  console.log(tree.size); // 9;
56
56
 
57
57
  // Add new element
58
- tree.add(10, 'ten');
58
+ tree.set(10, 'ten');
59
59
  console.log(tree.size); // 10;
60
60
  ```
61
61
 
@@ -1194,9 +1194,9 @@ var BinaryTree = class extends IterableEntryBase {
1194
1194
  iterationType = "ITERATIVE";
1195
1195
  /**
1196
1196
  * Creates an instance of BinaryTree.
1197
- * @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.
1197
+ * @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.
1198
1198
  *
1199
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
1199
+ * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
1200
1200
  * @param [options] - Configuration options for the tree.
1201
1201
  */
1202
1202
  constructor(keysNodesEntriesOrRaws = [], options) {
@@ -1209,7 +1209,7 @@ var BinaryTree = class extends IterableEntryBase {
1209
1209
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1210
1210
  else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
1211
1211
  }
1212
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
1212
+ if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1213
1213
  }
1214
1214
  _isMapMode = true;
1215
1215
  /**
@@ -1423,10 +1423,20 @@ var BinaryTree = class extends IterableEntryBase {
1423
1423
  * @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).
1424
1424
  *
1425
1425
  * @param keyNodeOrEntry - The key, node, or entry to add.
1426
+ * @returns True if the addition was successful, false otherwise.
1427
+ */
1428
+ add(keyNodeOrEntry) {
1429
+ return this.set(keyNodeOrEntry);
1430
+ }
1431
+ /**
1432
+ * Adds or updates a new node to the tree.
1433
+ * @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).
1434
+ *
1435
+ * @param keyNodeOrEntry - The key, node, or entry to set or update.
1426
1436
  * @param [value] - The value, if providing just a key.
1427
1437
  * @returns True if the addition was successful, false otherwise.
1428
1438
  */
1429
- add(keyNodeOrEntry, value) {
1439
+ set(keyNodeOrEntry, value) {
1430
1440
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
1431
1441
  if (newNode === void 0) return false;
1432
1442
  if (!this._root) {
@@ -1470,25 +1480,24 @@ var BinaryTree = class extends IterableEntryBase {
1470
1480
  return false;
1471
1481
  }
1472
1482
  /**
1473
- * Adds or updates a new node to the tree.
1474
- * @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).
1483
+ * Adds multiple items to the tree.
1484
+ * @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).
1475
1485
  *
1476
- * @param keyNodeOrEntry - The key, node, or entry to add or update.
1477
- * @param [value] - The value, if providing just a key.
1478
- * @returns True if the addition was successful, false otherwise.
1486
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
1487
+ * @returns An array of booleans indicating the success of each individual `set` operation.
1479
1488
  */
1480
- set(keyNodeOrEntry, value) {
1481
- return this.add(keyNodeOrEntry, value);
1489
+ addMany(keysNodesEntriesOrRaws) {
1490
+ return this.setMany(keysNodesEntriesOrRaws);
1482
1491
  }
1483
1492
  /**
1484
- * Adds multiple items to the tree.
1485
- * @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).
1493
+ * Adds or updates multiple items to the tree.
1494
+ * @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).
1486
1495
  *
1487
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
1496
+ * @param keysNodesEntriesOrRaws - An iterable of items to set or update.
1488
1497
  * @param [values] - An optional parallel iterable of values.
1489
- * @returns An array of booleans indicating the success of each individual `add` operation.
1498
+ * @returns An array of booleans indicating the success of each individual `set` operation.
1490
1499
  */
1491
- addMany(keysNodesEntriesOrRaws, values) {
1500
+ setMany(keysNodesEntriesOrRaws, values) {
1492
1501
  const inserted = [];
1493
1502
  let valuesIterator;
1494
1503
  if (values) {
@@ -1503,40 +1512,29 @@ var BinaryTree = class extends IterableEntryBase {
1503
1512
  }
1504
1513
  }
1505
1514
  if (this.isRaw(keyNodeEntryOrRaw)) keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
1506
- inserted.push(this.add(keyNodeEntryOrRaw, value));
1515
+ inserted.push(this.set(keyNodeEntryOrRaw, value));
1507
1516
  }
1508
1517
  return inserted;
1509
1518
  }
1510
1519
  /**
1511
- * Adds or updates multiple items to the tree.
1512
- * @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).
1513
- *
1514
- * @param keysNodesEntriesOrRaws - An iterable of items to add or update.
1515
- * @param [values] - An optional parallel iterable of values.
1516
- * @returns An array of booleans indicating the success of each individual `add` operation.
1517
- */
1518
- setMany(keysNodesEntriesOrRaws, values) {
1519
- return this.addMany(keysNodesEntriesOrRaws, values);
1520
- }
1521
- /**
1522
- * Merges another tree into this one by adding all its nodes.
1523
- * @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`).
1520
+ * Merges another tree into this one by seting all its nodes.
1521
+ * @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`).
1524
1522
  *
1525
1523
  * @param anotherTree - The tree to merge.
1526
1524
  */
1527
1525
  merge(anotherTree) {
1528
- this.addMany(anotherTree, []);
1526
+ this.setMany(anotherTree, []);
1529
1527
  }
1530
1528
  /**
1531
1529
  * Clears the tree and refills it with new items.
1532
- * @remarks Time O(N) (for `clear`) + O(N * M) (for `addMany`) = O(N * M). Space O(M) (from `addMany`).
1530
+ * @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
1533
1531
  *
1534
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
1532
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
1535
1533
  * @param [values] - An optional parallel iterable of values.
1536
1534
  */
1537
1535
  refill(keysNodesEntriesOrRaws, values) {
1538
1536
  this.clear();
1539
- this.addMany(keysNodesEntriesOrRaws, values);
1537
+ this.setMany(keysNodesEntriesOrRaws, values);
1540
1538
  }
1541
1539
  /**
1542
1540
  * Deletes a node from the tree.
@@ -2201,7 +2199,7 @@ var BinaryTree = class extends IterableEntryBase {
2201
2199
  }
2202
2200
  /**
2203
2201
  * Clones the tree.
2204
- * @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.
2202
+ * @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.
2205
2203
  *
2206
2204
  * @returns A new, cloned instance of the tree.
2207
2205
  */
@@ -2212,7 +2210,7 @@ var BinaryTree = class extends IterableEntryBase {
2212
2210
  }
2213
2211
  /**
2214
2212
  * Creates a new tree containing only the entries that satisfy the predicate.
2215
- * @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.
2213
+ * @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.
2216
2214
  *
2217
2215
  * @param predicate - A function to test each [key, value] pair.
2218
2216
  * @param [thisArg] - `this` context for the predicate.
@@ -2221,7 +2219,7 @@ var BinaryTree = class extends IterableEntryBase {
2221
2219
  filter(predicate, thisArg) {
2222
2220
  const out = this._createInstance();
2223
2221
  let i = 0;
2224
- for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.add([k, v]);
2222
+ for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.set([k, v]);
2225
2223
  return out;
2226
2224
  }
2227
2225
  /**
@@ -2239,7 +2237,7 @@ var BinaryTree = class extends IterableEntryBase {
2239
2237
  map(cb, options, thisArg) {
2240
2238
  const out = this._createLike([], options);
2241
2239
  let i = 0;
2242
- for (const [k, v] of this) out.add(cb.call(thisArg, v, k, i++, this));
2240
+ for (const [k, v] of this) out.set(cb.call(thisArg, v, k, i++, this));
2243
2241
  return out;
2244
2242
  }
2245
2243
  /**
@@ -2491,18 +2489,18 @@ var BinaryTree = class extends IterableEntryBase {
2491
2489
  return [this.createNode(keyNodeOrEntry, value), value];
2492
2490
  }
2493
2491
  /**
2494
- * (Protected) Helper for cloning. Performs a BFS and adds all nodes to the new tree.
2495
- * @remarks Time O(N * M) (O(N) BFS + O(M) `add` for each node).
2492
+ * (Protected) Helper for cloning. Performs a BFS and sets all nodes to the new tree.
2493
+ * @remarks Time O(N * M) (O(N) BFS + O(M) `set` for each node).
2496
2494
  *
2497
2495
  * @param cloned - The new, empty tree instance to populate.
2498
2496
  */
2499
2497
  _clone(cloned) {
2500
2498
  this.bfs(
2501
2499
  (node) => {
2502
- if (node === null) cloned.add(null);
2500
+ if (node === null) cloned.set(null);
2503
2501
  else {
2504
- if (this._isMapMode) cloned.add([node.key, this._store.get(node.key)]);
2505
- else cloned.add([node.key, node.value]);
2502
+ if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
2503
+ else cloned.set([node.key, node.value]);
2506
2504
  }
2507
2505
  },
2508
2506
  this._root,