data-structure-typed 2.2.7 → 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.
- package/CHANGELOG.md +1 -1
- package/README.md +14 -3
- package/README_CN.md +119 -275
- package/benchmark/report.html +1 -1
- package/benchmark/report.json +20 -324
- package/dist/cjs/index.cjs +106 -107
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +106 -107
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +106 -107
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +106 -107
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/leetcode/avl-tree-counter.mjs +2957 -0
- package/dist/leetcode/avl-tree-multi-map.mjs +2889 -0
- package/dist/leetcode/avl-tree.mjs +2720 -0
- package/dist/leetcode/binary-tree.mjs +1594 -0
- package/dist/leetcode/bst.mjs +2398 -0
- package/dist/leetcode/deque.mjs +683 -0
- package/dist/leetcode/directed-graph.mjs +1733 -0
- package/dist/leetcode/doubly-linked-list.mjs +709 -0
- package/dist/leetcode/hash-map.mjs +493 -0
- package/dist/leetcode/heap.mjs +542 -0
- package/dist/leetcode/max-heap.mjs +375 -0
- package/dist/leetcode/max-priority-queue.mjs +383 -0
- package/dist/leetcode/min-heap.mjs +363 -0
- package/dist/leetcode/min-priority-queue.mjs +371 -0
- package/dist/leetcode/priority-queue.mjs +363 -0
- package/dist/leetcode/queue.mjs +943 -0
- package/dist/leetcode/red-black-tree.mjs +2765 -0
- package/dist/leetcode/singly-linked-list.mjs +754 -0
- package/dist/leetcode/stack.mjs +217 -0
- package/dist/leetcode/tree-counter.mjs +3039 -0
- package/dist/leetcode/tree-multi-map.mjs +2913 -0
- package/dist/leetcode/trie.mjs +413 -0
- package/dist/leetcode/undirected-graph.mjs +1650 -0
- 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 -23
- 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/data-structure-typed.js +102 -103
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +48 -171
- 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 +53 -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
- package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +1 -2
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +30 -30
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +46 -46
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +43 -43
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +151 -151
- package/test/unit/data-structures/binary-tree/bst.test.ts +99 -99
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +141 -141
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +37 -37
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +145 -145
- package/tsup.config.js +50 -21
- package/tsup.umd.config.js +29 -0
- 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
|
|
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 `
|
|
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.
|
|
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) `
|
|
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
|
|
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.
|
|
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
|
|
703
|
-
* @remarks Time O(
|
|
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
|
|
706
|
-
* @param [
|
|
707
|
-
* @returns
|
|
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
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
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
|
|
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
|
|
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 `
|
|
737
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
723
738
|
*/
|
|
724
|
-
|
|
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.
|
|
762
|
+
inserted.push(this.set(keyNodeEntryOrRaw, value));
|
|
748
763
|
}
|
|
749
764
|
|
|
750
765
|
return inserted;
|
|
751
766
|
}
|
|
752
767
|
|
|
753
768
|
/**
|
|
754
|
-
*
|
|
755
|
-
* @remarks Time O(N * M), where N is the
|
|
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.
|
|
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 `
|
|
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
|
|
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.
|
|
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` + `
|
|
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) `
|
|
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.
|
|
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.
|
|
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
|
|
2208
|
-
* @remarks Time O(N * M) (O(N) BFS + O(M) `
|
|
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.
|
|
2214
|
+
if (node === null) cloned.set(null);
|
|
2217
2215
|
else {
|
|
2218
|
-
if (this._isMapMode) cloned.
|
|
2219
|
-
else cloned.
|
|
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.
|
|
223
|
-
* bst.
|
|
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.
|
|
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
|
|
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.
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
777
|
-
* @returns An array of booleans indicating the success of each individual `
|
|
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
|
|
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.
|
|
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.
|
|
834
|
+
inserted[orgIndex] = this.set(entry);
|
|
835
835
|
} else {
|
|
836
|
-
inserted[orgIndex] = this.
|
|
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.
|
|
855
|
+
inserted[orgIndex] = this.set(entry);
|
|
856
856
|
} else {
|
|
857
|
-
inserted[orgIndex] = this.
|
|
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.
|
|
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.
|
|
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
|
|
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.
|
|
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.
|
|
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
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
|
388
|
+
override set(
|
|
389
389
|
keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined
|
|
390
390
|
): boolean;
|
|
391
391
|
|
|
392
|
-
override
|
|
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
|
|
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
|
|
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.
|
|
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
|
|
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
|
|
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.
|
|
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.
|
|
434
|
+
return super.set(key, values);
|
|
435
435
|
}
|
|
436
436
|
};
|
|
437
437
|
|
|
438
438
|
if (this._isMapMode) {
|
|
439
|
-
return
|
|
439
|
+
return _setByNode() || _setToValues();
|
|
440
440
|
}
|
|
441
|
-
return
|
|
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.
|
|
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.
|
|
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]);
|