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
package/CHANGELOG.md
CHANGED
|
@@ -8,7 +8,9 @@ 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.4](https://github.com/zrwusa/data-structure-typed/compare/v2.2.3...main) (upcoming)
|
|
12
|
+
|
|
13
|
+
## [v2.2.3](https://github.com/zrwusa/data-structure-typed/compare/v2.2.2...v2.2.3) (6 January 2026)
|
|
12
14
|
|
|
13
15
|
## [v2.2.2](https://github.com/zrwusa/data-structure-typed/compare/v2.0.4...v2.2.2) (5 January 2026)
|
|
14
16
|
|
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) • [Full Docs](./docs/CONCEPTS.md) • [API Reference](./docs/REFERENCE.md) • [Examples](./docs/GUIDES.md)**
|
|
5
|
+
**📚 [Quick Start](#-quick-start-30-seconds) • [Installation](#-installation) • [Full Docs](./docs/CONCEPTS.md) • [API Reference](./docs/REFERENCE.md) • [Playground](#-playground) • [Installation](#-installation) • [Examples](./docs/GUIDES.md)**
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
@@ -56,10 +56,11 @@ for (let i = 0; i < 100000; i++) {
|
|
|
56
56
|
queue.shift(); // O(n) - Reindexes EVERY element!
|
|
57
57
|
}
|
|
58
58
|
// Time: 2829ms ❌
|
|
59
|
+
```
|
|
59
60
|
|
|
61
|
+
```javascript
|
|
60
62
|
// ✅ WITH data-structure-typed (Deque)
|
|
61
|
-
const deque = new Deque([1, 2, 3, ..., 100000
|
|
62
|
-
])
|
|
63
|
+
const deque = new Deque([1, 2, 3, ..., 100000])
|
|
63
64
|
;
|
|
64
65
|
for (let i = 0; i < 100000; i++) {
|
|
65
66
|
deque.shift(); // O(1) - Just moves a pointer
|
|
@@ -95,7 +96,8 @@ Don't learn new APIs. Just use `push`, `pop`, `map`, `filter`, and `reduce` ever
|
|
|
95
96
|
// All linear structures use THE SAME 4 methods
|
|
96
97
|
const deque = new Deque([1, 2, 3]);
|
|
97
98
|
const queue = new Queue([1, 2, 3]);
|
|
98
|
-
const
|
|
99
|
+
const doublyLinkeList = new DoublyLinkedList([1, 2, 3]);
|
|
100
|
+
const singlyLinkedList = new SinglyLinkedList([1, 2, 3]);
|
|
99
101
|
|
|
100
102
|
// They ALL support:
|
|
101
103
|
structure.push(item); // Add to end
|
|
@@ -211,7 +213,7 @@ const topPlayers = [...leaderboard.values()].reverse().slice(0, 3);
|
|
|
211
213
|
```typescript
|
|
212
214
|
import { MaxPriorityQueue } from 'data-structure-typed';
|
|
213
215
|
|
|
214
|
-
const taskQueue = new MaxPriorityQueue([], {
|
|
216
|
+
const taskQueue = new MaxPriorityQueue<{priority: number; task: string}>([], {
|
|
215
217
|
comparator: (a, b) => b.priority - a.priority
|
|
216
218
|
});
|
|
217
219
|
|
|
@@ -329,20 +331,67 @@ class LRUCache<K, V> {
|
|
|
329
331
|
### Leaderboard
|
|
330
332
|
|
|
331
333
|
```typescript
|
|
332
|
-
class Leaderboard {
|
|
333
|
-
private scores = new RedBlackTree<number, Player>(
|
|
334
|
-
(a, b) => b - a // Descending
|
|
335
|
-
);
|
|
336
334
|
|
|
337
|
-
|
|
338
|
-
|
|
335
|
+
type Player = {
|
|
336
|
+
id: string;
|
|
337
|
+
name: string;
|
|
338
|
+
score: number;
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
const seedPlayers: Player[] = [
|
|
342
|
+
{ id: 'player_01HZX4E8Q2K8Y3J9M7T1A6B3C4', name: 'Pablo', score: 65 },
|
|
343
|
+
{ id: 'player_01HZX4E9R6V2D8K1P0N5S4T7U8', name: 'Bunny', score: 10 },
|
|
344
|
+
{ id: 'player_01HZX4EA3M9Q7W1E2R8T6Y5U0I', name: 'Jeff', score: 99 },
|
|
345
|
+
];
|
|
346
|
+
|
|
347
|
+
class ScoreLeaderboard {
|
|
348
|
+
private readonly byScore: RedBlackTree<number, Player, Player>;
|
|
349
|
+
|
|
350
|
+
constructor(initialPlayers: Player[]) {
|
|
351
|
+
this.byScore = new RedBlackTree<number, Player, Player>(initialPlayers, {
|
|
352
|
+
isMapMode: false,// Use "node value" storage rather than Map-style.
|
|
353
|
+
toEntryFn: (player) => [player.score, player], // Convert a player object into the tree entry: key = score, value = player.
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Returns players whose scores fall within the given range.
|
|
359
|
+
* Supports either a tuple [min, max] or a Range object for inclusive/exclusive bounds.
|
|
360
|
+
*/
|
|
361
|
+
public findPlayersByScoreRange(range: [number, number] | Range<number>): (Player | undefined)[] {
|
|
362
|
+
return this.byScore.rangeSearch(range, (node) => node.value);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
public upsertPlayer(player: Player) {
|
|
366
|
+
return this.byScore.set(player.score, player);
|
|
339
367
|
}
|
|
340
368
|
}
|
|
369
|
+
|
|
370
|
+
const leaderboard = new ScoreLeaderboard(seedPlayers);
|
|
371
|
+
|
|
372
|
+
console.log(leaderboard.findPlayersByScoreRange([65, 100]));
|
|
373
|
+
|
|
374
|
+
leaderboard.upsertPlayer({
|
|
375
|
+
id: 'player_01HZX4EB7C4N2M9Q8R1T3Y6U5I',
|
|
376
|
+
name: 'Alex',
|
|
377
|
+
score: 80,
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
console.log(leaderboard.findPlayersByScoreRange(new Range(65, 100, true, true)));
|
|
341
381
|
```
|
|
342
382
|
|
|
343
383
|
### Message Queue
|
|
344
384
|
|
|
345
385
|
```typescript
|
|
386
|
+
type Message = {
|
|
387
|
+
id: string;
|
|
388
|
+
type: string;
|
|
389
|
+
payload: unknown;
|
|
390
|
+
priority: 'urgent' | 'normal';
|
|
391
|
+
createdAt: number;
|
|
392
|
+
retryCount?: number;
|
|
393
|
+
};
|
|
394
|
+
|
|
346
395
|
class MessageQueue {
|
|
347
396
|
private urgent = new Deque<Message>();
|
|
348
397
|
private normal = new Deque<Message>();
|
|
@@ -435,6 +484,16 @@ const tree = new RedBlackTree([5, 2, 8]);
|
|
|
435
484
|
console.log([...tree]); // [2, 5, 8] - Automatically sorted!
|
|
436
485
|
```
|
|
437
486
|
|
|
487
|
+
## 🏃🏻♀️ Playground
|
|
488
|
+
|
|
489
|
+
Try it instantly:
|
|
490
|
+
|
|
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-dgvchziu?file=src%2Findex.js)
|
|
493
|
+
- [React TypeScript](https://stackblitz.com/edit/vitejs-vite-6xvhtdua?file=src%2FApp.tsx)
|
|
494
|
+
- [NestJS](https://stackblitz.com/edit/nestjs-typescript-starter-3cyp7pel?file=src%2Franking%2Franking.service.ts)
|
|
495
|
+
|
|
496
|
+
|
|
438
497
|
### Step 4: Learn More
|
|
439
498
|
|
|
440
499
|
👉 Check [CONCEPTS.md](./docs/CONCEPTS.md) for core concepts
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -8636,9 +8636,13 @@ var BST = class extends BinaryTree {
|
|
|
8636
8636
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
8637
8637
|
super([], options);
|
|
8638
8638
|
if (options) {
|
|
8639
|
-
|
|
8640
|
-
|
|
8641
|
-
|
|
8639
|
+
if ("comparator" in options && options.comparator !== void 0) {
|
|
8640
|
+
this._comparator = options.comparator;
|
|
8641
|
+
} else {
|
|
8642
|
+
this._comparator = this._createDefaultComparator();
|
|
8643
|
+
}
|
|
8644
|
+
} else {
|
|
8645
|
+
this._comparator = this._createDefaultComparator();
|
|
8642
8646
|
}
|
|
8643
8647
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
8644
8648
|
}
|
|
@@ -8652,40 +8656,33 @@ var BST = class extends BinaryTree {
|
|
|
8652
8656
|
get root() {
|
|
8653
8657
|
return this._root;
|
|
8654
8658
|
}
|
|
8655
|
-
_isReverse = false;
|
|
8656
8659
|
/**
|
|
8657
|
-
*
|
|
8658
|
-
* @remarks Time O(1)
|
|
8659
|
-
*
|
|
8660
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
8660
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
8661
|
+
* @remarks Time O(1) Space O(1)
|
|
8662
|
+
* @returns The default comparator function.
|
|
8661
8663
|
*/
|
|
8662
|
-
|
|
8663
|
-
return
|
|
8664
|
+
_createDefaultComparator() {
|
|
8665
|
+
return (a, b) => {
|
|
8666
|
+
debugger;
|
|
8667
|
+
if (isComparable(a) && isComparable(b)) {
|
|
8668
|
+
if (a > b) return 1;
|
|
8669
|
+
if (a < b) return -1;
|
|
8670
|
+
return 0;
|
|
8671
|
+
}
|
|
8672
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
8673
|
+
throw TypeError(
|
|
8674
|
+
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
8675
|
+
);
|
|
8676
|
+
}
|
|
8677
|
+
return 0;
|
|
8678
|
+
};
|
|
8664
8679
|
}
|
|
8665
8680
|
/**
|
|
8666
|
-
|
|
8667
|
-
|
|
8668
|
-
|
|
8669
|
-
|
|
8670
|
-
|
|
8671
|
-
if (a > b) return 1;
|
|
8672
|
-
if (a < b) return -1;
|
|
8673
|
-
return 0;
|
|
8674
|
-
}
|
|
8675
|
-
if (this._specifyComparable) {
|
|
8676
|
-
const va = this._specifyComparable(a);
|
|
8677
|
-
const vb = this._specifyComparable(b);
|
|
8678
|
-
if (va > vb) return 1;
|
|
8679
|
-
if (va < vb) return -1;
|
|
8680
|
-
return 0;
|
|
8681
|
-
}
|
|
8682
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
8683
|
-
throw TypeError(
|
|
8684
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
8685
|
-
);
|
|
8686
|
-
}
|
|
8687
|
-
return 0;
|
|
8688
|
-
}, "_comparator");
|
|
8681
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
8682
|
+
|
|
8683
|
+
* @remarks Time O(1) Space O(1)
|
|
8684
|
+
*/
|
|
8685
|
+
_comparator;
|
|
8689
8686
|
/**
|
|
8690
8687
|
* Gets the comparator function used by the tree.
|
|
8691
8688
|
* @remarks Time O(1)
|
|
@@ -8695,16 +8692,6 @@ var BST = class extends BinaryTree {
|
|
|
8695
8692
|
get comparator() {
|
|
8696
8693
|
return this._comparator;
|
|
8697
8694
|
}
|
|
8698
|
-
_specifyComparable;
|
|
8699
|
-
/**
|
|
8700
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
8701
|
-
* @remarks Time O(1)
|
|
8702
|
-
*
|
|
8703
|
-
* @returns The key-to-comparable conversion function.
|
|
8704
|
-
*/
|
|
8705
|
-
get specifyComparable() {
|
|
8706
|
-
return this._specifyComparable;
|
|
8707
|
-
}
|
|
8708
8695
|
/**
|
|
8709
8696
|
* (Protected) Creates a new BST node.
|
|
8710
8697
|
* @remarks Time O(1), Space O(1)
|
|
@@ -8745,7 +8732,7 @@ var BST = class extends BinaryTree {
|
|
|
8745
8732
|
* @returns True if the key is valid, false otherwise.
|
|
8746
8733
|
*/
|
|
8747
8734
|
isValidKey(key) {
|
|
8748
|
-
return isComparable(key
|
|
8735
|
+
return isComparable(key);
|
|
8749
8736
|
}
|
|
8750
8737
|
/**
|
|
8751
8738
|
* Performs a Depth-First Search (DFS) traversal.
|
|
@@ -8834,8 +8821,8 @@ var BST = class extends BinaryTree {
|
|
|
8834
8821
|
if (!this.isRealNode(cur.left)) return false;
|
|
8835
8822
|
if (isRange) {
|
|
8836
8823
|
const range = keyNodeEntryOrPredicate;
|
|
8837
|
-
const leftS =
|
|
8838
|
-
const leftI =
|
|
8824
|
+
const leftS = range.low;
|
|
8825
|
+
const leftI = range.includeLow;
|
|
8839
8826
|
return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
|
|
8840
8827
|
}
|
|
8841
8828
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -8849,8 +8836,8 @@ var BST = class extends BinaryTree {
|
|
|
8849
8836
|
if (!this.isRealNode(cur.right)) return false;
|
|
8850
8837
|
if (isRange) {
|
|
8851
8838
|
const range = keyNodeEntryOrPredicate;
|
|
8852
|
-
const rightS =
|
|
8853
|
-
const rightI =
|
|
8839
|
+
const rightS = range.high;
|
|
8840
|
+
const rightI = range.includeHigh;
|
|
8854
8841
|
return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
|
|
8855
8842
|
}
|
|
8856
8843
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -9171,31 +9158,55 @@ var BST = class extends BinaryTree {
|
|
|
9171
9158
|
return out;
|
|
9172
9159
|
}
|
|
9173
9160
|
/**
|
|
9174
|
-
* Deletes
|
|
9175
|
-
* @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.
|
|
9161
|
+
* Deletes nodes that match a key, node, entry, predicate, or range.
|
|
9176
9162
|
*
|
|
9177
|
-
* @
|
|
9178
|
-
*
|
|
9179
|
-
|
|
9180
|
-
|
|
9181
|
-
|
|
9182
|
-
|
|
9183
|
-
|
|
9184
|
-
|
|
9185
|
-
|
|
9186
|
-
|
|
9187
|
-
|
|
9188
|
-
|
|
9189
|
-
|
|
9190
|
-
|
|
9191
|
-
|
|
9192
|
-
|
|
9193
|
-
|
|
9194
|
-
|
|
9195
|
-
|
|
9196
|
-
|
|
9163
|
+
* @remarks
|
|
9164
|
+
* Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
|
|
9165
|
+
* Space Complexity: O(M) for storing matched nodes and result map.
|
|
9166
|
+
*
|
|
9167
|
+
* @template K - The key type.
|
|
9168
|
+
* @template V - The value type.
|
|
9169
|
+
*
|
|
9170
|
+
* @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
|
|
9171
|
+
* - A key (type K): searches for exact key match using the comparator.
|
|
9172
|
+
* - A BSTNode: searches for the matching node in the tree.
|
|
9173
|
+
* - An entry tuple: searches for the key-value pair.
|
|
9174
|
+
* - A NodePredicate function: tests each node and returns true for matches.
|
|
9175
|
+
* - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
|
|
9176
|
+
* - null or undefined: treated as no match, returns empty results.
|
|
9177
|
+
*
|
|
9178
|
+
* @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
|
|
9179
|
+
* If false (default), searches for and deletes all matching nodes.
|
|
9180
|
+
*
|
|
9181
|
+
* @param startNode - The node to start the search from. Can be:
|
|
9182
|
+
* - A key, node, or entry: the method resolves it to a node and searches from that subtree.
|
|
9183
|
+
* - null or undefined: defaults to the root, searching the entire tree.
|
|
9184
|
+
* - Default value: this._root (the tree's root).
|
|
9185
|
+
*
|
|
9186
|
+
* @param iterationType - Controls the internal traversal implementation:
|
|
9187
|
+
* - 'RECURSIVE': uses recursive function calls for traversal.
|
|
9188
|
+
* - 'ITERATIVE': uses explicit stack-based iteration.
|
|
9189
|
+
* - Default: this.iterationType (the tree's default iteration mode).
|
|
9190
|
+
*
|
|
9191
|
+
* @returns A Map<K, boolean> containing the deletion results:
|
|
9192
|
+
* - Key: the matched node's key.
|
|
9193
|
+
* - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
|
|
9194
|
+
* - If no nodes match the search criteria, the returned map is empty.
|
|
9195
|
+
*/
|
|
9196
|
+
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
9197
|
+
const toDelete = this.search(
|
|
9198
|
+
keyNodeEntryOrPredicate,
|
|
9199
|
+
onlyOne,
|
|
9200
|
+
(node) => node,
|
|
9201
|
+
startNode,
|
|
9202
|
+
iterationType
|
|
9203
|
+
);
|
|
9204
|
+
let results = [];
|
|
9205
|
+
for (const node of toDelete) {
|
|
9206
|
+
const deleteInfo = this.delete(node);
|
|
9207
|
+
results = results.concat(deleteInfo);
|
|
9197
9208
|
}
|
|
9198
|
-
return
|
|
9209
|
+
return results;
|
|
9199
9210
|
}
|
|
9200
9211
|
/**
|
|
9201
9212
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
@@ -9347,8 +9358,7 @@ var BST = class extends BinaryTree {
|
|
|
9347
9358
|
_snapshotOptions() {
|
|
9348
9359
|
return {
|
|
9349
9360
|
...super._snapshotOptions(),
|
|
9350
|
-
|
|
9351
|
-
isReverse: this.isReverse
|
|
9361
|
+
comparator: this._comparator
|
|
9352
9362
|
};
|
|
9353
9363
|
}
|
|
9354
9364
|
/**
|
|
@@ -9376,14 +9386,14 @@ var BST = class extends BinaryTree {
|
|
|
9376
9386
|
}
|
|
9377
9387
|
/**
|
|
9378
9388
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
9379
|
-
* @remarks Time O(1)
|
|
9389
|
+
* @remarks Time O(1) Space O(1)
|
|
9380
9390
|
*
|
|
9381
9391
|
* @param a - The first key.
|
|
9382
9392
|
* @param b - The second key.
|
|
9383
9393
|
* @returns A number (1, -1, or 0) representing the comparison.
|
|
9384
9394
|
*/
|
|
9385
9395
|
_compare(a, b) {
|
|
9386
|
-
return this.
|
|
9396
|
+
return this._comparator(a, b);
|
|
9387
9397
|
}
|
|
9388
9398
|
/**
|
|
9389
9399
|
* (Private) Deletes a node by its key.
|