data-structure-typed 2.2.1 → 2.2.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.
Files changed (83) hide show
  1. package/CHANGELOG.md +3 -1
  2. package/README.md +440 -1190
  3. package/README_CN.md +509 -0
  4. package/SECURITY.md +962 -11
  5. package/SECURITY.zh-CN.md +966 -0
  6. package/SPECIFICATION.md +689 -30
  7. package/SPECIFICATION.zh-CN.md +715 -0
  8. package/SPONSOR.zh-CN.md +62 -0
  9. package/SPONSOR_POLISHED.md +62 -0
  10. package/benchmark/report.html +1 -1
  11. package/benchmark/report.json +215 -172
  12. package/dist/cjs/index.cjs +163 -0
  13. package/dist/cjs/index.cjs.map +1 -1
  14. package/dist/cjs-legacy/index.cjs +164 -0
  15. package/dist/cjs-legacy/index.cjs.map +1 -1
  16. package/dist/esm/index.mjs +163 -0
  17. package/dist/esm/index.mjs.map +1 -1
  18. package/dist/esm-legacy/index.mjs +164 -0
  19. package/dist/esm-legacy/index.mjs.map +1 -1
  20. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +96 -2
  21. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
  22. package/dist/types/data-structures/binary-tree/bst.d.ts +156 -13
  23. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +84 -35
  24. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  25. package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
  27. package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
  28. package/dist/types/data-structures/heap/heap.d.ts +107 -58
  29. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
  30. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
  31. package/dist/types/data-structures/queue/deque.d.ts +95 -67
  32. package/dist/types/data-structures/queue/queue.d.ts +90 -34
  33. package/dist/types/data-structures/stack/stack.d.ts +58 -40
  34. package/dist/types/data-structures/trie/trie.d.ts +109 -47
  35. package/dist/types/interfaces/binary-tree.d.ts +1 -0
  36. package/dist/umd/data-structure-typed.js +164 -0
  37. package/dist/umd/data-structure-typed.js.map +1 -1
  38. package/dist/umd/data-structure-typed.min.js +3 -3
  39. package/dist/umd/data-structure-typed.min.js.map +1 -1
  40. package/package.json +3 -2
  41. package/src/data-structures/binary-tree/avl-tree.ts +96 -2
  42. package/src/data-structures/binary-tree/binary-tree.ts +117 -7
  43. package/src/data-structures/binary-tree/bst.ts +322 -13
  44. package/src/data-structures/binary-tree/red-black-tree.ts +84 -35
  45. package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
  46. package/src/data-structures/graph/directed-graph.ts +126 -1
  47. package/src/data-structures/graph/undirected-graph.ts +160 -1
  48. package/src/data-structures/hash/hash-map.ts +110 -27
  49. package/src/data-structures/heap/heap.ts +107 -58
  50. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
  51. package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
  52. package/src/data-structures/queue/deque.ts +95 -67
  53. package/src/data-structures/queue/queue.ts +90 -34
  54. package/src/data-structures/stack/stack.ts +58 -40
  55. package/src/data-structures/trie/trie.ts +109 -47
  56. package/src/interfaces/binary-tree.ts +2 -0
  57. package/test/performance/benchmark-runner.ts +14 -11
  58. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +8 -8
  59. package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +8 -8
  60. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -6
  61. package/test/performance/data-structures/binary-tree/bst.test.ts +5 -5
  62. package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +10 -10
  63. package/test/performance/reportor.ts +2 -1
  64. package/test/performance/single-suite-runner.ts +7 -4
  65. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +117 -0
  66. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +166 -0
  67. package/test/unit/data-structures/binary-tree/bst.test.ts +766 -8
  68. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +89 -37
  69. package/test/unit/data-structures/graph/directed-graph.test.ts +133 -0
  70. package/test/unit/data-structures/graph/undirected-graph.test.ts +167 -0
  71. package/test/unit/data-structures/hash/hash-map.test.ts +149 -3
  72. package/test/unit/data-structures/heap/heap.test.ts +182 -47
  73. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +118 -14
  74. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +121 -0
  75. package/test/unit/data-structures/queue/deque.test.ts +98 -67
  76. package/test/unit/data-structures/queue/queue.test.ts +85 -51
  77. package/test/unit/data-structures/stack/stack.test.ts +142 -33
  78. package/test/unit/data-structures/trie/trie.test.ts +135 -39
  79. package/tsup.leetcode.config.js +99 -0
  80. package/typedoc.json +2 -1
  81. package/POSTS_zh-CN.md +0 -54
  82. package/README_zh-CN.md +0 -1208
  83. package/SPECIFICATION_zh-CN.md +0 -81
