data-structure-typed 1.15.2 → 1.16.1
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/README.md +24 -24
- package/dist/data-structures/heap/heap.d.ts +30 -15
- package/dist/data-structures/heap/heap.js +60 -15
- package/dist/data-structures/heap/max-heap.d.ts +3 -3
- package/dist/data-structures/heap/min-heap.d.ts +3 -3
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +4 -5
- package/dist/data-structures/priority-queue/max-priority-queue.js +30 -6
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +4 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +31 -6
- package/dist/data-structures/priority-queue/priority-queue.d.ts +13 -2
- package/dist/data-structures/priority-queue/priority-queue.js +28 -22
- 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 +9 -0
- package/dist/data-structures/tree/tree.js +52 -0
- package/dist/data-structures/types/heap.d.ts +0 -4
- package/dist/utils/types/utils.d.ts +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +92 -0
- package/docs/assets/main.js +58 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1367 -0
- package/docs/classes/AVLTree.html +2192 -0
- package/docs/classes/AVLTreeNode.html +574 -0
- package/docs/classes/AaTree.html +150 -0
- package/docs/classes/AbstractEdge.html +269 -0
- package/docs/classes/AbstractGraph.html +927 -0
- package/docs/classes/AbstractVertex.html +215 -0
- package/docs/classes/ArrayDeque.html +417 -0
- package/docs/classes/BST.html +2038 -0
- package/docs/classes/BSTNode.html +570 -0
- package/docs/classes/BTree.html +150 -0
- package/docs/classes/BinaryIndexedTree.html +289 -0
- package/docs/classes/BinaryTree.html +1827 -0
- package/docs/classes/BinaryTreeNode.html +533 -0
- package/docs/classes/Character.html +198 -0
- package/docs/classes/CoordinateMap.html +469 -0
- package/docs/classes/CoordinateSet.html +430 -0
- package/docs/classes/Deque.html +767 -0
- package/docs/classes/DirectedEdge.html +354 -0
- package/docs/classes/DirectedGraph.html +1243 -0
- package/docs/classes/DirectedVertex.html +226 -0
- package/docs/classes/DoublyLinkedList.html +733 -0
- package/docs/classes/DoublyLinkedListNode.html +258 -0
- package/docs/classes/Heap.html +482 -0
- package/docs/classes/HeapItem.html +233 -0
- package/docs/classes/Matrix2D.html +480 -0
- package/docs/classes/MatrixNTI2D.html +218 -0
- package/docs/classes/MaxHeap.html +500 -0
- package/docs/classes/MaxPriorityQueue.html +809 -0
- package/docs/classes/MinHeap.html +501 -0
- package/docs/classes/MinPriorityQueue.html +811 -0
- package/docs/classes/Navigator.html +291 -0
- package/docs/classes/ObjectDeque.html +423 -0
- package/docs/classes/PriorityQueue.html +733 -0
- package/docs/classes/Queue.html +370 -0
- package/docs/classes/RBTree.html +150 -0
- package/docs/classes/SegmentTree.html +345 -0
- package/docs/classes/SegmentTreeNode.html +418 -0
- package/docs/classes/SinglyLinkedList.html +1105 -0
- package/docs/classes/SinglyLinkedListNode.html +375 -0
- package/docs/classes/SplayTree.html +150 -0
- package/docs/classes/Stack.html +346 -0
- package/docs/classes/TreeMultiSet.html +2036 -0
- package/docs/classes/TreeNode.html +236 -0
- package/docs/classes/Trie.html +350 -0
- package/docs/classes/TrieNode.html +258 -0
- package/docs/classes/TwoThreeTree.html +150 -0
- package/docs/classes/UndirectedEdge.html +313 -0
- package/docs/classes/UndirectedGraph.html +1080 -0
- package/docs/classes/UndirectedVertex.html +226 -0
- package/docs/classes/Vector2D.html +783 -0
- package/docs/enums/CP.html +159 -0
- package/docs/enums/FamilyPosition.html +159 -0
- package/docs/enums/LoopType.html +160 -0
- package/docs/index.html +494 -0
- package/docs/interfaces/AVLTreeDeleted.html +161 -0
- package/docs/interfaces/BinaryTreeNodeObj.html +168 -0
- package/docs/interfaces/HeapOptions.html +167 -0
- package/docs/interfaces/IDirectedGraph.html +243 -0
- package/docs/interfaces/IGraph.html +427 -0
- package/docs/interfaces/NavigatorParams.html +197 -0
- package/docs/interfaces/PriorityQueueOptions.html +168 -0
- package/docs/modules.html +218 -0
- package/docs/types/BSTComparator.html +140 -0
- package/docs/types/BSTDeletedResult.html +137 -0
- package/docs/types/BinaryTreeDeleted.html +137 -0
- package/docs/types/BinaryTreeNodeId.html +125 -0
- package/docs/types/BinaryTreeNodePropertyName.html +125 -0
- package/docs/types/DFSOrderPattern.html +125 -0
- package/docs/types/DijkstraResult.html +145 -0
- package/docs/types/Direction.html +125 -0
- package/docs/types/DoublyLinkedListGetBy.html +125 -0
- package/docs/types/NodeOrPropertyName.html +125 -0
- package/docs/types/PriorityQueueComparator.html +145 -0
- package/docs/types/PriorityQueueDFSOrderPattern.html +125 -0
- package/docs/types/ResultByProperty.html +130 -0
- package/docs/types/ResultsByProperty.html +130 -0
- package/docs/types/SegmentTreeNodeVal.html +125 -0
- package/docs/types/SpecifyOptional.html +132 -0
- package/docs/types/Thunk.html +133 -0
- package/docs/types/ToThunkFn.html +133 -0
- package/docs/types/TopologicalStatus.html +125 -0
- package/docs/types/TreeMultiSetDeletedResult.html +137 -0
- package/docs/types/TrlAsyncFn.html +138 -0
- package/docs/types/TrlFn.html +138 -0
- package/docs/types/Turning.html +125 -0
- package/docs/types/VertexId.html +125 -0
- package/{tests/unit/data-structures/binary-tree → notes}/bst.test.ts +37 -50
- package/notes/note.md +23 -0
- package/package.json +1 -1
- package/.idea/data-structure-typed.iml +0 -14
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/assets/logo.png +0 -0
- package/src/assets/overview-diagram-of-data-structures.png +0 -0
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -293
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -69
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1492
- package/src/data-structures/binary-tree/bst.ts +0 -497
- 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 -267
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -53
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/diagrams/README.md +0 -5
- package/src/data-structures/graph/abstract-graph.ts +0 -958
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/src/data-structures/graph/diagrams/mst.jpg +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/diagrams/tarjan.webp +0 -0
- package/src/data-structures/graph/directed-graph.ts +0 -429
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -259
- package/src/data-structures/hash/coordinate-map.ts +0 -74
- package/src/data-structures/hash/coordinate-set.ts +0 -63
- package/src/data-structures/hash/hash-table.ts +0 -1
- package/src/data-structures/hash/index.ts +0 -6
- package/src/data-structures/hash/pair.ts +0 -1
- package/src/data-structures/hash/tree-map.ts +0 -1
- package/src/data-structures/hash/tree-set.ts +0 -1
- package/src/data-structures/heap/heap.ts +0 -162
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -31
- package/src/data-structures/heap/min-heap.ts +0 -34
- package/src/data-structures/index.ts +0 -13
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -365
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -757
- 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 -27
- package/src/data-structures/matrix/matrix2d.ts +0 -208
- package/src/data-structures/matrix/navigator.ts +0 -122
- package/src/data-structures/matrix/vector2d.ts +0 -316
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -24
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -24
- package/src/data-structures/priority-queue/priority-queue.ts +0 -349
- package/src/data-structures/queue/deque.ts +0 -251
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -120
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -98
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -225
- 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/index.ts +0 -13
- package/src/data-structures/types/navigator.ts +0 -13
- 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 -1
- package/src/data-structures/types/tree-multiset.ts +0 -3
- package/src/index.ts +0 -1
- package/src/utils/index.ts +0 -2
- package/src/utils/types/index.ts +0 -1
- package/src/utils/types/utils.ts +0 -4
- package/src/utils/utils.ts +0 -78
- package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
- package/tests/unit/data-structures/graph/directed-graph.test.ts +0 -492
- package/tests/unit/data-structures/graph/index.ts +0 -3
- package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
package/README.md
CHANGED
|
@@ -15,6 +15,12 @@ Hash, Coordinate Set, Coordinate Map, Heap, Priority Queue, Max Priority Queue,
|
|
|
15
15
|
|
|
16
16
|
# How
|
|
17
17
|
|
|
18
|
+
## Live Examples
|
|
19
|
+
|
|
20
|
+
[Live Examples](https://data-structure-typed-examples.vercel.app)
|
|
21
|
+
|
|
22
|
+
<a href="https://data-structure-typed-examples.vercel.app" target="_blank">Live Examples</a>
|
|
23
|
+
|
|
18
24
|
## install
|
|
19
25
|
|
|
20
26
|
### yarn
|
|
@@ -130,12 +136,6 @@ npm install data-structure-typed
|
|
|
130
136
|
expect(bfsNodes[2].id).toBe(16);
|
|
131
137
|
```
|
|
132
138
|
|
|
133
|
-
## Live Examples
|
|
134
|
-
|
|
135
|
-
[//]: # ([Live Examples](https://data-structure-typed-examples.vercel.app))
|
|
136
|
-
|
|
137
|
-
<a href="https://data-structure-typed-examples.vercel.app" target="_blank">Live Examples</a>
|
|
138
|
-
|
|
139
139
|
### Directed Graph simple snippet
|
|
140
140
|
|
|
141
141
|
```typescript
|
|
@@ -733,41 +733,41 @@ describe('DirectedGraph Test3', () => {
|
|
|
733
733
|
</tbody>
|
|
734
734
|
</table>
|
|
735
735
|
|
|
736
|
-

|
|
737
737
|
|
|
738
|
-

|
|
739
739
|
|
|
740
|
-

|
|
741
741
|
|
|
742
|
-

|
|
743
743
|
|
|
744
|
-

|
|
745
745
|
|
|
746
|
-

|
|
747
747
|
|
|
748
|
-

|
|
749
749
|
|
|
750
|
-

|
|
751
751
|
|
|
752
|
-

|
|
753
753
|
|
|
754
|
-

|
|
755
755
|
|
|
756
|
-

|
|
757
757
|
|
|
758
|
-

|
|
759
759
|
|
|
760
|
-

|
|
761
761
|
|
|
762
|
-

|
|
763
763
|
|
|
764
|
-

|
|
765
765
|
|
|
766
|
-
[//]: # ()
|
|
767
767
|
|
|
768
|
-
[//]: # ()
|
|
769
769
|
|
|
770
|
-
[//]: # ()
|
|
771
771
|
|
|
772
772
|
|
|
773
773
|
|
|
@@ -6,8 +6,17 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { PriorityQueue } from '../priority-queue';
|
|
9
|
-
import type {
|
|
10
|
-
export declare
|
|
9
|
+
import type { HeapOptions } from '../types';
|
|
10
|
+
export declare class HeapItem<T = number> {
|
|
11
|
+
constructor(priority?: number, val?: T | null);
|
|
12
|
+
private _priority;
|
|
13
|
+
get priority(): number;
|
|
14
|
+
set priority(value: number);
|
|
15
|
+
private _val;
|
|
16
|
+
get val(): T | null;
|
|
17
|
+
set val(value: T | null);
|
|
18
|
+
}
|
|
19
|
+
export declare abstract class Heap<T = number> {
|
|
11
20
|
/**
|
|
12
21
|
* The function is a constructor for a class that initializes a priority callback function based on the
|
|
13
22
|
* options provided.
|
|
@@ -17,9 +26,9 @@ export declare abstract class Heap<T> {
|
|
|
17
26
|
protected abstract _pq: PriorityQueue<HeapItem<T>>;
|
|
18
27
|
get pq(): PriorityQueue<HeapItem<T>>;
|
|
19
28
|
protected set pq(v: PriorityQueue<HeapItem<T>>);
|
|
20
|
-
protected _priorityCb: (
|
|
21
|
-
get priorityCb(): (
|
|
22
|
-
protected set priorityCb(v: (
|
|
29
|
+
protected _priorityCb: (val: T) => number;
|
|
30
|
+
get priorityCb(): (val: T) => number;
|
|
31
|
+
protected set priorityCb(v: (val: T) => number);
|
|
23
32
|
/**
|
|
24
33
|
* The function returns the size of a priority queue.
|
|
25
34
|
* @returns The size of the priority queue.
|
|
@@ -32,7 +41,7 @@ export declare abstract class Heap<T> {
|
|
|
32
41
|
/**
|
|
33
42
|
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
34
43
|
*/
|
|
35
|
-
getPriorityCb(): (
|
|
44
|
+
getPriorityCb(): (val: T) => number;
|
|
36
45
|
/**
|
|
37
46
|
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
38
47
|
*/
|
|
@@ -44,33 +53,39 @@ export declare abstract class Heap<T> {
|
|
|
44
53
|
isEmpty(): boolean;
|
|
45
54
|
/**
|
|
46
55
|
* The `peek` function returns the top item in the priority queue without removing it.
|
|
47
|
-
* @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an
|
|
56
|
+
* @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an val with the highest priority in the queue
|
|
48
57
|
*/
|
|
49
58
|
peek(): HeapItem<T> | null;
|
|
50
59
|
/**
|
|
51
60
|
* The `peekLast` function returns the last item in the heap.
|
|
52
|
-
* @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an
|
|
61
|
+
* @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
|
|
53
62
|
*/
|
|
54
63
|
peekLast(): HeapItem<T> | null;
|
|
55
64
|
/**
|
|
56
|
-
* The `add` function adds an
|
|
57
|
-
* @param {T}
|
|
65
|
+
* The `add` function adds an val to a priority queue with an optional priority value.
|
|
66
|
+
* @param {T} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
|
|
58
67
|
* type.
|
|
59
68
|
* @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
|
|
60
|
-
*
|
|
61
|
-
* the value of `
|
|
69
|
+
* val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
|
|
70
|
+
* the value of `val`. If the `val` parameter is not a number, then the
|
|
62
71
|
* @returns The `add` method returns the instance of the `Heap` class.
|
|
63
72
|
* @throws {Error} if priority is not a valid number
|
|
64
73
|
*/
|
|
65
|
-
add(
|
|
74
|
+
add(val: T, priority?: number): Heap<T>;
|
|
66
75
|
/**
|
|
67
|
-
* The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an
|
|
76
|
+
* 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
|
|
68
77
|
* @returns either a HeapItem<T> object or null.
|
|
69
78
|
*/
|
|
70
79
|
poll(): HeapItem<T> | null;
|
|
80
|
+
/**
|
|
81
|
+
* The function checks if a given node or value exists in the priority queue.
|
|
82
|
+
* @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
|
|
83
|
+
* @returns a boolean value.
|
|
84
|
+
*/
|
|
85
|
+
has(node: T | HeapItem<T>): boolean;
|
|
71
86
|
/**
|
|
72
87
|
* The `toArray` function returns an array of `HeapItem<T>` objects.
|
|
73
|
-
* @returns An array of HeapItem<T> objects.Returns a sorted list of
|
|
88
|
+
* @returns An array of HeapItem<T> objects.Returns a sorted list of vals
|
|
74
89
|
*/
|
|
75
90
|
toArray(): HeapItem<T>[];
|
|
76
91
|
/**
|
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Heap = void 0;
|
|
3
|
+
exports.Heap = exports.HeapItem = void 0;
|
|
4
|
+
var HeapItem = /** @class */ (function () {
|
|
5
|
+
function HeapItem(priority, val) {
|
|
6
|
+
if (priority === void 0) { priority = NaN; }
|
|
7
|
+
if (val === void 0) { val = null; }
|
|
8
|
+
this._val = val;
|
|
9
|
+
this._priority = priority;
|
|
10
|
+
}
|
|
11
|
+
Object.defineProperty(HeapItem.prototype, "priority", {
|
|
12
|
+
get: function () {
|
|
13
|
+
return this._priority;
|
|
14
|
+
},
|
|
15
|
+
set: function (value) {
|
|
16
|
+
this._priority = value;
|
|
17
|
+
},
|
|
18
|
+
enumerable: false,
|
|
19
|
+
configurable: true
|
|
20
|
+
});
|
|
21
|
+
Object.defineProperty(HeapItem.prototype, "val", {
|
|
22
|
+
get: function () {
|
|
23
|
+
return this._val;
|
|
24
|
+
},
|
|
25
|
+
set: function (value) {
|
|
26
|
+
this._val = value;
|
|
27
|
+
},
|
|
28
|
+
enumerable: false,
|
|
29
|
+
configurable: true
|
|
30
|
+
});
|
|
31
|
+
return HeapItem;
|
|
32
|
+
}());
|
|
33
|
+
exports.HeapItem = HeapItem;
|
|
4
34
|
var Heap = /** @class */ (function () {
|
|
5
35
|
/**
|
|
6
36
|
* The function is a constructor for a class that initializes a priority callback function based on the
|
|
@@ -77,31 +107,31 @@ var Heap = /** @class */ (function () {
|
|
|
77
107
|
};
|
|
78
108
|
/**
|
|
79
109
|
* The `peek` function returns the top item in the priority queue without removing it.
|
|
80
|
-
* @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an
|
|
110
|
+
* @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an val with the highest priority in the queue
|
|
81
111
|
*/
|
|
82
112
|
Heap.prototype.peek = function () {
|
|
83
113
|
return this._pq.peek();
|
|
84
114
|
};
|
|
85
115
|
/**
|
|
86
116
|
* The `peekLast` function returns the last item in the heap.
|
|
87
|
-
* @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an
|
|
117
|
+
* @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
|
|
88
118
|
*/
|
|
89
119
|
Heap.prototype.peekLast = function () {
|
|
90
120
|
return this._pq.leaf();
|
|
91
121
|
};
|
|
92
122
|
/**
|
|
93
|
-
* The `add` function adds an
|
|
94
|
-
* @param {T}
|
|
123
|
+
* The `add` function adds an val to a priority queue with an optional priority value.
|
|
124
|
+
* @param {T} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
|
|
95
125
|
* type.
|
|
96
126
|
* @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
|
|
97
|
-
*
|
|
98
|
-
* the value of `
|
|
127
|
+
* val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
|
|
128
|
+
* the value of `val`. If the `val` parameter is not a number, then the
|
|
99
129
|
* @returns The `add` method returns the instance of the `Heap` class.
|
|
100
130
|
* @throws {Error} if priority is not a valid number
|
|
101
131
|
*/
|
|
102
|
-
Heap.prototype.add = function (
|
|
103
|
-
if (typeof
|
|
104
|
-
priority =
|
|
132
|
+
Heap.prototype.add = function (val, priority) {
|
|
133
|
+
if (typeof val === 'number') {
|
|
134
|
+
priority = val;
|
|
105
135
|
}
|
|
106
136
|
else {
|
|
107
137
|
if (priority === undefined) {
|
|
@@ -111,16 +141,16 @@ var Heap = /** @class */ (function () {
|
|
|
111
141
|
if (priority && Number.isNaN(+priority)) {
|
|
112
142
|
throw new Error('.add expects a numeric priority');
|
|
113
143
|
}
|
|
114
|
-
if (Number.isNaN(+priority) && Number.isNaN(this._priorityCb(
|
|
144
|
+
if (Number.isNaN(+priority) && Number.isNaN(this._priorityCb(val))) {
|
|
115
145
|
throw new Error('.add expects a numeric priority '
|
|
116
146
|
+ 'or a constructor callback that returns a number');
|
|
117
147
|
}
|
|
118
|
-
var _priority = !Number.isNaN(+priority) ? priority : this._priorityCb(
|
|
119
|
-
this._pq.add(
|
|
148
|
+
var _priority = !Number.isNaN(+priority) ? priority : this._priorityCb(val);
|
|
149
|
+
this._pq.add(new HeapItem(_priority, val));
|
|
120
150
|
return this;
|
|
121
151
|
};
|
|
122
152
|
/**
|
|
123
|
-
* The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an
|
|
153
|
+
* 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
|
|
124
154
|
* @returns either a HeapItem<T> object or null.
|
|
125
155
|
*/
|
|
126
156
|
Heap.prototype.poll = function () {
|
|
@@ -130,9 +160,24 @@ var Heap = /** @class */ (function () {
|
|
|
130
160
|
}
|
|
131
161
|
return top;
|
|
132
162
|
};
|
|
163
|
+
/**
|
|
164
|
+
* The function checks if a given node or value exists in the priority queue.
|
|
165
|
+
* @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
|
|
166
|
+
* @returns a boolean value.
|
|
167
|
+
*/
|
|
168
|
+
Heap.prototype.has = function (node) {
|
|
169
|
+
if (node instanceof (HeapItem)) {
|
|
170
|
+
return this.getPq().getNodes().includes(node);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
return this.getPq().getNodes().findIndex(function (item) {
|
|
174
|
+
return item.val === node;
|
|
175
|
+
}) !== -1;
|
|
176
|
+
}
|
|
177
|
+
};
|
|
133
178
|
/**
|
|
134
179
|
* The `toArray` function returns an array of `HeapItem<T>` objects.
|
|
135
|
-
* @returns An array of HeapItem<T> objects.Returns a sorted list of
|
|
180
|
+
* @returns An array of HeapItem<T> objects.Returns a sorted list of vals
|
|
136
181
|
*/
|
|
137
182
|
Heap.prototype.toArray = function () {
|
|
138
183
|
return this._pq.toArray();
|
|
@@ -5,14 +5,14 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { Heap } from './heap';
|
|
8
|
+
import { Heap, HeapItem } from './heap';
|
|
9
9
|
import { PriorityQueue } from '../priority-queue';
|
|
10
|
-
import type {
|
|
10
|
+
import type { HeapOptions } from '../types';
|
|
11
11
|
/**
|
|
12
12
|
* @class MaxHeap
|
|
13
13
|
* @extends Heap
|
|
14
14
|
*/
|
|
15
|
-
export declare class MaxHeap<T> extends Heap<T> {
|
|
15
|
+
export declare class MaxHeap<T = number> extends Heap<T> {
|
|
16
16
|
protected _pq: PriorityQueue<HeapItem<T>>;
|
|
17
17
|
/**
|
|
18
18
|
* The constructor initializes a PriorityQueue with a custom comparator function.
|
|
@@ -5,14 +5,14 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { Heap } from './heap';
|
|
8
|
+
import { Heap, HeapItem } from './heap';
|
|
9
9
|
import { PriorityQueue } from '../priority-queue';
|
|
10
|
-
import type {
|
|
10
|
+
import type { HeapOptions } from '../types';
|
|
11
11
|
/**
|
|
12
12
|
* @class MinHeap
|
|
13
13
|
* @extends Heap
|
|
14
14
|
*/
|
|
15
|
-
export declare class MinHeap<T> extends Heap<T> {
|
|
15
|
+
export declare class MinHeap<T = number> extends Heap<T> {
|
|
16
16
|
protected _pq: PriorityQueue<HeapItem<T>>;
|
|
17
17
|
/**
|
|
18
18
|
* The constructor initializes a PriorityQueue with a comparator function that compares the priority of two HeapItem
|
|
@@ -20,6 +20,7 @@ __exportStar(require("./stack"), exports);
|
|
|
20
20
|
__exportStar(require("./queue"), exports);
|
|
21
21
|
__exportStar(require("./graph"), exports);
|
|
22
22
|
__exportStar(require("./binary-tree"), exports);
|
|
23
|
+
__exportStar(require("./tree"), exports);
|
|
23
24
|
__exportStar(require("./heap"), exports);
|
|
24
25
|
__exportStar(require("./priority-queue"), exports);
|
|
25
26
|
__exportStar(require("./matrix"), exports);
|
|
@@ -8,9 +8,8 @@
|
|
|
8
8
|
import { PriorityQueue } from './priority-queue';
|
|
9
9
|
import type { PriorityQueueOptions } from '../types';
|
|
10
10
|
export declare class MaxPriorityQueue<T = number> extends PriorityQueue<T> {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
constructor(options?: PriorityQueueOptions<T>);
|
|
11
|
+
constructor(options?: Omit<PriorityQueueOptions<number>, 'comparator'>);
|
|
12
|
+
constructor(options: PriorityQueueOptions<T>);
|
|
13
|
+
static heapify<T extends number>(options?: Omit<PriorityQueueOptions<T>, 'comparator'>): MaxPriorityQueue<T>;
|
|
14
|
+
static heapify<T>(options: PriorityQueueOptions<T>): MaxPriorityQueue<T>;
|
|
16
15
|
}
|
|
@@ -14,6 +14,17 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
14
14
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
15
|
};
|
|
16
16
|
})();
|
|
17
|
+
var __assign = (this && this.__assign) || function () {
|
|
18
|
+
__assign = Object.assign || function(t) {
|
|
19
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
+
s = arguments[i];
|
|
21
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
+
t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
17
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
29
|
exports.MaxPriorityQueue = void 0;
|
|
19
30
|
/**
|
|
@@ -27,17 +38,30 @@ var priority_queue_1 = require("./priority-queue");
|
|
|
27
38
|
var MaxPriorityQueue = /** @class */ (function (_super) {
|
|
28
39
|
__extends(MaxPriorityQueue, _super);
|
|
29
40
|
/**
|
|
30
|
-
* The constructor initializes a
|
|
31
|
-
* @param [options] -
|
|
41
|
+
* The constructor initializes a priority queue with an optional comparator function.
|
|
42
|
+
* @param [options] - The `options` parameter is an optional object that can contain various properties to configure
|
|
43
|
+
* the priority queue.
|
|
32
44
|
*/
|
|
33
45
|
function MaxPriorityQueue(options) {
|
|
34
|
-
return _super.call(this, {
|
|
35
|
-
nodes: options === null || options === void 0 ? void 0 : options.nodes, comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : function (a, b) {
|
|
46
|
+
return _super.call(this, __assign(__assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : function (a, b) {
|
|
36
47
|
var aKey = a, bKey = b;
|
|
37
48
|
return bKey - aKey;
|
|
38
|
-
}
|
|
39
|
-
}) || this;
|
|
49
|
+
} })) || this;
|
|
40
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* The function `heapify` creates a max priority queue from the given options and returns it.
|
|
53
|
+
* @param options - The `options` parameter is an object that contains configuration options for creating a priority
|
|
54
|
+
* queue. It can have the following properties:
|
|
55
|
+
* @returns a MaxPriorityQueue object.
|
|
56
|
+
*/
|
|
57
|
+
MaxPriorityQueue.heapify = function (options) {
|
|
58
|
+
var maxPQ = new MaxPriorityQueue(__assign(__assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : function (a, b) {
|
|
59
|
+
var aKey = a, bKey = b;
|
|
60
|
+
return bKey - aKey;
|
|
61
|
+
} }));
|
|
62
|
+
maxPQ._fix();
|
|
63
|
+
return maxPQ;
|
|
64
|
+
};
|
|
41
65
|
return MaxPriorityQueue;
|
|
42
66
|
}(priority_queue_1.PriorityQueue));
|
|
43
67
|
exports.MaxPriorityQueue = MaxPriorityQueue;
|
|
@@ -8,9 +8,8 @@
|
|
|
8
8
|
import { PriorityQueue } from './priority-queue';
|
|
9
9
|
import type { PriorityQueueOptions } from '../types';
|
|
10
10
|
export declare class MinPriorityQueue<T = number> extends PriorityQueue<T> {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
constructor(options?: PriorityQueueOptions<T>);
|
|
11
|
+
constructor(options?: Omit<PriorityQueueOptions<number>, 'comparator'>);
|
|
12
|
+
constructor(options: PriorityQueueOptions<T>);
|
|
13
|
+
static heapify<T extends number>(options?: Omit<PriorityQueueOptions<T>, 'comparator'>): MinPriorityQueue<T>;
|
|
14
|
+
static heapify<T>(options: PriorityQueueOptions<T>): MinPriorityQueue<T>;
|
|
16
15
|
}
|
|
@@ -14,6 +14,17 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
14
14
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
15
|
};
|
|
16
16
|
})();
|
|
17
|
+
var __assign = (this && this.__assign) || function () {
|
|
18
|
+
__assign = Object.assign || function(t) {
|
|
19
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
+
s = arguments[i];
|
|
21
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
+
t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
17
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
29
|
exports.MinPriorityQueue = void 0;
|
|
19
30
|
/**
|
|
@@ -27,17 +38,31 @@ var priority_queue_1 = require("./priority-queue");
|
|
|
27
38
|
var MinPriorityQueue = /** @class */ (function (_super) {
|
|
28
39
|
__extends(MinPriorityQueue, _super);
|
|
29
40
|
/**
|
|
30
|
-
* The constructor initializes a
|
|
31
|
-
* @param [options] -
|
|
41
|
+
* The constructor initializes a priority queue with an optional comparator function.
|
|
42
|
+
* @param [options] - The `options` parameter is an optional object that can contain various configuration options for
|
|
43
|
+
* the `PriorityQueue` constructor.
|
|
32
44
|
*/
|
|
33
45
|
function MinPriorityQueue(options) {
|
|
34
|
-
return _super.call(this, {
|
|
35
|
-
nodes: options === null || options === void 0 ? void 0 : options.nodes, comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : function (a, b) {
|
|
46
|
+
return _super.call(this, __assign(__assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : function (a, b) {
|
|
36
47
|
var aKey = a, bKey = b;
|
|
37
48
|
return aKey - bKey;
|
|
38
|
-
}
|
|
39
|
-
}) || this;
|
|
49
|
+
} })) || this;
|
|
40
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* The function `heapify` creates a new MinPriorityQueue instance and sets the comparator function based on the options
|
|
53
|
+
* provided, and then fixes the heap structure of the queue.
|
|
54
|
+
* @param options - The `options` parameter is an object that contains configuration options for creating a priority
|
|
55
|
+
* queue. It can have the following properties:
|
|
56
|
+
* @returns a MinPriorityQueue object.
|
|
57
|
+
*/
|
|
58
|
+
MinPriorityQueue.heapify = function (options) {
|
|
59
|
+
var minPQ = new MinPriorityQueue(__assign(__assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : function (a, b) {
|
|
60
|
+
var aKey = a, bKey = b;
|
|
61
|
+
return aKey - bKey;
|
|
62
|
+
} }));
|
|
63
|
+
minPQ._fix();
|
|
64
|
+
return minPQ;
|
|
65
|
+
};
|
|
41
66
|
return MinPriorityQueue;
|
|
42
67
|
}(priority_queue_1.PriorityQueue));
|
|
43
68
|
exports.MinPriorityQueue = MinPriorityQueue;
|
|
@@ -43,6 +43,13 @@ export declare class PriorityQueue<T = number> {
|
|
|
43
43
|
* that needs to be added to the heap.
|
|
44
44
|
*/
|
|
45
45
|
add(node: T): void;
|
|
46
|
+
/**
|
|
47
|
+
* The "has" function checks if a given node is present in the list of nodes.
|
|
48
|
+
* @param {T} node - The parameter `node` is of type `T`, which means it can be any type. It represents the node that
|
|
49
|
+
* we want to check if it exists in the `nodes` array.
|
|
50
|
+
* @returns a boolean value indicating whether the given node is included in the array of nodes.
|
|
51
|
+
*/
|
|
52
|
+
has(node: T): boolean;
|
|
46
53
|
/**
|
|
47
54
|
* The `peek` function returns the first element of the `nodes` array if it exists, otherwise it returns `null`.
|
|
48
55
|
* @returns The `peek()` function is returning the first element (`T`) of the `nodes` array if the `size` is not zero.
|
|
@@ -89,8 +96,12 @@ export declare class PriorityQueue<T = number> {
|
|
|
89
96
|
*/
|
|
90
97
|
isValid(): boolean;
|
|
91
98
|
/**
|
|
92
|
-
*
|
|
93
|
-
|
|
99
|
+
* Plan to support sorting of duplicate elements.
|
|
100
|
+
*/
|
|
101
|
+
/**
|
|
102
|
+
* The function sorts the elements in a data structure and returns them in an array.
|
|
103
|
+
* Plan to support sorting of duplicate elements.
|
|
104
|
+
* @returns The `sort()` method is returning an array of type `T[]`.
|
|
94
105
|
*/
|
|
95
106
|
sort(): T[];
|
|
96
107
|
/**
|
|
@@ -51,9 +51,9 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
51
51
|
};
|
|
52
52
|
var nodes = options.nodes, comparator = options.comparator, _a = options.isFix, isFix = _a === void 0 ? true : _a;
|
|
53
53
|
this._comparator = comparator;
|
|
54
|
-
if (nodes && nodes
|
|
54
|
+
if (nodes && Array.isArray(nodes) && nodes.length > 0) {
|
|
55
55
|
// TODO support distinct
|
|
56
|
-
this._nodes =
|
|
56
|
+
this._nodes = __spreadArray([], __read(nodes), false);
|
|
57
57
|
isFix && this._fix();
|
|
58
58
|
}
|
|
59
59
|
}
|
|
@@ -94,7 +94,7 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
94
94
|
* @returns the result of calling the `isValid()` method on a new instance of the `PriorityQueue` class.
|
|
95
95
|
*/
|
|
96
96
|
PriorityQueue.isPriorityQueueified = function (options) {
|
|
97
|
-
return new PriorityQueue(__assign(__assign({}, options), { isFix:
|
|
97
|
+
return new PriorityQueue(__assign(__assign({}, options), { isFix: false })).isValid();
|
|
98
98
|
};
|
|
99
99
|
/**
|
|
100
100
|
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
@@ -111,6 +111,15 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
111
111
|
this.nodes.push(node);
|
|
112
112
|
this._heapifyUp(this.size - 1);
|
|
113
113
|
};
|
|
114
|
+
/**
|
|
115
|
+
* The "has" function checks if a given node is present in the list of nodes.
|
|
116
|
+
* @param {T} node - The parameter `node` is of type `T`, which means it can be any type. It represents the node that
|
|
117
|
+
* we want to check if it exists in the `nodes` array.
|
|
118
|
+
* @returns a boolean value indicating whether the given node is included in the array of nodes.
|
|
119
|
+
*/
|
|
120
|
+
PriorityQueue.prototype.has = function (node) {
|
|
121
|
+
return this.nodes.includes(node);
|
|
122
|
+
};
|
|
114
123
|
/**
|
|
115
124
|
* The `peek` function returns the first element of the `nodes` array if it exists, otherwise it returns `null`.
|
|
116
125
|
* @returns The `peek()` function is returning the first element (`T`) of the `nodes` array if the `size` is not zero.
|
|
@@ -182,31 +191,28 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
182
191
|
* @returns The function `isValid()` returns a boolean value.
|
|
183
192
|
*/
|
|
184
193
|
PriorityQueue.prototype.isValid = function () {
|
|
185
|
-
var
|
|
186
|
-
|
|
187
|
-
var
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
var leftChildIndex = (parentIndex * 2) + 1;
|
|
191
|
-
if (!_this._compare(parentIndex, leftChildIndex))
|
|
192
|
-
return false;
|
|
193
|
-
isValidLeft = isValidRecursive(leftChildIndex);
|
|
194
|
+
for (var i = 0; i < this.nodes.length; i++) {
|
|
195
|
+
var leftChildIndex = this._getLeft(i);
|
|
196
|
+
var rightChildIndex = this._getRight(i);
|
|
197
|
+
if (this._isValidIndex(leftChildIndex) && !this._compare(leftChildIndex, i)) {
|
|
198
|
+
return false;
|
|
194
199
|
}
|
|
195
|
-
if (
|
|
196
|
-
|
|
197
|
-
if (!_this._compare(parentIndex, rightChildIndex))
|
|
198
|
-
return false;
|
|
199
|
-
isValidRight = isValidRecursive(rightChildIndex);
|
|
200
|
+
if (this._isValidIndex(rightChildIndex) && !this._compare(rightChildIndex, i)) {
|
|
201
|
+
return false;
|
|
200
202
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
return isValidRecursive(0);
|
|
203
|
+
}
|
|
204
|
+
return true;
|
|
204
205
|
};
|
|
205
206
|
/**
|
|
206
|
-
*
|
|
207
|
-
|
|
207
|
+
* Plan to support sorting of duplicate elements.
|
|
208
|
+
*/
|
|
209
|
+
/**
|
|
210
|
+
* The function sorts the elements in a data structure and returns them in an array.
|
|
211
|
+
* Plan to support sorting of duplicate elements.
|
|
212
|
+
* @returns The `sort()` method is returning an array of type `T[]`.
|
|
208
213
|
*/
|
|
209
214
|
PriorityQueue.prototype.sort = function () {
|
|
215
|
+
// TODO Plan to support sorting of duplicate elements.
|
|
210
216
|
var visitedNode = [];
|
|
211
217
|
while (this.size !== 0) {
|
|
212
218
|
var top = this.poll();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './tree';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./tree"), exports);
|