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.
- package/README.md +1 -1
- package/dist/cjs/index.cjs +41 -43
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +41 -43
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +41 -43
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +41 -43
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +10 -10
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -24
- package/dist/types/data-structures/binary-tree/bst.d.ts +11 -11
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/umd/binary-tree-typed.js +41 -43
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +2 -2
- package/dist/umd/binary-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -13
- package/src/data-structures/binary-tree/avl-tree.ts +15 -15
- package/src/data-structures/binary-tree/binary-tree.ts +52 -55
- package/src/data-structures/binary-tree/bst.ts +21 -22
- package/src/data-structures/binary-tree/red-black-tree.ts +3 -3
- package/src/data-structures/binary-tree/tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
|
@@ -1186,9 +1186,9 @@ var BinaryTreeNode = _BinaryTreeNode;
|
|
|
1186
1186
|
var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
1187
1187
|
/**
|
|
1188
1188
|
* Creates an instance of BinaryTree.
|
|
1189
|
-
* @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) `
|
|
1189
|
+
* @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.
|
|
1190
1190
|
*
|
|
1191
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
1191
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
1192
1192
|
* @param [options] - Configuration options for the tree.
|
|
1193
1193
|
*/
|
|
1194
1194
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
@@ -1217,7 +1217,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1217
1217
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
1218
1218
|
else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
|
|
1219
1219
|
}
|
|
1220
|
-
if (keysNodesEntriesOrRaws) this.
|
|
1220
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
1221
1221
|
}
|
|
1222
1222
|
/**
|
|
1223
1223
|
* Gets whether the tree is in Map mode.
|
|
@@ -1424,10 +1424,20 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1424
1424
|
* @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).
|
|
1425
1425
|
*
|
|
1426
1426
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
1427
|
+
* @returns True if the addition was successful, false otherwise.
|
|
1428
|
+
*/
|
|
1429
|
+
add(keyNodeOrEntry) {
|
|
1430
|
+
return this.set(keyNodeOrEntry);
|
|
1431
|
+
}
|
|
1432
|
+
/**
|
|
1433
|
+
* Adds or updates a new node to the tree.
|
|
1434
|
+
* @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).
|
|
1435
|
+
*
|
|
1436
|
+
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
1427
1437
|
* @param [value] - The value, if providing just a key.
|
|
1428
1438
|
* @returns True if the addition was successful, false otherwise.
|
|
1429
1439
|
*/
|
|
1430
|
-
|
|
1440
|
+
set(keyNodeOrEntry, value) {
|
|
1431
1441
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
1432
1442
|
if (newNode === void 0) return false;
|
|
1433
1443
|
if (!this._root) {
|
|
@@ -1471,25 +1481,24 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1471
1481
|
return false;
|
|
1472
1482
|
}
|
|
1473
1483
|
/**
|
|
1474
|
-
* Adds
|
|
1475
|
-
* @remarks Time O(
|
|
1484
|
+
* Adds multiple items to the tree.
|
|
1485
|
+
* @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).
|
|
1476
1486
|
*
|
|
1477
|
-
* @param
|
|
1478
|
-
* @
|
|
1479
|
-
* @returns True if the addition was successful, false otherwise.
|
|
1487
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
1488
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
1480
1489
|
*/
|
|
1481
|
-
|
|
1482
|
-
return this.
|
|
1490
|
+
addMany(keysNodesEntriesOrRaws) {
|
|
1491
|
+
return this.setMany(keysNodesEntriesOrRaws);
|
|
1483
1492
|
}
|
|
1484
1493
|
/**
|
|
1485
|
-
* Adds multiple items to the tree.
|
|
1486
|
-
* @remarks Time O(N * M), where N is the number of items to
|
|
1494
|
+
* Adds or updates multiple items to the tree.
|
|
1495
|
+
* @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).
|
|
1487
1496
|
*
|
|
1488
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
1497
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set or update.
|
|
1489
1498
|
* @param [values] - An optional parallel iterable of values.
|
|
1490
|
-
* @returns An array of booleans indicating the success of each individual `
|
|
1499
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
1491
1500
|
*/
|
|
1492
|
-
|
|
1501
|
+
setMany(keysNodesEntriesOrRaws, values) {
|
|
1493
1502
|
const inserted = [];
|
|
1494
1503
|
let valuesIterator;
|
|
1495
1504
|
if (values) {
|
|
@@ -1504,40 +1513,29 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1504
1513
|
}
|
|
1505
1514
|
}
|
|
1506
1515
|
if (this.isRaw(keyNodeEntryOrRaw)) keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
|
|
1507
|
-
inserted.push(this.
|
|
1516
|
+
inserted.push(this.set(keyNodeEntryOrRaw, value));
|
|
1508
1517
|
}
|
|
1509
1518
|
return inserted;
|
|
1510
1519
|
}
|
|
1511
1520
|
/**
|
|
1512
|
-
*
|
|
1513
|
-
* @remarks Time O(N * M), where N is the
|
|
1514
|
-
*
|
|
1515
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to add or update.
|
|
1516
|
-
* @param [values] - An optional parallel iterable of values.
|
|
1517
|
-
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
1518
|
-
*/
|
|
1519
|
-
setMany(keysNodesEntriesOrRaws, values) {
|
|
1520
|
-
return this.addMany(keysNodesEntriesOrRaws, values);
|
|
1521
|
-
}
|
|
1522
|
-
/**
|
|
1523
|
-
* Merges another tree into this one by adding all its nodes.
|
|
1524
|
-
* @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`).
|
|
1521
|
+
* Merges another tree into this one by seting all its nodes.
|
|
1522
|
+
* @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`).
|
|
1525
1523
|
*
|
|
1526
1524
|
* @param anotherTree - The tree to merge.
|
|
1527
1525
|
*/
|
|
1528
1526
|
merge(anotherTree) {
|
|
1529
|
-
this.
|
|
1527
|
+
this.setMany(anotherTree, []);
|
|
1530
1528
|
}
|
|
1531
1529
|
/**
|
|
1532
1530
|
* Clears the tree and refills it with new items.
|
|
1533
|
-
* @remarks Time O(N) (for `clear`) + O(N * M) (for `
|
|
1531
|
+
* @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
|
|
1534
1532
|
*
|
|
1535
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
1533
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
1536
1534
|
* @param [values] - An optional parallel iterable of values.
|
|
1537
1535
|
*/
|
|
1538
1536
|
refill(keysNodesEntriesOrRaws, values) {
|
|
1539
1537
|
this.clear();
|
|
1540
|
-
this.
|
|
1538
|
+
this.setMany(keysNodesEntriesOrRaws, values);
|
|
1541
1539
|
}
|
|
1542
1540
|
/**
|
|
1543
1541
|
* Deletes a node from the tree.
|
|
@@ -2203,7 +2201,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2203
2201
|
}
|
|
2204
2202
|
/**
|
|
2205
2203
|
* Clones the tree.
|
|
2206
|
-
* @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `
|
|
2204
|
+
* @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.
|
|
2207
2205
|
*
|
|
2208
2206
|
* @returns A new, cloned instance of the tree.
|
|
2209
2207
|
*/
|
|
@@ -2214,7 +2212,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2214
2212
|
}
|
|
2215
2213
|
/**
|
|
2216
2214
|
* Creates a new tree containing only the entries that satisfy the predicate.
|
|
2217
|
-
* @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) `
|
|
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) `set` for each item). Space O(N) for the new tree.
|
|
2218
2216
|
*
|
|
2219
2217
|
* @param predicate - A function to test each [key, value] pair.
|
|
2220
2218
|
* @param [thisArg] - `this` context for the predicate.
|
|
@@ -2223,7 +2221,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2223
2221
|
filter(predicate, thisArg) {
|
|
2224
2222
|
const out = this._createInstance();
|
|
2225
2223
|
let i = 0;
|
|
2226
|
-
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.
|
|
2224
|
+
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.set([k, v]);
|
|
2227
2225
|
return out;
|
|
2228
2226
|
}
|
|
2229
2227
|
/**
|
|
@@ -2241,7 +2239,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2241
2239
|
map(cb, options, thisArg) {
|
|
2242
2240
|
const out = this._createLike([], options);
|
|
2243
2241
|
let i = 0;
|
|
2244
|
-
for (const [k, v] of this) out.
|
|
2242
|
+
for (const [k, v] of this) out.set(cb.call(thisArg, v, k, i++, this));
|
|
2245
2243
|
return out;
|
|
2246
2244
|
}
|
|
2247
2245
|
/**
|
|
@@ -2487,18 +2485,18 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2487
2485
|
return [this.createNode(keyNodeOrEntry, value), value];
|
|
2488
2486
|
}
|
|
2489
2487
|
/**
|
|
2490
|
-
* (Protected) Helper for cloning. Performs a BFS and
|
|
2491
|
-
* @remarks Time O(N * M) (O(N) BFS + O(M) `
|
|
2488
|
+
* (Protected) Helper for cloning. Performs a BFS and sets all nodes to the new tree.
|
|
2489
|
+
* @remarks Time O(N * M) (O(N) BFS + O(M) `set` for each node).
|
|
2492
2490
|
*
|
|
2493
2491
|
* @param cloned - The new, empty tree instance to populate.
|
|
2494
2492
|
*/
|
|
2495
2493
|
_clone(cloned) {
|
|
2496
2494
|
this.bfs(
|
|
2497
2495
|
(node) => {
|
|
2498
|
-
if (node === null) cloned.
|
|
2496
|
+
if (node === null) cloned.set(null);
|
|
2499
2497
|
else {
|
|
2500
|
-
if (this._isMapMode) cloned.
|
|
2501
|
-
else cloned.
|
|
2498
|
+
if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
|
|
2499
|
+
else cloned.set([node.key, node.value]);
|
|
2502
2500
|
}
|
|
2503
2501
|
},
|
|
2504
2502
|
this._root,
|