data-structure-typed 1.38.9 → 1.39.0
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 +9 -9
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +17 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +3 -3
- package/dist/cjs/data-structures/graph/abstract-graph.js +4 -4
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +4 -4
- package/dist/cjs/data-structures/graph/directed-graph.js +6 -6
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +3 -3
- package/dist/cjs/data-structures/graph/undirected-graph.js +4 -4
- package/dist/cjs/data-structures/heap/heap.d.ts +10 -5
- package/dist/cjs/data-structures/heap/heap.js +10 -10
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +4 -1
- package/dist/cjs/data-structures/heap/max-heap.js +9 -7
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +4 -1
- package/dist/cjs/data-structures/heap/min-heap.js +9 -7
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +4 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +9 -7
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +4 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +9 -7
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +5 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +17 -0
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +3 -3
- package/dist/mjs/data-structures/graph/abstract-graph.js +4 -4
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +4 -4
- package/dist/mjs/data-structures/graph/directed-graph.js +6 -6
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +3 -3
- package/dist/mjs/data-structures/graph/undirected-graph.js +4 -4
- package/dist/mjs/data-structures/heap/heap.d.ts +10 -5
- package/dist/mjs/data-structures/heap/heap.js +10 -10
- package/dist/mjs/data-structures/heap/max-heap.d.ts +4 -1
- package/dist/mjs/data-structures/heap/max-heap.js +9 -7
- package/dist/mjs/data-structures/heap/min-heap.d.ts +4 -1
- package/dist/mjs/data-structures/heap/min-heap.js +9 -7
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +4 -1
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +9 -7
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +4 -1
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +9 -7
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +5 -2
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +20 -0
- package/src/data-structures/graph/abstract-graph.ts +5 -5
- package/src/data-structures/graph/directed-graph.ts +6 -6
- package/src/data-structures/graph/undirected-graph.ts +4 -4
- package/src/data-structures/heap/heap.ts +12 -12
- package/src/data-structures/heap/max-heap.ts +8 -6
- package/src/data-structures/heap/min-heap.ts +8 -6
- package/src/data-structures/priority-queue/max-priority-queue.ts +8 -6
- package/src/data-structures/priority-queue/min-priority-queue.ts +8 -6
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/test/integration/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +35 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/bst.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +1 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +3 -3
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +2 -2
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +3 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +10 -10
- package/test/utils/big-o.ts +1 -1
|
@@ -10,15 +10,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
10
10
|
exports.MinHeap = void 0;
|
|
11
11
|
const heap_1 = require("./heap");
|
|
12
12
|
class MinHeap extends heap_1.Heap {
|
|
13
|
-
constructor(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
constructor(options = {
|
|
14
|
+
comparator: (a, b) => {
|
|
15
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
|
+
throw new Error('The a, b params of compare function must be number');
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return a - b;
|
|
20
|
+
}
|
|
19
21
|
}
|
|
20
22
|
}) {
|
|
21
|
-
super(
|
|
23
|
+
super(options);
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
exports.MinHeap = MinHeap;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"min-heap.js","sourceRoot":"","sources":["../../../../src/data-structures/heap/min-heap.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH,iCAA4B;AAG5B,MAAa,OAAiB,SAAQ,WAAO;IAC3C,YACE,
|
|
1
|
+
{"version":3,"file":"min-heap.js","sourceRoot":"","sources":["../../../../src/data-structures/heap/min-heap.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH,iCAA4B;AAG5B,MAAa,OAAiB,SAAQ,WAAO;IAC3C,YACE,UAAsD;QACpD,UAAU,EAAE,CAAC,CAAI,EAAE,CAAI,EAAE,EAAE;YACzB,IAAI,CAAC,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,OAAO,CAAC,KAAK,QAAQ,CAAC,EAAE;gBACrD,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;aACvE;iBAAM;gBACL,OAAO,CAAC,GAAG,CAAC,CAAC;aACd;QACH,CAAC;KACF;QAED,KAAK,CAAC,OAAO,CAAC,CAAC;IACjB,CAAC;CACF;AAdD,0BAcC"}
|
|
@@ -8,5 +8,8 @@
|
|
|
8
8
|
import { PriorityQueue } from './priority-queue';
|
|
9
9
|
import type { Comparator } from '../../types';
|
|
10
10
|
export declare class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
11
|
-
constructor(
|
|
11
|
+
constructor(options?: {
|
|
12
|
+
comparator: Comparator<E>;
|
|
13
|
+
nodes?: E[];
|
|
14
|
+
});
|
|
12
15
|
}
|
|
@@ -10,15 +10,17 @@ exports.MaxPriorityQueue = void 0;
|
|
|
10
10
|
*/
|
|
11
11
|
const priority_queue_1 = require("./priority-queue");
|
|
12
12
|
class MaxPriorityQueue extends priority_queue_1.PriorityQueue {
|
|
13
|
-
constructor(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
constructor(options = {
|
|
14
|
+
comparator: (a, b) => {
|
|
15
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
|
+
throw new Error('The a, b params of compare function must be number');
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return b - a;
|
|
20
|
+
}
|
|
19
21
|
}
|
|
20
22
|
}) {
|
|
21
|
-
super(
|
|
23
|
+
super(options);
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
exports.MaxPriorityQueue = MaxPriorityQueue;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"max-priority-queue.js","sourceRoot":"","sources":["../../../../src/data-structures/priority-queue/max-priority-queue.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,qDAA+C;AAG/C,MAAa,gBAA0B,SAAQ,8BAAgB;IAC7D,YACE,
|
|
1
|
+
{"version":3,"file":"max-priority-queue.js","sourceRoot":"","sources":["../../../../src/data-structures/priority-queue/max-priority-queue.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,qDAA+C;AAG/C,MAAa,gBAA0B,SAAQ,8BAAgB;IAC7D,YACE,UAAsD;QACpD,UAAU,EAAE,CAAC,CAAI,EAAE,CAAI,EAAE,EAAE;YACzB,IAAI,CAAC,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,OAAO,CAAC,KAAK,QAAQ,CAAC,EAAE;gBACrD,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;aACvE;iBAAM;gBACL,OAAO,CAAC,GAAG,CAAC,CAAC;aACd;QACH,CAAC;KACF;QAED,KAAK,CAAC,OAAO,CAAC,CAAC;IACjB,CAAC;CACF;AAdD,4CAcC"}
|
|
@@ -8,5 +8,8 @@
|
|
|
8
8
|
import { PriorityQueue } from './priority-queue';
|
|
9
9
|
import type { Comparator } from '../../types';
|
|
10
10
|
export declare class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
11
|
-
constructor(
|
|
11
|
+
constructor(options?: {
|
|
12
|
+
comparator: Comparator<E>;
|
|
13
|
+
nodes?: E[];
|
|
14
|
+
});
|
|
12
15
|
}
|
|
@@ -10,15 +10,17 @@ exports.MinPriorityQueue = void 0;
|
|
|
10
10
|
*/
|
|
11
11
|
const priority_queue_1 = require("./priority-queue");
|
|
12
12
|
class MinPriorityQueue extends priority_queue_1.PriorityQueue {
|
|
13
|
-
constructor(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
constructor(options = {
|
|
14
|
+
comparator: (a, b) => {
|
|
15
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
|
+
throw new Error('The a, b params of compare function must be number');
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return a - b;
|
|
20
|
+
}
|
|
19
21
|
}
|
|
20
22
|
}) {
|
|
21
|
-
super(
|
|
23
|
+
super(options);
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
exports.MinPriorityQueue = MinPriorityQueue;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"min-priority-queue.js","sourceRoot":"","sources":["../../../../src/data-structures/priority-queue/min-priority-queue.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,qDAA+C;AAG/C,MAAa,gBAA0B,SAAQ,8BAAgB;IAC7D,YACE,
|
|
1
|
+
{"version":3,"file":"min-priority-queue.js","sourceRoot":"","sources":["../../../../src/data-structures/priority-queue/min-priority-queue.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,qDAA+C;AAG/C,MAAa,gBAA0B,SAAQ,8BAAgB;IAC7D,YACE,UAAsD;QACpD,UAAU,EAAE,CAAC,CAAI,EAAE,CAAI,EAAE,EAAE;YACzB,IAAI,CAAC,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,OAAO,CAAC,KAAK,QAAQ,CAAC,EAAE;gBACrD,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;aACvE;iBAAM;gBACL,OAAO,CAAC,GAAG,CAAC,CAAC;aACd;QACH,CAAC;KACF;QAED,KAAK,CAAC,OAAO,CAAC,CAAC;IACjB,CAAC;CACF;AAdD,4CAcC"}
|
|
@@ -7,6 +7,9 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { Heap } from '../heap';
|
|
9
9
|
import { Comparator } from '../../types';
|
|
10
|
-
export declare class PriorityQueue<E> extends Heap<E> {
|
|
11
|
-
constructor(
|
|
10
|
+
export declare class PriorityQueue<E = any> extends Heap<E> {
|
|
11
|
+
constructor(options: {
|
|
12
|
+
comparator: Comparator<E>;
|
|
13
|
+
nodes?: E[];
|
|
14
|
+
});
|
|
12
15
|
}
|
|
@@ -10,8 +10,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
10
10
|
exports.PriorityQueue = void 0;
|
|
11
11
|
const heap_1 = require("../heap");
|
|
12
12
|
class PriorityQueue extends heap_1.Heap {
|
|
13
|
-
constructor(
|
|
14
|
-
super(
|
|
13
|
+
constructor(options) {
|
|
14
|
+
super(options);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
exports.PriorityQueue = PriorityQueue;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"priority-queue.js","sourceRoot":"","sources":["../../../../src/data-structures/priority-queue/priority-queue.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH,kCAA6B;AAG7B,MAAa,
|
|
1
|
+
{"version":3,"file":"priority-queue.js","sourceRoot":"","sources":["../../../../src/data-structures/priority-queue/priority-queue.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH,kCAA6B;AAG7B,MAAa,aAAuB,SAAQ,WAAO;IACjD,YAAY,OAAmD;QAC7D,KAAK,CAAC,OAAO,CAAC,CAAC;IACjB,CAAC;CACF;AAJD,sCAIC"}
|
|
@@ -69,6 +69,14 @@ export declare class BinaryIndexedTree {
|
|
|
69
69
|
* @returns The upperBound function is returning a number.
|
|
70
70
|
*/
|
|
71
71
|
upperBound(sum: number): number;
|
|
72
|
+
/**
|
|
73
|
+
* The function calculates the prefix sum of an array using a binary indexed tree.
|
|
74
|
+
* @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
|
|
75
|
+
* array for which we want to calculate the prefix sum.
|
|
76
|
+
* @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
|
|
77
|
+
* `i`.
|
|
78
|
+
*/
|
|
79
|
+
getPrefixSum(i: number): number;
|
|
72
80
|
/**
|
|
73
81
|
* The function returns the value of a specific index in a freqMap data structure, or a default value if
|
|
74
82
|
* the index is not found.
|
|
@@ -125,6 +125,23 @@ class BinaryIndexedTree {
|
|
|
125
125
|
}
|
|
126
126
|
return this._binarySearch(sum, (x, y) => x <= y);
|
|
127
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* The function calculates the prefix sum of an array using a binary indexed tree.
|
|
130
|
+
* @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
|
|
131
|
+
* array for which we want to calculate the prefix sum.
|
|
132
|
+
* @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
|
|
133
|
+
* `i`.
|
|
134
|
+
*/
|
|
135
|
+
getPrefixSum(i) {
|
|
136
|
+
this._checkIndex(i);
|
|
137
|
+
i++; // Convert to 1-based index
|
|
138
|
+
let sum = 0;
|
|
139
|
+
while (i > 0) {
|
|
140
|
+
sum += this._getFrequency(i);
|
|
141
|
+
i -= i & -i;
|
|
142
|
+
}
|
|
143
|
+
return sum;
|
|
144
|
+
}
|
|
128
145
|
/**
|
|
129
146
|
* The function returns the value of a specific index in a freqMap data structure, or a default value if
|
|
130
147
|
* the index is not found.
|
|
@@ -65,7 +65,7 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
65
65
|
* @param val
|
|
66
66
|
*/
|
|
67
67
|
abstract createEdge(srcOrV1: VertexKey | string, destOrV2: VertexKey | string, weight?: number, val?: E): E;
|
|
68
|
-
abstract
|
|
68
|
+
abstract deleteEdge(edge: E): E | null;
|
|
69
69
|
abstract getEdge(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null;
|
|
70
70
|
abstract degreeOf(vertexOrKey: V | VertexKey): number;
|
|
71
71
|
abstract edgeSet(): E[];
|
|
@@ -90,12 +90,12 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
90
90
|
addVertex(vertex: V): boolean;
|
|
91
91
|
addVertex(key: VertexKey, val?: V['val']): boolean;
|
|
92
92
|
/**
|
|
93
|
-
* The `
|
|
93
|
+
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
94
94
|
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
|
|
95
95
|
* (`VertexKey`).
|
|
96
96
|
* @returns The method is returning a boolean value.
|
|
97
97
|
*/
|
|
98
|
-
|
|
98
|
+
deleteVertex(vertexOrKey: V | VertexKey): boolean;
|
|
99
99
|
/**
|
|
100
100
|
* The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
|
|
101
101
|
* @param {V[] | VertexKey[]} vertices - The `vertices` parameter can be either an array of vertices (`V[]`) or an array
|
|
@@ -120,12 +120,12 @@ class AbstractGraph {
|
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
122
|
/**
|
|
123
|
-
* The `
|
|
123
|
+
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
124
124
|
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
|
|
125
125
|
* (`VertexKey`).
|
|
126
126
|
* @returns The method is returning a boolean value.
|
|
127
127
|
*/
|
|
128
|
-
|
|
128
|
+
deleteVertex(vertexOrKey) {
|
|
129
129
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
130
130
|
return this._vertices.delete(vertexKey);
|
|
131
131
|
}
|
|
@@ -139,7 +139,7 @@ class AbstractGraph {
|
|
|
139
139
|
removeAllVertices(vertices) {
|
|
140
140
|
const removed = [];
|
|
141
141
|
for (const v of vertices) {
|
|
142
|
-
removed.push(this.
|
|
142
|
+
removed.push(this.deleteVertex(v));
|
|
143
143
|
}
|
|
144
144
|
return removed.length > 0;
|
|
145
145
|
}
|
|
@@ -530,7 +530,7 @@ class AbstractGraph {
|
|
|
530
530
|
if (vertexOrKey instanceof AbstractVertex)
|
|
531
531
|
distMap.set(vertexOrKey, Infinity);
|
|
532
532
|
}
|
|
533
|
-
const heap = new priority_queue_1.PriorityQueue((a, b) => a.key - b.key);
|
|
533
|
+
const heap = new priority_queue_1.PriorityQueue({ comparator: (a, b) => a.key - b.key });
|
|
534
534
|
heap.add({ key: 0, val: srcVertex });
|
|
535
535
|
distMap.set(srcVertex, 0);
|
|
536
536
|
preMap.set(srcVertex, null);
|
|
@@ -84,14 +84,14 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
84
84
|
* @param {V | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
|
|
85
85
|
* @returns the removed edge (E) if it exists, or null if either the source or destination vertex does not exist.
|
|
86
86
|
*/
|
|
87
|
-
|
|
87
|
+
deleteEdgeSrcToDest(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null;
|
|
88
88
|
/**
|
|
89
89
|
* The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
|
|
90
90
|
* @param {E} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
|
|
91
91
|
* and `dest`, which represent the source and destination vertices of the edge, respectively.
|
|
92
|
-
* @returns The method `
|
|
92
|
+
* @returns The method `deleteEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
|
|
93
93
|
*/
|
|
94
|
-
|
|
94
|
+
deleteEdge(edge: E): E | null;
|
|
95
95
|
/**
|
|
96
96
|
* The function removes edges between two vertices and returns the removed edges.
|
|
97
97
|
* @param {VertexKey | V} v1 - The parameter `v1` can be either a `VertexKey` or a `V`. A `VertexKey` represents the
|
|
@@ -100,7 +100,7 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
100
100
|
* the second vertex in the edge that needs to be removed.
|
|
101
101
|
* @returns an array of removed edges (E[]).
|
|
102
102
|
*/
|
|
103
|
-
|
|
103
|
+
deleteEdgesBetween(v1: VertexKey | V, v2: VertexKey | V): E[];
|
|
104
104
|
/**
|
|
105
105
|
* The function `incomingEdgesOf` returns an array of incoming edges for a given vertex or vertex ID.
|
|
106
106
|
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
|
|
@@ -132,7 +132,7 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
132
132
|
* @param {V | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
|
|
133
133
|
* @returns the removed edge (E) if it exists, or null if either the source or destination vertex does not exist.
|
|
134
134
|
*/
|
|
135
|
-
|
|
135
|
+
deleteEdgeSrcToDest(srcOrKey, destOrKey) {
|
|
136
136
|
const src = this._getVertex(srcOrKey);
|
|
137
137
|
const dest = this._getVertex(destOrKey);
|
|
138
138
|
let removed = null;
|
|
@@ -153,9 +153,9 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
153
153
|
* The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
|
|
154
154
|
* @param {E} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
|
|
155
155
|
* and `dest`, which represent the source and destination vertices of the edge, respectively.
|
|
156
|
-
* @returns The method `
|
|
156
|
+
* @returns The method `deleteEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
|
|
157
157
|
*/
|
|
158
|
-
|
|
158
|
+
deleteEdge(edge) {
|
|
159
159
|
let removed = null;
|
|
160
160
|
const src = this._getVertex(edge.src);
|
|
161
161
|
const dest = this._getVertex(edge.dest);
|
|
@@ -179,11 +179,11 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
179
179
|
* the second vertex in the edge that needs to be removed.
|
|
180
180
|
* @returns an array of removed edges (E[]).
|
|
181
181
|
*/
|
|
182
|
-
|
|
182
|
+
deleteEdgesBetween(v1, v2) {
|
|
183
183
|
const removed = [];
|
|
184
184
|
if (v1 && v2) {
|
|
185
|
-
const v1ToV2 = this.
|
|
186
|
-
const v2ToV1 = this.
|
|
185
|
+
const v1ToV2 = this.deleteEdgeSrcToDest(v1, v2);
|
|
186
|
+
const v2ToV1 = this.deleteEdgeSrcToDest(v2, v1);
|
|
187
187
|
v1ToV2 && removed.push(v1ToV2);
|
|
188
188
|
v2ToV1 && removed.push(v2ToV1);
|
|
189
189
|
}
|
|
@@ -71,13 +71,13 @@ export declare class UndirectedGraph<V extends UndirectedVertex<any> = Undirecte
|
|
|
71
71
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
72
72
|
* @returns the removed edge (E) if it exists, or null if either of the vertices (V) does not exist.
|
|
73
73
|
*/
|
|
74
|
-
|
|
74
|
+
deleteEdgeBetween(v1: V | VertexKey, v2: V | VertexKey): E | null;
|
|
75
75
|
/**
|
|
76
|
-
* The
|
|
76
|
+
* The deleteEdge function removes an edge between two vertices in a graph.
|
|
77
77
|
* @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
|
|
78
78
|
* @returns The method is returning either the removed edge (of type E) or null if the edge was not found.
|
|
79
79
|
*/
|
|
80
|
-
|
|
80
|
+
deleteEdge(edge: E): E | null;
|
|
81
81
|
/**
|
|
82
82
|
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edges connected to that
|
|
83
83
|
* vertex.
|
|
@@ -110,7 +110,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
110
110
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
111
111
|
* @returns the removed edge (E) if it exists, or null if either of the vertices (V) does not exist.
|
|
112
112
|
*/
|
|
113
|
-
|
|
113
|
+
deleteEdgeBetween(v1, v2) {
|
|
114
114
|
const vertex1 = this._getVertex(v1);
|
|
115
115
|
const vertex2 = this._getVertex(v2);
|
|
116
116
|
if (!vertex1 || !vertex2) {
|
|
@@ -128,12 +128,12 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
128
128
|
return removed;
|
|
129
129
|
}
|
|
130
130
|
/**
|
|
131
|
-
* The
|
|
131
|
+
* The deleteEdge function removes an edge between two vertices in a graph.
|
|
132
132
|
* @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
|
|
133
133
|
* @returns The method is returning either the removed edge (of type E) or null if the edge was not found.
|
|
134
134
|
*/
|
|
135
|
-
|
|
136
|
-
return this.
|
|
135
|
+
deleteEdge(edge) {
|
|
136
|
+
return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
|
|
137
137
|
}
|
|
138
138
|
/**
|
|
139
139
|
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edges connected to that
|
|
@@ -5,10 +5,13 @@
|
|
|
5
5
|
* @license MIT License
|
|
6
6
|
*/
|
|
7
7
|
import type { Comparator, DFSOrderPattern } from '../../types';
|
|
8
|
-
export declare class Heap<E> {
|
|
8
|
+
export declare class Heap<E = any> {
|
|
9
9
|
protected nodes: E[];
|
|
10
10
|
protected readonly comparator: Comparator<E>;
|
|
11
|
-
constructor(
|
|
11
|
+
constructor(options: {
|
|
12
|
+
comparator: Comparator<E>;
|
|
13
|
+
nodes?: E[];
|
|
14
|
+
});
|
|
12
15
|
/**
|
|
13
16
|
* Get the size (number of elements) of the heap.
|
|
14
17
|
*/
|
|
@@ -20,11 +23,13 @@ export declare class Heap<E> {
|
|
|
20
23
|
get leaf(): E | undefined;
|
|
21
24
|
/**
|
|
22
25
|
* Static method that creates a binary heap from an array of nodes and a comparison function.
|
|
23
|
-
* @param nodes
|
|
24
|
-
* @param comparator - Comparison function.
|
|
25
26
|
* @returns A new Heap instance.
|
|
27
|
+
* @param options
|
|
26
28
|
*/
|
|
27
|
-
static heapify<E>(
|
|
29
|
+
static heapify<E>(options: {
|
|
30
|
+
nodes: E[];
|
|
31
|
+
comparator: Comparator<E>;
|
|
32
|
+
}): Heap<E>;
|
|
28
33
|
/**
|
|
29
34
|
* Insert an element into the heap and maintain the heap properties.
|
|
30
35
|
* @param element - The element to be inserted.
|
|
@@ -10,8 +10,12 @@ exports.FibonacciHeap = exports.FibonacciHeapNode = exports.Heap = void 0;
|
|
|
10
10
|
class Heap {
|
|
11
11
|
nodes = [];
|
|
12
12
|
comparator;
|
|
13
|
-
constructor(
|
|
14
|
-
this.comparator = comparator;
|
|
13
|
+
constructor(options) {
|
|
14
|
+
this.comparator = options.comparator;
|
|
15
|
+
if (options.nodes && options.nodes.length > 0) {
|
|
16
|
+
this.nodes = options.nodes;
|
|
17
|
+
this.fix();
|
|
18
|
+
}
|
|
15
19
|
}
|
|
16
20
|
/**
|
|
17
21
|
* Get the size (number of elements) of the heap.
|
|
@@ -28,15 +32,11 @@ class Heap {
|
|
|
28
32
|
}
|
|
29
33
|
/**
|
|
30
34
|
* Static method that creates a binary heap from an array of nodes and a comparison function.
|
|
31
|
-
* @param nodes
|
|
32
|
-
* @param comparator - Comparison function.
|
|
33
35
|
* @returns A new Heap instance.
|
|
36
|
+
* @param options
|
|
34
37
|
*/
|
|
35
|
-
static heapify(
|
|
36
|
-
|
|
37
|
-
binaryHeap.nodes = [...nodes];
|
|
38
|
-
binaryHeap.fix(); // Fix heap properties
|
|
39
|
-
return binaryHeap;
|
|
38
|
+
static heapify(options) {
|
|
39
|
+
return new Heap(options);
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
42
|
* Insert an element into the heap and maintain the heap properties.
|
|
@@ -161,7 +161,7 @@ class Heap {
|
|
|
161
161
|
* @returns A new Heap instance containing the same elements.
|
|
162
162
|
*/
|
|
163
163
|
clone() {
|
|
164
|
-
const clonedHeap = new Heap(this.comparator);
|
|
164
|
+
const clonedHeap = new Heap({ comparator: this.comparator });
|
|
165
165
|
clonedHeap.nodes = [...this.nodes];
|
|
166
166
|
return clonedHeap;
|
|
167
167
|
}
|
|
@@ -8,5 +8,8 @@
|
|
|
8
8
|
import { Heap } from './heap';
|
|
9
9
|
import type { Comparator } from '../../types';
|
|
10
10
|
export declare class MaxHeap<E = any> extends Heap<E> {
|
|
11
|
-
constructor(
|
|
11
|
+
constructor(options?: {
|
|
12
|
+
comparator: Comparator<E>;
|
|
13
|
+
nodes?: E[];
|
|
14
|
+
});
|
|
12
15
|
}
|
|
@@ -10,15 +10,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
10
10
|
exports.MaxHeap = void 0;
|
|
11
11
|
const heap_1 = require("./heap");
|
|
12
12
|
class MaxHeap extends heap_1.Heap {
|
|
13
|
-
constructor(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
constructor(options = {
|
|
14
|
+
comparator: (a, b) => {
|
|
15
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
|
+
throw new Error('The a, b params of compare function must be number');
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return b - a;
|
|
20
|
+
}
|
|
19
21
|
}
|
|
20
22
|
}) {
|
|
21
|
-
super(
|
|
23
|
+
super(options);
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
exports.MaxHeap = MaxHeap;
|
|
@@ -8,5 +8,8 @@
|
|
|
8
8
|
import { Heap } from './heap';
|
|
9
9
|
import type { Comparator } from '../../types';
|
|
10
10
|
export declare class MinHeap<E = any> extends Heap<E> {
|
|
11
|
-
constructor(
|
|
11
|
+
constructor(options?: {
|
|
12
|
+
comparator: Comparator<E>;
|
|
13
|
+
nodes?: E[];
|
|
14
|
+
});
|
|
12
15
|
}
|
|
@@ -10,15 +10,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
10
10
|
exports.MinHeap = void 0;
|
|
11
11
|
const heap_1 = require("./heap");
|
|
12
12
|
class MinHeap extends heap_1.Heap {
|
|
13
|
-
constructor(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
constructor(options = {
|
|
14
|
+
comparator: (a, b) => {
|
|
15
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
|
+
throw new Error('The a, b params of compare function must be number');
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return a - b;
|
|
20
|
+
}
|
|
19
21
|
}
|
|
20
22
|
}) {
|
|
21
|
-
super(
|
|
23
|
+
super(options);
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
exports.MinHeap = MinHeap;
|
|
@@ -8,5 +8,8 @@
|
|
|
8
8
|
import { PriorityQueue } from './priority-queue';
|
|
9
9
|
import type { Comparator } from '../../types';
|
|
10
10
|
export declare class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
11
|
-
constructor(
|
|
11
|
+
constructor(options?: {
|
|
12
|
+
comparator: Comparator<E>;
|
|
13
|
+
nodes?: E[];
|
|
14
|
+
});
|
|
12
15
|
}
|
|
@@ -10,15 +10,17 @@ exports.MaxPriorityQueue = void 0;
|
|
|
10
10
|
*/
|
|
11
11
|
const priority_queue_1 = require("./priority-queue");
|
|
12
12
|
class MaxPriorityQueue extends priority_queue_1.PriorityQueue {
|
|
13
|
-
constructor(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
constructor(options = {
|
|
14
|
+
comparator: (a, b) => {
|
|
15
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
|
+
throw new Error('The a, b params of compare function must be number');
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return b - a;
|
|
20
|
+
}
|
|
19
21
|
}
|
|
20
22
|
}) {
|
|
21
|
-
super(
|
|
23
|
+
super(options);
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
exports.MaxPriorityQueue = MaxPriorityQueue;
|
|
@@ -8,5 +8,8 @@
|
|
|
8
8
|
import { PriorityQueue } from './priority-queue';
|
|
9
9
|
import type { Comparator } from '../../types';
|
|
10
10
|
export declare class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
11
|
-
constructor(
|
|
11
|
+
constructor(options?: {
|
|
12
|
+
comparator: Comparator<E>;
|
|
13
|
+
nodes?: E[];
|
|
14
|
+
});
|
|
12
15
|
}
|
|
@@ -10,15 +10,17 @@ exports.MinPriorityQueue = void 0;
|
|
|
10
10
|
*/
|
|
11
11
|
const priority_queue_1 = require("./priority-queue");
|
|
12
12
|
class MinPriorityQueue extends priority_queue_1.PriorityQueue {
|
|
13
|
-
constructor(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
constructor(options = {
|
|
14
|
+
comparator: (a, b) => {
|
|
15
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
|
+
throw new Error('The a, b params of compare function must be number');
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return a - b;
|
|
20
|
+
}
|
|
19
21
|
}
|
|
20
22
|
}) {
|
|
21
|
-
super(
|
|
23
|
+
super(options);
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
exports.MinPriorityQueue = MinPriorityQueue;
|
|
@@ -7,6 +7,9 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { Heap } from '../heap';
|
|
9
9
|
import { Comparator } from '../../types';
|
|
10
|
-
export declare class PriorityQueue<E> extends Heap<E> {
|
|
11
|
-
constructor(
|
|
10
|
+
export declare class PriorityQueue<E = any> extends Heap<E> {
|
|
11
|
+
constructor(options: {
|
|
12
|
+
comparator: Comparator<E>;
|
|
13
|
+
nodes?: E[];
|
|
14
|
+
});
|
|
12
15
|
}
|
|
@@ -10,8 +10,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
10
10
|
exports.PriorityQueue = void 0;
|
|
11
11
|
const heap_1 = require("../heap");
|
|
12
12
|
class PriorityQueue extends heap_1.Heap {
|
|
13
|
-
constructor(
|
|
14
|
-
super(
|
|
13
|
+
constructor(options) {
|
|
14
|
+
super(options);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
exports.PriorityQueue = PriorityQueue;
|