data-structure-typed 2.5.1 → 2.5.3
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 +5 -1
- package/MIGRATION.md +169 -0
- package/README.md +135 -23
- package/README_CN.md +551 -143
- package/SPECIFICATION.md +20 -14
- package/SPECIFICATION.zh-CN.md +20 -14
- package/dist/cjs/binary-tree.cjs +6460 -1591
- package/dist/cjs/graph.cjs +440 -20
- package/dist/cjs/hash.cjs +125 -22
- package/dist/cjs/heap.cjs +196 -47
- package/dist/cjs/index.cjs +8486 -2429
- package/dist/cjs/linked-list.cjs +456 -31
- package/dist/cjs/matrix.cjs +79 -9
- package/dist/cjs/priority-queue.cjs +193 -44
- package/dist/cjs/queue.cjs +391 -2
- package/dist/cjs/stack.cjs +92 -6
- package/dist/cjs/trie.cjs +122 -28
- package/dist/cjs-legacy/binary-tree.cjs +6484 -1612
- package/dist/cjs-legacy/graph.cjs +440 -20
- package/dist/cjs-legacy/hash.cjs +125 -22
- package/dist/cjs-legacy/heap.cjs +196 -47
- package/dist/cjs-legacy/index.cjs +8654 -2594
- package/dist/cjs-legacy/linked-list.cjs +456 -31
- package/dist/cjs-legacy/matrix.cjs +79 -9
- package/dist/cjs-legacy/priority-queue.cjs +193 -44
- package/dist/cjs-legacy/queue.cjs +391 -2
- package/dist/cjs-legacy/stack.cjs +92 -6
- package/dist/cjs-legacy/trie.cjs +122 -28
- package/dist/esm/binary-tree.mjs +6460 -1591
- package/dist/esm/graph.mjs +440 -20
- package/dist/esm/hash.mjs +125 -22
- package/dist/esm/heap.mjs +196 -47
- package/dist/esm/index.mjs +8486 -2430
- package/dist/esm/linked-list.mjs +456 -31
- package/dist/esm/matrix.mjs +79 -9
- package/dist/esm/priority-queue.mjs +193 -44
- package/dist/esm/queue.mjs +391 -2
- package/dist/esm/stack.mjs +92 -6
- package/dist/esm/trie.mjs +122 -28
- package/dist/esm-legacy/binary-tree.mjs +6484 -1612
- package/dist/esm-legacy/graph.mjs +440 -20
- package/dist/esm-legacy/hash.mjs +125 -22
- package/dist/esm-legacy/heap.mjs +196 -47
- package/dist/esm-legacy/index.mjs +8654 -2595
- package/dist/esm-legacy/linked-list.mjs +456 -31
- package/dist/esm-legacy/matrix.mjs +79 -9
- package/dist/esm-legacy/priority-queue.mjs +193 -44
- package/dist/esm-legacy/queue.mjs +391 -2
- package/dist/esm-legacy/stack.mjs +92 -6
- package/dist/esm-legacy/trie.mjs +122 -28
- package/dist/types/common/error.d.ts +9 -0
- package/dist/types/common/index.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +98 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +112 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +214 -13
- package/dist/types/data-structures/binary-tree/bst.d.ts +294 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +155 -8
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +48 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1370 -323
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1329 -316
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +1116 -295
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1330 -326
- package/dist/types/data-structures/graph/directed-graph.d.ts +80 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +72 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +95 -6
- package/dist/types/data-structures/heap/heap.d.ts +154 -12
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +143 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -1
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +144 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +64 -0
- package/dist/types/data-structures/queue/deque.d.ts +142 -0
- package/dist/types/data-structures/queue/queue.d.ts +109 -0
- package/dist/types/data-structures/stack/stack.d.ts +82 -2
- package/dist/types/data-structures/trie/trie.d.ts +96 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
- package/dist/umd/data-structure-typed.js +8623 -2564
- package/dist/umd/data-structure-typed.min.js +5 -5
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +696 -194
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +11 -11
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +71 -71
- package/docs-site-docusaurus/docs/api/classes/BST.md +639 -189
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +15 -15
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +148 -160
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/Deque.md +105 -91
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +82 -82
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +104 -74
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +12 -12
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +51 -51
- package/docs-site-docusaurus/docs/api/classes/Heap.md +96 -85
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +25 -25
- package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +33 -33
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +50 -50
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +55 -55
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +55 -55
- package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +78 -78
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +82 -82
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +31 -31
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/Navigator.md +5 -5
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +103 -88
- package/docs-site-docusaurus/docs/api/classes/Queue.md +112 -60
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +708 -206
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +10 -10
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +79 -79
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +44 -44
- package/docs-site-docusaurus/docs/api/classes/Stack.md +42 -42
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +236 -33
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +162 -46
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +232 -32
- package/docs-site-docusaurus/docs/api/classes/Trie.md +47 -47
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +81 -81
- package/docs-site-docusaurus/docs/guide/architecture.md +75 -5
- package/docs-site-docusaurus/docs/guide/concepts.md +53 -3
- package/docs-site-docusaurus/docs/guide/faq.md +233 -0
- package/docs-site-docusaurus/docs/guide/guides.md +43 -58
- package/docs-site-docusaurus/docs/guide/installation.md +2 -0
- package/docs-site-docusaurus/docs/guide/integrations.md +75 -176
- package/docs-site-docusaurus/docs/guide/overview.md +132 -11
- package/docs-site-docusaurus/docs/guide/performance.md +2 -0
- package/docs-site-docusaurus/docs/guide/quick-start.md +31 -0
- package/docs-site-docusaurus/docs/guide/use-cases/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/use-cases/array-sort-alternative.md +158 -0
- package/docs-site-docusaurus/docs/guide/use-cases/heap-vs-sorting.md +92 -0
- package/docs-site-docusaurus/docs/guide/use-cases/map-vs-treemap.md +151 -0
- package/docs-site-docusaurus/docs/guide/use-cases/priority-queue-typescript.md +113 -0
- package/docs-site-docusaurus/docs/guide/use-cases/treemap-javascript.md +151 -0
- package/docs-site-docusaurus/docusaurus.config.ts +1 -1
- package/docs-site-docusaurus/src/pages/index.tsx +55 -2
- package/docs-site-docusaurus/static/llms.txt +37 -0
- package/docs-site-docusaurus/typedoc.json +1 -0
- package/llms.txt +37 -0
- package/package.json +65 -56
- package/src/common/error.ts +19 -1
- package/src/common/index.ts +1 -1
- package/src/data-structures/base/iterable-element-base.ts +3 -2
- package/src/data-structures/binary-tree/avl-tree.ts +99 -5
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +102 -4
- package/src/data-structures/binary-tree/binary-tree.ts +239 -78
- package/src/data-structures/binary-tree/bst.ts +542 -13
- package/src/data-structures/binary-tree/red-black-tree.ts +155 -15
- package/src/data-structures/binary-tree/segment-tree.ts +42 -0
- package/src/data-structures/binary-tree/tree-map.ts +1223 -261
- package/src/data-structures/binary-tree/tree-multi-map.ts +939 -30
- package/src/data-structures/binary-tree/tree-multi-set.ts +746 -10
- package/src/data-structures/binary-tree/tree-set.ts +1018 -99
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +71 -1
- package/src/data-structures/graph/undirected-graph.ts +64 -1
- package/src/data-structures/hash/hash-map.ts +102 -16
- package/src/data-structures/heap/heap.ts +153 -23
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +139 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
- package/src/data-structures/linked-list/skip-linked-list.ts +131 -5
- package/src/data-structures/matrix/matrix.ts +65 -9
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +130 -0
- package/src/data-structures/queue/queue.ts +109 -0
- package/src/data-structures/stack/stack.ts +75 -5
- package/src/data-structures/trie/trie.ts +86 -2
- package/src/interfaces/binary-tree.ts +1 -9
- package/src/types/data-structures/binary-tree/bst.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +0 -12984
- package/.vitepress/cache/deps_temp_51f5f1b0/package.json +0 -3
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +0 -4505
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +0 -9731
- package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +0 -347
|
@@ -42,15 +42,38 @@ const features = [
|
|
|
42
42
|
title: '📦 Zero Dependencies',
|
|
43
43
|
description: 'Pure TypeScript. No runtime dependencies. Tree-shakeable with subpath exports — bundle only what you use.',
|
|
44
44
|
},
|
|
45
|
+
{
|
|
46
|
+
title: '🔄 Raw Data In, Structure Out',
|
|
47
|
+
description: 'Pass raw objects directly with toEntryFn, toElementFn, or comparator. No .map() pre-processing needed — unique to this library in JS/TS.',
|
|
48
|
+
},
|
|
45
49
|
{
|
|
46
50
|
title: '✅ Battle-Tested',
|
|
47
|
-
description: '
|
|
51
|
+
description: '2600+ tests, 99%+ coverage. CLRS-correct Red-Black Tree, ACL-style Segment Tree. Production-ready.',
|
|
48
52
|
},
|
|
49
53
|
];
|
|
50
54
|
|
|
55
|
+
const structures = [
|
|
56
|
+
{ category: 'Trees', items: 'RedBlackTree, AVLTree, BST, TreeMap, TreeSet, TreeMultiMap, TreeMultiSet' },
|
|
57
|
+
{ category: 'Heaps', items: 'Heap, MinHeap, MaxHeap, MinPriorityQueue, MaxPriorityQueue' },
|
|
58
|
+
{ category: 'Queues & Stacks', items: 'Queue, Deque, Stack' },
|
|
59
|
+
{ category: 'Linked Lists', items: 'SinglyLinkedList, DoublyLinkedList, SkipList' },
|
|
60
|
+
{ category: 'Hashing', items: 'HashMap' },
|
|
61
|
+
{ category: 'Graphs', items: 'DirectedGraph, UndirectedGraph' },
|
|
62
|
+
{ category: 'Strings', items: 'Trie' },
|
|
63
|
+
{ category: 'Arrays', items: 'SegmentTree, BinaryIndexedTree, Matrix' },
|
|
64
|
+
];
|
|
65
|
+
|
|
66
|
+
const useCases = [
|
|
67
|
+
{ title: 'Priority Queue', link: '/docs/guide/use-cases/priority-queue-typescript', desc: 'Task scheduling, top-k problems, Dijkstra' },
|
|
68
|
+
{ title: 'TreeMap / TreeSet', link: '/docs/guide/use-cases/treemap-javascript', desc: 'Sorted maps, floor/ceiling, range queries' },
|
|
69
|
+
{ title: 'Array + Sort Too Slow?', link: '/docs/guide/use-cases/array-sort-alternative', desc: 'O(log n) insert vs O(n log n) re-sort' },
|
|
70
|
+
{ title: 'Heap vs Sorting', link: '/docs/guide/use-cases/heap-vs-sorting', desc: 'When to use which' },
|
|
71
|
+
{ title: 'Map vs TreeMap', link: '/docs/guide/use-cases/map-vs-treemap', desc: 'When native Map isn\'t enough' },
|
|
72
|
+
];
|
|
73
|
+
|
|
51
74
|
export default function Home(): React.JSX.Element {
|
|
52
75
|
return (
|
|
53
|
-
<Layout title="Home" description="
|
|
76
|
+
<Layout title="Home" description="Production-ready TypeScript data structures library — Heap, TreeMap, TreeSet, Trie, Graph, Priority Queue, Deque, and more. Zero dependencies, type-safe, with rank and range query support.">
|
|
54
77
|
<HomepageHeader />
|
|
55
78
|
<main>
|
|
56
79
|
<section style={{padding: '4rem 0'}}>
|
|
@@ -65,6 +88,36 @@ export default function Home(): React.JSX.Element {
|
|
|
65
88
|
</div>
|
|
66
89
|
</div>
|
|
67
90
|
</section>
|
|
91
|
+
|
|
92
|
+
<section style={{padding: '2rem 0 4rem', background: 'var(--ifm-background-surface-color)'}}>
|
|
93
|
+
<div className="container">
|
|
94
|
+
<h2 style={{textAlign: 'center', marginBottom: '2rem'}}>📚 Data Structures Available</h2>
|
|
95
|
+
<div className="row" style={{justifyContent: 'center'}}>
|
|
96
|
+
{structures.map((s, i) => (
|
|
97
|
+
<div key={i} className={clsx('col col--6')} style={{marginBottom: '1.5rem'}}>
|
|
98
|
+
<h4>{s.category}</h4>
|
|
99
|
+
<p style={{color: 'var(--ifm-color-emphasis-700)', fontSize: '0.95rem'}}>{s.items}</p>
|
|
100
|
+
</div>
|
|
101
|
+
))}
|
|
102
|
+
</div>
|
|
103
|
+
</div>
|
|
104
|
+
</section>
|
|
105
|
+
|
|
106
|
+
<section style={{padding: '4rem 0'}}>
|
|
107
|
+
<div className="container">
|
|
108
|
+
<h2 style={{textAlign: 'center', marginBottom: '2rem'}}>🎯 Use Cases & Guides</h2>
|
|
109
|
+
<div className="row" style={{justifyContent: 'center'}}>
|
|
110
|
+
{useCases.map((u, i) => (
|
|
111
|
+
<div key={i} className={clsx('col col--4')} style={{marginBottom: '1.5rem'}}>
|
|
112
|
+
<Link to={u.link} style={{textDecoration: 'none'}}>
|
|
113
|
+
<h4>{u.title}</h4>
|
|
114
|
+
<p style={{color: 'var(--ifm-color-emphasis-700)', fontSize: '0.9rem'}}>{u.desc}</p>
|
|
115
|
+
</Link>
|
|
116
|
+
</div>
|
|
117
|
+
))}
|
|
118
|
+
</div>
|
|
119
|
+
</div>
|
|
120
|
+
</section>
|
|
68
121
|
</main>
|
|
69
122
|
</Layout>
|
|
70
123
|
);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# data-structure-typed
|
|
2
|
+
|
|
3
|
+
> A production-ready TypeScript/JavaScript data structures library with zero dependencies.
|
|
4
|
+
|
|
5
|
+
## What it is
|
|
6
|
+
A comprehensive collection of 20+ data structures for TypeScript and JavaScript, with a unified API that feels like native Array. Includes Heap, Priority Queue, Deque, Trie, Graph, Red-Black Tree, TreeMap, TreeSet, SkipList, Segment Tree, Binary Indexed Tree, Matrix, and more.
|
|
7
|
+
|
|
8
|
+
## Key capabilities
|
|
9
|
+
- TreeMap / TreeSet: Sorted key-value and key-only collections (Red-Black Tree backed)
|
|
10
|
+
- Order-statistic operations: getRank(key), getByRank(k), rangeByRank(start, end)
|
|
11
|
+
- NavigableMap API: floor, ceiling, higher, lower, rangeSearch
|
|
12
|
+
- Priority Queue / Heap: MinHeap, MaxHeap, custom comparators
|
|
13
|
+
- Graph algorithms: Dijkstra, Bellman-Ford, Floyd-Warshall, Tarjan, topological sort, DFS, BFS
|
|
14
|
+
- Trie: Prefix search, autocomplete
|
|
15
|
+
- All structures support: forEach, filter, map, reduce, every, some, find, entries, keys, values
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
npm i data-structure-typed
|
|
19
|
+
|
|
20
|
+
## Links
|
|
21
|
+
- Documentation: https://data-structure-typed-docs.vercel.app/
|
|
22
|
+
- GitHub: https://github.com/zrwusa/data-structure-typed
|
|
23
|
+
- npm: https://www.npmjs.com/package/data-structure-typed
|
|
24
|
+
- Quick Start: https://data-structure-typed-docs.vercel.app/docs/guide/quick-start
|
|
25
|
+
- API Reference: https://data-structure-typed-docs.vercel.app/docs/api/
|
|
26
|
+
- Guides: https://data-structure-typed-docs.vercel.app/docs/guide/guides
|
|
27
|
+
- FAQ: https://github.com/zrwusa/data-structure-typed#-faq
|
|
28
|
+
|
|
29
|
+
## Common questions
|
|
30
|
+
- Does JavaScript have TreeMap? → Use TreeMap from this library (Red-Black Tree backed, sorted iteration, rank queries)
|
|
31
|
+
- Priority queue in TypeScript? → Use Heap, MinHeap, MaxHeap, or MinPriorityQueue/MaxPriorityQueue
|
|
32
|
+
- Sorted set / ordered set in JS? → Use TreeSet (maintains sorted order, supports floor/ceiling/range)
|
|
33
|
+
- How to find kth element efficiently? → Use getByRank(k) with enableOrderStatistic: true
|
|
34
|
+
- Alternative to repeatedly sorting arrays? → Use TreeMap/TreeSet for O(log n) insert with maintained order
|
|
35
|
+
- Bundle size? → ~143KB UMD min, tree-shakeable subpath imports (18KB–315KB per category)
|
|
36
|
+
- Zero dependencies? → Yes
|
|
37
|
+
- Production ready? → Yes, 2600+ tests, 99%+ coverage
|
package/llms.txt
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# data-structure-typed
|
|
2
|
+
|
|
3
|
+
> A production-ready TypeScript/JavaScript data structures library with zero dependencies.
|
|
4
|
+
|
|
5
|
+
## What it is
|
|
6
|
+
A comprehensive collection of 20+ data structures for TypeScript and JavaScript, with a unified API that feels like native Array. Includes Heap, Priority Queue, Deque, Trie, Graph, Red-Black Tree, TreeMap, TreeSet, SkipList, Segment Tree, Binary Indexed Tree, Matrix, and more.
|
|
7
|
+
|
|
8
|
+
## Key capabilities
|
|
9
|
+
- TreeMap / TreeSet: Sorted key-value and key-only collections (Red-Black Tree backed)
|
|
10
|
+
- Order-statistic operations: getRank(key), getByRank(k), rangeByRank(start, end)
|
|
11
|
+
- NavigableMap API: floor, ceiling, higher, lower, rangeSearch
|
|
12
|
+
- Priority Queue / Heap: MinHeap, MaxHeap, custom comparators
|
|
13
|
+
- Graph algorithms: Dijkstra, Bellman-Ford, Floyd-Warshall, Tarjan, topological sort, DFS, BFS
|
|
14
|
+
- Trie: Prefix search, autocomplete
|
|
15
|
+
- All structures support: forEach, filter, map, reduce, every, some, find, entries, keys, values
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
npm i data-structure-typed
|
|
19
|
+
|
|
20
|
+
## Links
|
|
21
|
+
- Documentation: https://data-structure-typed-docs.vercel.app/
|
|
22
|
+
- GitHub: https://github.com/zrwusa/data-structure-typed
|
|
23
|
+
- npm: https://www.npmjs.com/package/data-structure-typed
|
|
24
|
+
- Quick Start: https://data-structure-typed-docs.vercel.app/docs/guide/quick-start
|
|
25
|
+
- API Reference: https://data-structure-typed-docs.vercel.app/docs/api/
|
|
26
|
+
- Guides: https://data-structure-typed-docs.vercel.app/docs/guide/guides
|
|
27
|
+
- FAQ: https://github.com/zrwusa/data-structure-typed#-faq
|
|
28
|
+
|
|
29
|
+
## Common questions
|
|
30
|
+
- Does JavaScript have TreeMap? → Use TreeMap from this library (Red-Black Tree backed, sorted iteration, rank queries)
|
|
31
|
+
- Priority queue in TypeScript? → Use Heap, MinHeap, MaxHeap, or MinPriorityQueue/MaxPriorityQueue
|
|
32
|
+
- Sorted set / ordered set in JS? → Use TreeSet (maintains sorted order, supports floor/ceiling/range)
|
|
33
|
+
- How to find kth element efficiently? → Use getByRank(k) with enableOrderStatistic: true
|
|
34
|
+
- Alternative to repeatedly sorting arrays? → Use TreeMap/TreeSet for O(log n) insert with maintained order
|
|
35
|
+
- Bundle size? → ~143KB UMD min, tree-shakeable subpath imports (18KB–315KB per category)
|
|
36
|
+
- Zero dependencies? → Yes
|
|
37
|
+
- Production ready? → Yes, 2600+ tests, 99%+ coverage
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "2.5.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "2.5.3",
|
|
4
|
+
"description": "Production-ready TypeScript data structures: Heap, Deque, Trie, Graph, Red-Black Tree, TreeMap, TreeSet, and more. Zero dependencies, type-safe, with getRank/getByRank/rangeByRank support.",
|
|
5
5
|
"browser": "dist/umd/data-structure-typed.min.js",
|
|
6
6
|
"umd:main": "dist/umd/data-structure-typed.min.js",
|
|
7
7
|
"main": "dist/cjs/index.cjs",
|
|
@@ -129,49 +129,46 @@
|
|
|
129
129
|
"npm": ">=6.14.0"
|
|
130
130
|
},
|
|
131
131
|
"scripts": {
|
|
132
|
-
"build": "npm run build:
|
|
132
|
+
"build": "npm run build:node && npm run build:types && npm run build:umd",
|
|
133
133
|
"build:node": "tsup",
|
|
134
134
|
"build:umd": "tsup --config tsup.umd.config.js",
|
|
135
135
|
"build:types": "rm -rf dist/types && tsc -p tsconfig.types.json",
|
|
136
|
-
"build:ecut": "npm run build:node && npm run build:types && npm run build:umd",
|
|
137
136
|
"build:leetcode": "tsup --config tsup.leetcode.config.js",
|
|
138
137
|
"build:typedoc-plugin": "tsc scripts/typedoc-plugin-example-rewrite.ts --outDir scripts --esModuleInterop --module commonjs --target es2020 --moduleResolution node --skipLibCheck",
|
|
139
|
-
"build:docs": "npm run gen:examples && npm run generate:schema && npm run build:typedoc-plugin && typedoc --plugin ./scripts/typedoc-plugin-example-rewrite.js --out docs/api ./src",
|
|
140
|
-
"build:docs-class": "npm run gen:examples && npm run build:typedoc-plugin && typedoc --plugin ./scripts/typedoc-plugin-example-rewrite.js --out docs/api ./src/data-structures",
|
|
141
138
|
"gen:examples": "ts-node scripts/test-to-example.ts",
|
|
142
|
-
"
|
|
143
|
-
"test": "
|
|
139
|
+
"generate:schema": "ts-node scripts/generate-schema.ts",
|
|
140
|
+
"test": "jest --runInBand",
|
|
141
|
+
"test:coverage": "jest --runInBand --coverage",
|
|
144
142
|
"test:integration": "npm run update:subs && jest --config jest.integration.config.js && tsc test/integration/compile.test.ts && node test/integration/compile.mjs",
|
|
145
|
-
"test:perf": "npm run build
|
|
143
|
+
"test:perf": "npm run build && NODE_OPTIONS='--expose-gc --max-old-space-size=8192' node test/performance/benchmark-runner-enhanced.mjs",
|
|
146
144
|
"test:perf:report": "node test/performance/reportor-enhanced.mjs",
|
|
147
|
-
"test:coverage": "jest --runInBand --coverage",
|
|
148
145
|
"check": "npm run check:src && npm run check:test",
|
|
149
146
|
"check:src": "tsc --noEmit",
|
|
150
147
|
"check:test": "tsc -p tsconfig.test.json --noEmit",
|
|
151
148
|
"check:circular-refs": "dependency-cruiser src",
|
|
149
|
+
"lint": "npm run lint:src && npm run lint:test",
|
|
152
150
|
"lint:src": "eslint --fix 'src/**/*.{js,ts}'",
|
|
153
151
|
"lint:test": "eslint --fix 'test/**/*.{js,ts}'",
|
|
154
|
-
"
|
|
155
|
-
"inspect": "npm run build && npm run check && npm run lint",
|
|
152
|
+
"format": "npm run format:src && npm run format:test",
|
|
156
153
|
"format:src": "prettier --write 'src/**/*.{js,ts}'",
|
|
157
154
|
"format:test": "prettier --write 'test/**/*.{js,ts}'",
|
|
158
|
-
"
|
|
159
|
-
"
|
|
160
|
-
"
|
|
155
|
+
"inspect": "npm run build && npm run check && npm run lint",
|
|
156
|
+
"ci": "env && git fetch --tags && npm run update:subs && npm run inspect && npm run test:coverage && npm run changelog",
|
|
157
|
+
"docs:sync": "node scripts/sync-docs-to-site.mjs",
|
|
158
|
+
"docs:api": "npm run gen:examples && npm run build:typedoc-plugin && cd docs-site-docusaurus && npx typedoc --options typedoc.json && node sort-protected.mjs",
|
|
159
|
+
"docs:dev": "cd docs-site-docusaurus && npm run start",
|
|
160
|
+
"docs:build": "cd docs-site-docusaurus && rm -rf docs/api && npx typedoc --options typedoc.json && node sort-protected.mjs && npm run build",
|
|
161
|
+
"docs:preview": "cd docs-site-docusaurus && npm run serve",
|
|
161
162
|
"update:subs": "npm i avl-tree-typed binary-tree-typed bst-typed heap-typed data-structure-typed --save-dev",
|
|
162
163
|
"install:all-subs": "npm i avl-tree-typed binary-tree-typed bst-typed deque-typed directed-graph-typed doubly-linked-list-typed graph-typed heap-typed linked-list-typed max-heap-typed max-priority-queue-typed min-heap-typed min-priority-queue-typed priority-queue-typed singly-linked-list-typed stack-typed tree-multimap-typed trie-typed undirected-graph-typed queue-typed --save-dev",
|
|
163
|
-
"changelog": "auto-changelog",
|
|
164
|
-
"coverage:badge": "istanbul-badges-readme",
|
|
165
|
-
"toc": "doctoc README.md",
|
|
166
164
|
"copy:to-subs": "sh scripts/copy_to_all_subs.sh",
|
|
167
165
|
"publish:subs": "npm run copy:to-subs && sh scripts/publish_all_subs.sh",
|
|
168
166
|
"publish:docs": "sh scripts/publish_docs.sh",
|
|
169
167
|
"publish:all": "npm run ci && npm publish && npm run publish:docs && npm run check:exist-latest && npm run publish:subs",
|
|
170
|
-
"
|
|
171
|
-
"
|
|
172
|
-
"
|
|
173
|
-
"
|
|
174
|
-
"docs:preview": "cd docs-site-docusaurus && npm run serve"
|
|
168
|
+
"check:exist-latest": "sh scripts/check_exist_remotely.sh",
|
|
169
|
+
"changelog": "auto-changelog",
|
|
170
|
+
"coverage:badge": "istanbul-badges-readme",
|
|
171
|
+
"toc": "doctoc README.md"
|
|
175
172
|
},
|
|
176
173
|
"repository": {
|
|
177
174
|
"type": "git",
|
|
@@ -197,11 +194,11 @@
|
|
|
197
194
|
"@typescript-eslint/eslint-plugin": "^8.12.1",
|
|
198
195
|
"@typescript-eslint/parser": "^8.12.1",
|
|
199
196
|
"auto-changelog": "^2.5.0",
|
|
200
|
-
"avl-tree-typed": "^2.5.
|
|
197
|
+
"avl-tree-typed": "^2.5.2",
|
|
201
198
|
"benchmark": "^2.1.4",
|
|
202
|
-
"binary-tree-typed": "^2.5.
|
|
203
|
-
"bst-typed": "^2.5.
|
|
204
|
-
"data-structure-typed": "^2.5.
|
|
199
|
+
"binary-tree-typed": "^2.5.2",
|
|
200
|
+
"bst-typed": "^2.5.2",
|
|
201
|
+
"data-structure-typed": "^2.5.2",
|
|
205
202
|
"dependency-cruiser": "^16.5.0",
|
|
206
203
|
"doctoc": "^2.2.1",
|
|
207
204
|
"eslint": "^9.13.0",
|
|
@@ -210,7 +207,7 @@
|
|
|
210
207
|
"eslint-import-resolver-typescript": "^3.6.3",
|
|
211
208
|
"eslint-plugin-import": "^2.31.0",
|
|
212
209
|
"fast-glob": "^3.3.2",
|
|
213
|
-
"heap-typed": "^2.5.
|
|
210
|
+
"heap-typed": "^2.5.2",
|
|
214
211
|
"istanbul-badges-readme": "^1.9.0",
|
|
215
212
|
"jest": "^29.7.0",
|
|
216
213
|
"js-sdsl": "^4.4.2",
|
|
@@ -225,59 +222,71 @@
|
|
|
225
222
|
"typescript": "~5.9"
|
|
226
223
|
},
|
|
227
224
|
"keywords": [
|
|
228
|
-
"data structures
|
|
229
|
-
"
|
|
225
|
+
"data structures",
|
|
226
|
+
"typescript",
|
|
227
|
+
"javascript",
|
|
228
|
+
"data structures library",
|
|
230
229
|
"typescript data structures",
|
|
231
230
|
"javascript data structures",
|
|
232
|
-
"
|
|
233
|
-
"algorithm library",
|
|
231
|
+
"binary tree",
|
|
234
232
|
"binary search tree",
|
|
233
|
+
"BST",
|
|
235
234
|
"AVL tree",
|
|
236
235
|
"red black tree",
|
|
237
236
|
"tree map",
|
|
238
237
|
"tree set",
|
|
238
|
+
"treemap javascript",
|
|
239
|
+
"treeset javascript",
|
|
240
|
+
"sorted map",
|
|
241
|
+
"sorted set",
|
|
242
|
+
"ordered map",
|
|
243
|
+
"ordered set",
|
|
239
244
|
"tree multi map",
|
|
240
245
|
"tree multi set",
|
|
241
|
-
"balanced tree",
|
|
242
|
-
"binary tree",
|
|
243
|
-
"BST",
|
|
244
|
-
"graph algorithm",
|
|
245
|
-
"directed graph",
|
|
246
|
-
"undirected graph",
|
|
247
246
|
"heap",
|
|
248
|
-
"priority queue",
|
|
249
247
|
"min heap",
|
|
250
248
|
"max heap",
|
|
249
|
+
"priority queue",
|
|
251
250
|
"queue",
|
|
252
251
|
"deque",
|
|
252
|
+
"double ended queue",
|
|
253
253
|
"stack",
|
|
254
254
|
"linked list",
|
|
255
255
|
"singly linked list",
|
|
256
256
|
"doubly linked list",
|
|
257
|
+
"skip list",
|
|
257
258
|
"trie",
|
|
259
|
+
"prefix tree",
|
|
260
|
+
"graph",
|
|
261
|
+
"directed graph",
|
|
262
|
+
"undirected graph",
|
|
258
263
|
"hash map",
|
|
259
264
|
"hash table",
|
|
260
|
-
"tree
|
|
261
|
-
"tree
|
|
262
|
-
"tree
|
|
263
|
-
"
|
|
264
|
-
"
|
|
265
|
-
"
|
|
266
|
-
"
|
|
267
|
-
"
|
|
268
|
-
"
|
|
269
|
-
"
|
|
270
|
-
"dijkstra
|
|
265
|
+
"segment tree",
|
|
266
|
+
"binary indexed tree",
|
|
267
|
+
"fenwick tree",
|
|
268
|
+
"matrix",
|
|
269
|
+
"rank query",
|
|
270
|
+
"range query",
|
|
271
|
+
"order statistic tree",
|
|
272
|
+
"getRank",
|
|
273
|
+
"getByRank",
|
|
274
|
+
"rangeByRank",
|
|
275
|
+
"dijkstra",
|
|
271
276
|
"bellman ford",
|
|
272
|
-
"floyd warshall",
|
|
273
|
-
"tarjan algorithm",
|
|
274
277
|
"topological sort",
|
|
278
|
+
"DFS",
|
|
279
|
+
"BFS",
|
|
275
280
|
"leetcode",
|
|
276
281
|
"coding interview",
|
|
277
|
-
"algorithm
|
|
278
|
-
"
|
|
279
|
-
"
|
|
282
|
+
"algorithm",
|
|
283
|
+
"zero dependency",
|
|
284
|
+
"production ready",
|
|
285
|
+
"type safe",
|
|
286
|
+
"ES6",
|
|
280
287
|
"CommonJS",
|
|
281
|
-
"
|
|
288
|
+
"UMD",
|
|
289
|
+
"collection",
|
|
290
|
+
"sorted collection"
|
|
282
291
|
]
|
|
283
292
|
}
|
package/src/common/error.ts
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Centralized error dispatch.
|
|
3
|
+
* All library errors go through this function for consistent messaging and easy grep.
|
|
4
|
+
* @remarks Always throws — data structure errors are never recoverable.
|
|
5
|
+
* @param ErrorClass - The error constructor (Error, TypeError, RangeError, etc.)
|
|
6
|
+
* @param message - The error message.
|
|
7
|
+
*/
|
|
8
|
+
export function raise(
|
|
9
|
+
ErrorClass: new (msg: string) => Error,
|
|
10
|
+
message: string
|
|
11
|
+
): never {
|
|
12
|
+
throw new ErrorClass(message);
|
|
13
|
+
}
|
|
14
|
+
|
|
1
15
|
/**
|
|
2
16
|
* Centralized error message templates.
|
|
3
17
|
* Keep using native Error/TypeError/RangeError — this only standardizes messages.
|
|
@@ -56,5 +70,9 @@ export const ERR = {
|
|
|
56
70
|
'Matrix: Must be rectangular for transposition.',
|
|
57
71
|
|
|
58
72
|
matrixRowMismatch: (expected: number, got: number) =>
|
|
59
|
-
`Matrix: Expected row length ${expected}, but got ${got}
|
|
73
|
+
`Matrix: Expected row length ${expected}, but got ${got}.`,
|
|
74
|
+
|
|
75
|
+
// Order statistic
|
|
76
|
+
orderStatisticNotEnabled: (method: string, ctx?: string) =>
|
|
77
|
+
`${ctx ? ctx + ': ' : ''}${method}() requires enableOrderStatistic: true.`
|
|
60
78
|
} as const;
|
package/src/common/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ElementCallback, IterableElementBaseOptions, ReduceElementCallback } from '../../types';
|
|
2
|
+
import { raise } from '../../common';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Base class that makes a data structure iterable and provides common
|
|
@@ -25,7 +26,7 @@ export abstract class IterableElementBase<E, R> implements Iterable<E> {
|
|
|
25
26
|
if (options) {
|
|
26
27
|
const { toElementFn } = options;
|
|
27
28
|
if (typeof toElementFn === 'function') this._toElementFn = toElementFn;
|
|
28
|
-
else if (toElementFn)
|
|
29
|
+
else if (toElementFn) raise(TypeError, 'toElementFn must be a function type');
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
32
|
|
|
@@ -224,7 +225,7 @@ export abstract class IterableElementBase<E, R> implements Iterable<E> {
|
|
|
224
225
|
acc = initialValue as U;
|
|
225
226
|
} else {
|
|
226
227
|
const first = iter.next();
|
|
227
|
-
if (first.done)
|
|
228
|
+
if (first.done) raise(TypeError, 'Reduce of empty structure with no initial value');
|
|
228
229
|
acc = first.value as unknown as U;
|
|
229
230
|
index = 1;
|
|
230
231
|
}
|
|
@@ -9,7 +9,6 @@
|
|
|
9
9
|
import { BST } from './bst';
|
|
10
10
|
import type {
|
|
11
11
|
AVLTreeOptions,
|
|
12
|
-
BinaryTreeDeleteResult,
|
|
13
12
|
BinaryTreeOptions,
|
|
14
13
|
BSTNOptKeyOrNode,
|
|
15
14
|
EntryCallback,
|
|
@@ -459,6 +458,34 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
459
458
|
|
|
460
459
|
|
|
461
460
|
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
|
|
462
489
|
|
|
463
490
|
|
|
464
491
|
|
|
@@ -579,6 +606,27 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
579
606
|
|
|
580
607
|
|
|
581
608
|
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
582
630
|
|
|
583
631
|
|
|
584
632
|
|
|
@@ -609,15 +657,15 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
609
657
|
*/
|
|
610
658
|
override delete(
|
|
611
659
|
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
612
|
-
):
|
|
613
|
-
const deletedResults =
|
|
660
|
+
): boolean {
|
|
661
|
+
const deletedResults = this._deleteInternal(keyNodeOrEntry);
|
|
614
662
|
// After deletion, balance the path from the parent of the *physically deleted* node.
|
|
615
663
|
for (const { needBalanced } of deletedResults) {
|
|
616
664
|
if (needBalanced) {
|
|
617
|
-
this._balancePath(needBalanced);
|
|
665
|
+
this._balancePath(needBalanced as AVLTreeNode<K, V>);
|
|
618
666
|
}
|
|
619
667
|
}
|
|
620
|
-
return deletedResults;
|
|
668
|
+
return deletedResults.length > 0;
|
|
621
669
|
}
|
|
622
670
|
|
|
623
671
|
/**
|
|
@@ -666,6 +714,20 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
666
714
|
|
|
667
715
|
|
|
668
716
|
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
|
|
669
731
|
|
|
670
732
|
|
|
671
733
|
|
|
@@ -794,6 +856,27 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
794
856
|
|
|
795
857
|
|
|
796
858
|
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
|
|
797
880
|
|
|
798
881
|
|
|
799
882
|
|
|
@@ -970,6 +1053,8 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
970
1053
|
}
|
|
971
1054
|
this._updateHeight(A);
|
|
972
1055
|
if (B) this._updateHeight(B);
|
|
1056
|
+
this._updateCount(A);
|
|
1057
|
+
if (B) this._updateCount(B);
|
|
973
1058
|
}
|
|
974
1059
|
|
|
975
1060
|
/**
|
|
@@ -1022,6 +1107,9 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1022
1107
|
this._updateHeight(A);
|
|
1023
1108
|
if (B) this._updateHeight(B);
|
|
1024
1109
|
if (C) this._updateHeight(C);
|
|
1110
|
+
this._updateCount(A);
|
|
1111
|
+
if (B) this._updateCount(B);
|
|
1112
|
+
if (C) this._updateCount(C);
|
|
1025
1113
|
}
|
|
1026
1114
|
|
|
1027
1115
|
/**
|
|
@@ -1061,6 +1149,8 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1061
1149
|
}
|
|
1062
1150
|
this._updateHeight(A);
|
|
1063
1151
|
if (B) this._updateHeight(B);
|
|
1152
|
+
this._updateCount(A);
|
|
1153
|
+
if (B) this._updateCount(B);
|
|
1064
1154
|
}
|
|
1065
1155
|
|
|
1066
1156
|
/**
|
|
@@ -1112,6 +1202,9 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1112
1202
|
this._updateHeight(A);
|
|
1113
1203
|
if (B) this._updateHeight(B);
|
|
1114
1204
|
if (C) this._updateHeight(C);
|
|
1205
|
+
this._updateCount(A);
|
|
1206
|
+
if (B) this._updateCount(B);
|
|
1207
|
+
if (C) this._updateCount(C);
|
|
1115
1208
|
}
|
|
1116
1209
|
|
|
1117
1210
|
/**
|
|
@@ -1130,6 +1223,7 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1130
1223
|
const A = path[i];
|
|
1131
1224
|
if (A) {
|
|
1132
1225
|
this._updateHeight(A);
|
|
1226
|
+
this._updateCount(A);
|
|
1133
1227
|
|
|
1134
1228
|
// Check the balance factor
|
|
1135
1229
|
switch (this._balanceFactor(A)) {
|