linked-list-typed 1.44.1 → 1.45.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/dist/data-structures/hash/hash-map.d.ts +230 -37
- package/dist/data-structures/hash/hash-map.js +427 -115
- package/dist/types/data-structures/hash/hash-map.d.ts +15 -1
- package/dist/types/data-structures/hash/index.d.ts +6 -0
- package/dist/types/data-structures/hash/index.js +20 -0
- package/dist/utils/utils.d.ts +3 -0
- package/dist/utils/utils.js +15 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +7 -7
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -3
- package/src/data-structures/binary-tree/binary-tree.ts +39 -31
- package/src/data-structures/binary-tree/bst.ts +12 -8
- package/src/data-structures/binary-tree/rb-tree.ts +17 -6
- package/src/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/data-structures/binary-tree/tree-multimap.ts +12 -9
- package/src/data-structures/graph/abstract-graph.ts +46 -31
- package/src/data-structures/graph/directed-graph.ts +10 -5
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +9 -9
- package/src/data-structures/hash/hash-map.ts +430 -123
- package/src/data-structures/hash/hash-table.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +8 -5
- package/src/data-structures/heap/max-heap.ts +3 -3
- package/src/data-structures/heap/min-heap.ts +3 -3
- 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/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +3 -3
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -3
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +5 -4
- package/src/data-structures/queue/queue.ts +2 -2
- package/src/data-structures/tree/tree.ts +1 -1
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/interfaces/graph.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/tree-multimap.ts +2 -2
- package/src/types/data-structures/hash/hash-map.ts +17 -1
- package/src/types/data-structures/hash/index.ts +7 -0
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +18 -4
- package/src/utils/utils.ts +16 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "linked-list-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.45.1",
|
|
4
4
|
"description": "Linked List, Doubly Linked List, Singly Linked List. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -66,6 +66,6 @@
|
|
|
66
66
|
"typescript": "^4.9.5"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"data-structure-typed": "^1.
|
|
69
|
+
"data-structure-typed": "^1.45.0"
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {BST, BSTNode} from './bst';
|
|
9
|
-
import type {AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BTNKey} from '../../types';
|
|
10
|
-
import {BTNCallback} from '../../types';
|
|
11
|
-
import {IBinaryTree} from '../../interfaces';
|
|
8
|
+
import { BST, BSTNode } from './bst';
|
|
9
|
+
import type { AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BTNKey } from '../../types';
|
|
10
|
+
import { BTNCallback } from '../../types';
|
|
11
|
+
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
|
|
13
13
|
export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
14
14
|
height: number;
|
|
@@ -95,7 +95,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
95
95
|
): BiTreeDeleteResult<N>[] {
|
|
96
96
|
if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C;
|
|
97
97
|
const deletedResults = super.delete(identifier, callback);
|
|
98
|
-
for (const {needBalanced} of deletedResults) {
|
|
98
|
+
for (const { needBalanced } of deletedResults) {
|
|
99
99
|
if (needBalanced) {
|
|
100
100
|
this._balancePath(needBalanced);
|
|
101
101
|
}
|
|
@@ -118,7 +118,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
118
118
|
destNode = this.ensureNotKey(destNode);
|
|
119
119
|
|
|
120
120
|
if (srcNode && destNode) {
|
|
121
|
-
const {key, value, height} = destNode;
|
|
121
|
+
const { key, value, height } = destNode;
|
|
122
122
|
const tempNode = this.createNode(key, value);
|
|
123
123
|
|
|
124
124
|
if (tempNode) {
|
|
@@ -209,7 +209,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
209
209
|
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
210
210
|
switch (
|
|
211
211
|
this._balanceFactor(A) // second O(1)
|
|
212
|
-
|
|
212
|
+
) {
|
|
213
213
|
case -2:
|
|
214
214
|
if (A && A.left) {
|
|
215
215
|
if (this._balanceFactor(A.left) <= 0) {
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {getMSB} from '../../utils';
|
|
8
|
+
import { getMSB } from '../../utils';
|
|
9
9
|
|
|
10
10
|
export class BinaryIndexedTree {
|
|
11
11
|
protected readonly _freq: number;
|
|
@@ -17,10 +17,10 @@ export class BinaryIndexedTree {
|
|
|
17
17
|
* @param - - `frequency`: The default frequency value. It is optional and has a default
|
|
18
18
|
* value of 0.
|
|
19
19
|
*/
|
|
20
|
-
constructor({frequency = 0, max}: {frequency?: number; max: number}) {
|
|
20
|
+
constructor({ frequency = 0, max }: { frequency?: number; max: number }) {
|
|
21
21
|
this._freq = frequency;
|
|
22
22
|
this._max = max;
|
|
23
|
-
this._freqMap = {0: 0};
|
|
23
|
+
this._freqMap = { 0: 0 };
|
|
24
24
|
this._msb = getMSB(max);
|
|
25
25
|
this._negativeCount = frequency < 0 ? max : 0;
|
|
26
26
|
}
|
|
@@ -6,11 +6,11 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type {BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNKey} from '../../types';
|
|
10
|
-
import {BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType} from '../../types';
|
|
11
|
-
import {IBinaryTree} from '../../interfaces';
|
|
12
|
-
import {trampoline} from '../../utils';
|
|
13
|
-
import {Queue} from '../queue';
|
|
9
|
+
import type { BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNKey } from '../../types';
|
|
10
|
+
import { BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType } from '../../types';
|
|
11
|
+
import { IBinaryTree } from '../../interfaces';
|
|
12
|
+
import { trampoline } from '../../utils';
|
|
13
|
+
import { Queue } from '../queue';
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Represents a node in a binary tree.
|
|
@@ -107,7 +107,8 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
107
107
|
* Represents a binary tree data structure.
|
|
108
108
|
* @template N - The type of the binary tree's nodes.
|
|
109
109
|
*/
|
|
110
|
-
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
110
|
+
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
111
|
+
implements IBinaryTree<V, N> {
|
|
111
112
|
iterationType: IterationType = IterationType.ITERATIVE;
|
|
112
113
|
|
|
113
114
|
/**
|
|
@@ -116,7 +117,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
116
117
|
*/
|
|
117
118
|
constructor(options?: BinaryTreeOptions) {
|
|
118
119
|
if (options) {
|
|
119
|
-
const {iterationType = IterationType.ITERATIVE} = options;
|
|
120
|
+
const { iterationType = IterationType.ITERATIVE } = options;
|
|
120
121
|
this.iterationType = iterationType;
|
|
121
122
|
}
|
|
122
123
|
this._size = 0;
|
|
@@ -315,7 +316,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
315
316
|
// Handle the case when there's only one root node
|
|
316
317
|
this._setRoot(null);
|
|
317
318
|
} else {
|
|
318
|
-
const {familyPosition: fp} = curr;
|
|
319
|
+
const { familyPosition: fp } = curr;
|
|
319
320
|
if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
|
|
320
321
|
parent.left = curr.right;
|
|
321
322
|
} else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
|
|
@@ -330,7 +331,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
330
331
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
331
332
|
orgCurrent = this._swap(curr, leftSubTreeRightMost);
|
|
332
333
|
if (parentOfLeftSubTreeMax) {
|
|
333
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
334
|
+
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
335
|
+
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
334
336
|
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
335
337
|
needBalanced = parentOfLeftSubTreeMax;
|
|
336
338
|
}
|
|
@@ -339,7 +341,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
339
341
|
}
|
|
340
342
|
this._size = this.size - 1;
|
|
341
343
|
|
|
342
|
-
deletedResult.push({deleted: orgCurrent, needBalanced});
|
|
344
|
+
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
343
345
|
return deletedResult;
|
|
344
346
|
}
|
|
345
347
|
|
|
@@ -409,14 +411,14 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
409
411
|
|
|
410
412
|
return _getMaxHeight(beginRoot);
|
|
411
413
|
} else {
|
|
412
|
-
const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
|
|
414
|
+
const stack: { node: N; depth: number }[] = [{ node: beginRoot, depth: 0 }];
|
|
413
415
|
let maxHeight = 0;
|
|
414
416
|
|
|
415
417
|
while (stack.length > 0) {
|
|
416
|
-
const {node, depth} = stack.pop()!;
|
|
418
|
+
const { node, depth } = stack.pop()!;
|
|
417
419
|
|
|
418
|
-
if (node.left) stack.push({node: node.left, depth: depth + 1});
|
|
419
|
-
if (node.right) stack.push({node: node.right, depth: depth + 1});
|
|
420
|
+
if (node.left) stack.push({ node: node.left, depth: depth + 1 });
|
|
421
|
+
if (node.right) stack.push({ node: node.right, depth: depth + 1 });
|
|
420
422
|
|
|
421
423
|
maxHeight = Math.max(maxHeight, depth);
|
|
422
424
|
}
|
|
@@ -912,7 +914,10 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
912
914
|
* @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
|
|
913
915
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
914
916
|
*/
|
|
915
|
-
getLeftMost(
|
|
917
|
+
getLeftMost(
|
|
918
|
+
beginRoot: BTNKey | N | null | undefined = this.root,
|
|
919
|
+
iterationType = this.iterationType
|
|
920
|
+
): N | null | undefined {
|
|
916
921
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
917
922
|
|
|
918
923
|
if (!beginRoot) return beginRoot;
|
|
@@ -955,7 +960,10 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
955
960
|
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
|
|
956
961
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
957
962
|
*/
|
|
958
|
-
getRightMost(
|
|
963
|
+
getRightMost(
|
|
964
|
+
beginRoot: BTNKey | N | null | undefined = this.root,
|
|
965
|
+
iterationType = this.iterationType
|
|
966
|
+
): N | null | undefined {
|
|
959
967
|
// TODO support get right most by passing key in
|
|
960
968
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
961
969
|
if (!beginRoot) return beginRoot;
|
|
@@ -1284,7 +1292,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1284
1292
|
_traverse(beginRoot);
|
|
1285
1293
|
} else {
|
|
1286
1294
|
// 0: visit, 1: print
|
|
1287
|
-
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
|
|
1295
|
+
const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{ opt: 0, node: beginRoot }];
|
|
1288
1296
|
|
|
1289
1297
|
while (stack.length > 0) {
|
|
1290
1298
|
const cur = stack.pop();
|
|
@@ -1299,24 +1307,24 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1299
1307
|
} else {
|
|
1300
1308
|
switch (pattern) {
|
|
1301
1309
|
case 'in':
|
|
1302
|
-
cur.node && stack.push({opt: 0, node: cur.node.right});
|
|
1303
|
-
stack.push({opt: 1, node: cur.node});
|
|
1304
|
-
cur.node && stack.push({opt: 0, node: cur.node.left});
|
|
1310
|
+
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1311
|
+
stack.push({ opt: 1, node: cur.node });
|
|
1312
|
+
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1305
1313
|
break;
|
|
1306
1314
|
case 'pre':
|
|
1307
|
-
cur.node && stack.push({opt: 0, node: cur.node.right});
|
|
1308
|
-
cur.node && stack.push({opt: 0, node: cur.node.left});
|
|
1309
|
-
stack.push({opt: 1, node: cur.node});
|
|
1315
|
+
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1316
|
+
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1317
|
+
stack.push({ opt: 1, node: cur.node });
|
|
1310
1318
|
break;
|
|
1311
1319
|
case 'post':
|
|
1312
|
-
stack.push({opt: 1, node: cur.node});
|
|
1313
|
-
cur.node && stack.push({opt: 0, node: cur.node.right});
|
|
1314
|
-
cur.node && stack.push({opt: 0, node: cur.node.left});
|
|
1320
|
+
stack.push({ opt: 1, node: cur.node });
|
|
1321
|
+
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1322
|
+
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1315
1323
|
break;
|
|
1316
1324
|
default:
|
|
1317
|
-
cur.node && stack.push({opt: 0, node: cur.node.right});
|
|
1318
|
-
stack.push({opt: 1, node: cur.node});
|
|
1319
|
-
cur.node && stack.push({opt: 0, node: cur.node.left});
|
|
1325
|
+
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1326
|
+
stack.push({ opt: 1, node: cur.node });
|
|
1327
|
+
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1320
1328
|
break;
|
|
1321
1329
|
}
|
|
1322
1330
|
}
|
|
@@ -1686,7 +1694,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1686
1694
|
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1687
1695
|
* binary tree nodes in a specific order.
|
|
1688
1696
|
*/
|
|
1689
|
-
*[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
|
|
1697
|
+
* [Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
|
|
1690
1698
|
if (!node) {
|
|
1691
1699
|
return;
|
|
1692
1700
|
}
|
|
@@ -1798,7 +1806,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1798
1806
|
destNode = this.ensureNotKey(destNode);
|
|
1799
1807
|
|
|
1800
1808
|
if (srcNode && destNode) {
|
|
1801
|
-
const {key, value} = destNode;
|
|
1809
|
+
const { key, value } = destNode;
|
|
1802
1810
|
const tempNode = this.createNode(key, value);
|
|
1803
1811
|
|
|
1804
1812
|
if (tempNode) {
|
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {BSTComparator, BSTNodeNested, BSTOptions, BTNCallback, BTNKey} from '../../types';
|
|
9
|
-
import {CP, IterationType} from '../../types';
|
|
10
|
-
import {BinaryTree, BinaryTreeNode} from './binary-tree';
|
|
11
|
-
import {IBinaryTree} from '../../interfaces';
|
|
12
|
-
import {Queue} from '../queue';
|
|
8
|
+
import type { BSTComparator, BSTNodeNested, BSTOptions, BTNCallback, BTNKey } from '../../types';
|
|
9
|
+
import { CP, IterationType } from '../../types';
|
|
10
|
+
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
|
+
import { IBinaryTree } from '../../interfaces';
|
|
12
|
+
import { Queue } from '../queue';
|
|
13
13
|
|
|
14
14
|
export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
|
15
15
|
override parent?: N;
|
|
@@ -62,7 +62,9 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
|
|
65
|
+
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
|
|
66
|
+
extends BinaryTree<V, N>
|
|
67
|
+
implements IBinaryTree<V, N> {
|
|
66
68
|
/**
|
|
67
69
|
* The constructor function initializes a binary search tree with an optional comparator function.
|
|
68
70
|
* @param {BSTOptions} [options] - An optional object that contains additional configuration options
|
|
@@ -72,7 +74,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
72
74
|
super(options);
|
|
73
75
|
this._root = undefined;
|
|
74
76
|
if (options !== undefined) {
|
|
75
|
-
const {comparator} = options;
|
|
77
|
+
const { comparator } = options;
|
|
76
78
|
if (comparator !== undefined) {
|
|
77
79
|
this._comparator = comparator;
|
|
78
80
|
}
|
|
@@ -225,7 +227,9 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
225
227
|
}
|
|
226
228
|
|
|
227
229
|
const inserted: (N | undefined)[] = [];
|
|
228
|
-
const combinedArr: [BTNKey | N, V][] = keysOrNodes.map(
|
|
230
|
+
const combinedArr: [BTNKey | N, V][] = keysOrNodes.map(
|
|
231
|
+
(value: BTNKey | N, index) => [value, data?.[index]] as [BTNKey | N, V]
|
|
232
|
+
);
|
|
229
233
|
|
|
230
234
|
let sorted = [];
|
|
231
235
|
|
|
@@ -6,12 +6,23 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
import {
|
|
10
|
+
BiTreeDeleteResult,
|
|
11
|
+
BTNCallback,
|
|
12
|
+
BTNKey,
|
|
13
|
+
IterationType,
|
|
14
|
+
RBTNColor,
|
|
15
|
+
RBTreeOptions,
|
|
16
|
+
RedBlackTreeNodeNested
|
|
17
|
+
} from '../../types';
|
|
18
|
+
import { BST, BSTNode } from './bst';
|
|
19
|
+
import { IBinaryTree } from '../../interfaces';
|
|
20
|
+
import { BinaryTreeNode } from './binary-tree';
|
|
21
|
+
|
|
22
|
+
export class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<
|
|
23
|
+
V,
|
|
24
|
+
N
|
|
25
|
+
> {
|
|
15
26
|
color: RBTNColor;
|
|
16
27
|
|
|
17
28
|
constructor(key: BTNKey, value?: V, color: RBTNColor = RBTNColor.BLACK) {
|
|
@@ -5,12 +5,15 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {BTNKey, TreeMultimapNodeNested, TreeMultimapOptions} from '../../types';
|
|
9
|
-
import {BiTreeDeleteResult, BTNCallback, CP, FamilyPosition, IterationType} from '../../types';
|
|
10
|
-
import {IBinaryTree} from '../../interfaces';
|
|
11
|
-
import {AVLTree, AVLTreeNode} from './avl-tree';
|
|
12
|
-
|
|
13
|
-
export class TreeMultimapNode<
|
|
8
|
+
import type { BTNKey, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
|
|
9
|
+
import { BiTreeDeleteResult, BTNCallback, CP, FamilyPosition, IterationType } from '../../types';
|
|
10
|
+
import { IBinaryTree } from '../../interfaces';
|
|
11
|
+
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
|
+
|
|
13
|
+
export class TreeMultimapNode<
|
|
14
|
+
V = any,
|
|
15
|
+
N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>
|
|
16
|
+
> extends AVLTreeNode<V, N> {
|
|
14
17
|
count: number;
|
|
15
18
|
|
|
16
19
|
/**
|
|
@@ -292,7 +295,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
292
295
|
if (!parent) {
|
|
293
296
|
if (curr.right !== undefined) this._setRoot(curr.right);
|
|
294
297
|
} else {
|
|
295
|
-
const {familyPosition: fp} = curr;
|
|
298
|
+
const { familyPosition: fp } = curr;
|
|
296
299
|
if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
|
|
297
300
|
parent.left = curr.right;
|
|
298
301
|
} else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
|
|
@@ -320,7 +323,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
320
323
|
if (orgCurrent) this._count -= orgCurrent.count;
|
|
321
324
|
}
|
|
322
325
|
|
|
323
|
-
deletedResult.push({deleted: orgCurrent, needBalanced});
|
|
326
|
+
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
324
327
|
|
|
325
328
|
if (needBalanced) {
|
|
326
329
|
this._balancePath(needBalanced);
|
|
@@ -396,7 +399,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
396
399
|
srcNode = this.ensureNotKey(srcNode);
|
|
397
400
|
destNode = this.ensureNotKey(destNode);
|
|
398
401
|
if (srcNode && destNode) {
|
|
399
|
-
const {key, value, count, height} = destNode;
|
|
402
|
+
const { key, value, count, height } = destNode;
|
|
400
403
|
const tempNode = this.createNode(key, value, count);
|
|
401
404
|
if (tempNode) {
|
|
402
405
|
tempNode.height = height;
|
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {uuidV4} from '../../utils';
|
|
9
|
-
import {PriorityQueue} from '../priority-queue';
|
|
10
|
-
import type {DijkstraResult, VertexKey} from '../../types';
|
|
11
|
-
import {IGraph} from '../../interfaces';
|
|
12
|
-
import {Queue} from '../queue';
|
|
8
|
+
import { uuidV4 } from '../../utils';
|
|
9
|
+
import { PriorityQueue } from '../priority-queue';
|
|
10
|
+
import type { DijkstraResult, VertexKey } from '../../types';
|
|
11
|
+
import { IGraph } from '../../interfaces';
|
|
12
|
+
import { Queue } from '../queue';
|
|
13
13
|
|
|
14
14
|
export abstract class AbstractVertex<V = any> {
|
|
15
15
|
key: VertexKey;
|
|
@@ -300,11 +300,11 @@ export abstract class AbstractGraph<
|
|
|
300
300
|
return [];
|
|
301
301
|
}
|
|
302
302
|
|
|
303
|
-
const stack: {vertex: VO; path: VO[]}[] = [];
|
|
304
|
-
stack.push({vertex: vertex1, path: [vertex1]});
|
|
303
|
+
const stack: { vertex: VO; path: VO[] }[] = [];
|
|
304
|
+
stack.push({ vertex: vertex1, path: [vertex1] });
|
|
305
305
|
|
|
306
306
|
while (stack.length > 0) {
|
|
307
|
-
const {vertex, path} = stack.pop()!;
|
|
307
|
+
const { vertex, path } = stack.pop()!;
|
|
308
308
|
|
|
309
309
|
if (vertex === vertex2) {
|
|
310
310
|
paths.push(path);
|
|
@@ -315,7 +315,7 @@ export abstract class AbstractGraph<
|
|
|
315
315
|
for (const neighbor of neighbors) {
|
|
316
316
|
if (!path.includes(neighbor)) {
|
|
317
317
|
const newPath = [...path, neighbor];
|
|
318
|
-
stack.push({vertex: neighbor, path: newPath});
|
|
318
|
+
stack.push({ vertex: neighbor, path: newPath });
|
|
319
319
|
}
|
|
320
320
|
}
|
|
321
321
|
}
|
|
@@ -514,7 +514,12 @@ export abstract class AbstractGraph<
|
|
|
514
514
|
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
515
515
|
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
516
516
|
*/
|
|
517
|
-
dijkstraWithoutHeap(
|
|
517
|
+
dijkstraWithoutHeap(
|
|
518
|
+
src: VO | VertexKey,
|
|
519
|
+
dest?: VO | VertexKey | null,
|
|
520
|
+
getMinDist?: boolean,
|
|
521
|
+
genPaths?: boolean
|
|
522
|
+
): DijkstraResult<VO> {
|
|
518
523
|
if (getMinDist === undefined) getMinDist = false;
|
|
519
524
|
if (genPaths === undefined) genPaths = false;
|
|
520
525
|
|
|
@@ -586,7 +591,7 @@ export abstract class AbstractGraph<
|
|
|
586
591
|
if (genPaths) {
|
|
587
592
|
getPaths(destVertex);
|
|
588
593
|
}
|
|
589
|
-
return {distMap, preMap, seen, paths, minDist, minPath};
|
|
594
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
590
595
|
}
|
|
591
596
|
const neighbors = this.getNeighbors(cur);
|
|
592
597
|
for (const neighbor of neighbors) {
|
|
@@ -609,18 +614,18 @@ export abstract class AbstractGraph<
|
|
|
609
614
|
}
|
|
610
615
|
|
|
611
616
|
getMinDist &&
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
}
|
|
617
|
+
distMap.forEach((d, v) => {
|
|
618
|
+
if (v !== srcVertex) {
|
|
619
|
+
if (d < minDist) {
|
|
620
|
+
minDist = d;
|
|
621
|
+
if (genPaths) minDest = v;
|
|
618
622
|
}
|
|
619
|
-
}
|
|
623
|
+
}
|
|
624
|
+
});
|
|
620
625
|
|
|
621
626
|
genPaths && getPaths(minDest);
|
|
622
627
|
|
|
623
|
-
return {distMap, preMap, seen, paths, minDist, minPath};
|
|
628
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
624
629
|
}
|
|
625
630
|
|
|
626
631
|
/**
|
|
@@ -657,7 +662,12 @@ export abstract class AbstractGraph<
|
|
|
657
662
|
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
658
663
|
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
659
664
|
*/
|
|
660
|
-
dijkstra(
|
|
665
|
+
dijkstra(
|
|
666
|
+
src: VO | VertexKey,
|
|
667
|
+
dest?: VO | VertexKey | null,
|
|
668
|
+
getMinDist?: boolean,
|
|
669
|
+
genPaths?: boolean
|
|
670
|
+
): DijkstraResult<VO> {
|
|
661
671
|
if (getMinDist === undefined) getMinDist = false;
|
|
662
672
|
if (genPaths === undefined) genPaths = false;
|
|
663
673
|
|
|
@@ -681,8 +691,8 @@ export abstract class AbstractGraph<
|
|
|
681
691
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
682
692
|
}
|
|
683
693
|
|
|
684
|
-
const heap = new PriorityQueue<{key: number; value: VO}>({comparator: (a, b) => a.key - b.key});
|
|
685
|
-
heap.add({key: 0, value: srcVertex});
|
|
694
|
+
const heap = new PriorityQueue<{ key: number; value: VO }>({ comparator: (a, b) => a.key - b.key });
|
|
695
|
+
heap.add({ key: 0, value: srcVertex });
|
|
686
696
|
|
|
687
697
|
distMap.set(srcVertex, 0);
|
|
688
698
|
preMap.set(srcVertex, null);
|
|
@@ -723,7 +733,7 @@ export abstract class AbstractGraph<
|
|
|
723
733
|
if (genPaths) {
|
|
724
734
|
getPaths(destVertex);
|
|
725
735
|
}
|
|
726
|
-
return {distMap, preMap, seen, paths, minDist, minPath};
|
|
736
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
727
737
|
}
|
|
728
738
|
const neighbors = this.getNeighbors(cur);
|
|
729
739
|
for (const neighbor of neighbors) {
|
|
@@ -733,7 +743,7 @@ export abstract class AbstractGraph<
|
|
|
733
743
|
const distSrcToNeighbor = distMap.get(neighbor);
|
|
734
744
|
if (distSrcToNeighbor) {
|
|
735
745
|
if (dist + weight < distSrcToNeighbor) {
|
|
736
|
-
heap.add({key: dist + weight, value: neighbor});
|
|
746
|
+
heap.add({ key: dist + weight, value: neighbor });
|
|
737
747
|
preMap.set(neighbor, cur);
|
|
738
748
|
distMap.set(neighbor, dist + weight);
|
|
739
749
|
}
|
|
@@ -760,7 +770,7 @@ export abstract class AbstractGraph<
|
|
|
760
770
|
getPaths(minDest);
|
|
761
771
|
}
|
|
762
772
|
|
|
763
|
-
return {distMap, preMap, seen, paths, minDist, minPath};
|
|
773
|
+
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
764
774
|
}
|
|
765
775
|
|
|
766
776
|
/**
|
|
@@ -800,7 +810,7 @@ export abstract class AbstractGraph<
|
|
|
800
810
|
// TODO
|
|
801
811
|
let hasNegativeCycle: boolean | undefined;
|
|
802
812
|
if (scanNegativeCycle) hasNegativeCycle = false;
|
|
803
|
-
if (!srcVertex) return {hasNegativeCycle, distMap, preMap, paths, min, minPath};
|
|
813
|
+
if (!srcVertex) return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
804
814
|
|
|
805
815
|
const vertices = this._vertices;
|
|
806
816
|
const numOfVertices = vertices.size;
|
|
@@ -872,7 +882,7 @@ export abstract class AbstractGraph<
|
|
|
872
882
|
}
|
|
873
883
|
}
|
|
874
884
|
|
|
875
|
-
return {hasNegativeCycle, distMap, preMap, paths, min, minPath};
|
|
885
|
+
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
876
886
|
}
|
|
877
887
|
|
|
878
888
|
/**
|
|
@@ -913,7 +923,7 @@ export abstract class AbstractGraph<
|
|
|
913
923
|
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
|
914
924
|
* path between vertices in the
|
|
915
925
|
*/
|
|
916
|
-
floydWarshall(): {costs: number[][]; predecessor: (VO | null)[][]} {
|
|
926
|
+
floydWarshall(): { costs: number[][]; predecessor: (VO | null)[][] } {
|
|
917
927
|
const idAndVertices = [...this._vertices];
|
|
918
928
|
const n = idAndVertices.length;
|
|
919
929
|
|
|
@@ -945,7 +955,7 @@ export abstract class AbstractGraph<
|
|
|
945
955
|
}
|
|
946
956
|
}
|
|
947
957
|
}
|
|
948
|
-
return {costs, predecessor};
|
|
958
|
+
return { costs, predecessor };
|
|
949
959
|
}
|
|
950
960
|
|
|
951
961
|
/**
|
|
@@ -982,7 +992,12 @@ export abstract class AbstractGraph<
|
|
|
982
992
|
* are arrays of vertices that form cycles within the SCCs.
|
|
983
993
|
* @returns The function `tarjan` returns an object with the following properties:
|
|
984
994
|
*/
|
|
985
|
-
tarjan(
|
|
995
|
+
tarjan(
|
|
996
|
+
needCutVertexes: boolean = false,
|
|
997
|
+
needBridges: boolean = false,
|
|
998
|
+
needSCCs: boolean = true,
|
|
999
|
+
needCycles: boolean = false
|
|
1000
|
+
) {
|
|
986
1001
|
// !! in undirected graph we will not let child visit parent when dfs
|
|
987
1002
|
// !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
|
|
988
1003
|
// !! bridge: low(child) > dfn(cur)
|
|
@@ -1081,7 +1096,7 @@ export abstract class AbstractGraph<
|
|
|
1081
1096
|
});
|
|
1082
1097
|
}
|
|
1083
1098
|
|
|
1084
|
-
return {dfnMap, lowMap, bridges, cutVertexes, SCCs, cycles};
|
|
1099
|
+
return { dfnMap, lowMap, bridges, cutVertexes, SCCs, cycles };
|
|
1085
1100
|
}
|
|
1086
1101
|
|
|
1087
1102
|
/**
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {arrayRemove} from '../../utils';
|
|
9
|
-
import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
|
|
10
|
-
import type {TopologicalStatus, VertexKey} from '../../types';
|
|
11
|
-
import {IGraph} from '../../interfaces';
|
|
8
|
+
import { arrayRemove } from '../../utils';
|
|
9
|
+
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
10
|
+
import type { TopologicalStatus, VertexKey } from '../../types';
|
|
11
|
+
import { IGraph } from '../../interfaces';
|
|
12
12
|
|
|
13
13
|
export class DirectedVertex<V = any> extends AbstractVertex<V> {
|
|
14
14
|
/**
|
|
@@ -45,7 +45,12 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
export class DirectedGraph<
|
|
48
|
+
export class DirectedGraph<
|
|
49
|
+
V = any,
|
|
50
|
+
E = any,
|
|
51
|
+
VO extends DirectedVertex<V> = DirectedVertex<V>,
|
|
52
|
+
EO extends DirectedEdge<E> = DirectedEdge<E>
|
|
53
|
+
>
|
|
49
54
|
extends AbstractGraph<V, E, VO, EO>
|
|
50
55
|
implements IGraph<V, E, VO, EO> {
|
|
51
56
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {MapGraphCoordinate, VertexKey} from '../../types';
|
|
2
|
-
import {DirectedEdge, DirectedGraph, DirectedVertex} from './directed-graph';
|
|
1
|
+
import { MapGraphCoordinate, VertexKey } from '../../types';
|
|
2
|
+
import { DirectedEdge, DirectedGraph, DirectedVertex } from './directed-graph';
|
|
3
3
|
|
|
4
4
|
export class MapVertex<V = any> extends DirectedVertex<V> {
|
|
5
5
|
lat: number;
|
|
@@ -40,12 +40,12 @@ export class MapEdge<E = any> extends DirectedEdge<E> {
|
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
export class MapGraph<
|
|
44
|
-
V,
|
|
45
|
-
E,
|
|
46
|
-
VO
|
|
47
|
-
EO
|
|
48
|
-
> {
|
|
43
|
+
export class MapGraph<
|
|
44
|
+
V = any,
|
|
45
|
+
E = any,
|
|
46
|
+
VO extends MapVertex<V> = MapVertex<V>,
|
|
47
|
+
EO extends MapEdge<E> = MapEdge<E>
|
|
48
|
+
> extends DirectedGraph<V, E, VO, EO> {
|
|
49
49
|
/**
|
|
50
50
|
* The constructor function initializes the origin and bottomRight properties of a MapGraphCoordinate object.
|
|
51
51
|
* @param {MapGraphCoordinate} origin - The `origin` parameter is a `MapGraphCoordinate` object that represents the
|