data-structure-typed 2.2.3 → 2.2.4
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/CHANGELOG.md +3 -1
- package/README.md +70 -11
- package/dist/cjs/index.cjs +85 -75
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +85 -75
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +85 -75
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +85 -75
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +46 -26
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
- package/dist/umd/data-structure-typed.js +85 -75
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
- package/src/data-structures/binary-tree/avl-tree.ts +4 -5
- package/src/data-structures/binary-tree/bst.ts +111 -82
- package/src/data-structures/binary-tree/red-black-tree.ts +1 -2
- package/src/data-structures/binary-tree/tree-counter.ts +5 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +7 -8
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/bst.test.ts +5 -8
- package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +2 -2
|
@@ -8616,36 +8616,20 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8616
8616
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
8617
8617
|
super([], options);
|
|
8618
8618
|
__publicField(this, "_root");
|
|
8619
|
-
__publicField(this, "_isReverse", false);
|
|
8620
8619
|
/**
|
|
8621
|
-
|
|
8622
|
-
|
|
8623
|
-
|
|
8624
|
-
|
|
8625
|
-
|
|
8626
|
-
if (a > b) return 1;
|
|
8627
|
-
if (a < b) return -1;
|
|
8628
|
-
return 0;
|
|
8629
|
-
}
|
|
8630
|
-
if (this._specifyComparable) {
|
|
8631
|
-
const va = this._specifyComparable(a);
|
|
8632
|
-
const vb = this._specifyComparable(b);
|
|
8633
|
-
if (va > vb) return 1;
|
|
8634
|
-
if (va < vb) return -1;
|
|
8635
|
-
return 0;
|
|
8636
|
-
}
|
|
8637
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
8638
|
-
throw TypeError(
|
|
8639
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
8640
|
-
);
|
|
8641
|
-
}
|
|
8642
|
-
return 0;
|
|
8643
|
-
}, "_comparator"));
|
|
8644
|
-
__publicField(this, "_specifyComparable");
|
|
8620
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
8621
|
+
|
|
8622
|
+
* @remarks Time O(1) Space O(1)
|
|
8623
|
+
*/
|
|
8624
|
+
__publicField(this, "_comparator");
|
|
8645
8625
|
if (options) {
|
|
8646
|
-
|
|
8647
|
-
|
|
8648
|
-
|
|
8626
|
+
if ("comparator" in options && options.comparator !== void 0) {
|
|
8627
|
+
this._comparator = options.comparator;
|
|
8628
|
+
} else {
|
|
8629
|
+
this._comparator = this._createDefaultComparator();
|
|
8630
|
+
}
|
|
8631
|
+
} else {
|
|
8632
|
+
this._comparator = this._createDefaultComparator();
|
|
8649
8633
|
}
|
|
8650
8634
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
8651
8635
|
}
|
|
@@ -8659,13 +8643,25 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8659
8643
|
return this._root;
|
|
8660
8644
|
}
|
|
8661
8645
|
/**
|
|
8662
|
-
*
|
|
8663
|
-
* @remarks Time O(1)
|
|
8664
|
-
*
|
|
8665
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
8646
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
8647
|
+
* @remarks Time O(1) Space O(1)
|
|
8648
|
+
* @returns The default comparator function.
|
|
8666
8649
|
*/
|
|
8667
|
-
|
|
8668
|
-
return
|
|
8650
|
+
_createDefaultComparator() {
|
|
8651
|
+
return (a, b) => {
|
|
8652
|
+
debugger;
|
|
8653
|
+
if (isComparable(a) && isComparable(b)) {
|
|
8654
|
+
if (a > b) return 1;
|
|
8655
|
+
if (a < b) return -1;
|
|
8656
|
+
return 0;
|
|
8657
|
+
}
|
|
8658
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
8659
|
+
throw TypeError(
|
|
8660
|
+
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
8661
|
+
);
|
|
8662
|
+
}
|
|
8663
|
+
return 0;
|
|
8664
|
+
};
|
|
8669
8665
|
}
|
|
8670
8666
|
/**
|
|
8671
8667
|
* Gets the comparator function used by the tree.
|
|
@@ -8676,15 +8672,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8676
8672
|
get comparator() {
|
|
8677
8673
|
return this._comparator;
|
|
8678
8674
|
}
|
|
8679
|
-
/**
|
|
8680
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
8681
|
-
* @remarks Time O(1)
|
|
8682
|
-
*
|
|
8683
|
-
* @returns The key-to-comparable conversion function.
|
|
8684
|
-
*/
|
|
8685
|
-
get specifyComparable() {
|
|
8686
|
-
return this._specifyComparable;
|
|
8687
|
-
}
|
|
8688
8675
|
/**
|
|
8689
8676
|
* (Protected) Creates a new BST node.
|
|
8690
8677
|
* @remarks Time O(1), Space O(1)
|
|
@@ -8726,7 +8713,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8726
8713
|
* @returns True if the key is valid, false otherwise.
|
|
8727
8714
|
*/
|
|
8728
8715
|
isValidKey(key) {
|
|
8729
|
-
return isComparable(key
|
|
8716
|
+
return isComparable(key);
|
|
8730
8717
|
}
|
|
8731
8718
|
/**
|
|
8732
8719
|
* Performs a Depth-First Search (DFS) traversal.
|
|
@@ -8816,8 +8803,8 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8816
8803
|
if (!this.isRealNode(cur.left)) return false;
|
|
8817
8804
|
if (isRange) {
|
|
8818
8805
|
const range = keyNodeEntryOrPredicate;
|
|
8819
|
-
const leftS =
|
|
8820
|
-
const leftI =
|
|
8806
|
+
const leftS = range.low;
|
|
8807
|
+
const leftI = range.includeLow;
|
|
8821
8808
|
return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
|
|
8822
8809
|
}
|
|
8823
8810
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -8831,8 +8818,8 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8831
8818
|
if (!this.isRealNode(cur.right)) return false;
|
|
8832
8819
|
if (isRange) {
|
|
8833
8820
|
const range = keyNodeEntryOrPredicate;
|
|
8834
|
-
const rightS =
|
|
8835
|
-
const rightI =
|
|
8821
|
+
const rightS = range.high;
|
|
8822
|
+
const rightI = range.includeHigh;
|
|
8836
8823
|
return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
|
|
8837
8824
|
}
|
|
8838
8825
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -9153,31 +9140,55 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9153
9140
|
return out;
|
|
9154
9141
|
}
|
|
9155
9142
|
/**
|
|
9156
|
-
* Deletes
|
|
9157
|
-
* @remarks Performs an in-order traversal. Time O(N) worst-case (O(log N) to find + O(log N) to delete). Space O(log N) for stack.
|
|
9143
|
+
* Deletes nodes that match a key, node, entry, predicate, or range.
|
|
9158
9144
|
*
|
|
9159
|
-
* @
|
|
9160
|
-
*
|
|
9161
|
-
|
|
9162
|
-
|
|
9163
|
-
|
|
9164
|
-
|
|
9165
|
-
|
|
9166
|
-
|
|
9167
|
-
|
|
9168
|
-
|
|
9169
|
-
|
|
9170
|
-
|
|
9171
|
-
|
|
9172
|
-
|
|
9173
|
-
|
|
9174
|
-
|
|
9175
|
-
|
|
9176
|
-
|
|
9177
|
-
|
|
9178
|
-
|
|
9145
|
+
* @remarks
|
|
9146
|
+
* Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
|
|
9147
|
+
* Space Complexity: O(M) for storing matched nodes and result map.
|
|
9148
|
+
*
|
|
9149
|
+
* @template K - The key type.
|
|
9150
|
+
* @template V - The value type.
|
|
9151
|
+
*
|
|
9152
|
+
* @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
|
|
9153
|
+
* - A key (type K): searches for exact key match using the comparator.
|
|
9154
|
+
* - A BSTNode: searches for the matching node in the tree.
|
|
9155
|
+
* - An entry tuple: searches for the key-value pair.
|
|
9156
|
+
* - A NodePredicate function: tests each node and returns true for matches.
|
|
9157
|
+
* - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
|
|
9158
|
+
* - null or undefined: treated as no match, returns empty results.
|
|
9159
|
+
*
|
|
9160
|
+
* @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
|
|
9161
|
+
* If false (default), searches for and deletes all matching nodes.
|
|
9162
|
+
*
|
|
9163
|
+
* @param startNode - The node to start the search from. Can be:
|
|
9164
|
+
* - A key, node, or entry: the method resolves it to a node and searches from that subtree.
|
|
9165
|
+
* - null or undefined: defaults to the root, searching the entire tree.
|
|
9166
|
+
* - Default value: this._root (the tree's root).
|
|
9167
|
+
*
|
|
9168
|
+
* @param iterationType - Controls the internal traversal implementation:
|
|
9169
|
+
* - 'RECURSIVE': uses recursive function calls for traversal.
|
|
9170
|
+
* - 'ITERATIVE': uses explicit stack-based iteration.
|
|
9171
|
+
* - Default: this.iterationType (the tree's default iteration mode).
|
|
9172
|
+
*
|
|
9173
|
+
* @returns A Map<K, boolean> containing the deletion results:
|
|
9174
|
+
* - Key: the matched node's key.
|
|
9175
|
+
* - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
|
|
9176
|
+
* - If no nodes match the search criteria, the returned map is empty.
|
|
9177
|
+
*/
|
|
9178
|
+
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
9179
|
+
const toDelete = this.search(
|
|
9180
|
+
keyNodeEntryOrPredicate,
|
|
9181
|
+
onlyOne,
|
|
9182
|
+
(node) => node,
|
|
9183
|
+
startNode,
|
|
9184
|
+
iterationType
|
|
9185
|
+
);
|
|
9186
|
+
let results = [];
|
|
9187
|
+
for (const node of toDelete) {
|
|
9188
|
+
const deleteInfo = this.delete(node);
|
|
9189
|
+
results = results.concat(deleteInfo);
|
|
9179
9190
|
}
|
|
9180
|
-
return
|
|
9191
|
+
return results;
|
|
9181
9192
|
}
|
|
9182
9193
|
/**
|
|
9183
9194
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
@@ -9330,8 +9341,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9330
9341
|
_snapshotOptions() {
|
|
9331
9342
|
return {
|
|
9332
9343
|
...super._snapshotOptions(),
|
|
9333
|
-
|
|
9334
|
-
isReverse: this.isReverse
|
|
9344
|
+
comparator: this._comparator
|
|
9335
9345
|
};
|
|
9336
9346
|
}
|
|
9337
9347
|
/**
|
|
@@ -9359,14 +9369,14 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9359
9369
|
}
|
|
9360
9370
|
/**
|
|
9361
9371
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
9362
|
-
* @remarks Time O(1)
|
|
9372
|
+
* @remarks Time O(1) Space O(1)
|
|
9363
9373
|
*
|
|
9364
9374
|
* @param a - The first key.
|
|
9365
9375
|
* @param b - The second key.
|
|
9366
9376
|
* @returns A number (1, -1, or 0) representing the comparison.
|
|
9367
9377
|
*/
|
|
9368
9378
|
_compare(a, b) {
|
|
9369
|
-
return this.
|
|
9379
|
+
return this._comparator(a, b);
|
|
9370
9380
|
}
|
|
9371
9381
|
/**
|
|
9372
9382
|
* (Private) Deletes a node by its key.
|