data-structure-typed 1.49.9 → 1.50.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +192 -200
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +158 -152
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +0 -7
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +0 -9
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +5 -22
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +9 -80
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +89 -27
- package/dist/cjs/data-structures/binary-tree/bst.js +131 -46
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +0 -7
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +1 -10
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +0 -7
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +2 -11
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/cjs/data-structures/hash/hash-map.js +6 -6
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +0 -7
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +0 -9
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +5 -22
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +9 -80
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +89 -27
- package/dist/mjs/data-structures/binary-tree/bst.js +131 -46
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +0 -7
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +1 -10
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +0 -7
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +2 -11
- package/dist/mjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/mjs/data-structures/hash/hash-map.js +15 -15
- package/dist/umd/data-structure-typed.js +143 -153
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +12 -41
- package/src/data-structures/base/iterable-base.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +7 -18
- package/src/data-structures/binary-tree/binary-tree.ts +111 -149
- package/src/data-structures/binary-tree/bst.ts +159 -56
- package/src/data-structures/binary-tree/rb-tree.ts +7 -18
- package/src/data-structures/binary-tree/tree-multimap.ts +8 -19
- package/src/data-structures/graph/abstract-graph.ts +14 -15
- package/src/data-structures/graph/directed-graph.ts +6 -7
- package/src/data-structures/graph/undirected-graph.ts +6 -7
- package/src/data-structures/hash/hash-map.ts +23 -22
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/queue/deque.ts +3 -3
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/stack/stack.ts +1 -1
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/types/data-structures/graph/abstract-graph.ts +8 -8
- package/test/integration/all-in-one.test.ts +1 -1
- package/test/integration/index.html +2 -2
- package/test/performance/data-structures/queue/deque.test.ts +8 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +38 -9
- package/test/unit/data-structures/binary-tree/bst.test.ts +8 -12
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +4 -4
- package/test/unit/data-structures/graph/abstract-graph.test.ts +2 -1
- package/test/unit/data-structures/hash/hash-map.test.ts +4 -2
- package/test/unit/data-structures/queue/deque.test.ts +8 -6
- package/test/unit/data-structures/queue/queue.test.ts +1 -1
- package/test/unit/unrestricted-interconversion.test.ts +143 -10
package/CHANGELOG.md
CHANGED
|
@@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file.
|
|
|
8
8
|
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
|
|
9
9
|
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)
|
|
10
10
|
|
|
11
|
-
## [v1.
|
|
11
|
+
## [v1.50.1](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
|
|
12
12
|
|
|
13
13
|
### Changes
|
|
14
14
|
|
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
[//]: # ()
|
|
12
12
|
|
|
13
|
-
<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>
|
|
13
|
+
[//]: # (<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>)
|
|
14
14
|
|
|
15
15
|
## Why
|
|
16
16
|
|
|
@@ -28,7 +28,7 @@ anymore! JavaScript and TypeScript now have [data-structure-typed]().**`Benchmar
|
|
|
28
28
|
|
|
29
29
|
### We provide data structures that are not available in JS/TS
|
|
30
30
|
|
|
31
|
-
Heap, Binary Tree,
|
|
31
|
+
Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, Directed Graph, Undirected Graph, BST, AVL Tree, Priority Queue, Queue, Tree Multiset.
|
|
32
32
|
|
|
33
33
|
|
|
34
34
|
### Performance surpasses that of native JS/TS
|
|
@@ -41,6 +41,7 @@ Heap, Binary Tree, RedBlack Tree, Linked List, Deque, Trie, Directed Graph, Undi
|
|
|
41
41
|
<th>Time Taken (ms)</th>
|
|
42
42
|
<th>Scale</th>
|
|
43
43
|
<th>Belongs To</th>
|
|
44
|
+
<th>Time Complexity</th>
|
|
44
45
|
</tr>
|
|
45
46
|
</thead>
|
|
46
47
|
<tbody>
|
|
@@ -49,59 +50,55 @@ Heap, Binary Tree, RedBlack Tree, Linked List, Deque, Trie, Directed Graph, Undi
|
|
|
49
50
|
<td>5.83</td>
|
|
50
51
|
<td>100,000</td>
|
|
51
52
|
<td>data-structure-typed</td>
|
|
53
|
+
<td>O(1)</td>
|
|
52
54
|
</tr>
|
|
53
55
|
<tr>
|
|
54
56
|
<td>Array.push & shift</td>
|
|
55
57
|
<td>2829.59</td>
|
|
56
58
|
<td>100,000</td>
|
|
57
59
|
<td>Native JS</td>
|
|
60
|
+
<td>O(n)</td>
|
|
58
61
|
</tr>
|
|
59
62
|
<tr>
|
|
60
63
|
<td>Deque.unshift & shift</td>
|
|
61
64
|
<td>2.44</td>
|
|
62
65
|
<td>100,000</td>
|
|
63
66
|
<td>data-structure-typed</td>
|
|
67
|
+
<td>O(1)</td>
|
|
64
68
|
</tr>
|
|
65
69
|
<tr>
|
|
66
70
|
<td>Array.unshift & shift</td>
|
|
67
71
|
<td>4750.37</td>
|
|
68
72
|
<td>100,000</td>
|
|
69
73
|
<td>Native JS</td>
|
|
74
|
+
<td>O(n)</td>
|
|
70
75
|
</tr>
|
|
71
76
|
<tr>
|
|
72
77
|
<td>HashMap.set</td>
|
|
73
78
|
<td>122.51</td>
|
|
74
79
|
<td>1,000,000</td>
|
|
75
80
|
<td>data-structure-typed</td>
|
|
81
|
+
<td>O(1)</td>
|
|
76
82
|
</tr>
|
|
77
83
|
<tr>
|
|
78
84
|
<td>Map.set</td>
|
|
79
85
|
<td>223.80</td>
|
|
80
86
|
<td>1,000,000</td>
|
|
81
87
|
<td>Native JS</td>
|
|
88
|
+
<td>O(1)</td>
|
|
82
89
|
</tr>
|
|
83
90
|
<tr>
|
|
84
91
|
<td>Set.add</td>
|
|
85
92
|
<td>185.06</td>
|
|
86
93
|
<td>1,000,000</td>
|
|
87
94
|
<td>Native JS</td>
|
|
95
|
+
<td>O(1)</td>
|
|
88
96
|
</tr>
|
|
89
97
|
</tbody>
|
|
90
98
|
</table>
|
|
91
99
|
|
|
92
100
|
## Installation and Usage
|
|
93
101
|
|
|
94
|
-
Now you can use it in Node.js and browser environments
|
|
95
|
-
|
|
96
|
-
CommonJS:**`require export.modules =`**
|
|
97
|
-
|
|
98
|
-
ESModule: **`import export`**
|
|
99
|
-
|
|
100
|
-
Typescript: **`import export`**
|
|
101
|
-
|
|
102
|
-
UMD: **`var Deque = dataStructureTyped.Deque`**
|
|
103
|
-
|
|
104
|
-
|
|
105
102
|
### npm
|
|
106
103
|
|
|
107
104
|
```bash
|
|
@@ -116,58 +113,19 @@ yarn add data-structure-typed
|
|
|
116
113
|
|
|
117
114
|
```js
|
|
118
115
|
import {
|
|
119
|
-
|
|
120
|
-
AVLTree,
|
|
121
|
-
DirectedVertex, AVLTreeNode
|
|
116
|
+
Heap, Graph, Queue, Deque, PriorityQueue, BST, Trie, DoublyLinkedList,
|
|
117
|
+
AVLTree, SinglyLinkedList, DirectedGraph, RedBlackTree, TreeMultimap,
|
|
118
|
+
DirectedVertex, Stack, AVLTreeNode
|
|
122
119
|
} from 'data-structure-typed';
|
|
123
120
|
```
|
|
124
121
|
|
|
125
|
-
### CDN
|
|
126
|
-
|
|
127
|
-
Copy the line below into the head tag in an HTML document.
|
|
128
|
-
|
|
129
|
-
#### development
|
|
130
|
-
|
|
131
|
-
```html
|
|
132
|
-
<script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.js'></script>
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
#### production
|
|
136
|
-
|
|
137
|
-
```html
|
|
138
|
-
<script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.min.js'></script>
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
Copy the code below into the script tag of your HTML, and you're good to go with your development.
|
|
142
|
-
|
|
143
|
-
```js
|
|
144
|
-
const {Heap} = dataStructureTyped;
|
|
145
|
-
const {
|
|
146
|
-
BinaryTree, Graph, Queue, Stack, PriorityQueue, BST, Trie, DoublyLinkedList,
|
|
147
|
-
AVLTree, MinHeap, SinglyLinkedList, DirectedGraph, TreeMultimap,
|
|
148
|
-
DirectedVertex, AVLTreeNode
|
|
149
|
-
} = dataStructureTyped;
|
|
150
|
-
```
|
|
151
|
-
|
|
152
122
|
## Vivid Examples
|
|
153
123
|
|
|
154
|
-
###
|
|
124
|
+
### AVL Tree
|
|
155
125
|
|
|
156
126
|
[Try it out](https://vivid-algorithm.vercel.app/), or you can run your own code using
|
|
157
127
|
our [visual tool](https://github.com/zrwusa/vivid-algorithm)
|
|
158
128
|
|
|
159
|
-

|
|
160
|
-
|
|
161
|
-
### Binary Tree DFS
|
|
162
|
-
|
|
163
|
-
[Try it out](https://vivid-algorithm.vercel.app/)
|
|
164
|
-
|
|
165
|
-

|
|
166
|
-
|
|
167
|
-
### AVL Tree
|
|
168
|
-
|
|
169
|
-
[Try it out](https://vivid-algorithm.vercel.app/)
|
|
170
|
-
|
|
171
129
|

|
|
172
130
|
|
|
173
131
|
### Tree Multi Map
|
|
@@ -176,12 +134,6 @@ our [visual tool](https://github.com/zrwusa/vivid-algorithm)
|
|
|
176
134
|
|
|
177
135
|

|
|
178
136
|
|
|
179
|
-
### Matrix
|
|
180
|
-
|
|
181
|
-
[Try it out](https://vivid-algorithm.vercel.app/algorithm/graph/)
|
|
182
|
-
|
|
183
|
-

|
|
184
|
-
|
|
185
137
|
### Directed Graph
|
|
186
138
|
|
|
187
139
|
[Try it out](https://vivid-algorithm.vercel.app/algorithm/graph/)
|
|
@@ -196,7 +148,7 @@ our [visual tool](https://github.com/zrwusa/vivid-algorithm)
|
|
|
196
148
|
|
|
197
149
|
## Code Snippets
|
|
198
150
|
|
|
199
|
-
###
|
|
151
|
+
### Red Black Tree snippet
|
|
200
152
|
|
|
201
153
|
#### TS
|
|
202
154
|
|
|
@@ -242,134 +194,12 @@ rbTree.print()
|
|
|
242
194
|
// 13 16
|
|
243
195
|
```
|
|
244
196
|
|
|
245
|
-
### Binary Search Tree (BST) snippet
|
|
246
|
-
|
|
247
|
-
```ts
|
|
248
|
-
import {BST, BSTNode} from 'data-structure-typed';
|
|
249
|
-
|
|
250
|
-
const bst = new BST<number>();
|
|
251
|
-
bst.add(11);
|
|
252
|
-
bst.add(3);
|
|
253
|
-
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
254
|
-
bst.size === 16; // true
|
|
255
|
-
bst.has(6); // true
|
|
256
|
-
const node6 = bst.getNode(6); // BSTNode
|
|
257
|
-
bst.getHeight(6) === 2; // true
|
|
258
|
-
bst.getHeight() === 5; // true
|
|
259
|
-
bst.getDepth(6) === 3; // true
|
|
260
|
-
|
|
261
|
-
bst.getLeftMost()?.key === 1; // true
|
|
262
|
-
|
|
263
|
-
bst.delete(6);
|
|
264
|
-
bst.get(6); // undefined
|
|
265
|
-
bst.isAVLBalanced(); // true
|
|
266
|
-
bst.bfs()[0] === 11; // true
|
|
267
|
-
bst.print()
|
|
268
|
-
// ______________11_____
|
|
269
|
-
// / \
|
|
270
|
-
// ___3_______ _13_____
|
|
271
|
-
// / \ / \
|
|
272
|
-
// 1_ _____8____ 12 _15__
|
|
273
|
-
// \ / \ / \
|
|
274
|
-
// 2 4_ _10 14 16
|
|
275
|
-
// \ /
|
|
276
|
-
// 5_ 9
|
|
277
|
-
// \
|
|
278
|
-
// 7
|
|
279
|
-
|
|
280
|
-
const objBST = new BST<number, {height: number, age: number}>();
|
|
281
|
-
|
|
282
|
-
objBST.add(11, { "name": "Pablo", "age": 15 });
|
|
283
|
-
objBST.add(3, { "name": "Kirk", "age": 1 });
|
|
284
|
-
|
|
285
|
-
objBST.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], [
|
|
286
|
-
{ "name": "Alice", "age": 15 },
|
|
287
|
-
{ "name": "Bob", "age": 1 },
|
|
288
|
-
{ "name": "Charlie", "age": 8 },
|
|
289
|
-
{ "name": "David", "age": 13 },
|
|
290
|
-
{ "name": "Emma", "age": 16 },
|
|
291
|
-
{ "name": "Frank", "age": 2 },
|
|
292
|
-
{ "name": "Grace", "age": 6 },
|
|
293
|
-
{ "name": "Hannah", "age": 9 },
|
|
294
|
-
{ "name": "Isaac", "age": 12 },
|
|
295
|
-
{ "name": "Jack", "age": 14 },
|
|
296
|
-
{ "name": "Katie", "age": 4 },
|
|
297
|
-
{ "name": "Liam", "age": 7 },
|
|
298
|
-
{ "name": "Mia", "age": 10 },
|
|
299
|
-
{ "name": "Noah", "age": 5 }
|
|
300
|
-
]
|
|
301
|
-
);
|
|
302
|
-
|
|
303
|
-
objBST.delete(11);
|
|
304
|
-
```
|
|
305
|
-
|
|
306
|
-
### AVLTree snippet
|
|
307
|
-
|
|
308
|
-
```ts
|
|
309
|
-
import {AVLTree} from 'data-structure-typed';
|
|
310
|
-
|
|
311
|
-
const avlTree = new AVLTree<number>();
|
|
312
|
-
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
313
|
-
avlTree.isAVLBalanced(); // true
|
|
314
|
-
avlTree.delete(10);
|
|
315
|
-
avlTree.isAVLBalanced(); // true
|
|
316
|
-
```
|
|
317
|
-
|
|
318
|
-
### Directed Graph simple snippet
|
|
319
|
-
|
|
320
|
-
```ts
|
|
321
|
-
import {DirectedGraph} from 'data-structure-typed';
|
|
322
|
-
|
|
323
|
-
const graph = new DirectedGraph<string>();
|
|
324
|
-
|
|
325
|
-
graph.addVertex('A');
|
|
326
|
-
graph.addVertex('B');
|
|
327
|
-
|
|
328
|
-
graph.hasVertex('A'); // true
|
|
329
|
-
graph.hasVertex('B'); // true
|
|
330
|
-
graph.hasVertex('C'); // false
|
|
331
|
-
|
|
332
|
-
graph.addEdge('A', 'B');
|
|
333
|
-
graph.hasEdge('A', 'B'); // true
|
|
334
|
-
graph.hasEdge('B', 'A'); // false
|
|
335
|
-
|
|
336
|
-
graph.deleteEdgeSrcToDest('A', 'B');
|
|
337
|
-
graph.hasEdge('A', 'B'); // false
|
|
338
|
-
|
|
339
|
-
graph.addVertex('C');
|
|
340
|
-
|
|
341
|
-
graph.addEdge('A', 'B');
|
|
342
|
-
graph.addEdge('B', 'C');
|
|
343
|
-
|
|
344
|
-
const topologicalOrderKeys = graph.topologicalSort(); // ['A', 'B', 'C']
|
|
345
|
-
```
|
|
346
|
-
|
|
347
|
-
### Undirected Graph snippet
|
|
348
|
-
|
|
349
|
-
```ts
|
|
350
|
-
import {UndirectedGraph} from 'data-structure-typed';
|
|
351
|
-
|
|
352
|
-
const graph = new UndirectedGraph<string>();
|
|
353
|
-
graph.addVertex('A');
|
|
354
|
-
graph.addVertex('B');
|
|
355
|
-
graph.addVertex('C');
|
|
356
|
-
graph.addVertex('D');
|
|
357
|
-
graph.deleteVertex('C');
|
|
358
|
-
graph.addEdge('A', 'B');
|
|
359
|
-
graph.addEdge('B', 'D');
|
|
360
|
-
|
|
361
|
-
const dijkstraResult = graph.dijkstra('A');
|
|
362
|
-
Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.key) // ['A', 'B', 'D']
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
```
|
|
366
|
-
|
|
367
197
|
### Free conversion between data structures.
|
|
368
198
|
|
|
369
199
|
```js
|
|
370
200
|
const orgArr = [6, 1, 2, 7, 5, 3, 4, 9, 8];
|
|
371
201
|
const orgStrArr = ["trie", "trial", "trick", "trip", "tree", "trend", "triangle", "track", "trace", "transmit"];
|
|
372
|
-
const entries = [[6, 6], [1, 1], [2, 2], [7, 7], [5, 5], [3, 3], [4, 4], [9, 9], [8, 8]];
|
|
202
|
+
const entries = [[6, "6"], [1, "1"], [2, "2"], [7, "7"], [5, "5"], [3, "3"], [4, "4"], [9, "9"], [8, "8"]];
|
|
373
203
|
|
|
374
204
|
const queue = new Queue(orgArr);
|
|
375
205
|
queue.print();
|
|
@@ -453,7 +283,7 @@ treeMulti.print();
|
|
|
453
283
|
|
|
454
284
|
const hm = new HashMap(entries);
|
|
455
285
|
hm.print()
|
|
456
|
-
// [[6, 6], [1, 1], [2, 2], [7, 7], [5, 5], [3, 3], [4, 4], [9, 9], [8, 8]]
|
|
286
|
+
// [[6, "6"], [1, "1"], [2, "2"], [7, "7"], [5, "5"], [3, "3"], [4, "4"], [9, "9"], [8, "8"]]
|
|
457
287
|
|
|
458
288
|
const rbTreeH = new RedBlackTree(hm);
|
|
459
289
|
rbTreeH.print();
|
|
@@ -514,6 +344,128 @@ avl2.print();
|
|
|
514
344
|
// 4 6 9
|
|
515
345
|
```
|
|
516
346
|
|
|
347
|
+
### Binary Search Tree (BST) snippet
|
|
348
|
+
|
|
349
|
+
```ts
|
|
350
|
+
import {BST, BSTNode} from 'data-structure-typed';
|
|
351
|
+
|
|
352
|
+
const bst = new BST<number>();
|
|
353
|
+
bst.add(11);
|
|
354
|
+
bst.add(3);
|
|
355
|
+
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
356
|
+
bst.size === 16; // true
|
|
357
|
+
bst.has(6); // true
|
|
358
|
+
const node6 = bst.getNode(6); // BSTNode
|
|
359
|
+
bst.getHeight(6) === 2; // true
|
|
360
|
+
bst.getHeight() === 5; // true
|
|
361
|
+
bst.getDepth(6) === 3; // true
|
|
362
|
+
|
|
363
|
+
bst.getLeftMost()?.key === 1; // true
|
|
364
|
+
|
|
365
|
+
bst.delete(6);
|
|
366
|
+
bst.get(6); // undefined
|
|
367
|
+
bst.isAVLBalanced(); // true
|
|
368
|
+
bst.bfs()[0] === 11; // true
|
|
369
|
+
bst.print()
|
|
370
|
+
// ______________11_____
|
|
371
|
+
// / \
|
|
372
|
+
// ___3_______ _13_____
|
|
373
|
+
// / \ / \
|
|
374
|
+
// 1_ _____8____ 12 _15__
|
|
375
|
+
// \ / \ / \
|
|
376
|
+
// 2 4_ _10 14 16
|
|
377
|
+
// \ /
|
|
378
|
+
// 5_ 9
|
|
379
|
+
// \
|
|
380
|
+
// 7
|
|
381
|
+
|
|
382
|
+
const objBST = new BST<number, {height: number, age: number}>();
|
|
383
|
+
|
|
384
|
+
objBST.add(11, { "name": "Pablo", "age": 15 });
|
|
385
|
+
objBST.add(3, { "name": "Kirk", "age": 1 });
|
|
386
|
+
|
|
387
|
+
objBST.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], [
|
|
388
|
+
{ "name": "Alice", "age": 15 },
|
|
389
|
+
{ "name": "Bob", "age": 1 },
|
|
390
|
+
{ "name": "Charlie", "age": 8 },
|
|
391
|
+
{ "name": "David", "age": 13 },
|
|
392
|
+
{ "name": "Emma", "age": 16 },
|
|
393
|
+
{ "name": "Frank", "age": 2 },
|
|
394
|
+
{ "name": "Grace", "age": 6 },
|
|
395
|
+
{ "name": "Hannah", "age": 9 },
|
|
396
|
+
{ "name": "Isaac", "age": 12 },
|
|
397
|
+
{ "name": "Jack", "age": 14 },
|
|
398
|
+
{ "name": "Katie", "age": 4 },
|
|
399
|
+
{ "name": "Liam", "age": 7 },
|
|
400
|
+
{ "name": "Mia", "age": 10 },
|
|
401
|
+
{ "name": "Noah", "age": 5 }
|
|
402
|
+
]
|
|
403
|
+
);
|
|
404
|
+
|
|
405
|
+
objBST.delete(11);
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
### AVLTree snippet
|
|
409
|
+
|
|
410
|
+
```ts
|
|
411
|
+
import {AVLTree} from 'data-structure-typed';
|
|
412
|
+
|
|
413
|
+
const avlTree = new AVLTree<number>();
|
|
414
|
+
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
415
|
+
avlTree.isAVLBalanced(); // true
|
|
416
|
+
avlTree.delete(10);
|
|
417
|
+
avlTree.isAVLBalanced(); // true
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
### Directed Graph simple snippet
|
|
421
|
+
|
|
422
|
+
```ts
|
|
423
|
+
import {DirectedGraph} from 'data-structure-typed';
|
|
424
|
+
|
|
425
|
+
const graph = new DirectedGraph<string>();
|
|
426
|
+
|
|
427
|
+
graph.addVertex('A');
|
|
428
|
+
graph.addVertex('B');
|
|
429
|
+
|
|
430
|
+
graph.hasVertex('A'); // true
|
|
431
|
+
graph.hasVertex('B'); // true
|
|
432
|
+
graph.hasVertex('C'); // false
|
|
433
|
+
|
|
434
|
+
graph.addEdge('A', 'B');
|
|
435
|
+
graph.hasEdge('A', 'B'); // true
|
|
436
|
+
graph.hasEdge('B', 'A'); // false
|
|
437
|
+
|
|
438
|
+
graph.deleteEdgeSrcToDest('A', 'B');
|
|
439
|
+
graph.hasEdge('A', 'B'); // false
|
|
440
|
+
|
|
441
|
+
graph.addVertex('C');
|
|
442
|
+
|
|
443
|
+
graph.addEdge('A', 'B');
|
|
444
|
+
graph.addEdge('B', 'C');
|
|
445
|
+
|
|
446
|
+
const topologicalOrderKeys = graph.topologicalSort(); // ['A', 'B', 'C']
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
### Undirected Graph snippet
|
|
450
|
+
|
|
451
|
+
```ts
|
|
452
|
+
import {UndirectedGraph} from 'data-structure-typed';
|
|
453
|
+
|
|
454
|
+
const graph = new UndirectedGraph<string>();
|
|
455
|
+
graph.addVertex('A');
|
|
456
|
+
graph.addVertex('B');
|
|
457
|
+
graph.addVertex('C');
|
|
458
|
+
graph.addVertex('D');
|
|
459
|
+
graph.deleteVertex('C');
|
|
460
|
+
graph.addEdge('A', 'B');
|
|
461
|
+
graph.addEdge('B', 'D');
|
|
462
|
+
|
|
463
|
+
const dijkstraResult = graph.dijkstra('A');
|
|
464
|
+
Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.key) // ['A', 'B', 'D']
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
```
|
|
468
|
+
|
|
517
469
|
## API docs & Examples
|
|
518
470
|
|
|
519
471
|
[API Docs](https://data-structure-typed-docs.vercel.app)
|
|
@@ -987,43 +939,83 @@ We strictly adhere to computer science theory and software development standards
|
|
|
987
939
|
[//]: # (No deletion!!! Start of Replace Section)
|
|
988
940
|
<div class="json-to-html-collapse clearfix 0">
|
|
989
941
|
<div class='collapsible level0' ><span class='json-to-html-label'>avl-tree</span></div>
|
|
990
|
-
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add randomly</td><td>
|
|
942
|
+
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add randomly</td><td>132.61</td><td>7.54</td><td>0.03</td></tr><tr><td>10,000 add & delete randomly</td><td>179.82</td><td>5.56</td><td>0.00</td></tr><tr><td>10,000 addMany</td><td>128.84</td><td>7.76</td><td>7.04e-4</td></tr><tr><td>10,000 get</td><td>48.40</td><td>20.66</td><td>3.34e-4</td></tr></table></div>
|
|
991
943
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
992
944
|
<div class='collapsible level0' ><span class='json-to-html-label'>binary-tree-overall</span></div>
|
|
993
|
-
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 RBTree add</td><td>
|
|
945
|
+
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 RBTree add</td><td>5.84</td><td>171.12</td><td>8.80e-5</td></tr><tr><td>10,000 RBTree add & delete randomly</td><td>16.30</td><td>61.34</td><td>0.01</td></tr><tr><td>10,000 RBTree get</td><td>19.80</td><td>50.50</td><td>0.00</td></tr><tr><td>10,000 AVLTree add</td><td>122.94</td><td>8.13</td><td>0.00</td></tr><tr><td>10,000 AVLTree add & delete randomly</td><td>185.43</td><td>5.39</td><td>0.00</td></tr><tr><td>10,000 AVLTree get</td><td>0.96</td><td>1044.69</td><td>6.87e-6</td></tr></table></div>
|
|
994
946
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
995
947
|
<div class='collapsible level0' ><span class='json-to-html-label'>rb-tree</span></div>
|
|
996
|
-
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add</td><td>
|
|
948
|
+
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add</td><td>79.39</td><td>12.60</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>211.76</td><td>4.72</td><td>0.00</td></tr><tr><td>100,000 getNode</td><td>169.34</td><td>5.91</td><td>6.62e-4</td></tr><tr><td>100,000 add & iterator</td><td>112.02</td><td>8.93</td><td>0.01</td></tr></table></div>
|
|
997
949
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
998
950
|
<div class='collapsible level0' ><span class='json-to-html-label'>directed-graph</span></div>
|
|
999
|
-
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000 addVertex</td><td>0.
|
|
951
|
+
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000 addVertex</td><td>0.10</td><td>9590.36</td><td>1.32e-6</td></tr><tr><td>1,000 addEdge</td><td>6.19</td><td>161.68</td><td>4.32e-4</td></tr><tr><td>1,000 getVertex</td><td>0.05</td><td>2.16e+4</td><td>3.75e-7</td></tr><tr><td>1,000 getEdge</td><td>24.72</td><td>40.45</td><td>0.01</td></tr><tr><td>tarjan</td><td>226.08</td><td>4.42</td><td>0.01</td></tr><tr><td>tarjan all</td><td>6667.55</td><td>0.15</td><td>0.27</td></tr><tr><td>topologicalSort</td><td>186.59</td><td>5.36</td><td>0.00</td></tr></table></div>
|
|
1000
952
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
1001
953
|
<div class='collapsible level0' ><span class='json-to-html-label'>hash-map</span></div>
|
|
1002
|
-
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 set</td><td>
|
|
954
|
+
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 set</td><td>137.00</td><td>7.30</td><td>0.08</td></tr><tr><td>Native Map 1,000,000 set</td><td>236.58</td><td>4.23</td><td>0.05</td></tr><tr><td>Native Set 1,000,000 add</td><td>187.78</td><td>5.33</td><td>0.05</td></tr><tr><td>1,000,000 set & get</td><td>123.91</td><td>8.07</td><td>0.04</td></tr><tr><td>Native Map 1,000,000 set & get</td><td>286.03</td><td>3.50</td><td>0.03</td></tr><tr><td>Native Set 1,000,000 add & has</td><td>188.67</td><td>5.30</td><td>0.03</td></tr><tr><td>1,000,000 ObjKey set & get</td><td>327.70</td><td>3.05</td><td>0.05</td></tr><tr><td>Native Map 1,000,000 ObjKey set & get</td><td>285.22</td><td>3.51</td><td>0.05</td></tr><tr><td>Native Set 1,000,000 ObjKey add & has</td><td>278.08</td><td>3.60</td><td>0.07</td></tr></table></div>
|
|
1003
955
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
1004
956
|
<div class='collapsible level0' ><span class='json-to-html-label'>heap</span></div>
|
|
1005
|
-
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add & poll</td><td>
|
|
957
|
+
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add & poll</td><td>23.99</td><td>41.68</td><td>0.00</td></tr><tr><td>100,000 add & dfs</td><td>33.23</td><td>30.09</td><td>0.00</td></tr><tr><td>10,000 fib add & pop</td><td>358.16</td><td>2.79</td><td>0.00</td></tr></table></div>
|
|
1006
958
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
1007
959
|
<div class='collapsible level0' ><span class='json-to-html-label'>doubly-linked-list</span></div>
|
|
1008
|
-
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>
|
|
960
|
+
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>229.07</td><td>4.37</td><td>0.06</td></tr><tr><td>1,000,000 unshift</td><td>217.64</td><td>4.59</td><td>0.08</td></tr><tr><td>1,000,000 unshift & shift</td><td>175.13</td><td>5.71</td><td>0.04</td></tr><tr><td>1,000,000 addBefore</td><td>342.22</td><td>2.92</td><td>0.08</td></tr></table></div>
|
|
1009
961
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
1010
962
|
<div class='collapsible level0' ><span class='json-to-html-label'>singly-linked-list</span></div>
|
|
1011
|
-
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push & shift</td><td>
|
|
963
|
+
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push & shift</td><td>210.65</td><td>4.75</td><td>0.06</td></tr><tr><td>10,000 push & pop</td><td>214.54</td><td>4.66</td><td>0.01</td></tr><tr><td>10,000 addBefore</td><td>248.45</td><td>4.02</td><td>0.01</td></tr></table></div>
|
|
1012
964
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
1013
965
|
<div class='collapsible level0' ><span class='json-to-html-label'>priority-queue</span></div>
|
|
1014
|
-
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add & poll</td><td>75.
|
|
966
|
+
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add & poll</td><td>75.67</td><td>13.22</td><td>0.00</td></tr></table></div>
|
|
1015
967
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
1016
968
|
<div class='collapsible level0' ><span class='json-to-html-label'>deque</span></div>
|
|
1017
|
-
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>
|
|
969
|
+
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>13.14</td><td>76.13</td><td>1.36e-4</td></tr><tr><td>10,000 push & delete</td><td>4716.79</td><td>0.21</td><td>0.13</td></tr><tr><td>1,000,000 push & pop</td><td>22.38</td><td>44.68</td><td>0.00</td></tr><tr><td>100,000 push & shift</td><td>2.15</td><td>464.20</td><td>1.98e-5</td></tr><tr><td>Native Array 100,000 push & shift</td><td>2241.30</td><td>0.45</td><td>0.14</td></tr><tr><td>100,000 unshift & shift</td><td>2.34</td><td>426.69</td><td>0.00</td></tr><tr><td>Native Array 100,000 unshift & shift</td><td>3971.32</td><td>0.25</td><td>0.18</td></tr></table></div>
|
|
1018
970
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
1019
971
|
<div class='collapsible level0' ><span class='json-to-html-label'>queue</span></div>
|
|
1020
|
-
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>
|
|
972
|
+
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>44.80</td><td>22.32</td><td>0.01</td></tr><tr><td>100,000 push & shift</td><td>4.91</td><td>203.64</td><td>1.15e-4</td></tr><tr><td>Native Array 100,000 push & shift</td><td>2116.78</td><td>0.47</td><td>0.12</td></tr><tr><td>Native Array 100,000 push & pop</td><td>4.30</td><td>232.29</td><td>9.32e-5</td></tr></table></div>
|
|
1021
973
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
1022
974
|
<div class='collapsible level0' ><span class='json-to-html-label'>stack</span></div>
|
|
1023
|
-
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>
|
|
975
|
+
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>42.15</td><td>23.72</td><td>0.00</td></tr><tr><td>1,000,000 push & pop</td><td>52.90</td><td>18.90</td><td>0.02</td></tr></table></div>
|
|
1024
976
|
</div><div class="json-to-html-collapse clearfix 0">
|
|
1025
977
|
<div class='collapsible level0' ><span class='json-to-html-label'>trie</span></div>
|
|
1026
|
-
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 push</td><td>
|
|
978
|
+
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 push</td><td>44.55</td><td>22.45</td><td>8.46e-4</td></tr><tr><td>100,000 getWords</td><td>87.48</td><td>11.43</td><td>0.00</td></tr></table></div>
|
|
1027
979
|
</div>
|
|
1028
980
|
|
|
1029
981
|
[//]: # (No deletion!!! End of Replace Section)
|
|
982
|
+
|
|
983
|
+
|
|
984
|
+
|
|
985
|
+
## supported module system
|
|
986
|
+
Now you can use it in Node.js and browser environments
|
|
987
|
+
|
|
988
|
+
CommonJS:**`require export.modules =`**
|
|
989
|
+
|
|
990
|
+
ESModule: **`import export`**
|
|
991
|
+
|
|
992
|
+
Typescript: **`import export`**
|
|
993
|
+
|
|
994
|
+
UMD: **`var Deque = dataStructureTyped.Deque`**
|
|
995
|
+
|
|
996
|
+
### CDN
|
|
997
|
+
|
|
998
|
+
Copy the line below into the head tag in an HTML document.
|
|
999
|
+
|
|
1000
|
+
#### development
|
|
1001
|
+
|
|
1002
|
+
```html
|
|
1003
|
+
<script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.js'></script>
|
|
1004
|
+
```
|
|
1005
|
+
|
|
1006
|
+
#### production
|
|
1007
|
+
|
|
1008
|
+
```html
|
|
1009
|
+
<script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.min.js'></script>
|
|
1010
|
+
```
|
|
1011
|
+
|
|
1012
|
+
Copy the code below into the script tag of your HTML, and you're good to go with your development.
|
|
1013
|
+
|
|
1014
|
+
```js
|
|
1015
|
+
const {Heap} = dataStructureTyped;
|
|
1016
|
+
const {
|
|
1017
|
+
BinaryTree, Graph, Queue, Stack, PriorityQueue, BST, Trie, DoublyLinkedList,
|
|
1018
|
+
AVLTree, MinHeap, SinglyLinkedList, DirectedGraph, TreeMultimap,
|
|
1019
|
+
DirectedVertex, AVLTreeNode
|
|
1020
|
+
} = dataStructureTyped;
|
|
1021
|
+
```
|