min-heap-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.
Files changed (31) hide show
  1. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
  2. package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -0
  3. package/dist/data-structures/graph/abstract-graph.d.ts +3 -3
  4. package/dist/data-structures/graph/abstract-graph.js +4 -4
  5. package/dist/data-structures/graph/directed-graph.d.ts +4 -4
  6. package/dist/data-structures/graph/directed-graph.js +6 -6
  7. package/dist/data-structures/graph/undirected-graph.d.ts +3 -3
  8. package/dist/data-structures/graph/undirected-graph.js +4 -4
  9. package/dist/data-structures/heap/heap.d.ts +10 -5
  10. package/dist/data-structures/heap/heap.js +10 -10
  11. package/dist/data-structures/heap/max-heap.d.ts +4 -1
  12. package/dist/data-structures/heap/max-heap.js +9 -7
  13. package/dist/data-structures/heap/min-heap.d.ts +4 -1
  14. package/dist/data-structures/heap/min-heap.js +9 -7
  15. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +4 -1
  16. package/dist/data-structures/priority-queue/max-priority-queue.js +9 -7
  17. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +4 -1
  18. package/dist/data-structures/priority-queue/min-priority-queue.js +9 -7
  19. package/dist/data-structures/priority-queue/priority-queue.d.ts +5 -2
  20. package/dist/data-structures/priority-queue/priority-queue.js +2 -2
  21. package/package.json +2 -2
  22. package/src/data-structures/binary-tree/binary-indexed-tree.ts +20 -0
  23. package/src/data-structures/graph/abstract-graph.ts +5 -5
  24. package/src/data-structures/graph/directed-graph.ts +6 -6
  25. package/src/data-structures/graph/undirected-graph.ts +4 -4
  26. package/src/data-structures/heap/heap.ts +12 -12
  27. package/src/data-structures/heap/max-heap.ts +8 -6
  28. package/src/data-structures/heap/min-heap.ts +8 -6
  29. package/src/data-structures/priority-queue/max-priority-queue.ts +8 -6
  30. package/src/data-structures/priority-queue/min-priority-queue.ts +8 -6
  31. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
@@ -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 removeEdge(edge: E): E | null;
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 `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
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
- removeVertex(vertexOrKey: V | VertexKey): boolean;
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 `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
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
- removeVertex(vertexOrKey) {
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.removeVertex(v));
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
- removeEdgeSrcToDest(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null;
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 `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
92
+ * @returns The method `deleteEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
93
93
  */
94
- removeEdge(edge: E): E | null;
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
- removeEdgesBetween(v1: VertexKey | V, v2: VertexKey | V): E[];
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
- removeEdgeSrcToDest(srcOrKey, destOrKey) {
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 `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
154
+ * @returns The method `deleteEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
155
155
  */
156
- removeEdge(edge) {
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
- removeEdgesBetween(v1, v2) {
180
+ deleteEdgesBetween(v1, v2) {
181
181
  const removed = [];
182
182
  if (v1 && v2) {
183
- const v1ToV2 = this.removeEdgeSrcToDest(v1, v2);
184
- const v2ToV1 = this.removeEdgeSrcToDest(v2, v1);
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
- removeEdgeBetween(v1: V | VertexKey, v2: V | VertexKey): E | null;
74
+ deleteEdgeBetween(v1: V | VertexKey, v2: V | VertexKey): E | null;
75
75
  /**
76
- * The removeEdge function removes an edge between two vertices in a graph.
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
- removeEdge(edge: E): E | null;
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
- removeEdgeBetween(v1, v2) {
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 removeEdge function removes an edge between two vertices in a graph.
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
- removeEdge(edge) {
135
- return this.removeEdgeBetween(edge.vertices[0], edge.vertices[1]);
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(comparator: Comparator<E>);
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>(nodes: E[], comparator: Comparator<E>): Heap<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(comparator) {
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(nodes, comparator) {
36
- const binaryHeap = new Heap(comparator);
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(comparator?: Comparator<E>);
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(comparator = (a, b) => {
14
- if (!(typeof a === 'number' && typeof b === 'number')) {
15
- throw new Error('The a, b params of compare function must be number');
16
- }
17
- else {
18
- return b - a;
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(comparator);
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(comparator?: Comparator<E>);
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(comparator = (a, b) => {
14
- if (!(typeof a === 'number' && typeof b === 'number')) {
15
- throw new Error('The a, b params of compare function must be number');
16
- }
17
- else {
18
- return a - b;
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(comparator);
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(compare?: Comparator<E>);
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(compare = (a, b) => {
14
- if (!(typeof a === 'number' && typeof b === 'number')) {
15
- throw new Error('The a, b params of compare function must be number');
16
- }
17
- else {
18
- return b - a;
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(compare);
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(compare?: Comparator<E>);
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(compare = (a, b) => {
14
- if (!(typeof a === 'number' && typeof b === 'number')) {
15
- throw new Error('The a, b params of compare function must be number');
16
- }
17
- else {
18
- return a - b;
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(compare);
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(comparator: Comparator<E>);
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(comparator) {
14
- super(comparator);
13
+ constructor(options) {
14
+ super(options);
15
15
  }
16
16
  }
17
17
  exports.PriorityQueue = PriorityQueue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "min-heap-typed",
3
- "version": "1.38.9",
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.38.9"
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.
@@ -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 removeEdge(edge: E): E | null;
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 `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
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
- removeVertex(vertexOrKey: V | VertexKey): boolean {
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.removeVertex(v));
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
- removeEdgeSrcToDest(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null {
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 `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
180
+ * @returns The method `deleteEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
181
181
  */
182
- removeEdge(edge: E): E | null {
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
- removeEdgesBetween(v1: VertexKey | V, v2: VertexKey | V): E[] {
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.removeEdgeSrcToDest(v1, v2);
214
- const v2ToV1 = this.removeEdgeSrcToDest(v2, v1);
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
- removeEdgeBetween(v1: V | VertexKey, v2: V | VertexKey): E | null {
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 removeEdge function removes an edge between two vertices in a graph.
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
- removeEdge(edge: E): E | null {
156
- return this.removeEdgeBetween(edge.vertices[0], edge.vertices[1]);
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[], comparator: Comparator<E>): Heap<E> {
40
- const binaryHeap = new Heap<E>(comparator);
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: number = 0;
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> = (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;
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(comparator);
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> = (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;
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(comparator);
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
- compare: Comparator<E> = (a: E, b: E) => {
14
- if (!(typeof a === 'number' && typeof b === 'number')) {
15
- throw new Error('The a, b params of compare function must be number');
16
- } else {
17
- return b - a;
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(compare);
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
- compare: Comparator<E> = (a: E, b: E) => {
14
- if (!(typeof a === 'number' && typeof b === 'number')) {
15
- throw new Error('The a, b params of compare function must be number');
16
- } else {
17
- return a - b;
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(compare);
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(comparator);
12
+ export class PriorityQueue<E = any> extends Heap<E> {
13
+ constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
14
+ super(options);
15
15
  }
16
16
  }