data-structure-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/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/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-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/cjs/data-structures/trie/trie.d.ts +2 -1
- package/dist/cjs/data-structures/trie/trie.js +3 -2
- package/dist/cjs/data-structures/trie/trie.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/mjs/data-structures/trie/trie.d.ts +2 -1
- package/dist/mjs/data-structures/trie/trie.js +3 -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/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
- 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 +8 -5
- package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/overall.test.ts +18 -18
- 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/priority-queue/max-priority-queue.test.ts +3 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +10 -10
|
@@ -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;
|
|
@@ -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
|
}
|
|
@@ -231,9 +231,10 @@ class Trie {
|
|
|
231
231
|
* @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
232
232
|
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
233
233
|
* @param {number} max - The max count of words will be found
|
|
234
|
+
* @param isAllWhenEmptyPrefix - If true, when the prefix provided as '', returns all the words in the trie.
|
|
234
235
|
* @returns {string[]} an array of strings.
|
|
235
236
|
*/
|
|
236
|
-
getWords(prefix = '', max = Number.MAX_SAFE_INTEGER) {
|
|
237
|
+
getWords(prefix = '', max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false) {
|
|
237
238
|
prefix = this._caseProcess(prefix);
|
|
238
239
|
const words = [];
|
|
239
240
|
let found = 0;
|
|
@@ -259,7 +260,7 @@ class Trie {
|
|
|
259
260
|
startNode = nodeC;
|
|
260
261
|
}
|
|
261
262
|
}
|
|
262
|
-
if (startNode !== this.root)
|
|
263
|
+
if (isAllWhenEmptyPrefix || startNode !== this.root)
|
|
263
264
|
dfs(startNode, prefix);
|
|
264
265
|
return words;
|
|
265
266
|
}
|