data-structure-typed 2.2.5 → 2.2.7
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/CONTRIBUTING.md +47 -1
- package/README.md +6 -6
- package/dist/cjs/index.cjs +100 -83
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +100 -83
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +100 -83
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +100 -83
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +14 -57
- package/dist/types/data-structures/binary-tree/bst.d.ts +46 -126
- package/dist/umd/data-structure-typed.js +100 -83
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -3
- package/src/common/index.ts +2 -4
- package/src/data-structures/base/iterable-entry-base.ts +9 -0
- package/src/data-structures/binary-tree/binary-tree.ts +67 -0
- package/src/data-structures/binary-tree/bst.ts +295 -89
- package/test/unit/data-structures/binary-tree/bst.test.ts +1138 -525
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +12 -10
- package/tsup.config.js +6 -0
- package/tsup.leetcode.config.js +3 -0
- package/tsup.node.config.js +12 -0
package/CHANGELOG.md
CHANGED
|
@@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file.
|
|
|
8
8
|
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
|
|
9
9
|
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)
|
|
10
10
|
|
|
11
|
-
## [v2.2.
|
|
11
|
+
## [v2.2.7](https://github.com/zrwusa/data-structure-typed/compare/v2.2.3...main) (upcoming)
|
|
12
12
|
|
|
13
13
|
## [v2.2.3](https://github.com/zrwusa/data-structure-typed/compare/v2.2.2...v2.2.3) (6 January 2026)
|
|
14
14
|
|
package/CONTRIBUTING.md
CHANGED
|
@@ -83,9 +83,55 @@
|
|
|
83
83
|
- click the `New pull request` on Github https://github.com/zrwusa/data-structure-typed/branches
|
|
84
84
|
|
|
85
85
|
**performance inspection**
|
|
86
|
-
- `node --inspect-brk
|
|
86
|
+
- `node --inspect-brk perf/hello-world.js`
|
|
87
87
|
- chrome://inspect/#devices -> Open dedicated DevTools for Node
|
|
88
88
|
|
|
89
|
+
|
|
90
|
+
**node:perf_hooks**
|
|
91
|
+
```javascript
|
|
92
|
+
const { performance } = require('node:perf_hooks');
|
|
93
|
+
|
|
94
|
+
// Force garbage collection (run with --expose-gc flag)
|
|
95
|
+
const forceGC = () => {
|
|
96
|
+
if (global.gc) {
|
|
97
|
+
global.gc();
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// Test Queue
|
|
102
|
+
function testQueue(QUANTITY) {
|
|
103
|
+
forceGC();
|
|
104
|
+
const initialMemory = process.memoryUsage().heapUsed;
|
|
105
|
+
|
|
106
|
+
const queue = new Queue();
|
|
107
|
+
const start = performance.now();
|
|
108
|
+
|
|
109
|
+
for (let i = 0; i < QUANTITY; i++) {
|
|
110
|
+
queue.push(i);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const end = performance.now();
|
|
114
|
+
const finalMemory = process.memoryUsage().heapUsed;
|
|
115
|
+
const memoryDelta = formatMemory(finalMemory - initialMemory);
|
|
116
|
+
|
|
117
|
+
console.log(`Queue push ${QUANTITY} elements: ${(end - start).toFixed(2)}ms`);
|
|
118
|
+
console.log(`Memory usage: ${memoryDelta}${CURRENT_UNIT} (growth)`);
|
|
119
|
+
|
|
120
|
+
return queue;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
testDeque(1000000);
|
|
124
|
+
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
```shell
|
|
128
|
+
node --expose-gc benchmark/deque.js
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
**performance info output**
|
|
132
|
+
-`node --prof perf/hello-world.js`
|
|
133
|
+
-`node --prof-process isolate-*.log > prof.txt`
|
|
134
|
+
|
|
89
135
|
**Contributing New Data Structures**
|
|
90
136
|
|
|
91
137
|
- Make your pull requests to be **specific** and **focused**. Instead of
|
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
A comprehensive TypeScript data structures library with production-ready implementations.
|
|
4
4
|
|
|
5
|
-
**📚 [Quick Start](#-quick-start-30-seconds) • [
|
|
5
|
+
**📚 [Installation](#-installation) • [Quick Start](#-quick-start-30-seconds) • [Full Docs](./docs/CONCEPTS.md) • [API Reference](./docs/REFERENCE.md) • [Playground](#playground) • [Examples](./docs/GUIDES.md)**
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
@@ -484,14 +484,14 @@ const tree = new RedBlackTree([5, 2, 8]);
|
|
|
484
484
|
console.log([...tree]); // [2, 5, 8] - Automatically sorted!
|
|
485
485
|
```
|
|
486
486
|
|
|
487
|
-
##
|
|
487
|
+
## Playground
|
|
488
488
|
|
|
489
|
-
Try it instantly:
|
|
489
|
+
🏃🏻♀️ Try it instantly:
|
|
490
490
|
|
|
491
491
|
- [Node.js TypeScript](https://stackblitz.com/edit/stackblitz-starters-e1vdy3zw?file=src%2Findex.ts)
|
|
492
|
-
- [Node.js JavaScript](https://stackblitz.com/edit/stackblitz-starters-
|
|
493
|
-
- [React TypeScript](https://stackblitz.com/edit/vitejs-vite-
|
|
494
|
-
- [NestJS](https://stackblitz.com/edit/nestjs-typescript-starter-
|
|
492
|
+
- [Node.js JavaScript](https://stackblitz.com/edit/stackblitz-starters-oczhrfzn?file=src%2Findex.js)
|
|
493
|
+
- [React TypeScript](https://stackblitz.com/edit/vitejs-vite-7bva1zhd?file=src%2FApp.tsx)
|
|
494
|
+
- [NestJS](https://stackblitz.com/edit/nestjs-typescript-starter-q9n7okgc?file=src%2Fproduct%2Fservices%2Fproduct-price-index.service.ts)
|
|
495
495
|
|
|
496
496
|
|
|
497
497
|
### Step 4: Learn More
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -160,6 +160,14 @@ var IterableEntryBase = class {
|
|
|
160
160
|
}
|
|
161
161
|
return accumulator;
|
|
162
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* Converts data structure to `[key, value]` pairs.
|
|
165
|
+
* @returns Array of entries.
|
|
166
|
+
* @remarks Time O(n), Space O(n)
|
|
167
|
+
*/
|
|
168
|
+
toArray() {
|
|
169
|
+
return [...this];
|
|
170
|
+
}
|
|
163
171
|
/**
|
|
164
172
|
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
165
173
|
* @returns Array of entries (default) or a string.
|
|
@@ -6830,8 +6838,6 @@ var Range = class {
|
|
|
6830
6838
|
this.high = high;
|
|
6831
6839
|
this.includeLow = includeLow;
|
|
6832
6840
|
this.includeHigh = includeHigh;
|
|
6833
|
-
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
6834
|
-
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
6835
6841
|
}
|
|
6836
6842
|
static {
|
|
6837
6843
|
__name(this, "Range");
|
|
@@ -8977,77 +8983,63 @@ var BST = class extends BinaryTree {
|
|
|
8977
8983
|
else _iterate();
|
|
8978
8984
|
return inserted;
|
|
8979
8985
|
}
|
|
8980
|
-
|
|
8981
|
-
|
|
8982
|
-
|
|
8983
|
-
|
|
8984
|
-
|
|
8985
|
-
|
|
8986
|
-
|
|
8987
|
-
|
|
8988
|
-
|
|
8989
|
-
|
|
8990
|
-
|
|
8991
|
-
|
|
8992
|
-
|
|
8993
|
-
|
|
8994
|
-
|
|
8995
|
-
|
|
8996
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
8997
|
-
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
8998
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
8999
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9000
|
-
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
9001
|
-
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
9002
|
-
*/
|
|
9003
|
-
upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9004
|
-
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
9005
|
-
}
|
|
9006
|
-
/**
|
|
9007
|
-
* Returns the first node with a key greater than or equal to the given key.
|
|
9008
|
-
* This is equivalent to Java TreeMap.ceilingEntry().
|
|
9009
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9010
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9011
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9012
|
-
*
|
|
9013
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9014
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9015
|
-
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
9016
|
-
*/
|
|
9017
|
-
ceilingEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9018
|
-
return this.lowerBound(keyNodeEntryOrPredicate, iterationType);
|
|
8986
|
+
ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
8987
|
+
let actualCallback = void 0;
|
|
8988
|
+
let actualIterationType = this.iterationType;
|
|
8989
|
+
if (typeof callback === "string") {
|
|
8990
|
+
actualIterationType = callback;
|
|
8991
|
+
} else if (callback) {
|
|
8992
|
+
actualCallback = callback;
|
|
8993
|
+
if (iterationType) {
|
|
8994
|
+
actualIterationType = iterationType;
|
|
8995
|
+
}
|
|
8996
|
+
}
|
|
8997
|
+
const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
|
|
8998
|
+
if (!actualCallback) {
|
|
8999
|
+
return node?.key;
|
|
9000
|
+
}
|
|
9001
|
+
return node ? actualCallback(node) : void 0;
|
|
9019
9002
|
}
|
|
9020
|
-
|
|
9021
|
-
|
|
9022
|
-
|
|
9023
|
-
|
|
9024
|
-
|
|
9025
|
-
|
|
9026
|
-
|
|
9027
|
-
|
|
9028
|
-
|
|
9029
|
-
|
|
9030
|
-
|
|
9031
|
-
|
|
9032
|
-
|
|
9003
|
+
higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
9004
|
+
let actualCallback = void 0;
|
|
9005
|
+
let actualIterationType = this.iterationType;
|
|
9006
|
+
if (typeof callback === "string") {
|
|
9007
|
+
actualIterationType = callback;
|
|
9008
|
+
} else if (callback) {
|
|
9009
|
+
actualCallback = callback;
|
|
9010
|
+
if (iterationType) {
|
|
9011
|
+
actualIterationType = iterationType;
|
|
9012
|
+
}
|
|
9013
|
+
}
|
|
9014
|
+
const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
|
|
9015
|
+
if (!actualCallback) {
|
|
9016
|
+
return node?.key;
|
|
9017
|
+
}
|
|
9018
|
+
return node ? actualCallback(node) : void 0;
|
|
9033
9019
|
}
|
|
9034
|
-
|
|
9035
|
-
* Returns the first node with a key less than or equal to the given key.
|
|
9036
|
-
* This is equivalent to Java TreeMap.floorEntry().
|
|
9037
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9038
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9039
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9040
|
-
*
|
|
9041
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9042
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9043
|
-
* @returns The first node with key <= given key, or undefined if no such node exists.
|
|
9044
|
-
*/
|
|
9045
|
-
floorEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9020
|
+
floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
9046
9021
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9022
|
+
if (typeof callback === "string" || !callback) {
|
|
9023
|
+
return void 0;
|
|
9024
|
+
}
|
|
9047
9025
|
return void 0;
|
|
9048
9026
|
}
|
|
9027
|
+
let actualCallback = void 0;
|
|
9028
|
+
let actualIterationType = this.iterationType;
|
|
9029
|
+
if (typeof callback === "string") {
|
|
9030
|
+
actualIterationType = callback;
|
|
9031
|
+
} else if (callback) {
|
|
9032
|
+
actualCallback = callback;
|
|
9033
|
+
if (iterationType) {
|
|
9034
|
+
actualIterationType = iterationType;
|
|
9035
|
+
}
|
|
9036
|
+
}
|
|
9049
9037
|
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9050
|
-
|
|
9038
|
+
const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
9039
|
+
if (!actualCallback) {
|
|
9040
|
+
return node?.key;
|
|
9041
|
+
}
|
|
9042
|
+
return node ? actualCallback(node) : void 0;
|
|
9051
9043
|
}
|
|
9052
9044
|
let targetKey;
|
|
9053
9045
|
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
@@ -9055,6 +9047,9 @@ var BST = class extends BinaryTree {
|
|
|
9055
9047
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9056
9048
|
const key = keyNodeEntryOrPredicate[0];
|
|
9057
9049
|
if (key === null || key === void 0) {
|
|
9050
|
+
if (typeof callback === "string" || !callback) {
|
|
9051
|
+
return void 0;
|
|
9052
|
+
}
|
|
9058
9053
|
return void 0;
|
|
9059
9054
|
}
|
|
9060
9055
|
targetKey = key;
|
|
@@ -9062,27 +9057,40 @@ var BST = class extends BinaryTree {
|
|
|
9062
9057
|
targetKey = keyNodeEntryOrPredicate;
|
|
9063
9058
|
}
|
|
9064
9059
|
if (targetKey !== void 0) {
|
|
9065
|
-
|
|
9060
|
+
const node = this._floorByKey(targetKey, actualIterationType);
|
|
9061
|
+
if (!actualCallback) {
|
|
9062
|
+
return node?.key;
|
|
9063
|
+
}
|
|
9064
|
+
return node ? actualCallback(node) : void 0;
|
|
9065
|
+
}
|
|
9066
|
+
if (typeof callback === "string" || !callback) {
|
|
9067
|
+
return void 0;
|
|
9066
9068
|
}
|
|
9067
9069
|
return void 0;
|
|
9068
9070
|
}
|
|
9069
|
-
|
|
9070
|
-
* Returns the first node with a key strictly less than the given key.
|
|
9071
|
-
* This is equivalent to Java TreeMap.lowerEntry().
|
|
9072
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9073
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9074
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9075
|
-
*
|
|
9076
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9077
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9078
|
-
* @returns The first node with key < given key, or undefined if no such node exists.
|
|
9079
|
-
*/
|
|
9080
|
-
lowerEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9071
|
+
lower(keyNodeEntryOrPredicate, callback, iterationType) {
|
|
9081
9072
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9073
|
+
if (typeof callback === "string" || !callback) {
|
|
9074
|
+
return void 0;
|
|
9075
|
+
}
|
|
9082
9076
|
return void 0;
|
|
9083
9077
|
}
|
|
9078
|
+
let actualCallback = void 0;
|
|
9079
|
+
let actualIterationType = this.iterationType;
|
|
9080
|
+
if (typeof callback === "string") {
|
|
9081
|
+
actualIterationType = callback;
|
|
9082
|
+
} else if (callback) {
|
|
9083
|
+
actualCallback = callback;
|
|
9084
|
+
if (iterationType) {
|
|
9085
|
+
actualIterationType = iterationType;
|
|
9086
|
+
}
|
|
9087
|
+
}
|
|
9084
9088
|
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9085
|
-
|
|
9089
|
+
const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
9090
|
+
if (!actualCallback) {
|
|
9091
|
+
return node?.key;
|
|
9092
|
+
}
|
|
9093
|
+
return node ? actualCallback(node) : void 0;
|
|
9086
9094
|
}
|
|
9087
9095
|
let targetKey;
|
|
9088
9096
|
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
@@ -9090,6 +9098,9 @@ var BST = class extends BinaryTree {
|
|
|
9090
9098
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9091
9099
|
const key = keyNodeEntryOrPredicate[0];
|
|
9092
9100
|
if (key === null || key === void 0) {
|
|
9101
|
+
if (typeof callback === "string" || !callback) {
|
|
9102
|
+
return void 0;
|
|
9103
|
+
}
|
|
9093
9104
|
return void 0;
|
|
9094
9105
|
}
|
|
9095
9106
|
targetKey = key;
|
|
@@ -9097,7 +9108,14 @@ var BST = class extends BinaryTree {
|
|
|
9097
9108
|
targetKey = keyNodeEntryOrPredicate;
|
|
9098
9109
|
}
|
|
9099
9110
|
if (targetKey !== void 0) {
|
|
9100
|
-
|
|
9111
|
+
const node = this._lowerByKey(targetKey, actualIterationType);
|
|
9112
|
+
if (!actualCallback) {
|
|
9113
|
+
return node?.key;
|
|
9114
|
+
}
|
|
9115
|
+
return node ? actualCallback(node) : void 0;
|
|
9116
|
+
}
|
|
9117
|
+
if (typeof callback === "string" || !callback) {
|
|
9118
|
+
return void 0;
|
|
9101
9119
|
}
|
|
9102
9120
|
return void 0;
|
|
9103
9121
|
}
|
|
@@ -9286,7 +9304,6 @@ var BST = class extends BinaryTree {
|
|
|
9286
9304
|
*/
|
|
9287
9305
|
_createDefaultComparator() {
|
|
9288
9306
|
return (a, b) => {
|
|
9289
|
-
debugger;
|
|
9290
9307
|
if (isComparable(a) && isComparable(b)) {
|
|
9291
9308
|
if (a > b) return 1;
|
|
9292
9309
|
if (a < b) return -1;
|