data-structure-typed 0.9.16 → 1.3.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/LICENSE +21 -0
- package/README.md +665 -172
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.LICENSE.txt +13 -0
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -14
- package/dist/data-structures/binary-tree/avl-tree.js +142 -116
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
- package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
- package/dist/data-structures/binary-tree/bst.d.ts +117 -23
- package/dist/data-structures/binary-tree/bst.js +233 -240
- package/dist/data-structures/binary-tree/index.d.ts +1 -0
- package/dist/data-structures/binary-tree/index.js +1 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
- package/dist/data-structures/binary-tree/rb-tree.js +40 -5
- package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
- package/dist/data-structures/binary-tree/segment-tree.js +126 -93
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
- package/dist/data-structures/graph/abstract-graph.js +610 -572
- package/dist/data-structures/graph/directed-graph.d.ts +173 -16
- package/dist/data-structures/graph/directed-graph.js +345 -313
- package/dist/data-structures/graph/index.d.ts +1 -0
- package/dist/data-structures/graph/index.js +1 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +111 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +111 -9
- package/dist/data-structures/graph/undirected-graph.js +203 -178
- package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
- package/dist/data-structures/hash/coordinate-map.js +59 -36
- package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
- package/dist/data-structures/hash/coordinate-set.js +49 -33
- package/dist/data-structures/hash/hash-table.d.ts +2 -1
- package/dist/data-structures/hash/hash-table.js +4 -0
- package/dist/data-structures/hash/pair.d.ts +2 -1
- package/dist/data-structures/hash/pair.js +4 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -1
- package/dist/data-structures/hash/tree-map.js +4 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -1
- package/dist/data-structures/hash/tree-set.js +4 -0
- package/dist/data-structures/heap/heap.d.ts +62 -45
- package/dist/data-structures/heap/heap.js +124 -86
- package/dist/data-structures/heap/max-heap.d.ts +13 -5
- package/dist/data-structures/heap/max-heap.js +18 -28
- package/dist/data-structures/heap/min-heap.d.ts +14 -5
- package/dist/data-structures/heap/min-heap.js +19 -28
- package/dist/data-structures/index.d.ts +1 -1
- package/dist/data-structures/index.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
- package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
- package/dist/data-structures/linked-list/index.d.ts +1 -0
- package/dist/data-structures/linked-list/index.js +1 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -315
- package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +12 -0
- package/dist/data-structures/matrix/matrix.js +21 -8
- package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
- package/dist/data-structures/matrix/matrix2d.js +146 -80
- package/dist/data-structures/matrix/navigator.d.ts +36 -1
- package/dist/data-structures/matrix/navigator.js +46 -37
- package/dist/data-structures/matrix/vector2d.d.ts +142 -15
- package/dist/data-structures/matrix/vector2d.js +215 -109
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
- package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
- package/dist/data-structures/priority-queue/priority-queue.js +244 -143
- package/dist/data-structures/queue/deque.d.ts +141 -13
- package/dist/data-structures/queue/deque.js +200 -82
- package/dist/data-structures/queue/queue.d.ts +65 -38
- package/dist/data-structures/queue/queue.js +110 -66
- package/dist/data-structures/stack/stack.d.ts +27 -32
- package/dist/data-structures/stack/stack.js +47 -53
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +14 -0
- package/dist/data-structures/tree/tree.js +60 -0
- package/dist/data-structures/trie/trie.d.ts +33 -10
- package/dist/data-structures/trie/trie.js +123 -208
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
- package/dist/interfaces/abstract-graph.d.ts +17 -0
- package/dist/interfaces/avl-tree.d.ts +9 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/bst.d.ts +17 -0
- package/dist/interfaces/directed-graph.d.ts +12 -0
- package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
- package/dist/interfaces/index.js +31 -0
- package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
- package/dist/interfaces/rb-tree.d.ts +8 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/interfaces/tree-multiset.d.ts +7 -0
- package/dist/interfaces/tree-multiset.js +2 -0
- package/dist/interfaces/undirected-graph.d.ts +5 -0
- package/dist/interfaces/undirected-graph.js +2 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
- package/dist/types/data-structures/abstract-binary-tree.js +25 -0
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/abstract-graph.js +2 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.js +2 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/bst.js +9 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/directed-graph.js +9 -0
- package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/heap.d.ts +3 -0
- package/dist/types/data-structures/heap.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/index.d.ts +3 -1
- package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/map-graph.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
- package/dist/types/data-structures/navigator.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
- package/dist/types/data-structures/priority-queue.js +2 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/rb-tree.js +8 -0
- package/dist/types/data-structures/segment-tree.js +2 -0
- package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/singly-linked-list.js +2 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/data-structures/tree-multiset.js +2 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/helpers.js +2 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/utils.d.ts +17 -103
- package/dist/utils/utils.js +40 -625
- package/package.json +96 -23
- package/.idea/data-structure-typed.iml +0 -12
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/dist/data-structures/trampoline.d.ts +0 -16
- package/dist/data-structures/trampoline.js +0 -130
- package/dist/data-structures/types/abstract-graph.d.ts +0 -29
- package/dist/data-structures/types/avl-tree.d.ts +0 -5
- package/dist/data-structures/types/binary-tree.d.ts +0 -16
- package/dist/data-structures/types/bst.d.ts +0 -7
- package/dist/data-structures/types/directed-graph.d.ts +0 -10
- package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
- package/dist/data-structures/types/heap.d.ts +0 -7
- package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
- package/dist/data-structures/types/tree-multiset.d.ts +0 -5
- package/dist/data-structures/types/utils.d.ts +0 -52
- package/dist/data-structures/types/utils.js +0 -54
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -227
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1133
- package/src/data-structures/binary-tree/bst.ts +0 -395
- package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/src/data-structures/binary-tree/index.ts +0 -11
- package/src/data-structures/binary-tree/rb-tree.ts +0 -3
- package/src/data-structures/binary-tree/segment-tree.ts +0 -172
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/diagrams/README.md +0 -7
- package/src/data-structures/graph/abstract-graph.ts +0 -753
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list.png +0 -0
- package/src/data-structures/graph/diagrams/max-flow.png +0 -0
- package/src/data-structures/graph/diagrams/mst.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/src/data-structures/graph/directed-graph.ts +0 -306
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -155
- package/src/data-structures/hash/coordinate-map.ts +0 -24
- package/src/data-structures/hash/coordinate-set.ts +0 -20
- package/src/data-structures/hash/index.ts +0 -6
- package/src/data-structures/heap/heap.ts +0 -127
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -23
- package/src/data-structures/heap/min-heap.ts +0 -25
- package/src/data-structures/index.ts +0 -12
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -13
- package/src/data-structures/matrix/matrix2d.ts +0 -125
- package/src/data-structures/matrix/navigator.ts +0 -87
- package/src/data-structures/matrix/vector2d.ts +0 -189
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
- package/src/data-structures/priority-queue/priority-queue.ts +0 -200
- package/src/data-structures/queue/deque.ts +0 -139
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -122
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -103
- package/src/data-structures/trampoline.ts +0 -51
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -203
- package/src/data-structures/types/abstract-graph.ts +0 -51
- package/src/data-structures/types/avl-tree.ts +0 -6
- package/src/data-structures/types/binary-tree.ts +0 -15
- package/src/data-structures/types/bst.ts +0 -5
- package/src/data-structures/types/directed-graph.ts +0 -18
- package/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/src/data-structures/types/heap.ts +0 -8
- package/src/data-structures/types/navigator.ts +0 -12
- package/src/data-structures/types/priority-queue.ts +0 -9
- package/src/data-structures/types/segment-tree.ts +0 -1
- package/src/data-structures/types/singly-linked-list.ts +0 -15
- package/src/data-structures/types/tree-multiset.ts +0 -3
- package/src/data-structures/types/utils.ts +0 -173
- package/src/index.ts +0 -1
- package/src/utils/index.ts +0 -1
- package/src/utils/utils.ts +0 -505
- package/tsconfig.json +0 -56
- /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
- /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
- /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
- /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
- /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
- /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
- /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
- /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
- /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
- /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
- /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
- /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
- /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
- /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
- /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
- /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
|
@@ -1,39 +1,55 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
3
|
exports.CoordinateSet = void 0;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
4
|
+
/**
|
|
5
|
+
* data-structure-typed
|
|
6
|
+
*
|
|
7
|
+
* @author Tyler Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
+
* @license MIT License
|
|
10
|
+
*/
|
|
11
|
+
class CoordinateSet extends Set {
|
|
12
|
+
constructor(joint) {
|
|
13
|
+
super();
|
|
14
|
+
this._joint = '_';
|
|
24
15
|
if (joint !== undefined)
|
|
25
|
-
|
|
26
|
-
return _this;
|
|
16
|
+
this._joint = joint;
|
|
27
17
|
}
|
|
28
|
-
|
|
29
|
-
return
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
18
|
+
get joint() {
|
|
19
|
+
return this._joint;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The "has" function overrides the "has" method of the superclass and checks if a value exists in an array after
|
|
23
|
+
* joining its elements with a specified separator.
|
|
24
|
+
* @param {number[]} value - The parameter "value" is an array of numbers.
|
|
25
|
+
* @returns The overridden `has` method is returning the result of calling the `has` method of the superclass, passing
|
|
26
|
+
* in the joined value as an argument.
|
|
27
|
+
*/
|
|
28
|
+
has(value) {
|
|
29
|
+
return super.has(value.join(this._joint));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The "add" function overrides the parent class's "add" function by joining the elements of the input array with a
|
|
33
|
+
* specified delimiter before calling the parent class's "add" function.
|
|
34
|
+
* @param {number[]} value - An array of numbers
|
|
35
|
+
* @returns The overridden `add` method is returning the result of calling the `add` method of the superclass
|
|
36
|
+
* (`super.add`) with the joined string representation of the `value` array (`value.join(this._joint)`).
|
|
37
|
+
*/
|
|
38
|
+
add(value) {
|
|
39
|
+
return super.add(value.join(this._joint));
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The function overrides the delete method and deletes an element from a Set by joining the elements of the input
|
|
43
|
+
* array with a specified joint and then calling the delete method of the parent class.
|
|
44
|
+
* @param {number[]} value - An array of numbers
|
|
45
|
+
* @returns The `delete` method is returning the result of calling the `delete` method of the superclass, with the
|
|
46
|
+
* `value` array joined together using the `_joint` property.
|
|
47
|
+
*/
|
|
48
|
+
delete(value) {
|
|
49
|
+
return super.delete(value.join(this._joint));
|
|
50
|
+
}
|
|
51
|
+
_setJoint(v) {
|
|
52
|
+
this._joint = v;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
39
55
|
exports.CoordinateSet = CoordinateSet;
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export declare class HashTable {
|
|
2
|
+
}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export declare class Pair {
|
|
2
|
+
}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export declare class TreeMap {
|
|
2
|
+
}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export declare class TreeSet {
|
|
2
|
+
}
|
|
@@ -1,66 +1,83 @@
|
|
|
1
|
-
import { PriorityQueue } from '../priority-queue';
|
|
2
|
-
import type { HeapItem, HeapOptions } from '../types';
|
|
3
1
|
/**
|
|
4
|
-
*
|
|
5
|
-
* @license MIT
|
|
2
|
+
* data-structure-typed
|
|
6
3
|
*
|
|
7
|
-
* @
|
|
8
|
-
* @
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
9
7
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
import { PriorityQueue } from '../priority-queue';
|
|
9
|
+
import type { HeapOptions } from '../../types';
|
|
10
|
+
export declare class HeapItem<T = number> {
|
|
13
11
|
/**
|
|
14
|
-
*
|
|
15
|
-
* @
|
|
16
|
-
*
|
|
12
|
+
* The constructor function initializes an instance of a class with a priority and a value.
|
|
13
|
+
* @param {number} priority - The `priority` parameter is a number that represents the priority of the value. It is
|
|
14
|
+
* optional and has a default value of `NaN`.
|
|
15
|
+
* @param {T | null} [val=null] - The `val` parameter is of type `T | null`, which means it can accept a value of type
|
|
16
|
+
* `T` or `null`.
|
|
17
|
+
*/
|
|
18
|
+
constructor(priority?: number, val?: T | null);
|
|
19
|
+
private _priority;
|
|
20
|
+
get priority(): number;
|
|
21
|
+
set priority(value: number);
|
|
22
|
+
private _val;
|
|
23
|
+
get val(): T | null;
|
|
24
|
+
set val(value: T | null);
|
|
25
|
+
}
|
|
26
|
+
export declare abstract class Heap<T = number> {
|
|
27
|
+
/**
|
|
28
|
+
* The function is a constructor for a class that initializes a priority callback function based on the
|
|
29
|
+
* options provided.
|
|
30
|
+
* @param [options] - An optional object that contains configuration options for the Heap.
|
|
17
31
|
*/
|
|
18
32
|
protected constructor(options?: HeapOptions<T>);
|
|
33
|
+
protected abstract _pq: PriorityQueue<HeapItem<T>>;
|
|
34
|
+
get pq(): PriorityQueue<HeapItem<T>>;
|
|
35
|
+
protected _priorityExtractor: (val: T) => number;
|
|
36
|
+
get priorityExtractor(): (val: T) => number;
|
|
19
37
|
/**
|
|
20
|
-
*
|
|
21
|
-
* @returns
|
|
38
|
+
* The function returns the size of a priority queue.
|
|
39
|
+
* @returns The size of the priority queue.
|
|
22
40
|
*/
|
|
23
41
|
get size(): number;
|
|
24
42
|
/**
|
|
25
|
-
*
|
|
26
|
-
* @returns {boolean}
|
|
43
|
+
* The function checks if a priority queue is empty.
|
|
44
|
+
* @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
|
|
27
45
|
*/
|
|
28
46
|
isEmpty(): boolean;
|
|
47
|
+
peek(isItem?: undefined): T | undefined;
|
|
48
|
+
peek(isItem: false): T | undefined;
|
|
49
|
+
peek(isItem: true): HeapItem<T> | null;
|
|
50
|
+
peekLast(isItem?: undefined): T | undefined;
|
|
51
|
+
peekLast(isItem: false): T | undefined;
|
|
52
|
+
peekLast(isItem: true): HeapItem<T> | null;
|
|
29
53
|
/**
|
|
30
|
-
*
|
|
31
|
-
* @
|
|
32
|
-
*
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
*
|
|
37
|
-
* @public
|
|
38
|
-
* @returns {object}
|
|
39
|
-
*/
|
|
40
|
-
peekLast(): HeapItem<T> | null;
|
|
41
|
-
/**
|
|
42
|
-
* Adds an element to the queue
|
|
43
|
-
* @public
|
|
44
|
-
* @param {any} element
|
|
45
|
-
* @param priority
|
|
54
|
+
* The `add` function adds an val to a priority queue with an optional priority value.
|
|
55
|
+
* @param {T} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
|
|
56
|
+
* type.
|
|
57
|
+
* @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
|
|
58
|
+
* val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
|
|
59
|
+
* the value of `val`. If the `val` parameter is not a number, then the
|
|
60
|
+
* @returns The `add` method returns the instance of the `Heap` class.
|
|
46
61
|
* @throws {Error} if priority is not a valid number
|
|
47
62
|
*/
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
* @returns {object}
|
|
53
|
-
*/
|
|
54
|
-
poll(): HeapItem<T> | null;
|
|
63
|
+
add(priority: number, val?: T): Heap<T>;
|
|
64
|
+
poll(isItem?: undefined): T | undefined;
|
|
65
|
+
poll(isItem: false): T | undefined;
|
|
66
|
+
poll(isItem: true): HeapItem<T> | null;
|
|
55
67
|
/**
|
|
56
|
-
*
|
|
57
|
-
* @
|
|
58
|
-
* @returns
|
|
68
|
+
* The function checks if a given node or value exists in the priority queue.
|
|
69
|
+
* @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
|
|
70
|
+
* @returns a boolean value.
|
|
59
71
|
*/
|
|
60
|
-
|
|
72
|
+
has(node: T | HeapItem<T>): boolean;
|
|
73
|
+
toArray(isItem?: undefined): (T | undefined)[];
|
|
74
|
+
toArray(isItem: false): (T | undefined)[];
|
|
75
|
+
toArray(isItem: true): (HeapItem<T> | null)[];
|
|
76
|
+
sort(isItem?: undefined): (T | undefined)[];
|
|
77
|
+
sort(isItem: false): (T | undefined)[];
|
|
78
|
+
sort(isItem: true): (HeapItem<T> | null)[];
|
|
61
79
|
/**
|
|
62
|
-
*
|
|
63
|
-
* @public
|
|
80
|
+
* The clear function clears the priority queue.
|
|
64
81
|
*/
|
|
65
82
|
clear(): void;
|
|
66
83
|
}
|
|
@@ -1,119 +1,157 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Heap = void 0;
|
|
4
|
-
|
|
5
|
-
* @copyright 2021 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT
|
|
7
|
-
*
|
|
8
|
-
* @abstract
|
|
9
|
-
* @class Heap
|
|
10
|
-
*/
|
|
11
|
-
var Heap = /** @class */ (function () {
|
|
3
|
+
exports.Heap = exports.HeapItem = void 0;
|
|
4
|
+
class HeapItem {
|
|
12
5
|
/**
|
|
13
|
-
*
|
|
14
|
-
* @
|
|
15
|
-
*
|
|
6
|
+
* The constructor function initializes an instance of a class with a priority and a value.
|
|
7
|
+
* @param {number} priority - The `priority` parameter is a number that represents the priority of the value. It is
|
|
8
|
+
* optional and has a default value of `NaN`.
|
|
9
|
+
* @param {T | null} [val=null] - The `val` parameter is of type `T | null`, which means it can accept a value of type
|
|
10
|
+
* `T` or `null`.
|
|
16
11
|
*/
|
|
17
|
-
|
|
12
|
+
constructor(priority = Number.MAX_SAFE_INTEGER, val = null) {
|
|
13
|
+
this._val = val;
|
|
14
|
+
this._priority = priority;
|
|
15
|
+
}
|
|
16
|
+
get priority() {
|
|
17
|
+
return this._priority;
|
|
18
|
+
}
|
|
19
|
+
set priority(value) {
|
|
20
|
+
this._priority = value;
|
|
21
|
+
}
|
|
22
|
+
get val() {
|
|
23
|
+
return this._val;
|
|
24
|
+
}
|
|
25
|
+
set val(value) {
|
|
26
|
+
this._val = value;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.HeapItem = HeapItem;
|
|
30
|
+
class Heap {
|
|
31
|
+
/**
|
|
32
|
+
* The function is a constructor for a class that initializes a priority callback function based on the
|
|
33
|
+
* options provided.
|
|
34
|
+
* @param [options] - An optional object that contains configuration options for the Heap.
|
|
35
|
+
*/
|
|
36
|
+
constructor(options) {
|
|
18
37
|
if (options) {
|
|
19
|
-
|
|
20
|
-
if (
|
|
38
|
+
const { priorityExtractor } = options;
|
|
39
|
+
if (priorityExtractor !== undefined && typeof priorityExtractor !== 'function') {
|
|
21
40
|
throw new Error('.constructor expects a valid priority function');
|
|
22
41
|
}
|
|
23
|
-
this.
|
|
42
|
+
this._priorityExtractor = priorityExtractor || ((el) => +el);
|
|
24
43
|
}
|
|
25
44
|
else {
|
|
26
|
-
this.
|
|
45
|
+
this._priorityExtractor = (el) => +el;
|
|
27
46
|
}
|
|
28
47
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return this._pq.size;
|
|
36
|
-
},
|
|
37
|
-
enumerable: false,
|
|
38
|
-
configurable: true
|
|
39
|
-
});
|
|
48
|
+
get pq() {
|
|
49
|
+
return this._pq;
|
|
50
|
+
}
|
|
51
|
+
get priorityExtractor() {
|
|
52
|
+
return this._priorityExtractor;
|
|
53
|
+
}
|
|
40
54
|
/**
|
|
41
|
-
*
|
|
42
|
-
* @returns
|
|
55
|
+
* The function returns the size of a priority queue.
|
|
56
|
+
* @returns The size of the priority queue.
|
|
43
57
|
*/
|
|
44
|
-
|
|
58
|
+
get size() {
|
|
59
|
+
return this._pq.size;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The function checks if a priority queue is empty.
|
|
63
|
+
* @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
|
|
64
|
+
*/
|
|
65
|
+
isEmpty() {
|
|
45
66
|
return this._pq.size < 1;
|
|
46
|
-
}
|
|
67
|
+
}
|
|
47
68
|
/**
|
|
48
|
-
*
|
|
49
|
-
* @
|
|
50
|
-
* @returns {object}
|
|
69
|
+
* The `peek` function returns the top item in the priority queue without removing it.
|
|
70
|
+
* @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an val with the highest priority in the queue
|
|
51
71
|
*/
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
72
|
+
peek(isItem) {
|
|
73
|
+
isItem = isItem !== null && isItem !== void 0 ? isItem : false;
|
|
74
|
+
const peeked = this._pq.peek();
|
|
75
|
+
return isItem ? peeked : peeked === null || peeked === void 0 ? void 0 : peeked.val;
|
|
76
|
+
}
|
|
55
77
|
/**
|
|
56
|
-
*
|
|
57
|
-
* @
|
|
58
|
-
* @returns {object}
|
|
78
|
+
* The `peekLast` function returns the last item in the heap.
|
|
79
|
+
* @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
|
|
59
80
|
*/
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
81
|
+
peekLast(isItem) {
|
|
82
|
+
isItem = isItem !== null && isItem !== void 0 ? isItem : false;
|
|
83
|
+
const leafItem = this._pq.leaf();
|
|
84
|
+
return isItem ? leafItem : leafItem === null || leafItem === void 0 ? void 0 : leafItem.val;
|
|
85
|
+
}
|
|
63
86
|
/**
|
|
64
|
-
*
|
|
65
|
-
* @
|
|
66
|
-
*
|
|
67
|
-
* @param priority
|
|
87
|
+
* The `add` function adds an val to a priority queue with an optional priority value.
|
|
88
|
+
* @param {T} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
|
|
89
|
+
* type.
|
|
90
|
+
* @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
|
|
91
|
+
* val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
|
|
92
|
+
* the value of `val`. If the `val` parameter is not a number, then the
|
|
93
|
+
* @returns The `add` method returns the instance of the `Heap` class.
|
|
68
94
|
* @throws {Error} if priority is not a valid number
|
|
69
95
|
*/
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
75
|
-
if (priority === undefined) {
|
|
76
|
-
throw new Error('.offer expects a numeric priority');
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
if (priority && Number.isNaN(+priority)) {
|
|
80
|
-
throw new Error('.offer expects a numeric priority');
|
|
81
|
-
}
|
|
82
|
-
if (Number.isNaN(+priority) && Number.isNaN(this._priorityCb(element))) {
|
|
83
|
-
throw new Error('.offer expects a numeric priority '
|
|
84
|
-
+ 'or a constructor callback that returns a number');
|
|
85
|
-
}
|
|
86
|
-
var _priority = !Number.isNaN(+priority) ? priority : this._priorityCb(element);
|
|
87
|
-
this._pq.offer({ priority: _priority, element: element });
|
|
96
|
+
add(priority, val) {
|
|
97
|
+
val = (val === undefined) ? priority : val;
|
|
98
|
+
this._pq.add(new HeapItem(priority, val));
|
|
88
99
|
return this;
|
|
89
|
-
}
|
|
100
|
+
}
|
|
90
101
|
/**
|
|
91
|
-
* Removes and returns an
|
|
92
|
-
* @
|
|
93
|
-
* @returns {object}
|
|
102
|
+
* The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an val with the highest priority in the queue
|
|
103
|
+
* @returns either a HeapItem<T> object or null.
|
|
94
104
|
*/
|
|
95
|
-
|
|
96
|
-
|
|
105
|
+
poll(isItem) {
|
|
106
|
+
isItem = isItem !== null && isItem !== void 0 ? isItem : false;
|
|
107
|
+
const top = this._pq.poll();
|
|
97
108
|
if (!top) {
|
|
98
109
|
return null;
|
|
99
110
|
}
|
|
100
|
-
return top;
|
|
101
|
-
}
|
|
111
|
+
return isItem ? top : top.val;
|
|
112
|
+
}
|
|
102
113
|
/**
|
|
103
|
-
*
|
|
104
|
-
* @
|
|
105
|
-
* @returns
|
|
114
|
+
* The function checks if a given node or value exists in the priority queue.
|
|
115
|
+
* @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
|
|
116
|
+
* @returns a boolean value.
|
|
106
117
|
*/
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
118
|
+
has(node) {
|
|
119
|
+
if (node instanceof HeapItem) {
|
|
120
|
+
return this.pq.getNodes().includes(node);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
return this.pq.getNodes().findIndex(item => {
|
|
124
|
+
return item.val === node;
|
|
125
|
+
}) !== -1;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The `toArray` function returns an array of `HeapItem<T>` objects.
|
|
130
|
+
* @returns An array of HeapItem<T> objects.Returns a sorted list of vals
|
|
131
|
+
*/
|
|
132
|
+
toArray(isItem) {
|
|
133
|
+
isItem = isItem !== null && isItem !== void 0 ? isItem : false;
|
|
134
|
+
const itemArray = this._pq.toArray();
|
|
135
|
+
return isItem ? itemArray : itemArray.map(item => item.val);
|
|
136
|
+
}
|
|
110
137
|
/**
|
|
111
|
-
*
|
|
112
|
-
*
|
|
138
|
+
* The function sorts the elements in the priority queue and returns either the sorted items or their values depending
|
|
139
|
+
* on the value of the isItem parameter.
|
|
140
|
+
* @param {boolean} [isItem] - The `isItem` parameter is a boolean flag that indicates whether the sorted result should
|
|
141
|
+
* be an array of `HeapItem<T>` objects or an array of the values (`T`) of those objects. If `isItem` is `true`, the
|
|
142
|
+
* sorted result will be an array of `HeapItem
|
|
143
|
+
* @returns an array of either `HeapItem<T>`, `null`, `T`, or `undefined` values.
|
|
113
144
|
*/
|
|
114
|
-
|
|
145
|
+
sort(isItem) {
|
|
146
|
+
isItem = isItem !== null && isItem !== void 0 ? isItem : false;
|
|
147
|
+
const sorted = this._pq.sort();
|
|
148
|
+
return isItem ? sorted : sorted.map(item => item.val);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The clear function clears the priority queue.
|
|
152
|
+
*/
|
|
153
|
+
clear() {
|
|
115
154
|
this._pq.clear();
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
}());
|
|
155
|
+
}
|
|
156
|
+
}
|
|
119
157
|
exports.Heap = Heap;
|
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
4
7
|
*/
|
|
5
|
-
import { Heap } from './heap';
|
|
8
|
+
import { Heap, HeapItem } from './heap';
|
|
6
9
|
import { PriorityQueue } from '../priority-queue';
|
|
7
|
-
import type {
|
|
10
|
+
import type { HeapOptions } from '../../types';
|
|
8
11
|
/**
|
|
9
12
|
* @class MaxHeap
|
|
10
13
|
* @extends Heap
|
|
11
14
|
*/
|
|
12
|
-
export declare class MaxHeap<T> extends Heap<T> {
|
|
15
|
+
export declare class MaxHeap<T = number> extends Heap<T> {
|
|
13
16
|
protected _pq: PriorityQueue<HeapItem<T>>;
|
|
17
|
+
/**
|
|
18
|
+
* The constructor initializes a PriorityQueue with a custom comparator function.
|
|
19
|
+
* @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
|
|
20
|
+
* type `HeapOptions<T>`, which is a generic type that represents the options for the heap.
|
|
21
|
+
*/
|
|
14
22
|
constructor(options?: HeapOptions<T>);
|
|
15
23
|
}
|
|
@@ -1,40 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Tyler Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
5
8
|
*/
|
|
6
|
-
var __extends = (this && this.__extends) || (function () {
|
|
7
|
-
var extendStatics = function (d, b) {
|
|
8
|
-
extendStatics = Object.setPrototypeOf ||
|
|
9
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
10
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
11
|
-
return extendStatics(d, b);
|
|
12
|
-
};
|
|
13
|
-
return function (d, b) {
|
|
14
|
-
if (typeof b !== "function" && b !== null)
|
|
15
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
16
|
-
extendStatics(d, b);
|
|
17
|
-
function __() { this.constructor = d; }
|
|
18
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
19
|
-
};
|
|
20
|
-
})();
|
|
21
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
10
|
exports.MaxHeap = void 0;
|
|
23
|
-
|
|
24
|
-
|
|
11
|
+
const heap_1 = require("./heap");
|
|
12
|
+
const priority_queue_1 = require("../priority-queue");
|
|
25
13
|
/**
|
|
26
14
|
* @class MaxHeap
|
|
27
15
|
* @extends Heap
|
|
28
16
|
*/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
17
|
+
class MaxHeap extends heap_1.Heap {
|
|
18
|
+
/**
|
|
19
|
+
* The constructor initializes a PriorityQueue with a custom comparator function.
|
|
20
|
+
* @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
|
|
21
|
+
* type `HeapOptions<T>`, which is a generic type that represents the options for the heap.
|
|
22
|
+
*/
|
|
23
|
+
constructor(options) {
|
|
24
|
+
super(options);
|
|
25
|
+
this._pq = new priority_queue_1.PriorityQueue({
|
|
26
|
+
comparator: (a, b) => b.priority - a.priority
|
|
35
27
|
});
|
|
36
|
-
return _this;
|
|
37
28
|
}
|
|
38
|
-
|
|
39
|
-
}(heap_1.Heap));
|
|
29
|
+
}
|
|
40
30
|
exports.MaxHeap = MaxHeap;
|
|
@@ -1,15 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
4
7
|
*/
|
|
5
|
-
import { Heap } from './heap';
|
|
8
|
+
import { Heap, HeapItem } from './heap';
|
|
6
9
|
import { PriorityQueue } from '../priority-queue';
|
|
7
|
-
import type {
|
|
10
|
+
import type { HeapOptions } from '../../types';
|
|
8
11
|
/**
|
|
9
12
|
* @class MinHeap
|
|
10
13
|
* @extends Heap
|
|
11
14
|
*/
|
|
12
|
-
export declare class MinHeap<T> extends Heap<T> {
|
|
15
|
+
export declare class MinHeap<T = number> extends Heap<T> {
|
|
13
16
|
protected _pq: PriorityQueue<HeapItem<T>>;
|
|
17
|
+
/**
|
|
18
|
+
* The constructor initializes a PriorityQueue with a comparator function that compares the priority of two HeapItem
|
|
19
|
+
* objects.
|
|
20
|
+
* @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
|
|
21
|
+
* type `HeapOptions<T>`, which is a generic type that represents the options for the heap.
|
|
22
|
+
*/
|
|
14
23
|
constructor(options?: HeapOptions<T>);
|
|
15
24
|
}
|