min-heap-typed 1.38.8 → 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/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +3 -3
- package/dist/data-structures/graph/abstract-graph.js +4 -4
- package/dist/data-structures/graph/directed-graph.d.ts +4 -4
- package/dist/data-structures/graph/directed-graph.js +6 -6
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -3
- package/dist/data-structures/graph/undirected-graph.js +4 -4
- package/dist/data-structures/heap/heap.d.ts +10 -5
- package/dist/data-structures/heap/heap.js +10 -10
- package/dist/data-structures/heap/max-heap.d.ts +4 -1
- package/dist/data-structures/heap/max-heap.js +9 -7
- package/dist/data-structures/heap/min-heap.d.ts +4 -1
- package/dist/data-structures/heap/min-heap.js +9 -7
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +4 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +9 -7
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +4 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +9 -7
- package/dist/data-structures/priority-queue/priority-queue.d.ts +5 -2
- package/dist/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/data-structures/trie/trie.d.ts +2 -1
- package/dist/data-structures/trie/trie.js +3 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +20 -0
- package/src/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/data-structures/binary-tree/bst.ts +3 -1
- package/src/data-structures/binary-tree/rb-tree.ts +3 -1
- 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/src/data-structures/trie/trie.ts +4 -2
|
@@ -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.
|
|
@@ -120,6 +120,23 @@ class BinaryIndexedTree {
|
|
|
120
120
|
}
|
|
121
121
|
return this._binarySearch(sum, (x, y) => x <= y);
|
|
122
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* The function calculates the prefix sum of an array using a binary indexed tree.
|
|
125
|
+
* @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
|
|
126
|
+
* array for which we want to calculate the prefix sum.
|
|
127
|
+
* @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
|
|
128
|
+
* `i`.
|
|
129
|
+
*/
|
|
130
|
+
getPrefixSum(i) {
|
|
131
|
+
this._checkIndex(i);
|
|
132
|
+
i++; // Convert to 1-based index
|
|
133
|
+
let sum = 0;
|
|
134
|
+
while (i > 0) {
|
|
135
|
+
sum += this._getFrequency(i);
|
|
136
|
+
i -= i & -i;
|
|
137
|
+
}
|
|
138
|
+
return sum;
|
|
139
|
+
}
|
|
123
140
|
/**
|
|
124
141
|
* The function returns the value of a specific index in a freqMap data structure, or a default value if
|
|
125
142
|
* 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
|
|
@@ -117,12 +117,12 @@ class AbstractGraph {
|
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
119
|
/**
|
|
120
|
-
* The `
|
|
120
|
+
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
121
121
|
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
|
|
122
122
|
* (`VertexKey`).
|
|
123
123
|
* @returns The method is returning a boolean value.
|
|
124
124
|
*/
|
|
125
|
-
|
|
125
|
+
deleteVertex(vertexOrKey) {
|
|
126
126
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
127
127
|
return this._vertices.delete(vertexKey);
|
|
128
128
|
}
|
|
@@ -136,7 +136,7 @@ class AbstractGraph {
|
|
|
136
136
|
removeAllVertices(vertices) {
|
|
137
137
|
const removed = [];
|
|
138
138
|
for (const v of vertices) {
|
|
139
|
-
removed.push(this.
|
|
139
|
+
removed.push(this.deleteVertex(v));
|
|
140
140
|
}
|
|
141
141
|
return removed.length > 0;
|
|
142
142
|
}
|
|
@@ -529,7 +529,7 @@ class AbstractGraph {
|
|
|
529
529
|
if (vertexOrKey instanceof AbstractVertex)
|
|
530
530
|
distMap.set(vertexOrKey, Infinity);
|
|
531
531
|
}
|
|
532
|
-
const heap = new priority_queue_1.PriorityQueue((a, b) => a.key - b.key);
|
|
532
|
+
const heap = new priority_queue_1.PriorityQueue({ comparator: (a, b) => a.key - b.key });
|
|
533
533
|
heap.add({ key: 0, val: srcVertex });
|
|
534
534
|
distMap.set(srcVertex, 0);
|
|
535
535
|
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
|
|
@@ -130,7 +130,7 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
130
130
|
* @param {V | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
|
|
131
131
|
* @returns the removed edge (E) if it exists, or null if either the source or destination vertex does not exist.
|
|
132
132
|
*/
|
|
133
|
-
|
|
133
|
+
deleteEdgeSrcToDest(srcOrKey, destOrKey) {
|
|
134
134
|
const src = this._getVertex(srcOrKey);
|
|
135
135
|
const dest = this._getVertex(destOrKey);
|
|
136
136
|
let removed = null;
|
|
@@ -151,9 +151,9 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
151
151
|
* The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
|
|
152
152
|
* @param {E} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
|
|
153
153
|
* and `dest`, which represent the source and destination vertices of the edge, respectively.
|
|
154
|
-
* @returns The method `
|
|
154
|
+
* @returns The method `deleteEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
|
|
155
155
|
*/
|
|
156
|
-
|
|
156
|
+
deleteEdge(edge) {
|
|
157
157
|
let removed = null;
|
|
158
158
|
const src = this._getVertex(edge.src);
|
|
159
159
|
const dest = this._getVertex(edge.dest);
|
|
@@ -177,11 +177,11 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
177
177
|
* the second vertex in the edge that needs to be removed.
|
|
178
178
|
* @returns an array of removed edges (E[]).
|
|
179
179
|
*/
|
|
180
|
-
|
|
180
|
+
deleteEdgesBetween(v1, v2) {
|
|
181
181
|
const removed = [];
|
|
182
182
|
if (v1 && v2) {
|
|
183
|
-
const v1ToV2 = this.
|
|
184
|
-
const v2ToV1 = this.
|
|
183
|
+
const v1ToV2 = this.deleteEdgeSrcToDest(v1, v2);
|
|
184
|
+
const v2ToV1 = this.deleteEdgeSrcToDest(v2, v1);
|
|
185
185
|
v1ToV2 && removed.push(v1ToV2);
|
|
186
186
|
v2ToV1 && removed.push(v2ToV1);
|
|
187
187
|
}
|
|
@@ -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.
|
|
@@ -109,7 +109,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
109
109
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
110
110
|
* @returns the removed edge (E) if it exists, or null if either of the vertices (V) does not exist.
|
|
111
111
|
*/
|
|
112
|
-
|
|
112
|
+
deleteEdgeBetween(v1, v2) {
|
|
113
113
|
const vertex1 = this._getVertex(v1);
|
|
114
114
|
const vertex2 = this._getVertex(v2);
|
|
115
115
|
if (!vertex1 || !vertex2) {
|
|
@@ -127,12 +127,12 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
127
127
|
return removed;
|
|
128
128
|
}
|
|
129
129
|
/**
|
|
130
|
-
* The
|
|
130
|
+
* The deleteEdge function removes an edge between two vertices in a graph.
|
|
131
131
|
* @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
|
|
132
132
|
* @returns The method is returning either the removed edge (of type E) or null if the edge was not found.
|
|
133
133
|
*/
|
|
134
|
-
|
|
135
|
-
return this.
|
|
134
|
+
deleteEdge(edge) {
|
|
135
|
+
return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
|
|
136
136
|
}
|
|
137
137
|
/**
|
|
138
138
|
* 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.
|
|
@@ -8,9 +8,13 @@
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.FibonacciHeap = exports.FibonacciHeapNode = exports.Heap = void 0;
|
|
10
10
|
class Heap {
|
|
11
|
-
constructor(
|
|
11
|
+
constructor(options) {
|
|
12
12
|
this.nodes = [];
|
|
13
|
-
this.comparator = comparator;
|
|
13
|
+
this.comparator = options.comparator;
|
|
14
|
+
if (options.nodes && options.nodes.length > 0) {
|
|
15
|
+
this.nodes = options.nodes;
|
|
16
|
+
this.fix();
|
|
17
|
+
}
|
|
14
18
|
}
|
|
15
19
|
/**
|
|
16
20
|
* 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;
|
|
@@ -77,8 +77,9 @@ export declare class Trie {
|
|
|
77
77
|
* @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
78
78
|
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
79
79
|
* @param {number} max - The max count of words will be found
|
|
80
|
+
* @param isAllWhenEmptyPrefix - If true, when the prefix provided as '', returns all the words in the trie.
|
|
80
81
|
* @returns {string[]} an array of strings.
|
|
81
82
|
*/
|
|
82
|
-
getWords(prefix?: string, max?: number): string[];
|
|
83
|
+
getWords(prefix?: string, max?: number, isAllWhenEmptyPrefix?: boolean): string[];
|
|
83
84
|
private _caseProcess;
|
|
84
85
|
}
|
|
@@ -226,9 +226,10 @@ class Trie {
|
|
|
226
226
|
* @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
227
227
|
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
228
228
|
* @param {number} max - The max count of words will be found
|
|
229
|
+
* @param isAllWhenEmptyPrefix - If true, when the prefix provided as '', returns all the words in the trie.
|
|
229
230
|
* @returns {string[]} an array of strings.
|
|
230
231
|
*/
|
|
231
|
-
getWords(prefix = '', max = Number.MAX_SAFE_INTEGER) {
|
|
232
|
+
getWords(prefix = '', max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false) {
|
|
232
233
|
prefix = this._caseProcess(prefix);
|
|
233
234
|
const words = [];
|
|
234
235
|
let found = 0;
|
|
@@ -254,7 +255,7 @@ class Trie {
|
|
|
254
255
|
startNode = nodeC;
|
|
255
256
|
}
|
|
256
257
|
}
|
|
257
|
-
if (startNode !== this.root)
|
|
258
|
+
if (isAllWhenEmptyPrefix || startNode !== this.root)
|
|
258
259
|
dfs(startNode, prefix);
|
|
259
260
|
return words;
|
|
260
261
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.39.0",
|
|
4
4
|
"description": "Min Heap. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -131,6 +131,6 @@
|
|
|
131
131
|
"typescript": "^4.9.5"
|
|
132
132
|
},
|
|
133
133
|
"dependencies": {
|
|
134
|
-
"data-structure-typed": "^1.
|
|
134
|
+
"data-structure-typed": "^1.39.0"
|
|
135
135
|
}
|
|
136
136
|
}
|
|
@@ -143,6 +143,26 @@ export class BinaryIndexedTree {
|
|
|
143
143
|
return this._binarySearch(sum, (x, y) => x <= y);
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
+
/**
|
|
147
|
+
* The function calculates the prefix sum of an array using a binary indexed tree.
|
|
148
|
+
* @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
|
|
149
|
+
* array for which we want to calculate the prefix sum.
|
|
150
|
+
* @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
|
|
151
|
+
* `i`.
|
|
152
|
+
*/
|
|
153
|
+
getPrefixSum(i: number): number {
|
|
154
|
+
this._checkIndex(i);
|
|
155
|
+
i++; // Convert to 1-based index
|
|
156
|
+
|
|
157
|
+
let sum = 0;
|
|
158
|
+
while (i > 0) {
|
|
159
|
+
sum += this._getFrequency(i);
|
|
160
|
+
i -= i & -i;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return sum;
|
|
164
|
+
}
|
|
165
|
+
|
|
146
166
|
/**
|
|
147
167
|
* The function returns the value of a specific index in a freqMap data structure, or a default value if
|
|
148
168
|
* the index is not found.
|
|
@@ -115,7 +115,8 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
115
115
|
* Represents a binary tree data structure.
|
|
116
116
|
* @template N - The type of the binary tree's nodes.
|
|
117
117
|
*/
|
|
118
|
-
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
118
|
+
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
119
|
+
implements IBinaryTree<V, N> {
|
|
119
120
|
/**
|
|
120
121
|
* Creates a new instance of BinaryTree.
|
|
121
122
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
@@ -974,7 +975,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
974
975
|
if (current.left) queue.push(current.left);
|
|
975
976
|
if (current.right) queue.push(current.right);
|
|
976
977
|
}
|
|
977
|
-
|
|
978
978
|
}
|
|
979
979
|
}
|
|
980
980
|
return ans;
|
|
@@ -24,7 +24,9 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
|
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
|
|
27
|
+
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
|
|
28
|
+
extends BinaryTree<V, N>
|
|
29
|
+
implements IBinaryTree<V, N> {
|
|
28
30
|
/**
|
|
29
31
|
* The constructor function initializes a binary search tree object with an optional comparator
|
|
30
32
|
* function.
|
|
@@ -19,7 +19,9 @@ export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>>
|
|
22
|
+
export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>>
|
|
23
|
+
extends BST<V, N>
|
|
24
|
+
implements IBinaryTree<V, N> {
|
|
23
25
|
constructor(options?: RBTreeOptions) {
|
|
24
26
|
super(options);
|
|
25
27
|
}
|
|
@@ -130,7 +130,7 @@ export abstract class AbstractGraph<
|
|
|
130
130
|
*/
|
|
131
131
|
abstract createEdge(srcOrV1: VertexKey | string, destOrV2: VertexKey | string, weight?: number, val?: E): E;
|
|
132
132
|
|
|
133
|
-
abstract
|
|
133
|
+
abstract deleteEdge(edge: E): E | null;
|
|
134
134
|
|
|
135
135
|
abstract getEdge(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null;
|
|
136
136
|
|
|
@@ -179,12 +179,12 @@ export abstract class AbstractGraph<
|
|
|
179
179
|
}
|
|
180
180
|
|
|
181
181
|
/**
|
|
182
|
-
* The `
|
|
182
|
+
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
183
183
|
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
|
|
184
184
|
* (`VertexKey`).
|
|
185
185
|
* @returns The method is returning a boolean value.
|
|
186
186
|
*/
|
|
187
|
-
|
|
187
|
+
deleteVertex(vertexOrKey: V | VertexKey): boolean {
|
|
188
188
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
189
189
|
return this._vertices.delete(vertexKey);
|
|
190
190
|
}
|
|
@@ -199,7 +199,7 @@ export abstract class AbstractGraph<
|
|
|
199
199
|
removeAllVertices(vertices: V[] | VertexKey[]): boolean {
|
|
200
200
|
const removed: boolean[] = [];
|
|
201
201
|
for (const v of vertices) {
|
|
202
|
-
removed.push(this.
|
|
202
|
+
removed.push(this.deleteVertex(v));
|
|
203
203
|
}
|
|
204
204
|
return removed.length > 0;
|
|
205
205
|
}
|
|
@@ -622,7 +622,7 @@ export abstract class AbstractGraph<
|
|
|
622
622
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
623
623
|
}
|
|
624
624
|
|
|
625
|
-
const heap = new PriorityQueue<{ key: number; val: V }>((a, b) => a.key - b.key);
|
|
625
|
+
const heap = new PriorityQueue<{ key: number; val: V }>({comparator: (a, b) => a.key - b.key});
|
|
626
626
|
heap.add({key: 0, val: srcVertex});
|
|
627
627
|
|
|
628
628
|
distMap.set(srcVertex, 0);
|
|
@@ -153,7 +153,7 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
153
153
|
* @param {V | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
|
|
154
154
|
* @returns the removed edge (E) if it exists, or null if either the source or destination vertex does not exist.
|
|
155
155
|
*/
|
|
156
|
-
|
|
156
|
+
deleteEdgeSrcToDest(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null {
|
|
157
157
|
const src: V | null = this._getVertex(srcOrKey);
|
|
158
158
|
const dest: V | null = this._getVertex(destOrKey);
|
|
159
159
|
let removed: E | null = null;
|
|
@@ -177,9 +177,9 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
177
177
|
* The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
|
|
178
178
|
* @param {E} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
|
|
179
179
|
* and `dest`, which represent the source and destination vertices of the edge, respectively.
|
|
180
|
-
* @returns The method `
|
|
180
|
+
* @returns The method `deleteEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
|
|
181
181
|
*/
|
|
182
|
-
|
|
182
|
+
deleteEdge(edge: E): E | null {
|
|
183
183
|
let removed: E | null = null;
|
|
184
184
|
const src = this._getVertex(edge.src);
|
|
185
185
|
const dest = this._getVertex(edge.dest);
|
|
@@ -206,12 +206,12 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
|
|
|
206
206
|
* the second vertex in the edge that needs to be removed.
|
|
207
207
|
* @returns an array of removed edges (E[]).
|
|
208
208
|
*/
|
|
209
|
-
|
|
209
|
+
deleteEdgesBetween(v1: VertexKey | V, v2: VertexKey | V): E[] {
|
|
210
210
|
const removed: E[] = [];
|
|
211
211
|
|
|
212
212
|
if (v1 && v2) {
|
|
213
|
-
const v1ToV2 = this.
|
|
214
|
-
const v2ToV1 = this.
|
|
213
|
+
const v1ToV2 = this.deleteEdgeSrcToDest(v1, v2);
|
|
214
|
+
const v2ToV1 = this.deleteEdgeSrcToDest(v2, v1);
|
|
215
215
|
|
|
216
216
|
v1ToV2 && removed.push(v1ToV2);
|
|
217
217
|
v2ToV1 && removed.push(v2ToV1);
|
|
@@ -127,7 +127,7 @@ export class UndirectedGraph<
|
|
|
127
127
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
128
128
|
* @returns the removed edge (E) if it exists, or null if either of the vertices (V) does not exist.
|
|
129
129
|
*/
|
|
130
|
-
|
|
130
|
+
deleteEdgeBetween(v1: V | VertexKey, v2: V | VertexKey): E | null {
|
|
131
131
|
const vertex1: V | null = this._getVertex(v1);
|
|
132
132
|
const vertex2: V | null = this._getVertex(v2);
|
|
133
133
|
|
|
@@ -148,12 +148,12 @@ export class UndirectedGraph<
|
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
/**
|
|
151
|
-
* The
|
|
151
|
+
* The deleteEdge function removes an edge between two vertices in a graph.
|
|
152
152
|
* @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
|
|
153
153
|
* @returns The method is returning either the removed edge (of type E) or null if the edge was not found.
|
|
154
154
|
*/
|
|
155
|
-
|
|
156
|
-
return this.
|
|
155
|
+
deleteEdge(edge: E): E | null {
|
|
156
|
+
return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
|
|
157
157
|
}
|
|
158
158
|
|
|
159
159
|
/**
|
|
@@ -7,12 +7,16 @@
|
|
|
7
7
|
|
|
8
8
|
import type {Comparator, DFSOrderPattern} from '../../types';
|
|
9
9
|
|
|
10
|
-
export class Heap<E> {
|
|
10
|
+
export class Heap<E = any> {
|
|
11
11
|
protected nodes: E[] = [];
|
|
12
12
|
protected readonly comparator: Comparator<E>;
|
|
13
13
|
|
|
14
|
-
constructor(comparator: Comparator<E
|
|
15
|
-
this.comparator = comparator;
|
|
14
|
+
constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
|
|
15
|
+
this.comparator = options.comparator;
|
|
16
|
+
if (options.nodes && options.nodes.length > 0) {
|
|
17
|
+
this.nodes = options.nodes;
|
|
18
|
+
this.fix();
|
|
19
|
+
}
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
/**
|
|
@@ -32,15 +36,11 @@ export class Heap<E> {
|
|
|
32
36
|
|
|
33
37
|
/**
|
|
34
38
|
* Static method that creates a binary heap from an array of nodes and a comparison function.
|
|
35
|
-
* @param nodes
|
|
36
|
-
* @param comparator - Comparison function.
|
|
37
39
|
* @returns A new Heap instance.
|
|
40
|
+
* @param options
|
|
38
41
|
*/
|
|
39
|
-
static heapify<E>(nodes: E[]
|
|
40
|
-
|
|
41
|
-
binaryHeap.nodes = [...nodes];
|
|
42
|
-
binaryHeap.fix(); // Fix heap properties
|
|
43
|
-
return binaryHeap;
|
|
42
|
+
static heapify<E>(options: { nodes: E[]; comparator: Comparator<E> }): Heap<E> {
|
|
43
|
+
return new Heap<E>(options);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
/**
|
|
@@ -180,7 +180,7 @@ export class Heap<E> {
|
|
|
180
180
|
* @returns A new Heap instance containing the same elements.
|
|
181
181
|
*/
|
|
182
182
|
clone(): Heap<E> {
|
|
183
|
-
const clonedHeap = new Heap<E>(this.comparator);
|
|
183
|
+
const clonedHeap = new Heap<E>({comparator: this.comparator});
|
|
184
184
|
clonedHeap.nodes = [...this.nodes];
|
|
185
185
|
return clonedHeap;
|
|
186
186
|
}
|
|
@@ -269,7 +269,7 @@ export class FibonacciHeapNode<E> {
|
|
|
269
269
|
|
|
270
270
|
export class FibonacciHeap<E> {
|
|
271
271
|
root?: FibonacciHeapNode<E>;
|
|
272
|
-
size
|
|
272
|
+
size = 0;
|
|
273
273
|
protected min?: FibonacciHeapNode<E>;
|
|
274
274
|
protected readonly comparator: Comparator<E>;
|
|
275
275
|
|
|
@@ -11,14 +11,16 @@ import type {Comparator} from '../../types';
|
|
|
11
11
|
|
|
12
12
|
export class MaxHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
comparator: Comparator<E
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
15
|
+
comparator: (a: E, b: E) => {
|
|
16
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
17
|
+
throw new Error('The a, b params of compare function must be number');
|
|
18
|
+
} else {
|
|
19
|
+
return b - a;
|
|
20
|
+
}
|
|
19
21
|
}
|
|
20
22
|
}
|
|
21
23
|
) {
|
|
22
|
-
super(
|
|
24
|
+
super(options);
|
|
23
25
|
}
|
|
24
26
|
}
|
|
@@ -11,14 +11,16 @@ import type {Comparator} from '../../types';
|
|
|
11
11
|
|
|
12
12
|
export class MinHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
comparator: Comparator<E
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
15
|
+
comparator: (a: E, b: E) => {
|
|
16
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
17
|
+
throw new Error('The a, b params of compare function must be number');
|
|
18
|
+
} else {
|
|
19
|
+
return a - b;
|
|
20
|
+
}
|
|
19
21
|
}
|
|
20
22
|
}
|
|
21
23
|
) {
|
|
22
|
-
super(
|
|
24
|
+
super(options);
|
|
23
25
|
}
|
|
24
26
|
}
|
|
@@ -10,14 +10,16 @@ import type {Comparator} from '../../types';
|
|
|
10
10
|
|
|
11
11
|
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
14
|
+
comparator: (a: E, b: E) => {
|
|
15
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
|
+
throw new Error('The a, b params of compare function must be number');
|
|
17
|
+
} else {
|
|
18
|
+
return b - a;
|
|
19
|
+
}
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
22
|
) {
|
|
21
|
-
super(
|
|
23
|
+
super(options);
|
|
22
24
|
}
|
|
23
25
|
}
|
|
@@ -10,14 +10,16 @@ import type {Comparator} from '../../types';
|
|
|
10
10
|
|
|
11
11
|
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
14
|
+
comparator: (a: E, b: E) => {
|
|
15
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
|
+
throw new Error('The a, b params of compare function must be number');
|
|
17
|
+
} else {
|
|
18
|
+
return a - b;
|
|
19
|
+
}
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
22
|
) {
|
|
21
|
-
super(
|
|
23
|
+
super(options);
|
|
22
24
|
}
|
|
23
25
|
}
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
import {Heap} from '../heap';
|
|
10
10
|
import {Comparator} from '../../types';
|
|
11
11
|
|
|
12
|
-
export class PriorityQueue<E> extends Heap<E> {
|
|
13
|
-
constructor(comparator: Comparator<E
|
|
14
|
-
super(
|
|
12
|
+
export class PriorityQueue<E = any> extends Heap<E> {
|
|
13
|
+
constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
|
|
14
|
+
super(options);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
@@ -241,9 +241,10 @@ export class Trie {
|
|
|
241
241
|
* @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
242
242
|
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
243
243
|
* @param {number} max - The max count of words will be found
|
|
244
|
+
* @param isAllWhenEmptyPrefix - If true, when the prefix provided as '', returns all the words in the trie.
|
|
244
245
|
* @returns {string[]} an array of strings.
|
|
245
246
|
*/
|
|
246
|
-
getWords(prefix = '', max = Number.MAX_SAFE_INTEGER): string[] {
|
|
247
|
+
getWords(prefix = '', max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false): string[] {
|
|
247
248
|
prefix = this._caseProcess(prefix);
|
|
248
249
|
const words: string[] = [];
|
|
249
250
|
let found = 0;
|
|
@@ -270,7 +271,8 @@ export class Trie {
|
|
|
270
271
|
if (nodeC) startNode = nodeC;
|
|
271
272
|
}
|
|
272
273
|
}
|
|
273
|
-
|
|
274
|
+
|
|
275
|
+
if (isAllWhenEmptyPrefix || startNode !== this.root) dfs(startNode, prefix);
|
|
274
276
|
|
|
275
277
|
return words;
|
|
276
278
|
}
|