min-heap-typed 1.42.2 → 1.42.4
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/binary-tree/avl-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/avl-tree.js +5 -3
- package/dist/data-structures/binary-tree/binary-tree.d.ts +57 -53
- package/dist/data-structures/binary-tree/binary-tree.js +116 -54
- package/dist/data-structures/binary-tree/bst.d.ts +42 -15
- package/dist/data-structures/binary-tree/bst.js +77 -21
- package/dist/data-structures/binary-tree/rb-tree.d.ts +28 -51
- package/dist/data-structures/binary-tree/rb-tree.js +148 -180
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +10 -10
- package/dist/data-structures/binary-tree/tree-multiset.js +20 -17
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/rb-tree.js +0 -5
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +5 -4
- package/src/data-structures/binary-tree/binary-tree.ts +227 -158
- package/src/data-structures/binary-tree/bst.ts +100 -34
- package/src/data-structures/binary-tree/rb-tree.ts +227 -236
- package/src/data-structures/binary-tree/tree-multiset.ts +24 -23
- package/src/data-structures/graph/abstract-graph.ts +18 -14
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +5 -5
|
@@ -71,23 +71,24 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
71
71
|
/**
|
|
72
72
|
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
73
73
|
* exists, and balancing the tree if necessary.
|
|
74
|
-
* @param {BTNKey | N |
|
|
74
|
+
* @param {BTNKey | N | undefined} keyOrNode - The `keyOrNode` parameter can be either a
|
|
75
75
|
* `BTNKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
76
|
-
* node to be added), or `
|
|
76
|
+
* node to be added), or `undefined` (which represents a undefined node).
|
|
77
77
|
* @param [value] - The `value` parameter represents the value associated with the key that is being
|
|
78
78
|
* added to the binary tree.
|
|
79
79
|
* @param [count=1] - The `count` parameter represents the number of occurrences of the key/value
|
|
80
80
|
* pair that will be added to the binary tree. It has a default value of 1, which means that if no
|
|
81
81
|
* count is specified, the default count will be 1.
|
|
82
|
-
* @returns The function `add` returns a value of type `N |
|
|
82
|
+
* @returns The function `add` returns a value of type `N | undefined | undefined`.
|
|
83
83
|
*/
|
|
84
|
-
override add(keyOrNode: BTNKey | N | null, value?: V, count = 1): N |
|
|
85
|
-
|
|
86
|
-
|
|
84
|
+
override add(keyOrNode: BTNKey | N | null | undefined, value?: V, count = 1): N | undefined {
|
|
85
|
+
if(keyOrNode === null) return undefined;
|
|
86
|
+
let inserted: N | undefined = undefined,
|
|
87
|
+
newNode: N | undefined;
|
|
87
88
|
if (keyOrNode instanceof TreeMultisetNode) {
|
|
88
89
|
newNode = this.createNode(keyOrNode.key, keyOrNode.value, keyOrNode.count);
|
|
89
|
-
} else if (keyOrNode ===
|
|
90
|
-
newNode =
|
|
90
|
+
} else if (keyOrNode === undefined) {
|
|
91
|
+
newNode = undefined;
|
|
91
92
|
} else {
|
|
92
93
|
newNode = this.createNode(keyOrNode, value, count);
|
|
93
94
|
}
|
|
@@ -138,7 +139,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
138
139
|
}
|
|
139
140
|
}
|
|
140
141
|
} else {
|
|
141
|
-
// TODO may need to support
|
|
142
|
+
// TODO may need to support undefined inserted
|
|
142
143
|
}
|
|
143
144
|
} else {
|
|
144
145
|
traversing = false;
|
|
@@ -151,17 +152,17 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
151
152
|
|
|
152
153
|
/**
|
|
153
154
|
* The function adds a new node to a binary tree if there is an available slot in the parent node.
|
|
154
|
-
* @param {N |
|
|
155
|
-
* the tree. It can be either a node object (`N`) or `
|
|
155
|
+
* @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be added to
|
|
156
|
+
* the tree. It can be either a node object (`N`) or `undefined`.
|
|
156
157
|
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will
|
|
157
158
|
* be added as a child.
|
|
158
159
|
* @returns The method `_addTo` returns either the `parent.left`, `parent.right`, or `undefined`.
|
|
159
160
|
*/
|
|
160
|
-
override _addTo(newNode: N |
|
|
161
|
+
override _addTo(newNode: N | undefined, parent: N): N | undefined {
|
|
161
162
|
if (parent) {
|
|
162
163
|
if (parent.left === undefined) {
|
|
163
164
|
parent.left = newNode;
|
|
164
|
-
if (newNode !==
|
|
165
|
+
if (newNode !== undefined) {
|
|
165
166
|
this._size = this.size + 1;
|
|
166
167
|
this._setCount(this.count + newNode.count);
|
|
167
168
|
}
|
|
@@ -169,7 +170,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
169
170
|
return parent.left;
|
|
170
171
|
} else if (parent.right === undefined) {
|
|
171
172
|
parent.right = newNode;
|
|
172
|
-
if (newNode !==
|
|
173
|
+
if (newNode !== undefined) {
|
|
173
174
|
this._size = this.size + 1;
|
|
174
175
|
this._setCount(this.count + newNode.count);
|
|
175
176
|
}
|
|
@@ -185,15 +186,15 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
185
186
|
/**
|
|
186
187
|
* The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
|
|
187
188
|
* inserted nodes.
|
|
188
|
-
* @param {(BTNKey |
|
|
189
|
+
* @param {(BTNKey | undefined)[] | (N | undefined)[]} keysOrNodes - An array of keys or nodes to be
|
|
189
190
|
* added to the multiset. Each element can be either a BTNKey or a TreeMultisetNode.
|
|
190
191
|
* @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
|
|
191
192
|
* to the keys or nodes being added to the multiset. It is used to associate additional data with
|
|
192
193
|
* each key or node.
|
|
193
|
-
* @returns The function `addMany` returns an array of `N`, `
|
|
194
|
+
* @returns The function `addMany` returns an array of `N`, `undefined`, or `undefined` values.
|
|
194
195
|
*/
|
|
195
|
-
override addMany(keysOrNodes: (BTNKey |
|
|
196
|
-
const inserted: (N |
|
|
196
|
+
override addMany(keysOrNodes: (BTNKey | undefined)[] | (N | undefined)[], data?: V[]): (N | undefined)[] {
|
|
197
|
+
const inserted: (N | undefined | undefined)[] = [];
|
|
197
198
|
|
|
198
199
|
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
199
200
|
const keyOrNode = keysOrNodes[i];
|
|
@@ -203,7 +204,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
203
204
|
continue;
|
|
204
205
|
}
|
|
205
206
|
|
|
206
|
-
if (keyOrNode ===
|
|
207
|
+
if (keyOrNode === undefined) {
|
|
207
208
|
inserted.push(this.add(NaN, undefined, 0));
|
|
208
209
|
continue;
|
|
209
210
|
}
|
|
@@ -283,11 +284,11 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
283
284
|
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
|
284
285
|
if (!this.root) return bstDeletedResult;
|
|
285
286
|
|
|
286
|
-
const curr: N |
|
|
287
|
+
const curr: N | undefined = this.getNode(identifier, callback) ?? undefined;
|
|
287
288
|
if (!curr) return bstDeletedResult;
|
|
288
289
|
|
|
289
|
-
const parent: N |
|
|
290
|
-
let needBalanced: N |
|
|
290
|
+
const parent: N | undefined = curr?.parent ? curr.parent : undefined;
|
|
291
|
+
let needBalanced: N | undefined = undefined,
|
|
291
292
|
orgCurrent = curr;
|
|
292
293
|
|
|
293
294
|
if (curr.count > 1 && !ignoreCount) {
|
|
@@ -307,7 +308,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
307
308
|
needBalanced = parent;
|
|
308
309
|
}
|
|
309
310
|
} else {
|
|
310
|
-
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) :
|
|
311
|
+
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : undefined;
|
|
311
312
|
if (leftSubTreeRightMost) {
|
|
312
313
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
313
314
|
orgCurrent = this._swap(curr, leftSubTreeRightMost);
|
|
@@ -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)
|
|
@@ -24,7 +24,7 @@ export enum FamilyPosition {
|
|
|
24
24
|
|
|
25
25
|
export type BTNKey = number;
|
|
26
26
|
|
|
27
|
-
export type BinaryTreeDeletedResult<N> = { deleted: N | null | undefined; needBalanced: N | null };
|
|
27
|
+
export type BinaryTreeDeletedResult<N> = { deleted: N | null | undefined; needBalanced: N | null | undefined };
|
|
28
28
|
|
|
29
29
|
export type BinaryTreeNodeNested<T> = BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
30
30
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import {RBTreeNode} from '../../../data-structures';
|
|
2
|
+
import {BSTOptions} from "./bst";
|
|
3
3
|
|
|
4
4
|
export enum RBTNColor { RED = 1, BLACK = 0}
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
export type RBTreeNodeNested<T> = RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
7
|
+
|
|
8
|
+
export type RBTreeOptions = BSTOptions & {};
|