package/README.md CHANGED
@@ -1,1263 +1,513 @@
1
- # data-structure-typed
1
+ # README: data-structure-typed Library
2
+
3
+ A comprehensive TypeScript data structures library with production-ready implementations.
4
+
5
+ **📚 [Quick Start](#-quick-start-30-seconds) • [Full Docs](./docs/CONCEPTS.md) • [API Reference](./docs/REFERENCE.md) • [Examples](./docs/GUIDES.md)**
6
+
7
+ ---
8
+
9
+ ## Table of Contents
10
+
11
+ 1. [Who Should Use This?](#-who-should-use-this)
12
+ 2. [Why Not Just Array or Map?](#-why-not-just-array-or-map)
13
+ 3. [Key Features](#-key-features)
14
+ 4. [Installation](#-installation)
15
+ 5. [Quick Start](#-quick-start-30-seconds)
16
+ 6. [Data Structures](#-data-structures-available)
17
+ 7. [Documentation](#-documentation)
18
+
19
+ ---
20
+
21
+ ## 🎯 Who Should Use This?
22
+
23
+ **If you are building ranked collections, scheduling queues, or sorted data structures in TypeScript,**
24
+ **consider `data-structure-typed` instead of hand-rolled Arrays or Maps.**
25
+
26
+ ### Perfect for:
27
+
28
+ - **Leaderboards & Rankings** — Maintain top-K efficiently without repeated sorting
29
+ - **Task Scheduling** — Priority queues, ordered execution, time-based operations
30
+ - **Real-Time Dashboards** — Grafana-style workloads with instant lookups
31
+ - **Time-Series Data** — Sorted insertion + fast range queries
32
+ - **Search & Autocomplete** — Prefix matching at scale
33
+ - **Graph Problems** — Pathfinding, cycle detection, topological sorting
34
+
35
+ ---
36
+
37
+ ## ⚡ Why Not Just Array or Map?
38
+
39
+ | Use Case | Array | Map | data-structure-typed |
40
+ |------------------------|----------------------|------------------|:--------------------:|
41
+ | **Sorted Lookup** | ❌ O(n) | ❌ Unordered | ✅ **O(log n)** |
42
+ | **Insert at Position** | ❌ O(n) shift | ❌ No position | ✅ **O(log n)** |
43
+ | **Leaderboard Top-K** | ❌ Re-sort O(n log n) | ❌ Manual sort | ✅ **Instant** |
44
+ | **Remove from Front** | ❌ O(n) | ❌ No dequeue | ✅ **O(1)** |
45
+ | **Prefix Search** | ❌ O(n*m) | ❌ Not applicable | ✅ **O(m + k)** |
46
+ | **Familiar API** | ✅ Yes | ✅ Yes | ✅ **Same** |
47
+
48
+ ### Real-World Pain Point
49
+
50
+ ```javascript
51
+ // ❌ WITHOUT data-structure-typed
52
+ const queue = [1, 2, 3, ..., 100000
53
+ ]
54
+ ;
55
+ for (let i = 0; i < 100000; i++) {
56
+ queue.shift(); // O(n) - Reindexes EVERY element!
57
+ }
58
+ // Time: 2829ms ❌
59
+
60
+ // ✅ WITH data-structure-typed (Deque)
61
+ const deque = new Deque([1, 2, 3, ..., 100000
62
+ ])
63
+ ;
64
+ for (let i = 0; i < 100000; i++) {
65
+ deque.shift(); // O(1) - Just moves a pointer
66
+ }
67
+ // Time: 5.83ms ✅
68
+ // **484x faster!**
69
+ ```
2
70
 
3
- ![npm](https://img.shields.io/npm/dm/data-structure-typed)
4
- ![GitHub contributors](https://img.shields.io/github/contributors/zrwusa/data-structure-typed)
5
- ![npm package minimized gzipped size (select exports)](https://img.shields.io/bundlejs/size/data-structure-typed)
6
- ![GitHub top language](https://img.shields.io/github/languages/top/zrwusa/data-structure-typed)
7
- ![GITHUB Star](https://img.shields.io/github/stars/zrwusa/data-structure-typed)
8
- ![eslint](https://aleen42.github.io/badges/src/eslint.svg)
9
- ![NPM](https://img.shields.io/npm/l/data-structure-typed)
10
- ![npm](https://img.shields.io/npm/v/data-structure-typed)
71
+ ---
11
72
 
12
- [//]: # (![npm bundle size]&#40;https://img.shields.io/bundlephobia/min/data-structure-typed&#41;)
73
+ ## 🚀 Performance (TL;DR)
13
74
 
14
- [//]: # (<p><a href="https://github.com/zrwusa/data-structure-typed/blob/main/README.md">English</a> | <a href="https://github.com/zrwusa/data-structure-typed/blob/main/README_zh-CN.md">简体中文</a></p>)
75
+ - **10–40% faster** than common JS implementations in hot paths
76
+ - Array.sort() O(n log n) → TreeSet O(log n) insertion
77
+ - Repeated Array.shift() O(n) → Queue O(1)
78
+ - Manual index tracking → RB-Tree auto-balance
15
79
 
80
+ - **Optimized for V8 JIT** (Node.js 18+, modern browsers)
16
81
 
17
- > ***Our goal is to make every data structure as convenient and efficient as JavaScript's Array.***
82
+ - **Tree-shakable** ESM / CJS / legacy builds
18
83
 
19
- ## Installation and Usage
84
+ 📊 [Full benchmarks →](./docs/PERFORMANCE.md)
20
85
 
21
- ### npm
86
+ ---
22
87
 
23
- ```bash
24
- npm i data-structure-typed --save
88
+ ## ✨ Key Features
89
+
90
+ ### 🏠 Uniform API
91
+
92
+ Don't learn new APIs. Just use `push`, `pop`, `map`, `filter`, and `reduce` everywhere.
93
+
94
+ ```javascript
95
+ // All linear structures use THE SAME 4 methods
96
+ const deque = new Deque([1, 2, 3]);
97
+ const queue = new Queue([1, 2, 3]);
98
+ const stack = new Stack([1, 2, 3]);
99
+
100
+ // They ALL support:
101
+ structure.push(item); // Add to end
102
+ structure.pop(); // Remove from end
103
+ structure.shift(); // Remove from start
104
+ structure.unshift(item); // Add to start
105
+ ```
106
+
107
+ ### 🛡️ Type Safe
108
+
109
+ Full generics and strict TypeScript support out of the box.
110
+
111
+ ```typescript
112
+ const tree = new RedBlackTree<number, string>();
113
+ tree.set(1, 'Alice');
114
+ tree.set(2, 'Bob');
115
+
116
+ // Type-safe access
117
+ const value = tree.get(1); // Type: string | undefined
25
118
  ```
26
119
 
27
- ### yarn
120
+ ### ✨ Zero Friction
121
+
122
+ Works everywhere. Spread it `[...]`, loop it `for..of`, convert it instantly.
123
+
124
+ ```javascript
125
+ // All data structures work with iterator protocol
126
+ const tree = new RedBlackTree([5, 2, 8]);
127
+ const sorted = [...tree]; // Spread operator
128
+ for (const item of tree) {
129
+ } // for...of loop
130
+ const set = new Set(tree); // Set constructor
131
+ ```
132
+
133
+ ---
134
+
135
+ ## 📥 Installation
28
136
 
29
137
  ```bash
30
- yarn add data-structure-typed
138
+ pnpm add data-structure-typed
31
139
  ```
32
140
 
33
- [Playground](https://stackblitz.com/edit/data-structure-typed-playground?file=src%2Fmain.ts)
141
+ ```bash
142
+ npm i data-structure-typed --save
143
+ ```
34
144
 
35
- ```js
36
- import {
37
- Heap, Graph, Queue, Deque, PriorityQueue, BST, Trie, DoublyLinkedList,
38
- AVLTree, SinglyLinkedList, DirectedGraph, RedBlackTree, TreeMultiMap,
39
- DirectedVertex, Stack, AVLTreeNode
40
- } from 'data-structure-typed';
145
+ ```bash
146
+ yarn add data-structure-typed
41
147
  ```
42
148
 
43
- If you only want to use a specific data structure independently, you can install it separately, for example, by running
149
+ ### Individual Packages
150
+
151
+ Use only what you need:
44
152
 
45
153
  ```bash
46
- npm i heap-typed --save
154
+ pnpm add heap-typed deque-typed red-black-tree-typed
47
155
  ```
48
156
 
49
- ## Why
50
-
51
- Do you envy C++ with [STL]() (std::), Python with [collections](), and Java with [java.util]() ? Well, no need to envy
52
- anymore! JavaScript and TypeScript now have [data-structure-typed]().**`Benchmark`** compared with C++ STL.
53
- **`API standards`** aligned with ES6 and Java. **`Usability`** is comparable to Python
54
-
55
-
56
- [//]: # (![Branches]&#40;https://img.shields.io/badge/branches-55.47%25-red.svg?style=flat&#41;)
57
-
58
- [//]: # (![Statements]&#40;https://img.shields.io/badge/statements-67%25-red.svg?style=flat&#41;)
59
-
60
- [//]: # (![Functions]&#40;https://img.shields.io/badge/functions-66.38%25-red.svg?style=flat&#41;)
61
-
62
- [//]: # (![Lines]&#40;https://img.shields.io/badge/lines-68.6%25-red.svg?style=flat&#41;)
63
-
64
- ### Performance
65
-
66
- Performance surpasses that of native JS/TS
67
-
68
- <table style="display: table; width:100%; table-layout: fixed;">
69
- <thead>
70
- <tr>
71
- <th>Method</th>
72
- <th>Time Taken</th>
73
- <th>Data Scale</th>
74
- <th>Belongs To</th>
75
- <th>big O</th>
76
- </tr>
77
- </thead>
78
- <tbody>
79
- <tr>
80
- <td>Queue.push &amp; shift</td>
81
- <td>5.83 ms</td>
82
- <td>100K</td>
83
- <td>Ours</td>
84
- <td>O(1)</td>
85
- </tr>
86
- <tr>
87
- <td>Array.push &amp; shift</td>
88
- <td>2829.59 ms</td>
89
- <td>100K</td>
90
- <td>Native JS</td>
91
- <td>O(n)</td>
92
- </tr>
93
- <tr>
94
- <td>Deque.unshift &amp; shift</td>
95
- <td>2.44 ms</td>
96
- <td>100K</td>
97
- <td>Ours</td>
98
- <td>O(1)</td>
99
- </tr>
100
- <tr>
101
- <td>Array.unshift &amp; shift</td>
102
- <td>4750.37 ms</td>
103
- <td>100K</td>
104
- <td>Native JS</td>
105
- <td>O(n)</td>
106
- </tr>
107
- <tr>
108
- <td>HashMap.set</td>
109
- <td>122.51 ms</td>
110
- <td>1M</td>
111
- <td>Ours</td>
112
- <td>O(1)</td>
113
- </tr>
114
- <tr>
115
- <td>Map.set</td>
116
- <td>223.80 ms</td>
117
- <td>1M</td>
118
- <td>Native JS</td>
119
- <td>O(1)</td>
120
- </tr>
121
- <tr>
122
- <td>Set.add</td>
123
- <td>185.06 ms</td>
124
- <td>1M</td>
125
- <td>Native JS</td>
126
- <td>O(1)</td>
127
- </tr>
128
- </tbody>
129
- </table>
130
-
131
- ### Plain language explanations
132
-
133
- <table>
134
- <tr>
135
- <th>Data Structure</th>
136
- <th>Plain Language Definition</th>
137
- <th>Diagram</th>
138
- </tr>
139
- <tr>
140
- <td>Linked List (Singly Linked List)</td>
141
- <td>A line of bunnies, where each bunny holds the tail of the bunny in front of it (each bunny only knows the name of the bunny behind it). You want to find a bunny named Pablo, and you have to start searching from the first bunny. If it's not Pablo, you continue following that bunny's tail to the next one. So, you might need to search n times to find Pablo (O(n) time complexity). If you want to insert a bunny named Remi between Pablo and Vicky, it's very simple. You just need to let Vicky release Pablo's tail, let Remi hold Pablo's tail, and then let Vicky hold Remi's tail (O(1) time complexity).</td>
142
- <td><img alt="singly linked list" src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/singly-linked-list.png"></td>
143
- </tr>
144
- <tr>
145
- <td>Array</td>
146
- <td>A line of numbered bunnies. If you want to find the bunny named Pablo, you can directly shout out Pablo's number 0680 (finding the element directly through array indexing, O(1) time complexity). However, if you don't know Pablo's number, you still need to search one by one (O(n) time complexity). Moreover, if you want to add a bunny named Vicky behind Pablo, you will need to renumber all the bunnies after Vicky (O(n) time complexity).</td>
147
- <td><img alt="array" src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/array.png"></td>
148
- </tr>
149
- <tr>
150
- <td>Queue</td>
151
- <td>A line of numbered bunnies with a sticky note on the first bunny. For this line with a sticky note on the first bunny, whenever we want to remove a bunny from the front of the line, we only need to move the sticky note to the face of the next bunny without actually removing the bunny to avoid renumbering all the bunnies behind (removing from the front is also O(1) time complexity). For the tail of the line, we don't need to worry because each new bunny added to the tail is directly given a new number (O(1) time complexity) without needing to renumber all the previous bunnies.</td>
152
- <td><img alt="queue" src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/queue.jpg"></td>
153
- </tr>
154
- <tr>
155
- <td>Deque</td>
156
- <td>A line of grouped, numbered bunnies with a sticky note on the first bunny. For this line, we manage it by groups. Each time we remove a bunny from the front of the line, we only move the sticky note to the next bunny. This way, we don't need to renumber all the bunnies behind the first bunny each time a bunny is removed. Only when all members of a group are removed do we reassign numbers and regroup. The tail is handled similarly. This is a strategy of delaying and batching operations to offset the drawbacks of the Array data structure that requires moving all elements behind when inserting or deleting elements in the middle.</td>
157
- <td><img alt="deque" src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/deque.png"></td>
158
- </tr>
159
- <tr>
160
- <td>Doubly Linked List</td>
161
- <td>A line of bunnies where each bunny holds the tail of the bunny in front (each bunny knows the names of the two adjacent bunnies). This provides the Singly Linked List the ability to search forward, and that's all. For example, if you directly come to the bunny Remi in the line and ask her where Vicky is, she will say the one holding my tail behind me, and if you ask her where Pablo is, she will say right in front.</td>
162
- <td><img alt="doubly linked list" src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/doubly-linked-list.png"></td>
163
- </tr>
164
- <tr>
165
- <td>Stack</td>
166
- <td>A line of bunnies in a dead-end tunnel, where bunnies can only be removed from the tunnel entrance (end), and new bunnies can only be added at the entrance (end) as well.</td>
167
- <td><img alt="stack" src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/stack.jpg"></td>
168
- </tr>
169
- <tr>
170
- <td>Binary Tree</td>
171
- <td>As the name suggests, it's a tree where each node has at most two children. When you add consecutive data such as [4, 2, 6, 1, 3, 5, 7], it will be a complete binary tree. When you add data like [4, 2, 6, null, 1, 3, null, 5, null, 7], you can specify whether any left or right child node is null, and the shape of the tree is fully controllable.</td>
172
- <td><img alt="binary tree" src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/binary-tree.png"></td>
173
- </tr>
174
- <tr>
175
- <td>Binary Search Tree (BST)</td>
176
- <td>A tree-like rabbit colony composed of doubly linked lists where each rabbit has at most two tails. These rabbits are disciplined and obedient, arranged in their positions according to a certain order. The most important data structure in a binary tree (the core is that the time complexity for insertion, deletion, modification, and search is O(log n)). The data stored in a BST is structured and ordered, not in strict order like 1, 2, 3, 4, 5, but maintaining that all nodes in the left subtree are less than the node, and all nodes in the right subtree are greater than the node. This order provides O(log n) time complexity for insertion, deletion, modification, and search. Reducing O(n) to O(log n) is the most common algorithm complexity optimization in the computer field, an exponential improvement in efficiency. It's also the most efficient way to organize unordered data into ordered data (most sorting algorithms only maintain O(n log n)). Of course, the binary search trees we provide support organizing data in both ascending and descending order. Remember that basic BSTs do not have self-balancing capabilities, and if you sequentially add sorted data to this data structure, it will degrade into a list, thus losing the O(log n) capability. Of course, our addMany method is specially handled to prevent degradation. However, for practical applications, please use Red-black Tree or AVL Tree as much as possible, as they inherently have self-balancing functions.</td>
177
- <td><img alt="binary search tree" src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/binary-search-tree.png"></td>
178
- </tr>
179
- <tr>
180
- <td>Red-black Tree</td>
181
- <td>A tree-like rabbit colony composed of doubly linked lists, where each rabbit has at most two tails. These rabbits are not only obedient but also intelligent, automatically arranging their positions in a certain order. A self-balancing binary search tree. Each node is marked with a red-black label. Ensuring that no path is more than twice as long as any other (maintaining a certain balance to improve the speed of search, addition, and deletion).</td>
182
- <td><img alt="red-black tree" src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/red-black tree.png"></td>
183
- </tr>
184
- <tr>
185
- <td>AVL Tree</td>
186
- <td>A tree-like rabbit colony composed of doubly linked lists, where each rabbit has at most two tails. These rabbits are not only obedient but also intelligent, automatically arranging their positions in a certain order, and they follow very strict rules. A self-balancing binary search tree. Each node is marked with a balance factor, representing the height difference between its left and right subtrees. The absolute value of the balance factor does not exceed 1 (maintaining stricter balance, which makes search efficiency higher than Red-black Tree, but insertion and deletion operations will be more complex and relatively less efficient).</td>
187
- <td><img alt="avl tree" src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/avl-tree.png"></td>
188
- </tr>
189
- <tr>
190
- <td>Heap</td>
191
- <td>A special type of complete binary tree, often stored in an array, where the children nodes of the node at index i are at indices 2i+1 and 2i+2. Naturally, the parent node of any node is at ⌊(i−1)/2⌋.</td>
192
- <td><img alt="heap" src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/heap.jpg"></td>
193
- </tr>
194
- <tr>
195
- <td>Priority Queue</td>
196
- <td>It's actually a Heap.</td>
197
- <td><img alt="priority queue" src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/heap.jpg"></td>
198
- </tr>
199
- <tr>
200
- <td>Graph</td>
201
- <td>The base class for Directed Graph and Undirected Graph, providing some common methods.</td>
202
- <td><img alt="graph" src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/graph.png"></td>
203
- </tr>
204
- <tr>
205
- <td>Directed Graph</td>
206
- <td>A network-like bunny group where each bunny can have up to n tails (Singly Linked List).</td>
207
- <td><img alt="directed graph" src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/directed-graph.png"></td>
208
- </tr>
209
- <tr>
210
- <td>Undirected Graph</td>
211
- <td>A network-like bunny group where each bunny can have up to n tails (Doubly Linked List).</td>
212
- <td><img alt="undirected graph" src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/undirected-graph.png"></td>
213
- </tr>
214
- </table>
215
-
216
- ### Conciseness and uniformity
217
-
218
- In [java.utils](), you need to memorize a table for all sequential data structures(Queue, Deque, LinkedList),
219
-
220
- <table style="display: table; width:100%; table-layout: fixed;">
221
- <thead>
222
- <tr>
223
- <th>Java ArrayList</th>
224
- <th>Java Queue</th>
225
- <th>Java ArrayDeque</th>
226
- <th>Java LinkedList</th>
227
- </tr>
228
- </thead>
229
- <tbody>
230
- <tr>
231
- <td>add</td>
232
- <td>offer</td>
233
- <td>push</td>
234
- <td>push</td>
235
- </tr>
236
- <tr>
237
- <td>remove</td>
238
- <td>poll</td>
239
- <td>removeLast</td>
240
- <td>removeLast</td>
241
- </tr>
242
- <tr>
243
- <td>remove</td>
244
- <td>poll</td>
245
- <td>removeFirst</td>
246
- <td>removeFirst</td>
247
- </tr>
248
- <tr>
249
- <td>add(0, element)</td>
250
- <td>offerFirst</td>
251
- <td>unshift</td>
252
- <td>unshift</td>
253
- </tr>
254
- </tbody>
255
- </table>
256
-
257
- whereas in our [data-structure-typed](), you **only** need to remember four methods: `push`, `pop`, `shift`, and `unshift` for all sequential data structures(Queue, Deque, DoublyLinkedList, SinglyLinkedList and Array).
258
-
259
- ### Data structures available
260
-
261
- We provide data structures that are not available in JS/TS
262
-
263
- <table style="display: table; width:100%; table-layout: fixed;">
264
- <thead>
265
- <tr>
266
- <th>Data Structure</th>
267
- <th>Unit Test</th>
268
- <th>Perf Test</th>
269
- <th>API Doc</th>
270
- <th>NPM</th>
271
- <th>Downloads</th>
272
- </tr>
273
- </thead>
274
- <tbody>
275
- <tr>
276
- <td>Binary Tree</td>
277
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
278
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
279
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/BinaryTree.html"><span>Docs</span></a></td>
280
- <td><a href="https://www.npmjs.com/package/binary-tree-typed"><span>NPM</span></a></td>
281
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/binary-tree-typed"></td>
282
- </tr>
283
- <tr>
284
- <td>Binary Search Tree (BST)</td>
285
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
286
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
287
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/BST.html"><span>Docs</span></a></td>
288
- <td><a href="https://www.npmjs.com/package/bst-typed"><span>NPM</span></a></td>
289
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/bst-typed"></td>
290
- </tr>
291
- <tr>
292
- <td>AVL Tree</td>
293
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
294
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
295
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/AVLTree.html"><span>Docs</span></a></td>
296
- <td><a href="https://www.npmjs.com/package/avl-tree-typed"><span>NPM</span></a></td>
297
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/avl-tree-typed"></td>
298
- </tr>
299
- <tr>
300
- <td>Red Black Tree</td>
301
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
302
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
303
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/RedBlackTree.html"><span>Docs</span></a></td>
304
- <td><a href="https://www.npmjs.com/package/red-black-tree-typed"><span>NPM</span></a></td>
305
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/red-black-tree-typed"></td>
306
- </tr>
307
- <tr>
308
- <td>Tree Multimap</td>
309
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
310
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
311
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/TreeMultiMap.html"><span>Docs</span></a></td>
312
- <td><a href="https://www.npmjs.com/package/tree-multimap-typed"><span>NPM</span></a></td>
313
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/tree-multimap-typed"></td>
314
- </tr>
315
- <tr>
316
- <td>Heap</td>
317
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
318
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
319
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/Heap.html"><span>Docs</span></a></td>
320
- <td><a href="https://www.npmjs.com/package/heap-typed"><span>NPM</span></a></td>
321
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/heap-typed"></td>
322
- </tr>
323
- <tr>
324
- <td>Priority Queue</td>
325
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
326
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
327
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/PriorityQueue.html"><span>Docs</span></a></td>
328
- <td><a href="https://www.npmjs.com/package/priority-queue-typed"><span>NPM</span></a></td>
329
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/priority-queue-typed"></td>
330
- </tr>
331
- <tr>
332
- <td>Max Priority Queue</td>
333
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
334
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
335
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/MaxPriorityQueue.html"><span>Docs</span></a></td>
336
- <td><a href="https://www.npmjs.com/package/max-priority-queue-typed"><span>NPM</span></a></td>
337
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/max-priority-queue-typed"></td>
338
- </tr>
339
- <tr>
340
- <td>Min Priority Queue</td>
341
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
342
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
343
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/MinPriorityQueue.html"><span>Docs</span></a></td>
344
- <td><a href="https://www.npmjs.com/package/min-priority-queue-typed"><span>NPM</span></a></td>
345
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/min-priority-queue-typed"></td>
346
- </tr>
347
- <tr>
348
- <td>Trie</td>
349
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
350
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
351
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/Trie.html"><span>Docs</span></a></td>
352
- <td><a href="https://www.npmjs.com/package/trie-typed"><span>NPM</span></a></td>
353
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/trie-typed"></td>
354
- </tr>
355
- <tr>
356
- <td>Graph</td>
357
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
358
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
359
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/AbstractGraph.html"><span>Docs</span></a></td>
360
- <td><a href="https://www.npmjs.com/package/graph-typed"><span>NPM</span></a></td>
361
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/graph-typed"></td>
362
- </tr>
363
- <tr>
364
- <td>Directed Graph</td>
365
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
366
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
367
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/DirectedGraph.html"><span>Docs</span></a></td>
368
- <td><a href="https://www.npmjs.com/package/directed-graph-typed"><span>NPM</span></a></td>
369
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/directed-graph-typed"></td>
370
- </tr>
371
- <tr>
372
- <td>Undirected Graph</td>
373
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
374
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
375
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/UndirectedGraph.html"><span>Docs</span></a></td>
376
- <td><a href="https://www.npmjs.com/package/undirected-graph-typed"><span>NPM</span></a></td>
377
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/undirected-graph-typed"></td>
378
- </tr>
379
- <tr>
380
- <td>Queue</td>
381
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
382
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
383
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/Queue.html"><span>Docs</span></a></td>
384
- <td><a href="https://www.npmjs.com/package/queue-typed"><span>NPM</span></a></td>
385
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/queue-typed"></td>
386
- </tr>
387
- <tr>
388
- <td>Deque</td>
389
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
390
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
391
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/Deque.html"><span>Docs</span></a></td>
392
- <td><a href="https://www.npmjs.com/package/deque-typed"><span>NPM</span></a></td>
393
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/deque-typed"></td>
394
- </tr>
395
- <tr>
396
- <td>Hash Map</td>
397
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
398
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
399
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/HashMap.html"><span>Docs</span></a></td>
400
- <td><a href="https://www.npmjs.com/package/hashmap-typed"><span></span></a></td>
401
- <td></td>
402
- </tr>
403
- <tr>
404
- <td>Linked List</td>
405
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
406
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
407
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/SinglyLinkedList.html"><span>Docs</span></a></td>
408
- <td><a href="https://www.npmjs.com/package/linked-list-typed"><span>NPM</span></a></td>
409
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/linked-list-typed"></td>
410
- </tr>
411
- <tr>
412
- <td>Singly Linked List</td>
413
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
414
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
415
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/SinglyLinkedList.html"><span>Docs</span></a></td>
416
- <td><a href="https://www.npmjs.com/package/singly-linked-list-typed"><span>NPM</span></a></td>
417
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/singly-linked-list-typed"></td>
418
- </tr>
419
- <tr>
420
- <td>Doubly Linked List</td>
421
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
422
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
423
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/DoublyLinkedList.html"><span>Docs</span></a></td>
424
- <td><a href="https://www.npmjs.com/package/doubly-linked-list-typed"><span>NPM</span></a></td>
425
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/doubly-linked-list-typed"></td>
426
- </tr>
427
- <tr>
428
- <td>Stack</td>
429
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
430
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
431
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/Stack.html"><span>Docs</span></a></td>
432
- <td><a href="https://www.npmjs.com/package/stack-typed"><span>NPM</span></a></td>
433
- <td><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/stack-typed"></td>
434
- </tr>
435
- <tr>
436
- <td>Segment Tree</td>
437
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
438
- <td></td>
439
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/SegmentTree.html"><span>Docs</span></a></td>
440
- <td><a href="https://www.npmjs.com/package/segment-tree-typed"><span></span></a></td>
441
- <td></td>
442
- </tr>
443
- <tr>
444
- <td>Binary Indexed Tree</td>
445
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
446
- <td></td>
447
- <td><a href="https://data-structure-typed-docs.vercel.app/classes/BinaryIndexedTree.html"><span>Docs</span></a></td>
448
- <td><a href="https://www.npmjs.com/package/binary-indexed-tree-typed"><span></span></a></td>
449
- <td></td>
450
- </tr>
451
- </tbody>
452
- </table>
453
-
454
- ## Vivid Examples
455
-
456
- ### AVL Tree
457
-
458
- [Try it out](https://vivid-algorithm.vercel.app/), or you can run your own code using
459
- our [visual tool](https://github.com/zrwusa/vivid-algorithm)
460
-
461
- ![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/avl-tree-test.webp)
462
-
463
- ### Tree Multi Map
464
-
465
- [Try it out](https://vivid-algorithm.vercel.app/)
466
-
467
- ![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/tree-multiset-test.webp)
468
-
469
- ### Directed Graph
470
-
471
- [Try it out](https://vivid-algorithm.vercel.app/algorithm/graph/)
472
-
473
- ![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/directed-graph-test.webp)
474
-
475
- ### Map Graph
476
-
477
- [Try it out](https://vivid-algorithm.vercel.app/algorithm/graph/)
478
-
479
- ![](https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/examples/videos/webp_output/map-graph-test.webp)
480
-
481
- ## Code Snippets
482
-
483
- ### Red Black Tree snippet
484
-
485
- #### TS
486
-
487
- ```ts
157
+ ---
158
+
159
+ ## 💡 When Should I Consider This Library?
160
+
161
+ **When you need:**
162
+
163
+ - Top-K / Leaderboard queries without repeated sorting
164
+ - Insertion order + lookup performance simultaneously
165
+ - Priority queues with fast position-based access
166
+ - Time-series data with range queries
167
+ - Red-Black Tree / Heap performance without learning new APIs
168
+
169
+ ✅ **When your current code has:**
170
+
171
+ - `array.sort()` in hot paths (request handlers, loops)
172
+ - Manual index tracking after insertions
173
+ - `Array.shift()` on large lists (queues)
174
+ - Custom sorting logic you repeat across files
175
+ - Map that needs to be ordered
176
+
177
+ ---
178
+
179
+ ## 🚀 Quick Start: 30 Seconds
180
+
181
+ ### Leaderboard (Ranked Collections)
182
+
183
+ ```typescript
488
184
  import { RedBlackTree } from 'data-structure-typed';
489
185
 
490
- const rbTree = new RedBlackTree<number>();
491
- rbTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
492
- rbTree.isAVLBalanced(); // true
493
- rbTree.delete(10);
494
- rbTree.isAVLBalanced(); // true
495
- rbTree.print()
496
- // ___6________
497
- // / \
498
- // ___4_ ___11________
499
- // / \ / \
500
- // _2_ 5 _8_ ____14__
501
- // / \ / \ / \
502
- // 1 3 7 9 12__ 15__
503
- // \ \
504
- // 13 16
186
+ const leaderboard = new RedBlackTree([
187
+ [100, 'Alice'],
188
+ [85, 'Bob'],
189
+ [92, 'Charlie']
190
+ ]);
191
+
192
+ // Get sorted scores (automatically maintained!)
193
+ for (const [score, player] of leaderboard) {
194
+ console.log(`${player}: ${score}`);
195
+ }
196
+ // Output:
197
+ // Alice: 100
198
+ // Charlie: 92
199
+ // Bob: 85
200
+
201
+ // Update score
202
+ leaderboard.delete(85);
203
+ leaderboard.set(95, 'Bob'); // O(log n)
204
+
205
+ // Query top players
206
+ const topPlayers = [...leaderboard.values()].reverse().slice(0, 3);
505
207
  ```
506
208
 
507
- #### JS
209
+ ### Task Queue (Scheduling)
508
210
 
509
- ```js
510
- import { RedBlackTree } from 'data-structure-typed';
211
+ ```typescript
212
+ import { MaxPriorityQueue } from 'data-structure-typed';
213
+
214
+ const taskQueue = new MaxPriorityQueue([], {
215
+ comparator: (a, b) => b.priority - a.priority
216
+ });
217
+
218
+ taskQueue.add({ priority: 5, task: 'Email' });
219
+ taskQueue.add({ priority: 9, task: 'Alert' }); // Instant priority handling
511
220
 
512
- const rbTree = new RedBlackTree();
513
- rbTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
514
- rbTree.isAVLBalanced(); // true
515
- rbTree.delete(10);
516
- rbTree.isAVLBalanced(); // true
517
- rbTree.print()
518
- // ___6________
519
- // / \
520
- // ___4_ ___11________
521
- // / \ / \
522
- // _2_ 5 _8_ ____14__
523
- // / \ / \ / \
524
- // 1 3 7 9 12__ 15__
525
- // \ \
526
- // 13 16
221
+ const nextTask = taskQueue.poll(); // { priority: 9, task: 'Alert' }
527
222
  ```
528
223
 
529
- ### Free conversion between data structures.
530
-
531
- ```js
532
- const orgArr = [6, 1, 2, 7, 5, 3, 4, 9, 8];
533
- const orgStrArr = ["trie", "trial", "trick", "trip", "tree", "trend", "triangle", "track", "trace", "transmit"];
534
- const entries = [[6, "6"], [1, "1"], [2, "2"], [7, "7"], [5, "5"], [3, "3"], [4, "4"], [9, "9"], [8, "8"]];
535
-
536
- const queue = new Queue(orgArr);
537
- queue.print();
538
- // [6, 1, 2, 7, 5, 3, 4, 9, 8]
539
-
540
- const deque = new Deque(orgArr);
541
- deque.print();
542
- // [6, 1, 2, 7, 5, 3, 4, 9, 8]
543
-
544
- const sList = new SinglyLinkedList(orgArr);
545
- sList.print();
546
- // [6, 1, 2, 7, 5, 3, 4, 9, 8]
547
-
548
- const dList = new DoublyLinkedList(orgArr);
549
- dList.print();
550
- // [6, 1, 2, 7, 5, 3, 4, 9, 8]
551
-
552
- const stack = new Stack(orgArr);
553
- stack.print();
554
- // [6, 1, 2, 7, 5, 3, 4, 9, 8]
555
-
556
- const minHeap = new MinHeap(orgArr);
557
- minHeap.print();
558
- // [1, 5, 2, 7, 6, 3, 4, 9, 8]
559
-
560
- const maxPQ = new MaxPriorityQueue(orgArr);
561
- maxPQ.print();
562
- // [9, 8, 4, 7, 5, 2, 3, 1, 6]
563
-
564
- const biTree = new BinaryTree(entries);
565
- biTree.print();
566
- // ___6___
567
- // / \
568
- // ___1_ _2_
569
- // / \ / \
570
- // _7_ 5 3 4
571
- // / \
572
- // 9 8
573
-
574
- const bst = new BST(entries);
575
- bst.print();
576
- // _____5___
577
- // / \
578
- // _2_ _7_
579
- // / \ / \
580
- // 1 3_ 6 8_
581
- // \ \
582
- // 4 9
583
-
584
-
585
- const rbTree = new RedBlackTree(entries);
586
- rbTree.print();
587
- // ___4___
588
- // / \
589
- // _2_ _6___
590
- // / \ / \
591
- // 1 3 5 _8_
592
- // / \
593
- // 7 9
594
-
595
-
596
- const avl = new AVLTree(entries);
597
- avl.print();
598
- // ___4___
599
- // / \
600
- // _2_ _6___
601
- // / \ / \
602
- // 1 3 5 _8_
603
- // / \
604
- // 7 9
605
-
606
- const treeMulti = new TreeMultiMap(entries);
607
- treeMulti.print();
608
- // ___4___
609
- // / \
610
- // _2_ _6___
611
- // / \ / \
612
- // 1 3 5 _8_
613
- // / \
614
- // 7 9
615
-
616
- const hm = new HashMap(entries);
617
- hm.print()
618
- // [[6, "6"], [1, "1"], [2, "2"], [7, "7"], [5, "5"], [3, "3"], [4, "4"], [9, "9"], [8, "8"]]
619
-
620
- const rbTreeH = new RedBlackTree(hm);
621
- rbTreeH.print();
622
- // ___4___
623
- // / \
624
- // _2_ _6___
625
- // / \ / \
626
- // 1 3 5 _8_
627
- // / \
628
- // 7 9
629
-
630
- const pq = new MinPriorityQueue(orgArr);
631
- pq.print();
632
- // [1, 5, 2, 7, 6, 3, 4, 9, 8]
633
-
634
- const bst1 = new BST(pq);
635
- bst1.print();
636
- // _____5___
637
- // / \
638
- // _2_ _7_
639
- // / \ / \
640
- // 1 3_ 6 8_
641
- // \ \
642
- // 4 9
643
-
644
- const dq1 = new Deque(orgArr);
645
- dq1.print();
646
- // [6, 1, 2, 7, 5, 3, 4, 9, 8]
647
- const rbTree1 = new RedBlackTree(dq1);
648
- rbTree1.print();
649
- // _____5___
650
- // / \
651
- // _2___ _7___
652
- // / \ / \
653
- // 1 _4 6 _9
654
- // / /
655
- // 3 8
656
-
657
-
658
- const trie2 = new Trie(orgStrArr);
659
- trie2.print();
660
- // ['trie', 'trial', 'triangle', 'trick', 'trip', 'tree', 'trend', 'track', 'trace', 'transmit']
661
- const heap2 = new Heap(trie2, { comparator: (a, b) => Number(a) - Number(b) });
662
- heap2.print();
663
- // ['transmit', 'trace', 'tree', 'trend', 'track', 'trial', 'trip', 'trie', 'trick', 'triangle']
664
- const dq2 = new Deque(heap2);
665
- dq2.print();
666
- // ['transmit', 'trace', 'tree', 'trend', 'track', 'trial', 'trip', 'trie', 'trick', 'triangle']
667
- const entries2 = dq2.map((el, i) => [i, el]);
668
- const avl2 = new AVLTree(entries2);
669
- avl2.print();
670
- // ___3_______
671
- // / \
672
- // _1_ ___7_
673
- // / \ / \
674
- // 0 2 _5_ 8_
675
- // / \ \
676
- // 4 6 9
224
+ ### Fast Queue (FIFO)
225
+
226
+ ```typescript
227
+ import { Deque } from 'data-structure-typed';
228
+
229
+ const queue = new Deque([1, 2, 3, 4, 5]);
230
+ queue.shift(); // Remove from front: O(1) not O(n)
231
+ queue.push(6); // Add to back: O(1)
677
232
  ```
678
233
 
679
- ### Binary Search Tree (BST) snippet
680
-
681
- ```ts
682
- import { BST, BSTNode } from 'data-structure-typed';
683
-
684
- const bst = new BST<number>();
685
- bst.add(11);
686
- bst.add(3);
687
- bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
688
- bst.size === 16; // true
689
- bst.has(6); // true
690
- const node6 = bst.getNode(6); // BSTNode
691
- bst.getHeight(6) === 2; // true
692
- bst.getHeight() === 5; // true
693
- bst.getDepth(6) === 3; // true
694
-
695
- bst.getLeftMost()?.key === 1; // true
696
-
697
- bst.delete(6);
698
- bst.get(6); // undefined
699
- bst.isAVLBalanced(); // true
700
- bst.bfs()[0] === 11; // true
701
- bst.print()
702
- // ______________11_____
703
- // / \
704
- // ___3_______ _13_____
705
- // / \ / \
706
- // 1_ _____8____ 12 _15__
707
- // \ / \ / \
708
- // 2 4_ _10 14 16
709
- // \ /
710
- // 5_ 9
711
- // \
712
- // 7
713
-
714
- const objBST = new BST<number, { height: number, age: number }>();
715
-
716
- objBST.add(11, { "name": "Pablo", "size": 15 });
717
- objBST.add(3, { "name": "Kirk", "size": 1 });
718
-
719
- objBST.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], [
720
- { "name": "Alice", "size": 15 },
721
- { "name": "Bob", "size": 1 },
722
- { "name": "Charlie", "size": 8 },
723
- { "name": "David", "size": 13 },
724
- { "name": "Emma", "size": 16 },
725
- { "name": "Frank", "size": 2 },
726
- { "name": "Grace", "size": 6 },
727
- { "name": "Hannah", "size": 9 },
728
- { "name": "Isaac", "size": 12 },
729
- { "name": "Jack", "size": 14 },
730
- { "name": "Katie", "size": 4 },
731
- { "name": "Liam", "size": 7 },
732
- { "name": "Mia", "size": 10 },
733
- { "name": "Noah", "size": 5 }
734
- ]
735
- );
736
-
737
- objBST.delete(11);
234
+ ---
235
+
236
+ ## 📊 Data Structures Available
237
+
238
+ | Structure | Use Case | Time Complexity | NPM |
239
+ |--------------------------|-----------------------------------|-----------------|-----------------------------------------------------------|
240
+ | **RedBlackTree** | Sorted collections, range queries | O(log n) | [npm](https://www.npmjs.com/package/red-black-tree-typed) |
241
+ | **Heap / PriorityQueue** | Task scheduling, top-K elements | O(log n) | [npm](https://www.npmjs.com/package/heap-typed) |
242
+ | **Deque** | Fast front/back operations | O(1) | [npm](https://www.npmjs.com/package/deque-typed) |
243
+ | **Trie** | Autocomplete, prefix search | O(m+k) | [npm](https://www.npmjs.com/package/trie-typed) |
244
+ | **DirectedGraph** | Pathfinding, DAG algorithms | O(V+E) | [npm](https://www.npmjs.com/package/directed-graph-typed) |
245
+ | **Stack** | Undo/redo, expression parsing | O(1) | [npm](https://www.npmjs.com/package/stack-typed) |
246
+ | **LinkedList** | Dynamic sizing, no index shift | O(1)* | [npm](https://www.npmjs.com/package/linked-list-typed) |
247
+ | **AVLTree** | Stricter balance than RB-Tree | O(log n) | [npm](https://www.npmjs.com/package/avl-tree-typed) |
248
+
249
+ 👉 [See all 20+ structures →](./docs/REFERENCE.md)
250
+
251
+ ---
252
+
253
+ ## 📖 Documentation
254
+
255
+ ### For Different Use Cases
256
+
257
+ | Your Goal | Start Here | Next Steps |
258
+ |---------------------------|-------------------------------------------|-----------------------------------------|
259
+ | **Learn concepts** | [CONCEPTS.md](./docs/CONCEPTS.md) | [GUIDES.md](./docs/GUIDES.md) |
260
+ | **Use in my project** | [GUIDES.md](./docs/GUIDES.md) | [REFERENCE.md](./docs/REFERENCE.md) |
261
+ | **Look up API** | [REFERENCE.md](./docs/REFERENCE.md) | [PERFORMANCE.md](./docs/PERFORMANCE.md) |
262
+ | **Performance questions** | [PERFORMANCE.md](./docs/PERFORMANCE.md) | [ARCHITECTURE.md](./docs/ARCHITECTURE.md) |
263
+ | **Framework integration** | [INTEGRATIONS.md](./docs/INTEGRATIONS.md) | [GUIDES.md](./docs/GUIDES.md) |
264
+ | **Understand design** | [ARCHITECTURE.md](./docs/ARCHITECTURE.md) | [CONCEPTS.md](./docs/CONCEPTS.md) |
265
+
266
+ ### Documentation Files
267
+
268
+ 1. **[CONCEPTS.md](./docs/CONCEPTS.md)** - Core Fundamentals & Theory
269
+ - Big Three Concepts (BST, Balanced Trees, Heap)
270
+ - 13 Plain Language Explanations
271
+ - Iterator Protocol Design
272
+ - 5 Comparisons with Native JavaScript
273
+ - Complete Decision Guide
274
+
275
+ 2. **[REFERENCE.md](./docs/REFERENCE.md)** - Complete API & Data Structures
276
+ - Quick Reference Table
277
+ - All 20+ Structures with Examples
278
+ - CRUD Operations
279
+ - Common Methods
280
+ - TypeScript Support
281
+
282
+ 3. **[ARCHITECTURE.md](./docs/ARCHITECTURE.md)** - Design & Implementation
283
+ - Design Philosophy & Principles
284
+ - 3 Pain Points Solved
285
+ - Why Deque is 484x Faster
286
+ - Iterator Protocol Design
287
+ - Self-Balancing Strategy
288
+ - V8 JIT Optimizations
289
+
290
+ 4. **[PERFORMANCE.md](./docs/PERFORMANCE.md)** - Benchmarks & Comparisons
291
+ - Performance Summary
292
+ - 3 Real-World Scenarios
293
+ - Detailed Benchmarks
294
+ - When to Use What
295
+ - Optimization Tips
296
+
297
+ 5. **[GUIDES.md](./docs/GUIDES.md)** - Real-World Examples
298
+ - 4 Design Patterns
299
+ - 5 Production Code Examples
300
+ - Common Mistakes
301
+ - Best Practices
302
+
303
+ 6. **[INTEGRATIONS.md](./docs/INTEGRATIONS.md)** - Framework Integration
304
+ - React Integration (State Management, Leaderboard)
305
+ - Express Integration (LRU Cache, Rate Limiting)
306
+ - Nest.js Integration (Ranking Service, Task Queue)
307
+ - TypeScript Configuration
308
+
309
+ ---
310
+
311
+ ## 💻 Real-World Examples
312
+
313
+ ### LRU Cache
314
+
315
+ ```typescript
316
+ class LRUCache<K, V> {
317
+ private cache = new Map<K, V>();
318
+ private order = new DoublyLinkedList<K>();
319
+
320
+ get(key: K): V | null {
321
+ if (!this.cache.has(key)) return null;
322
+ // Move to end (recently used)
323
+ // Efficient with O(1) operations
324
+ return this.cache.get(key)!;
325
+ }
326
+ }
738
327
  ```
739
328
 
740
- ### AVLTree snippet
329
+ ### Leaderboard
741
330
 
742
- ```ts
743
- import { AVLTree } from 'data-structure-typed';
331
+ ```typescript
332
+ class Leaderboard {
333
+ private scores = new RedBlackTree<number, Player>(
334
+ (a, b) => b - a // Descending
335
+ );
744
336
 
745
- const avlTree = new AVLTree<number>();
746
- avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
747
- avlTree.isAVLBalanced(); // true
748
- avlTree.delete(10);
749
- avlTree.isAVLBalanced(); // true
337
+ getTopN(n: number): Player[] {
338
+ return [...this.scores.values()].slice(0, n);
339
+ }
340
+ }
750
341
  ```
751
342
 
752
- ### Directed Graph simple snippet
343
+ ### Message Queue
753
344
 
754
- ```ts
755
- import { DirectedGraph } from 'data-structure-typed';
345
+ ```typescript
346
+ class MessageQueue {
347
+ private urgent = new Deque<Message>();
348
+ private normal = new Deque<Message>();
756
349
 
757
- const graph = new DirectedGraph<string>();
350
+ dequeue(): Message | null {
351
+ return this.urgent.shift() || this.normal.shift();
352
+ }
353
+ }
354
+ ```
758
355
 
759
- graph.addVertex('A');
760
- graph.addVertex('B');
356
+ 👉 [More examples in GUIDES.md](./docs/GUIDES.md)
761
357
 
762
- graph.hasVertex('A'); // true
763
- graph.hasVertex('B'); // true
764
- graph.hasVertex('C'); // false
358
+ ---
765
359
 
766
- graph.addEdge('A', 'B');
767
- graph.hasEdge('A', 'B'); // true
768
- graph.hasEdge('B', 'A'); // false
360
+ ## 🎯 Use Cases by Industry
769
361
 
770
- graph.deleteEdgeSrcToDest('A', 'B');
771
- graph.hasEdge('A', 'B'); // false
362
+ ### 📊 Finance
772
363
 
773
- graph.addVertex('C');
364
+ - Price-sorted order book
365
+ - Real-time portfolio rankings
366
+ - Option chain ordering
774
367
 
775
- graph.addEdge('A', 'B');
776
- graph.addEdge('B', 'C');
368
+ ### 🎮 Gaming
777
369
 
778
- const topologicalOrderKeys = graph.topologicalSort(); // ['A', 'B', 'C']
779
- ```
370
+ - Player leaderboards
371
+ - Enemy priority queues
372
+ - Game event scheduling
780
373
 
781
- ### Undirected Graph snippet
374
+ ### 📱 Social Media
782
375
 
783
- ```ts
784
- import { UndirectedGraph } from 'data-structure-typed';
376
+ - Trending posts (top-K)
377
+ - Feed ordering
378
+ - Notification scheduling
785
379
 
786
- const graph = new UndirectedGraph<string>();
787
- graph.addVertex('A');
788
- graph.addVertex('B');
789
- graph.addVertex('C');
790
- graph.addVertex('D');
791
- graph.deleteVertex('C');
792
- graph.addEdge('A', 'B');
793
- graph.addEdge('B', 'D');
380
+ ### 🏥 Healthcare
794
381
 
795
- const dijkstraResult = graph.dijkstra('A');
796
- Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.key) // ['A', 'B', 'D']
382
+ - Patient priority queues
383
+ - Appointment scheduling
384
+ - Medical record organization
797
385
 
386
+ ### 🛒 E-commerce
798
387
 
799
- ```
388
+ - Product price ranges
389
+ - Inventory management
390
+ - Order scheduling
800
391
 
392
+ ---
801
393
 
394
+ ## ✨ Why Developers Love This
802
395
 
803
- ## API docs & Examples
804
-
805
- [API Docs](https://data-structure-typed-docs.vercel.app)
806
-
807
- [Live Examples](https://vivid-algorithm.vercel.app)
808
-
809
- <a href="https://github.com/zrwusa/vivid-algorithm" target="_blank">Examples Repository</a>
810
-
811
- ## Benchmark
812
-
813
- MacBook Pro (15-inch, 2018)
814
-
815
- Processor 2.2 GHz 6-Core Intel Core i7
816
-
817
- Memory 16 GB 2400 MHz DDR4
818
-
819
- Graphics Radeon Pro 555X 4 GB
820
-
821
- Intel UHD Graphics 630 1536 MB
822
-
823
- macOS Sequoia 15.7.2
824
-
825
- ## Performance & Runtime Compatibility
826
-
827
- [//]: # (No deletion!!! Start of Replace Section)
828
-
829
- <h2>red-black-tree</h2><table><thead><tr><th>test name</th><th>time taken (ms)</th><th>sample mean (secs)</th><th>sample deviation</th></tr></thead><tbody><tr><td>1,000,000 add</td><td>410.34</td><td>0.41</td><td>0.01</td></tr><tr><td>1,000,000 get</td><td>5.20</td><td>0.01</td><td>8.16e-5</td></tr><tr><td>1,000,000 iterator</td><td>154.25</td><td>0.15</td><td>0.02</td></tr><tr><td>CPT 1,000,000 add</td><td>656.43</td><td>0.66</td><td>0.00</td></tr><tr><td>CPT 1,000,000 add</td><td>684.17</td><td>0.68</td><td>0.01</td></tr></tbody></table><h2>queue</h2><table><thead><tr><th>test name</th><th>time taken (ms)</th><th>sample mean (secs)</th><th>sample deviation</th></tr></thead><tbody><tr><td>1,000,000 push</td><td>26.97</td><td>0.03</td><td>0.00</td></tr><tr><td>100,000 push & shift</td><td>2.87</td><td>0.00</td><td>2.71e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>1120.94</td><td>1.12</td><td>0.20</td></tr></tbody></table><h2>deque</h2><table><thead><tr><th>test name</th><th>time taken (ms)</th><th>sample mean (secs)</th><th>sample deviation</th></tr></thead><tbody><tr><td>1,000,000 push</td><td>8.75</td><td>0.01</td><td>6.99e-4</td></tr><tr><td>1,000,000 push & pop</td><td>12.95</td><td>0.01</td><td>4.21e-4</td></tr><tr><td>1,000,000 push & shift</td><td>13.73</td><td>0.01</td><td>4.53e-4</td></tr><tr><td>100,000 push & shift</td><td>1.36</td><td>0.00</td><td>5.42e-5</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>1167.06</td><td>1.17</td><td>0.26</td></tr><tr><td>100,000 unshift & shift</td><td>1.31</td><td>0.00</td><td>4.73e-5</td></tr><tr><td>Native JS Array 100,000 unshift & shift</td><td>1911.47</td><td>1.91</td><td>0.02</td></tr></tbody></table><h2>heap</h2><table><thead><tr><th>test name</th><th>time taken (ms)</th><th>sample mean (secs)</th><th>sample deviation</th></tr></thead><tbody><tr><td>100,000 add</td><td>4.60</td><td>0.00</td><td>1.07e-4</td></tr><tr><td>100,000 add & poll</td><td>16.96</td><td>0.02</td><td>3.45e-4</td></tr></tbody></table><h2>avl-tree</h2><table><thead><tr><th>test name</th><th>time taken (ms)</th><th>sample mean (secs)</th><th>sample deviation</th></tr></thead><tbody><tr><td>100,000 add randomly</td><td>324.51</td><td>0.32</td><td>0.01</td></tr><tr><td>100,000 add</td><td>299.76</td><td>0.30</td><td>0.02</td></tr><tr><td>100,000 get</td><td>0.26</td><td>2.58e-4</td><td>3.65e-6</td></tr><tr><td>100,000 getNode</td><td>169.33</td><td>0.17</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>14.43</td><td>0.01</td><td>0.00</td></tr><tr><td>100,000 add & delete orderly</td><td>434.44</td><td>0.43</td><td>0.01</td></tr><tr><td>100,000 add & delete randomly</td><td>541.78</td><td>0.54</td><td>0.01</td></tr></tbody></table><h2>hash-map</h2><table><thead><tr><th>test name</th><th>time taken (ms)</th><th>sample mean (secs)</th><th>sample deviation</th></tr></thead><tbody><tr><td>1,000,000 set</td><td>43.23</td><td>0.04</td><td>0.01</td></tr><tr><td>Native JS Map 1,000,000 set</td><td>147.12</td><td>0.15</td><td>0.01</td></tr><tr><td>Native JS Set 1,000,000 add</td><td>116.18</td><td>0.12</td><td>0.01</td></tr><tr><td>1,000,000 set & get</td><td>46.39</td><td>0.05</td><td>0.01</td></tr><tr><td>Native JS Map 1,000,000 set & get</td><td>196.92</td><td>0.20</td><td>0.01</td></tr><tr><td>Native JS Set 1,000,000 add & has</td><td>163.92</td><td>0.16</td><td>0.01</td></tr><tr><td>1,000,000 ObjKey set & get</td><td>243.36</td><td>0.24</td><td>0.03</td></tr><tr><td>Native JS Map 1,000,000 ObjKey set & get</td><td>211.66</td><td>0.21</td><td>0.02</td></tr><tr><td>Native JS Set 1,000,000 ObjKey add & has</td><td>196.57</td><td>0.20</td><td>0.01</td></tr></tbody></table><h2>directed-graph</h2><table><thead><tr><th>test name</th><th>time taken (ms)</th><th>sample mean (secs)</th><th>sample deviation</th></tr></thead><tbody><tr><td>1,000 addVertex</td><td>0.05</td><td>4.60e-5</td><td>6.59e-7</td></tr><tr><td>1,000 addEdge</td><td>3.02</td><td>0.00</td><td>2.85e-4</td></tr><tr><td>1,000 getVertex</td><td>0.04</td><td>3.77e-5</td><td>4.66e-7</td></tr><tr><td>1,000 getEdge</td><td>41.48</td><td>0.04</td><td>0.01</td></tr><tr><td>tarjan</td><td>240.33</td><td>0.24</td><td>0.01</td></tr><tr><td>topologicalSort</td><td>195.62</td><td>0.20</td><td>0.01</td></tr></tbody></table><h2>trie</h2><table><thead><tr><th>test name</th><th>time taken (ms)</th><th>sample mean (secs)</th><th>sample deviation</th></tr></thead><tbody><tr><td>100,000 push</td><td>27.15</td><td>0.03</td><td>6.61e-4</td></tr><tr><td>100,000 getWords</td><td>41.18</td><td>0.04</td><td>0.00</td></tr></tbody></table><h2>stack</h2><table><thead><tr><th>test name</th><th>time taken (ms)</th><th>sample mean (secs)</th><th>sample deviation</th></tr></thead><tbody><tr><td>1,000,000 push</td><td>25.21</td><td>0.03</td><td>0.00</td></tr><tr><td>1,000,000 push & pop</td><td>29.12</td><td>0.03</td><td>0.00</td></tr></tbody></table>
830
-
831
- [//]: # (No deletion!!! End of Replace Section)
832
-
833
-
834
- ## The corresponding relationships between data structures in different language standard libraries.
835
-
836
- <table style="display: table; width:100%; table-layout: fixed;">
837
- <thead>
838
- <tr>
839
- <th>Data Structure Typed</th>
840
- <th>C++ STL</th>
841
- <th>java.util</th>
842
- <th>Python collections</th>
843
- </tr>
844
- </thead>
845
- <tbody>
846
- <tr>
847
- <td>Heap&lt;E&gt;</td>
848
- <td>-</td>
849
- <td>-</td>
850
- <td>heapq</td>
851
- </tr>
852
- <tr>
853
- <td>PriorityQueue&lt;E&gt;</td>
854
- <td>priority_queue&lt;T&gt;</td>
855
- <td>PriorityQueue&lt;E&gt;</td>
856
- <td>-</td>
857
- </tr>
858
- <tr>
859
- <td>Deque&lt;E&gt;</td>
860
- <td>deque&lt;T&gt;</td>
861
- <td>ArrayDeque&lt;E&gt;</td>
862
- <td>deque</td>
863
- </tr>
864
- <tr>
865
- <td>Queue&lt;E&gt;</td>
866
- <td>queue&lt;T&gt;</td>
867
- <td>Queue&lt;E&gt;</td>
868
- <td>-</td>
869
- </tr>
870
- <tr>
871
- <td>HashMap&lt;K, V&gt;</td>
872
- <td>unordered_map&lt;K, V&gt;</td>
873
- <td>HashMap&lt;K, V&gt;</td>
874
- <td>defaultdict</td>
875
- </tr>
876
- <tr>
877
- <td>DoublyLinkedList&lt;E&gt;</td>
878
- <td>list&lt;T&gt;</td>
879
- <td>LinkedList&lt;E&gt;</td>
880
- <td>-</td>
881
- </tr>
882
- <tr>
883
- <td>SinglyLinkedList&lt;E&gt;</td>
884
- <td>-</td>
885
- <td>-</td>
886
- <td>-</td>
887
- </tr>
888
- <tr>
889
- <td>BinaryTree&lt;K, V&gt;</td>
890
- <td>-</td>
891
- <td>-</td>
892
- <td>-</td>
893
- </tr>
894
- <tr>
895
- <td>BST&lt;K, V&gt;</td>
896
- <td>-</td>
897
- <td>-</td>
898
- <td>-</td>
899
- </tr>
900
- <tr>
901
- <td>RedBlackTree&lt;E&gt;</td>
902
- <td>set&lt;T&gt;</td>
903
- <td>TreeSet&lt;E&gt;</td>
904
- <td>-</td>
905
- </tr>
906
- <tr>
907
- <td>RedBlackTree&lt;K, V&gt;</td>
908
- <td>map&lt;K, V&gt;</td>
909
- <td>TreeMap&lt;K, V&gt;</td>
910
- <td>-</td>
911
- </tr>
912
- <tr>
913
- <td>TreeMultiMap&lt;K, V&gt;</td>
914
- <td>multimap&lt;K, V&gt;</td>
915
- <td>-</td>
916
- <td>-</td>
917
- </tr>
918
- <tr>
919
- <td>TreeMultiMap&lt;E&gt;</td>
920
- <td>multiset&lt;T&gt;</td>
921
- <td>-</td>
922
- <td>-</td>
923
- </tr>
924
- <tr>
925
- <td>Trie</td>
926
- <td>-</td>
927
- <td>-</td>
928
- <td>-</td>
929
- </tr>
930
- <tr>
931
- <td>DirectedGraph&lt;V, E&gt;</td>
932
- <td>-</td>
933
- <td>-</td>
934
- <td>-</td>
935
- </tr>
936
- <tr>
937
- <td>UndirectedGraph&lt;V, E&gt;</td>
938
- <td>-</td>
939
- <td>-</td>
940
- <td>-</td>
941
- </tr>
942
- <tr>
943
- <td>PriorityQueue&lt;E&gt;</td>
944
- <td>priority_queue&lt;T&gt;</td>
945
- <td>PriorityQueue&lt;E&gt;</td>
946
- <td>-</td>
947
- </tr>
948
- <tr>
949
- <td>Array&lt;E&gt;</td>
950
- <td>vector&lt;T&gt;</td>
951
- <td>ArrayList&lt;E&gt;</td>
952
- <td>list</td>
953
- </tr>
954
- <tr>
955
- <td>Stack&lt;E&gt;</td>
956
- <td>stack&lt;T&gt;</td>
957
- <td>Stack&lt;E&gt;</td>
958
- <td>-</td>
959
- </tr>
960
- <tr>
961
- <td>HashMap&lt;E&gt;</td>
962
- <td>unordered_set&lt;T&gt;</td>
963
- <td>HashSet&lt;E&gt;</td>
964
- <td>set</td>
965
- </tr>
966
- <tr>
967
- <td>-</td>
968
- <td>unordered_multiset</td>
969
- <td>-</td>
970
- <td>Counter</td>
971
- </tr>
972
- <tr>
973
- <td>ES6 Map&lt;K, V&gt;</td>
974
- <td>-</td>
975
- <td>LinkedHashMap&lt;K, V&gt;</td>
976
- <td>OrderedDict</td>
977
- </tr>
978
- <tr>
979
- <td>-</td>
980
- <td>unordered_multimap&lt;K, V&gt;</td>
981
- <td>-</td>
982
- <td>-</td>
983
- </tr>
984
- <tr>
985
- <td>-</td>
986
- <td>bitset&lt;N&gt;</td>
987
- <td>-</td>
988
- <td>-</td>
989
- </tr>
990
- </tbody>
991
- </table>
992
-
993
- ## Built-in classic algorithms
994
-
995
- <table style="display: table; width:100%; table-layout: fixed;">
996
- <thead>
997
- <tr>
998
- <th>Algorithm</th>
999
- <th>Function Description</th>
1000
- <th>Iteration Type</th>
1001
- </tr>
1002
- </thead>
1003
- <tbody>
1004
- <tr>
1005
- <td>Binary Tree DFS</td>
1006
- <td>Traverse a binary tree in a depth-first manner, starting from the root node, first visiting the left subtree,
1007
- and then the right subtree, using recursion.
1008
- </td>
1009
- <td>Recursion + Iteration</td>
1010
- </tr>
1011
- <tr>
1012
- <td>Binary Tree BFS</td>
1013
- <td>Traverse a binary tree in a breadth-first manner, starting from the root node, visiting nodes level by level
1014
- from left to right.
1015
- </td>
1016
- <td>Iteration</td>
1017
- </tr>
1018
- <tr>
1019
- <td>Graph DFS</td>
1020
- <td>Traverse a graph in a depth-first manner, starting from a given node, exploring along one path as deeply as
1021
- possible, and backtracking to explore other paths. Used for finding connected components, paths, etc.
1022
- </td>
1023
- <td>Recursion + Iteration</td>
1024
- </tr>
1025
- <tr>
1026
- <td>Binary Tree Morris</td>
1027
- <td>Morris traversal is an in-order traversal algorithm for binary trees with O(1) space complexity. It allows tree
1028
- traversal without additional stack or recursion.
1029
- </td>
1030
- <td>Iteration</td>
1031
- </tr>
1032
- <tr>
1033
- <td>Graph BFS</td>
1034
- <td>Traverse a graph in a breadth-first manner, starting from a given node, first visiting nodes directly connected
1035
- to the starting node, and then expanding level by level. Used for finding shortest paths, etc.
1036
- </td>
1037
- <td>Recursion + Iteration</td>
1038
- </tr>
1039
- <tr>
1040
- <td>Graph Tarjan's Algorithm</td>
1041
- <td>Find strongly connected components in a graph, typically implemented using depth-first search.</td>
1042
- <td>Recursion</td>
1043
- </tr>
1044
- <tr>
1045
- <td>Graph Bellman-Ford Algorithm</td>
1046
- <td>Finding the shortest paths from a single source, can handle negative weight edges</td>
1047
- <td>Iteration</td>
1048
- </tr>
1049
- <tr>
1050
- <td>Graph Dijkstra's Algorithm</td>
1051
- <td>Finding the shortest paths from a single source, cannot handle negative weight edges</td>
1052
- <td>Iteration</td>
1053
- </tr>
1054
- <tr>
1055
- <td>Graph Floyd-Warshall Algorithm</td>
1056
- <td>Finding the shortest paths between all pairs of nodes</td>
1057
- <td>Iteration</td>
1058
- </tr>
1059
- <tr>
1060
- <td>Graph getCycles</td>
1061
- <td>Find all cycles in a graph or detect the presence of cycles.</td>
1062
- <td>Recursion</td>
1063
- </tr>
1064
- <tr>
1065
- <td>Graph getCutVertices</td>
1066
- <td>Find cut vertices in a graph, which are nodes that, when removed, increase the number of connected components in
1067
- the graph.
1068
- </td>
1069
- <td>Recursion</td>
1070
- </tr>
1071
- <tr>
1072
- <td>Graph getSCCs</td>
1073
- <td>Find strongly connected components in a graph, which are subgraphs where any two nodes can reach each other.
1074
- </td>
1075
- <td>Recursion</td>
1076
- </tr>
1077
- <tr>
1078
- <td>Graph getBridges</td>
1079
- <td>Find bridges in a graph, which are edges that, when removed, increase the number of connected components in the
1080
- graph.
1081
- </td>
1082
- <td>Recursion</td>
1083
- </tr>
1084
- <tr>
1085
- <td>Graph topologicalSort</td>
1086
- <td>Perform topological sorting on a directed acyclic graph (DAG) to find a linear order of nodes such that all
1087
- directed edges go from earlier nodes to later nodes.
1088
- </td>
1089
- <td>Recursion</td>
1090
- </tr>
1091
- </tbody>
1092
- </table>
1093
-
1094
- Starting from v2.2.0, `data-structure-typed` ships **two runtime targets** to balance
1095
- maximum performance on modern engines with broad compatibility for older Node.js
1096
- and browsers.
1097
-
1098
- ### Build Targets
1099
-
1100
- | Target | Output | Recommended runtime | Minimum runtime | Notes |
1101
- |---|---|---:|---:|---|
1102
- | **Modern (ES2022)** | `dist/esm` / `dist/cjs` | **Node.js 18+** | Node.js 16+ | Best performance. Enables V8 to fully optimize hot paths (JIT-friendly class semantics). |
1103
- | **Legacy (ES2018)** | `dist/esm-legacy` / `dist/cjs-legacy` | Node.js 16+ | **Node.js 12+** | Maximum compatibility. Slightly slower due to older compilation target and reduced JIT opportunities. |
1104
-
1105
- ### Benchmarks (Insertion-heavy workloads)
1106
-
1107
- The modern build (ES2022) is significantly faster for insertion-heavy operations
1108
- (e.g. Red-Black Tree `add/insert`) because it avoids performance overhead from
1109
- transpiled class field initialization and improves JIT optimization.
1110
-
1111
- | Benchmark | Legacy (ES2018) | Modern (ES2022) | Speedup |
1112
- |---|---:|---:|---:|
1113
- | Red-Black Tree `add` (1,000,000 inserts) | **~1100 ms** *(example)* | **473.12 ms** | **~2.3×** |
1114
-
1115
- > Notes:
1116
- > - Results vary by hardware and Node version.
1117
- > - We recommend running benchmarks on **Node.js 18+** for stable, modern V8 behavior.
1118
-
1119
- ### Node.js Support Policy
1120
-
1121
- - **Supported:** Node.js **12+** (via the legacy build)
1122
- - **Recommended:** Node.js **18+** (modern build, best performance and stability)
1123
- - **Best experience:** Node.js **20+** (latest V8 improvements)
1124
-
1125
- If you target Node.js 18+ only, you will get the best performance out of the box.
1126
-
1127
- ### Browser Support
1128
-
1129
- - **Modern browsers (Evergreen):** use the **Modern (ES2022)** ESM build.
1130
- - **Older browsers / conservative bundlers:** use the **Legacy (ES2018)** ESM build.
1131
-
1132
- In most bundler setups, the correct build is resolved automatically via `exports`.
1133
- If you need to force a specific target, use the explicit entry points:
1134
-
1135
- ```js
1136
- // Force modern build (ES2022)
1137
- import { RedBlackTree } from "data-structure-typed/modern";
1138
-
1139
- // Force legacy build (ES2018)
1140
- import { RedBlackTree } from "data-structure-typed/legacy";
1141
- ```
396
+ | Pain Point | Solution |
397
+ |------------------------------------|-------------------------------------------|
398
+ | Repeated sorting slowing down code | TreeSet auto-maintains order |
399
+ | Array.shift timeout in loops | Deque O(1) shift instead of O(n) |
400
+ | Learning different APIs | All structures use push/pop/shift/unshift |
401
+ | Type safety nightmares | Full TypeScript generics support |
402
+ | Browser compatibility issues | Works everywhere: Node, browsers, CDN |
403
+
404
+ ---
405
+
406
+ ## 📦 What You Get
407
+
408
+ **20+ data structures** (production-ready)
409
+ ✅ **50+ code examples** (real-world patterns)
410
+ **Full TypeScript support** (strict typing)
411
+ ✅ **Performance benchmarks** (484x speedups)
412
+ **Framework integrations** (React, Express, Nest.js)
413
+ ✅ **6 core documentation files** (2500+ lines)
1142
414
 
1143
- Just run
415
+ ---
1144
416
 
1145
- ```shell
1146
- pnpm test:perf red-black-tree.test.ts
417
+ ## 🚀 Getting Started
418
+
419
+ ### Step 1: Install
420
+
421
+ ```bash
422
+ npm i data-structure-typed
1147
423
  ```
1148
424
 
1149
- ```html
1150
- 1,000,000 add randomly: 1.141s
1151
- 1,000,000 add: 374.859ms
1152
- 1,000,000 get: 5.99ms
425
+ ### Step 2: Import
426
+
427
+ ```typescript
428
+ import { RedBlackTree, Deque, MaxPriorityQueue } from 'data-structure-typed';
1153
429
  ```
1154
430
 
1155
- ## Software Engineering Design Standards
1156
-
1157
- We strictly adhere to computer science theory and software development standards. Our LinkedList is designed in the
1158
- traditional sense of the LinkedList data structure, and we refrain from substituting it with a Deque solely for the
1159
- purpose of showcasing performance test data. However, we have also implemented a Deque based on a dynamic array
1160
- concurrently.
1161
-
1162
-
1163
- <table style="display: table; width:100%; table-layout: fixed;">
1164
- <tr>
1165
- <th>Principle</th>
1166
- <th>Description</th>
1167
- </tr>
1168
- <tr>
1169
- <td>Practicality</td>
1170
- <td>Follows ES6 and ESNext standards, offering unified and considerate optional parameters, and simplifies method names.</td>
1171
- </tr>
1172
- <tr>
1173
- <td>Extensibility</td>
1174
- <td>Adheres to OOP (Object-Oriented Programming) principles, allowing inheritance for all data structures.</td>
1175
- </tr>
1176
- <tr>
1177
- <td>Modularization</td>
1178
- <td>Includes data structure modularization and independent NPM packages.</td>
1179
- </tr>
1180
- <tr>
1181
- <td>Efficiency</td>
1182
- <td>All methods provide time and space complexity, comparable to native JS performance.</td>
1183
- </tr>
1184
- <tr>
1185
- <td>Maintainability</td>
1186
- <td>Follows open-source community development standards, complete documentation, continuous integration, and adheres to TDD (Test-Driven Development) patterns.</td>
1187
- </tr>
1188
- <tr>
1189
- <td>Testability</td>
1190
- <td>Automated and customized unit testing, performance testing, and integration testing.</td>
1191
- </tr>
1192
- <tr>
1193
- <td>Portability</td>
1194
- <td>Plans for porting to Java, Python, and C++, currently achieved to 80%.</td>
1195
- </tr>
1196
- <tr>
1197
- <td>Reusability</td>
1198
- <td>Fully decoupled, minimized side effects, and adheres to OOP.</td>
1199
- </tr>
1200
- <tr>
1201
- <td>Security</td>
1202
- <td>Carefully designed security for member variables and methods. Read-write separation. Data structure software does not need to consider other security aspects.</td>
1203
- </tr>
1204
- <tr>
1205
- <td>Scalability</td>
1206
- <td>Data structure software does not involve load issues.</td>
1207
- </tr>
1208
- </table>
1209
-
1210
- ## supported module system
1211
-
1212
- Now you can use it in Node.js and browser environments
1213
-
1214
- CommonJS:**`require export.modules =`**
1215
-
1216
- ESModule:&nbsp;&nbsp;&nbsp;**`import export`**
1217
-
1218
- Typescript:&nbsp;&nbsp;&nbsp;**`import export`**
1219
-
1220
- UMD:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;**`var Deque = dataStructureTyped.Deque`**
1221
-
1222
- ### CDN
1223
-
1224
- Copy the line below into the head tag in an HTML document.
1225
-
1226
- #### development
1227
-
1228
- ##### ES Module
1229
-
1230
- ```html
1231
-
1232
- <script type="module">
1233
- import { BST } from "https://cdn.jsdelivr.net/npm/data-structure-typed/dist/esm/index.mjs";
1234
-
1235
- const bst = new BST([2, 1, 6, 7, 5, 3, 4, 8, 9]);
1236
- bst.print();
1237
- </script>
431
+ ### Step 3: Use
432
+
433
+ ```typescript
434
+ const tree = new RedBlackTree([5, 2, 8]);
435
+ console.log([...tree]); // [2, 5, 8] - Automatically sorted!
1238
436
  ```
1239
437
 
1240
- ##### UMD
438
+ ### Step 4: Learn More
1241
439
 
1242
- ```html
440
+ 👉 Check [CONCEPTS.md](./docs/CONCEPTS.md) for core concepts
441
+ 👉 See [GUIDES.md](./docs/GUIDES.md) for production examples
442
+ 👉 Read [REFERENCE.md](./docs/REFERENCE.md) for complete API
443
+
444
+ ---
445
+
446
+ ## 📊 Comparison Chart
1243
447
 
1244
- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.js'></script>
1245
448
  ```
449
+ Need frequent head/tail operations?
450
+ → Deque (O(1) shift/unshift/push/pop)
451
+
452
+ Need sorted + fast lookup?
453
+ → RedBlackTree (O(log n) guaranteed)
454
+
455
+ Need highest/lowest priority?
456
+ → Heap/PriorityQueue (O(log n) add/remove)
1246
457
 
1247
- #### production
458
+ Need prefix/text matching?
459
+ → Trie (O(m+k) where m=prefix)
1248
460
 
1249
- ```html
461
+ Need graph operations?
462
+ → DirectedGraph/UndirectedGraph
1250
463
 
1251
- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.min.js'></script>
464
+ Otherwise?
465
+ → Use Array (simplest case)
1252
466
  ```
1253
467
 
1254
- Copy the code below into the script tag of your HTML, and you're good to go with your development.
468
+ ---
469
+
470
+ ## 🤝 Contributing
471
+
472
+ Found a bug? Have suggestions? [Open an issue](https://github.com/zrwusa/data-structure-typed/issues)
473
+
474
+ ---
475
+
476
+ ## 📄 License
477
+
478
+ MIT
479
+
480
+ ---
481
+
482
+ ## 📚 Full Documentation Structure
1255
483
 
1256
- ```js
1257
- const { Heap } = dataStructureTyped;
1258
- const {
1259
- BinaryTree, Graph, Queue, Stack, PriorityQueue, BST, Trie, DoublyLinkedList,
1260
- AVLTree, MinHeap, SinglyLinkedList, DirectedGraph, TreeMultiMap,
1261
- DirectedVertex, AVLTreeNode
1262
- } = dataStructureTyped;
1263
484
  ```
485
+ docs/
486
+ ├── README.md (this file)
487
+ ├── CONCEPTS.md (theory & fundamentals)
488
+ ├── REFERENCE.md (API documentation)
489
+ ├── ARCHITECTURE.md (design principles)
490
+ ├── PERFORMANCE.md (benchmarks)
491
+ ├── GUIDES.md (real-world examples)
492
+ └── INTEGRATIONS.md (framework guides)
493
+ ```
494
+
495
+ ---
496
+
497
+ ## 🎓 Learn More
498
+
499
+ **Just started?** → [Quick Start](#-quick-start-30-seconds)
500
+
501
+ **Need concepts?** → [CONCEPTS.md](./docs/CONCEPTS.md)
502
+
503
+ **Want to build?** → [GUIDES.md](./docs/GUIDES.md)
504
+
505
+ **Need API?** → [REFERENCE.md](./docs/REFERENCE.md)
506
+
507
+ **Curious about performance?** → [PERFORMANCE.md](./docs/PERFORMANCE.md)
508
+
509
+ **Framework questions?** → [INTEGRATIONS.md](./docs/INTEGRATIONS.md)
510
+
511
+ ---
512
+
513
+ **Ready to supercharge your TypeScript data structures? [Get started now →](#-quick-start-30-seconds)**