min-heap-typed 1.42.2 → 1.42.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.
|
@@ -79,7 +79,6 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
79
79
|
* Get the number of nodes in the binary tree.
|
|
80
80
|
*/
|
|
81
81
|
get size(): number;
|
|
82
|
-
protected defaultOneParamCallback: (node: N) => number;
|
|
83
82
|
/**
|
|
84
83
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
85
84
|
* @param {BTNKey} key - The key for the new node.
|
|
@@ -289,6 +288,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
289
288
|
* binary tree nodes in a specific order.
|
|
290
289
|
*/
|
|
291
290
|
[Symbol.iterator](node?: N | null): Generator<BTNKey, void, undefined>;
|
|
291
|
+
protected defaultOneParamCallback: (node: N) => number;
|
|
292
292
|
/**
|
|
293
293
|
* Swap the data of two nodes in the binary tree.
|
|
294
294
|
* @param {N} srcNode - The source node to swap.
|
|
@@ -1034,7 +1034,6 @@ class BinaryTree {
|
|
|
1034
1034
|
}
|
|
1035
1035
|
return y;
|
|
1036
1036
|
}
|
|
1037
|
-
// --- start additional methods ---
|
|
1038
1037
|
/**
|
|
1039
1038
|
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
1040
1039
|
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
@@ -1133,6 +1132,7 @@ class BinaryTree {
|
|
|
1133
1132
|
}
|
|
1134
1133
|
return ans;
|
|
1135
1134
|
}
|
|
1135
|
+
// --- start additional methods ---
|
|
1136
1136
|
/**
|
|
1137
1137
|
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1138
1138
|
* either an iterative or recursive manner.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "1.42.
|
|
3
|
+
"version": "1.42.3",
|
|
4
4
|
"description": "Min Heap. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -132,6 +132,6 @@
|
|
|
132
132
|
"typescript": "^4.9.5"
|
|
133
133
|
},
|
|
134
134
|
"dependencies": {
|
|
135
|
-
"data-structure-typed": "^1.42.
|
|
135
|
+
"data-structure-typed": "^1.42.3"
|
|
136
136
|
}
|
|
137
137
|
}
|
|
@@ -108,7 +108,8 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
108
108
|
* @template N - The type of the binary tree's nodes.
|
|
109
109
|
*/
|
|
110
110
|
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
111
|
-
implements IBinaryTree<V, N>
|
|
111
|
+
implements IBinaryTree<V, N>
|
|
112
|
+
{
|
|
112
113
|
iterationType: IterationType = IterationType.ITERATIVE;
|
|
113
114
|
|
|
114
115
|
/**
|
|
@@ -140,8 +141,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
140
141
|
return this._size;
|
|
141
142
|
}
|
|
142
143
|
|
|
143
|
-
protected defaultOneParamCallback = (node: N) => node.key;
|
|
144
|
-
|
|
145
144
|
/**
|
|
146
145
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
147
146
|
* @param {BTNKey} key - The key for the new node.
|
|
@@ -390,7 +389,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
390
389
|
return -1;
|
|
391
390
|
}
|
|
392
391
|
|
|
393
|
-
const stack: {
|
|
392
|
+
const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
|
|
394
393
|
let maxHeight = 0;
|
|
395
394
|
|
|
396
395
|
while (stack.length > 0) {
|
|
@@ -850,21 +849,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
850
849
|
beginRoot?: BTNKey | N | null,
|
|
851
850
|
iterationType?: IterationType,
|
|
852
851
|
includeNull?: false
|
|
853
|
-
): ReturnType<C>[]
|
|
852
|
+
): ReturnType<C>[];
|
|
854
853
|
|
|
855
854
|
subTreeTraverse<C extends BTNCallback<N>>(
|
|
856
855
|
callback?: C,
|
|
857
856
|
beginRoot?: BTNKey | N | null,
|
|
858
857
|
iterationType?: IterationType,
|
|
859
858
|
includeNull?: undefined
|
|
860
|
-
): ReturnType<C>[]
|
|
859
|
+
): ReturnType<C>[];
|
|
861
860
|
|
|
862
861
|
subTreeTraverse<C extends BTNCallback<N | null>>(
|
|
863
862
|
callback?: C,
|
|
864
863
|
beginRoot?: BTNKey | N | null,
|
|
865
864
|
iterationType?: IterationType,
|
|
866
865
|
includeNull?: true
|
|
867
|
-
): ReturnType<C>[]
|
|
866
|
+
): ReturnType<C>[];
|
|
868
867
|
|
|
869
868
|
/**
|
|
870
869
|
* The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
|
|
@@ -908,7 +907,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
908
907
|
|
|
909
908
|
_traverse(beginRoot);
|
|
910
909
|
} else {
|
|
911
|
-
const stack: (N| null)[] = [beginRoot];
|
|
910
|
+
const stack: (N | null)[] = [beginRoot];
|
|
912
911
|
|
|
913
912
|
while (stack.length > 0) {
|
|
914
913
|
const cur = stack.pop();
|
|
@@ -933,7 +932,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
933
932
|
beginRoot?: N | null,
|
|
934
933
|
iterationType?: IterationType,
|
|
935
934
|
includeNull?: false
|
|
936
|
-
): ReturnType<C>[]
|
|
935
|
+
): ReturnType<C>[];
|
|
937
936
|
|
|
938
937
|
dfs<C extends BTNCallback<N>>(
|
|
939
938
|
callback?: C,
|
|
@@ -941,7 +940,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
941
940
|
beginRoot?: N | null,
|
|
942
941
|
iterationType?: IterationType,
|
|
943
942
|
includeNull?: undefined
|
|
944
|
-
): ReturnType<C>[]
|
|
943
|
+
): ReturnType<C>[];
|
|
945
944
|
|
|
946
945
|
dfs<C extends BTNCallback<N | null>>(
|
|
947
946
|
callback?: C,
|
|
@@ -949,7 +948,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
949
948
|
beginRoot?: N | null,
|
|
950
949
|
iterationType?: IterationType,
|
|
951
950
|
includeNull?: true
|
|
952
|
-
): ReturnType<C>[]
|
|
951
|
+
): ReturnType<C>[];
|
|
953
952
|
|
|
954
953
|
/**
|
|
955
954
|
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
@@ -1019,7 +1018,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1019
1018
|
_traverse(beginRoot);
|
|
1020
1019
|
} else {
|
|
1021
1020
|
// 0: visit, 1: print
|
|
1022
|
-
const stack: {
|
|
1021
|
+
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
|
|
1023
1022
|
|
|
1024
1023
|
while (stack.length > 0) {
|
|
1025
1024
|
const cur = stack.pop();
|
|
@@ -1066,21 +1065,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1066
1065
|
beginRoot?: N | null,
|
|
1067
1066
|
iterationType?: IterationType,
|
|
1068
1067
|
includeNull?: false
|
|
1069
|
-
): ReturnType<C>[]
|
|
1068
|
+
): ReturnType<C>[];
|
|
1070
1069
|
|
|
1071
1070
|
bfs<C extends BTNCallback<N>>(
|
|
1072
1071
|
callback?: C,
|
|
1073
1072
|
beginRoot?: N | null,
|
|
1074
1073
|
iterationType?: IterationType,
|
|
1075
1074
|
includeNull?: undefined
|
|
1076
|
-
): ReturnType<C>[]
|
|
1075
|
+
): ReturnType<C>[];
|
|
1077
1076
|
|
|
1078
1077
|
bfs<C extends BTNCallback<N | null>>(
|
|
1079
1078
|
callback?: C,
|
|
1080
1079
|
beginRoot?: N | null,
|
|
1081
1080
|
iterationType?: IterationType,
|
|
1082
1081
|
includeNull?: true
|
|
1083
|
-
): ReturnType<C>[]
|
|
1082
|
+
): ReturnType<C>[];
|
|
1084
1083
|
|
|
1085
1084
|
/**
|
|
1086
1085
|
* The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
|
|
@@ -1123,7 +1122,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1123
1122
|
if (current.right) queue.push(current.right);
|
|
1124
1123
|
}
|
|
1125
1124
|
|
|
1126
|
-
|
|
1127
1125
|
traverse(level + 1);
|
|
1128
1126
|
};
|
|
1129
1127
|
|
|
@@ -1144,7 +1142,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1144
1142
|
if (current.left) queue.push(current.left);
|
|
1145
1143
|
if (current.right) queue.push(current.right);
|
|
1146
1144
|
}
|
|
1147
|
-
|
|
1148
1145
|
}
|
|
1149
1146
|
}
|
|
1150
1147
|
}
|
|
@@ -1152,25 +1149,25 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1152
1149
|
}
|
|
1153
1150
|
|
|
1154
1151
|
listLevels<C extends BTNCallback<N>>(
|
|
1155
|
-
callback?: C
|
|
1156
|
-
beginRoot?: N | null
|
|
1152
|
+
callback?: C,
|
|
1153
|
+
beginRoot?: N | null,
|
|
1157
1154
|
iterationType?: IterationType,
|
|
1158
1155
|
includeNull?: false
|
|
1159
|
-
): ReturnType<C>[][]
|
|
1156
|
+
): ReturnType<C>[][];
|
|
1160
1157
|
|
|
1161
1158
|
listLevels<C extends BTNCallback<N>>(
|
|
1162
|
-
callback?: C
|
|
1163
|
-
beginRoot?: N | null
|
|
1159
|
+
callback?: C,
|
|
1160
|
+
beginRoot?: N | null,
|
|
1164
1161
|
iterationType?: IterationType,
|
|
1165
1162
|
includeNull?: undefined
|
|
1166
|
-
): ReturnType<C>[][]
|
|
1163
|
+
): ReturnType<C>[][];
|
|
1167
1164
|
|
|
1168
1165
|
listLevels<C extends BTNCallback<N | null>>(
|
|
1169
|
-
callback?: C
|
|
1170
|
-
beginRoot?: N | null
|
|
1166
|
+
callback?: C,
|
|
1167
|
+
beginRoot?: N | null,
|
|
1171
1168
|
iterationType?: IterationType,
|
|
1172
1169
|
includeNull?: true
|
|
1173
|
-
): ReturnType<C>[][]
|
|
1170
|
+
): ReturnType<C>[][];
|
|
1174
1171
|
|
|
1175
1172
|
/**
|
|
1176
1173
|
* The `listLevels` function takes a binary tree node and a callback function, and returns an array
|
|
@@ -1204,7 +1201,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1204
1201
|
if (includeNull) {
|
|
1205
1202
|
if (node && node.left !== undefined) _recursive(node.left, level + 1);
|
|
1206
1203
|
if (node && node.right !== undefined) _recursive(node.right, level + 1);
|
|
1207
|
-
} else
|
|
1204
|
+
} else {
|
|
1208
1205
|
if (node && node.left) _recursive(node.left, level + 1);
|
|
1209
1206
|
if (node && node.right) _recursive(node.right, level + 1);
|
|
1210
1207
|
}
|
|
@@ -1224,7 +1221,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1224
1221
|
if (includeNull) {
|
|
1225
1222
|
if (node && node.right !== undefined) stack.push([node.right, level + 1]);
|
|
1226
1223
|
if (node && node.left !== undefined) stack.push([node.left, level + 1]);
|
|
1227
|
-
} else
|
|
1224
|
+
} else {
|
|
1228
1225
|
if (node && node.right) stack.push([node.right, level + 1]);
|
|
1229
1226
|
if (node && node.left) stack.push([node.left, level + 1]);
|
|
1230
1227
|
}
|
|
@@ -1273,8 +1270,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1273
1270
|
return y;
|
|
1274
1271
|
}
|
|
1275
1272
|
|
|
1276
|
-
// --- start additional methods ---
|
|
1277
|
-
|
|
1278
1273
|
/**
|
|
1279
1274
|
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
1280
1275
|
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
@@ -1374,6 +1369,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1374
1369
|
return ans;
|
|
1375
1370
|
}
|
|
1376
1371
|
|
|
1372
|
+
// --- start additional methods ---
|
|
1373
|
+
|
|
1377
1374
|
/**
|
|
1378
1375
|
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1379
1376
|
* either an iterative or recursive manner.
|
|
@@ -1383,7 +1380,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1383
1380
|
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1384
1381
|
* binary tree nodes in a specific order.
|
|
1385
1382
|
*/
|
|
1386
|
-
*
|
|
1383
|
+
*[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
|
|
1387
1384
|
if (!node) {
|
|
1388
1385
|
return;
|
|
1389
1386
|
}
|
|
@@ -1416,6 +1413,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1416
1413
|
}
|
|
1417
1414
|
}
|
|
1418
1415
|
|
|
1416
|
+
protected defaultOneParamCallback = (node: N) => node.key;
|
|
1417
|
+
|
|
1419
1418
|
/**
|
|
1420
1419
|
* Swap the data of two nodes in the binary tree.
|
|
1421
1420
|
* @param {N} srcNode - The source node to swap.
|
|
@@ -64,7 +64,8 @@ export abstract class AbstractGraph<
|
|
|
64
64
|
E = any,
|
|
65
65
|
VO extends AbstractVertex<V> = AbstractVertex<V>,
|
|
66
66
|
EO extends AbstractEdge<E> = AbstractEdge<E>
|
|
67
|
-
> implements IGraph<V, E, VO, EO>
|
|
67
|
+
> implements IGraph<V, E, VO, EO>
|
|
68
|
+
{
|
|
68
69
|
protected _vertices: Map<VertexKey, VO> = new Map<VertexKey, VO>();
|
|
69
70
|
|
|
70
71
|
get vertices(): Map<VertexKey, VO> {
|
|
@@ -234,7 +235,7 @@ export abstract class AbstractGraph<
|
|
|
234
235
|
return [];
|
|
235
236
|
}
|
|
236
237
|
|
|
237
|
-
const stack: {
|
|
238
|
+
const stack: {vertex: VO; path: VO[]}[] = [];
|
|
238
239
|
stack.push({vertex: vertex1, path: [vertex1]});
|
|
239
240
|
|
|
240
241
|
while (stack.length > 0) {
|
|
@@ -256,7 +257,6 @@ export abstract class AbstractGraph<
|
|
|
256
257
|
return paths;
|
|
257
258
|
}
|
|
258
259
|
|
|
259
|
-
|
|
260
260
|
/**
|
|
261
261
|
* The function calculates the sum of weights along a given path.
|
|
262
262
|
* @param {VO[]} path - An array of vertices (VO) representing a path in a graph.
|
|
@@ -366,7 +366,6 @@ export abstract class AbstractGraph<
|
|
|
366
366
|
} else {
|
|
367
367
|
return this.dijkstra(v1, v2, true, true)?.minPath ?? [];
|
|
368
368
|
}
|
|
369
|
-
|
|
370
369
|
} else {
|
|
371
370
|
// DFS
|
|
372
371
|
let minPath: VO[] = [];
|
|
@@ -519,14 +518,14 @@ export abstract class AbstractGraph<
|
|
|
519
518
|
}
|
|
520
519
|
|
|
521
520
|
getMinDist &&
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
521
|
+
distMap.forEach((d, v) => {
|
|
522
|
+
if (v !== srcVertex) {
|
|
523
|
+
if (d < minDist) {
|
|
524
|
+
minDist = d;
|
|
525
|
+
if (genPaths) minDest = v;
|
|
526
|
+
}
|
|
527
527
|
}
|
|
528
|
-
}
|
|
529
|
-
});
|
|
528
|
+
});
|
|
530
529
|
|
|
531
530
|
genPaths && getPaths(minDest);
|
|
532
531
|
|
|
@@ -588,7 +587,7 @@ export abstract class AbstractGraph<
|
|
|
588
587
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
589
588
|
}
|
|
590
589
|
|
|
591
|
-
const heap = new PriorityQueue<{
|
|
590
|
+
const heap = new PriorityQueue<{key: number; value: VO}>({comparator: (a, b) => a.key - b.key});
|
|
592
591
|
heap.add({key: 0, value: srcVertex});
|
|
593
592
|
|
|
594
593
|
distMap.set(srcVertex, 0);
|
|
@@ -812,7 +811,7 @@ export abstract class AbstractGraph<
|
|
|
812
811
|
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
|
813
812
|
* path between vertices in the
|
|
814
813
|
*/
|
|
815
|
-
floydWarshall(): {
|
|
814
|
+
floydWarshall(): {costs: number[][]; predecessor: (VO | null)[][]} {
|
|
816
815
|
const idAndVertices = [...this._vertices];
|
|
817
816
|
const n = idAndVertices.length;
|
|
818
817
|
|
|
@@ -876,7 +875,12 @@ export abstract class AbstractGraph<
|
|
|
876
875
|
* are arrays of vertices that form cycles within the SCCs.
|
|
877
876
|
* @returns The function `tarjan` returns an object with the following properties:
|
|
878
877
|
*/
|
|
879
|
-
tarjan(
|
|
878
|
+
tarjan(
|
|
879
|
+
needCutVertexes: boolean = false,
|
|
880
|
+
needBridges: boolean = false,
|
|
881
|
+
needSCCs: boolean = true,
|
|
882
|
+
needCycles: boolean = false
|
|
883
|
+
) {
|
|
880
884
|
// !! in undirected graph we will not let child visit parent when dfs
|
|
881
885
|
// !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
|
|
882
886
|
// !! bridge: low(child) > dfn(cur)
|