data-structure-typed 1.50.4 → 1.50.6
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 +116 -55
- package/SPECIFICATION.md +2 -2
- package/SPECIFICATION_zh-CN.md +81 -0
- package/{SPONSOR-zh-CN.md → SPONSOR_zh-CN.md} +1 -1
- package/benchmark/report.html +24 -24
- package/benchmark/report.json +261 -237
- package/dist/cjs/data-structures/base/iterable-base.d.ts +10 -8
- package/dist/cjs/data-structures/base/iterable-base.js +8 -12
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +19 -19
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +158 -135
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +415 -386
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +84 -76
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -0
- package/dist/cjs/data-structures/graph/abstract-graph.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.d.ts +14 -76
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +16 -86
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +27 -69
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +35 -79
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +0 -53
- package/dist/cjs/data-structures/queue/deque.js +0 -61
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +0 -70
- package/dist/cjs/data-structures/queue/queue.js +0 -87
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +10 -8
- package/dist/mjs/data-structures/base/iterable-base.js +8 -12
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +19 -19
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +158 -135
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +412 -386
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +84 -76
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/mjs/data-structures/graph/abstract-graph.js +3 -0
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +14 -76
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +16 -86
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +27 -69
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +33 -79
- package/dist/mjs/data-structures/queue/deque.d.ts +0 -53
- package/dist/mjs/data-structures/queue/deque.js +0 -61
- package/dist/mjs/data-structures/queue/queue.d.ts +0 -70
- package/dist/mjs/data-structures/queue/queue.js +0 -86
- package/dist/umd/data-structure-typed.js +539 -756
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/base/iterable-base.ts +14 -10
- package/src/data-structures/binary-tree/binary-tree.ts +19 -19
- package/src/data-structures/binary-tree/rb-tree.ts +437 -395
- package/src/data-structures/binary-tree/tree-multi-map.ts +85 -82
- package/src/data-structures/graph/abstract-graph.ts +4 -0
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +16 -94
- package/src/data-structures/linked-list/singly-linked-list.ts +35 -87
- package/src/data-structures/queue/deque.ts +0 -67
- package/src/data-structures/queue/queue.ts +0 -98
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +3 -3
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +3 -3
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +26 -16
- package/test/performance/data-structures/hash/hash-map.test.ts +6 -6
- package/test/performance/data-structures/heap/heap.test.ts +14 -14
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +11 -6
- package/test/performance/data-structures/queue/deque.test.ts +8 -8
- package/test/performance/data-structures/queue/queue.test.ts +5 -12
- package/test/performance/reportor.ts +43 -1
- package/test/unit/data-structures/binary-tree/overall.test.ts +23 -21
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +168 -105
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +311 -192
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +6 -6
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +10 -10
- package/test/unit/data-structures/linked-list/skip-list.test.ts +4 -4
- package/test/unit/data-structures/queue/deque.test.ts +26 -26
- package/test/unit/data-structures/queue/queue.test.ts +20 -20
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.50.
|
|
3
|
+
"version": "1.50.6",
|
|
4
4
|
"description": "Javascript Data Structure. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
@@ -6,6 +6,8 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
6
6
|
* Space Complexity: O(1)
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
+
abstract get size(): number;
|
|
10
|
+
|
|
9
11
|
/**
|
|
10
12
|
* Time Complexity: O(n)
|
|
11
13
|
* Space Complexity: O(1)
|
|
@@ -125,6 +127,11 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
125
127
|
return false;
|
|
126
128
|
}
|
|
127
129
|
|
|
130
|
+
/**
|
|
131
|
+
* Time Complexity: O(n)
|
|
132
|
+
* Space Complexity: O(1)
|
|
133
|
+
*/
|
|
134
|
+
|
|
128
135
|
/**
|
|
129
136
|
* Time Complexity: O(n)
|
|
130
137
|
* Space Complexity: O(1)
|
|
@@ -248,11 +255,6 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
248
255
|
return;
|
|
249
256
|
}
|
|
250
257
|
|
|
251
|
-
/**
|
|
252
|
-
* Time Complexity: O(n)
|
|
253
|
-
* Space Complexity: O(1)
|
|
254
|
-
*/
|
|
255
|
-
|
|
256
258
|
/**
|
|
257
259
|
* Time Complexity: O(n)
|
|
258
260
|
* Space Complexity: O(1)
|
|
@@ -301,6 +303,8 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
301
303
|
}
|
|
302
304
|
|
|
303
305
|
export abstract class IterableElementBase<E = any, C = any> {
|
|
306
|
+
abstract get size(): number;
|
|
307
|
+
|
|
304
308
|
/**
|
|
305
309
|
* Time Complexity: O(n)
|
|
306
310
|
* Space Complexity: O(1)
|
|
@@ -362,6 +366,11 @@ export abstract class IterableElementBase<E = any, C = any> {
|
|
|
362
366
|
return true;
|
|
363
367
|
}
|
|
364
368
|
|
|
369
|
+
/**
|
|
370
|
+
* Time Complexity: O(n)
|
|
371
|
+
* Space Complexity: O(1)
|
|
372
|
+
*/
|
|
373
|
+
|
|
365
374
|
/**
|
|
366
375
|
* Time Complexity: O(n)
|
|
367
376
|
* Space Complexity: O(1)
|
|
@@ -445,11 +454,6 @@ export abstract class IterableElementBase<E = any, C = any> {
|
|
|
445
454
|
return;
|
|
446
455
|
}
|
|
447
456
|
|
|
448
|
-
/**
|
|
449
|
-
* Time Complexity: O(n)
|
|
450
|
-
* Space Complexity: O(1)
|
|
451
|
-
*/
|
|
452
|
-
|
|
453
457
|
/**
|
|
454
458
|
* Time Complexity: O(n)
|
|
455
459
|
* Space Complexity: O(1)
|
|
@@ -934,7 +934,7 @@ export class BinaryTree<
|
|
|
934
934
|
|
|
935
935
|
if (iterationType === IterationType.RECURSIVE) {
|
|
936
936
|
const dfs = (cur: NODE | null | undefined, min: number, max: number): boolean => {
|
|
937
|
-
if (!cur) return true;
|
|
937
|
+
if (!this.isRealNode(cur)) return true;
|
|
938
938
|
const numKey = this.extractor(cur.key);
|
|
939
939
|
if (numKey <= min || numKey >= max) return false;
|
|
940
940
|
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
@@ -949,14 +949,14 @@ export class BinaryTree<
|
|
|
949
949
|
let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
|
|
950
950
|
// @ts-ignore
|
|
951
951
|
let curr: NODE | null | undefined = beginRoot;
|
|
952
|
-
while (curr || stack.length > 0) {
|
|
953
|
-
while (curr) {
|
|
952
|
+
while (this.isRealNode(curr) || stack.length > 0) {
|
|
953
|
+
while (this.isRealNode(curr)) {
|
|
954
954
|
stack.push(curr);
|
|
955
955
|
curr = curr.left;
|
|
956
956
|
}
|
|
957
957
|
curr = stack.pop()!;
|
|
958
958
|
const numKey = this.extractor(curr.key);
|
|
959
|
-
if (!curr || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey)) return false;
|
|
959
|
+
if (!this.isRealNode(curr) || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey)) return false;
|
|
960
960
|
prev = numKey;
|
|
961
961
|
curr = curr.right;
|
|
962
962
|
}
|
|
@@ -1025,7 +1025,7 @@ export class BinaryTree<
|
|
|
1025
1025
|
|
|
1026
1026
|
if (iterationType === IterationType.RECURSIVE) {
|
|
1027
1027
|
const _getMaxHeight = (cur: NODE | null | undefined): number => {
|
|
1028
|
-
if (!cur) return -1;
|
|
1028
|
+
if (!this.isRealNode(cur)) return -1;
|
|
1029
1029
|
const leftHeight = _getMaxHeight(cur.left);
|
|
1030
1030
|
const rightHeight = _getMaxHeight(cur.right);
|
|
1031
1031
|
return Math.max(leftHeight, rightHeight) + 1;
|
|
@@ -1039,8 +1039,8 @@ export class BinaryTree<
|
|
|
1039
1039
|
while (stack.length > 0) {
|
|
1040
1040
|
const { node, depth } = stack.pop()!;
|
|
1041
1041
|
|
|
1042
|
-
if (node.left) stack.push({ node: node.left, depth: depth + 1 });
|
|
1043
|
-
if (node.right) stack.push({ node: node.right, depth: depth + 1 });
|
|
1042
|
+
if (this.isRealNode(node.left)) stack.push({ node: node.left, depth: depth + 1 });
|
|
1043
|
+
if (this.isRealNode(node.right)) stack.push({ node: node.right, depth: depth + 1 });
|
|
1044
1044
|
|
|
1045
1045
|
maxHeight = Math.max(maxHeight, depth);
|
|
1046
1046
|
}
|
|
@@ -1354,34 +1354,34 @@ export class BinaryTree<
|
|
|
1354
1354
|
switch (pattern) {
|
|
1355
1355
|
case 'in':
|
|
1356
1356
|
if (includeNull) {
|
|
1357
|
-
if (node && this.isNodeOrNull(node.left)) _traverse(node.left);
|
|
1357
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
|
|
1358
1358
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1359
|
-
if (node && this.isNodeOrNull(node.right)) _traverse(node.right);
|
|
1359
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.right)) _traverse(node.right);
|
|
1360
1360
|
} else {
|
|
1361
|
-
if (node && node.left) _traverse(node.left);
|
|
1361
|
+
if (this.isRealNode(node) && this.isRealNode(node.left)) _traverse(node.left);
|
|
1362
1362
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1363
|
-
if (node && node.right) _traverse(node.right);
|
|
1363
|
+
if (this.isRealNode(node) && this.isRealNode(node.right)) _traverse(node.right);
|
|
1364
1364
|
}
|
|
1365
1365
|
break;
|
|
1366
1366
|
case 'pre':
|
|
1367
1367
|
if (includeNull) {
|
|
1368
1368
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1369
|
-
if (node && this.isNodeOrNull(node.left)) _traverse(node.left);
|
|
1370
|
-
if (node && this.isNodeOrNull(node.right)) _traverse(node.right);
|
|
1369
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
|
|
1370
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.right)) _traverse(node.right);
|
|
1371
1371
|
} else {
|
|
1372
1372
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1373
|
-
if (node && node.left) _traverse(node.left);
|
|
1374
|
-
if (node && node.right) _traverse(node.right);
|
|
1373
|
+
if (this.isRealNode(node) && this.isRealNode(node.left)) _traverse(node.left);
|
|
1374
|
+
if (this.isRealNode(node) && this.isRealNode(node.right)) _traverse(node.right);
|
|
1375
1375
|
}
|
|
1376
1376
|
break;
|
|
1377
1377
|
case 'post':
|
|
1378
1378
|
if (includeNull) {
|
|
1379
|
-
if (node && this.isNodeOrNull(node.left)) _traverse(node.left);
|
|
1380
|
-
if (node && this.isNodeOrNull(node.right)) _traverse(node.right);
|
|
1379
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
|
|
1380
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.right)) _traverse(node.right);
|
|
1381
1381
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1382
1382
|
} else {
|
|
1383
|
-
if (node && node.left) _traverse(node.left);
|
|
1384
|
-
if (node && node.right) _traverse(node.right);
|
|
1383
|
+
if (this.isRealNode(node) && this.isRealNode(node.left)) _traverse(node.left);
|
|
1384
|
+
if (this.isRealNode(node) && this.isRealNode(node.right)) _traverse(node.right);
|
|
1385
1385
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1386
1386
|
}
|
|
1387
1387
|
|