data-structure-typed 1.18.0 → 1.18.5
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 +193 -66
- package/backup/recursive-type/src/assets/complexities-diff.jpg +0 -0
- package/backup/recursive-type/src/assets/data-structure-complexities.jpg +0 -0
- package/backup/recursive-type/src/assets/logo.png +0 -0
- package/backup/recursive-type/src/assets/overview-diagram-of-data-structures.png +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/aa-tree.ts +3 -0
- package/backup/recursive-type/src/data-structures/binary-tree/avl-tree.ts +288 -0
- package/backup/recursive-type/src/data-structures/binary-tree/b-tree.ts +3 -0
- package/backup/recursive-type/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
- package/backup/recursive-type/src/data-structures/binary-tree/binary-tree.ts +1502 -0
- package/backup/recursive-type/src/data-structures/binary-tree/bst.ts +503 -0
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/index.ts +11 -0
- package/backup/recursive-type/src/data-structures/binary-tree/rb-tree.ts +110 -0
- package/backup/recursive-type/src/data-structures/binary-tree/segment-tree.ts +243 -0
- package/backup/recursive-type/src/data-structures/binary-tree/splay-tree.ts +3 -0
- package/backup/recursive-type/src/data-structures/binary-tree/tree-multiset.ts +55 -0
- package/backup/recursive-type/src/data-structures/binary-tree/two-three-tree.ts +3 -0
- package/backup/recursive-type/src/data-structures/diagrams/README.md +5 -0
- package/backup/recursive-type/src/data-structures/graph/abstract-graph.ts +985 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/backup/recursive-type/src/data-structures/graph/directed-graph.ts +478 -0
- package/backup/recursive-type/src/data-structures/graph/index.ts +3 -0
- package/backup/recursive-type/src/data-structures/graph/undirected-graph.ts +293 -0
- package/backup/recursive-type/src/data-structures/hash/coordinate-map.ts +67 -0
- package/backup/recursive-type/src/data-structures/hash/coordinate-set.ts +56 -0
- package/backup/recursive-type/src/data-structures/hash/hash-table.ts +3 -0
- package/backup/recursive-type/src/data-structures/hash/index.ts +6 -0
- package/backup/recursive-type/src/data-structures/hash/pair.ts +3 -0
- package/backup/recursive-type/src/data-structures/hash/tree-map.ts +3 -0
- package/backup/recursive-type/src/data-structures/hash/tree-set.ts +3 -0
- package/backup/recursive-type/src/data-structures/heap/heap.ts +176 -0
- package/backup/recursive-type/src/data-structures/heap/index.ts +3 -0
- package/backup/recursive-type/src/data-structures/heap/max-heap.ts +31 -0
- package/backup/recursive-type/src/data-structures/heap/min-heap.ts +34 -0
- package/backup/recursive-type/src/data-structures/index.ts +15 -0
- package/backup/recursive-type/src/data-structures/interfaces/abstract-graph.ts +42 -0
- package/backup/recursive-type/src/data-structures/interfaces/avl-tree.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/binary-tree.ts +56 -0
- package/backup/recursive-type/src/data-structures/interfaces/bst.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/directed-graph.ts +15 -0
- package/backup/recursive-type/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/heap.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/index.ts +13 -0
- package/backup/recursive-type/src/data-structures/interfaces/navigator.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/priority-queue.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/segment-tree.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/singly-linked-list.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/tree-multiset.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/undirected-graph.ts +3 -0
- package/backup/recursive-type/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
- package/backup/recursive-type/src/data-structures/linked-list/index.ts +3 -0
- package/backup/recursive-type/src/data-structures/linked-list/singly-linked-list.ts +490 -0
- package/backup/recursive-type/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/backup/recursive-type/src/data-structures/matrix/index.ts +4 -0
- package/backup/recursive-type/src/data-structures/matrix/matrix.ts +27 -0
- package/backup/recursive-type/src/data-structures/matrix/matrix2d.ts +208 -0
- package/backup/recursive-type/src/data-structures/matrix/navigator.ts +122 -0
- package/backup/recursive-type/src/data-structures/matrix/vector2d.ts +316 -0
- package/backup/recursive-type/src/data-structures/priority-queue/index.ts +3 -0
- package/backup/recursive-type/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
- package/backup/recursive-type/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
- package/backup/recursive-type/src/data-structures/priority-queue/priority-queue.ts +354 -0
- package/backup/recursive-type/src/data-structures/queue/deque.ts +251 -0
- package/backup/recursive-type/src/data-structures/queue/index.ts +2 -0
- package/backup/recursive-type/src/data-structures/queue/queue.ts +120 -0
- package/backup/recursive-type/src/data-structures/stack/index.ts +1 -0
- package/backup/recursive-type/src/data-structures/stack/stack.ts +98 -0
- package/backup/recursive-type/src/data-structures/tree/index.ts +1 -0
- package/backup/recursive-type/src/data-structures/tree/tree.ts +80 -0
- package/backup/recursive-type/src/data-structures/trie/index.ts +1 -0
- package/backup/recursive-type/src/data-structures/trie/trie.ts +227 -0
- package/backup/recursive-type/src/data-structures/types/abstract-graph.ts +5 -0
- package/backup/recursive-type/src/data-structures/types/avl-tree.ts +8 -0
- package/backup/recursive-type/src/data-structures/types/binary-tree.ts +10 -0
- package/backup/recursive-type/src/data-structures/types/bst.ts +6 -0
- package/backup/recursive-type/src/data-structures/types/directed-graph.ts +8 -0
- package/backup/recursive-type/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/backup/recursive-type/src/data-structures/types/heap.ts +5 -0
- package/backup/recursive-type/src/data-structures/types/index.ts +12 -0
- package/backup/recursive-type/src/data-structures/types/navigator.ts +13 -0
- package/backup/recursive-type/src/data-structures/types/priority-queue.ts +9 -0
- package/backup/recursive-type/src/data-structures/types/segment-tree.ts +1 -0
- package/backup/recursive-type/src/data-structures/types/singly-linked-list.ts +1 -0
- package/backup/recursive-type/src/data-structures/types/tree-multiset.ts +1 -0
- package/backup/recursive-type/src/index.ts +1 -0
- package/backup/recursive-type/src/utils/index.ts +2 -0
- package/backup/recursive-type/src/utils/types/index.ts +1 -0
- package/backup/recursive-type/src/utils/types/utils.ts +6 -0
- package/backup/recursive-type/src/utils/utils.ts +78 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +19 -25
- package/dist/data-structures/binary-tree/avl-tree.js +8 -16
- package/dist/data-structures/binary-tree/binary-tree.d.ts +99 -98
- package/dist/data-structures/binary-tree/binary-tree.js +70 -65
- package/dist/data-structures/binary-tree/bst.d.ts +21 -21
- package/dist/data-structures/binary-tree/bst.js +15 -17
- package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -2
- package/dist/data-structures/binary-tree/rb-tree.js +68 -5
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +9 -8
- package/dist/data-structures/binary-tree/tree-multiset.js +7 -6
- package/dist/data-structures/graph/abstract-graph.d.ts +56 -58
- package/dist/data-structures/graph/abstract-graph.js +84 -68
- package/dist/data-structures/graph/directed-graph.d.ts +127 -96
- package/dist/data-structures/graph/directed-graph.js +161 -109
- package/dist/data-structures/graph/undirected-graph.d.ts +82 -59
- package/dist/data-structures/graph/undirected-graph.js +99 -72
- package/dist/data-structures/hash/coordinate-set.d.ts +1 -1
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/interfaces/abstract-graph.d.ts +22 -0
- package/dist/data-structures/interfaces/abstract-graph.js +2 -0
- package/dist/data-structures/interfaces/avl-tree.d.ts +1 -0
- package/dist/data-structures/interfaces/avl-tree.js +2 -0
- package/dist/data-structures/interfaces/binary-tree.d.ts +27 -0
- package/dist/data-structures/interfaces/binary-tree.js +2 -0
- package/dist/data-structures/interfaces/bst.d.ts +1 -0
- package/dist/data-structures/interfaces/bst.js +2 -0
- package/dist/data-structures/interfaces/directed-graph.d.ts +9 -0
- package/dist/data-structures/interfaces/directed-graph.js +2 -0
- package/dist/data-structures/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/data-structures/interfaces/doubly-linked-list.js +2 -0
- package/dist/data-structures/interfaces/heap.d.ts +1 -0
- package/dist/data-structures/interfaces/heap.js +2 -0
- package/dist/data-structures/interfaces/index.d.ts +13 -0
- package/dist/data-structures/interfaces/index.js +29 -0
- package/dist/data-structures/interfaces/navigator.d.ts +1 -0
- package/dist/data-structures/interfaces/navigator.js +2 -0
- package/dist/data-structures/interfaces/priority-queue.d.ts +1 -0
- package/dist/data-structures/interfaces/priority-queue.js +2 -0
- package/dist/data-structures/interfaces/segment-tree.d.ts +1 -0
- package/dist/data-structures/interfaces/segment-tree.js +2 -0
- package/dist/data-structures/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/data-structures/interfaces/singly-linked-list.js +2 -0
- package/dist/data-structures/interfaces/tree-multiset.d.ts +1 -0
- package/dist/data-structures/interfaces/tree-multiset.js +2 -0
- package/dist/data-structures/interfaces/undirected-graph.d.ts +2 -0
- package/dist/data-structures/interfaces/undirected-graph.js +2 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +1 -1
- package/dist/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +4 -4
- package/dist/data-structures/queue/deque.d.ts +5 -5
- package/dist/data-structures/queue/deque.js +6 -6
- package/dist/data-structures/queue/queue.d.ts +1 -1
- package/dist/data-structures/stack/stack.d.ts +1 -1
- package/dist/data-structures/types/abstract-graph.d.ts +1 -20
- package/dist/data-structures/types/avl-tree.d.ts +5 -4
- package/dist/data-structures/types/binary-tree.d.ts +6 -5
- package/dist/data-structures/types/bst.d.ts +4 -3
- package/dist/data-structures/types/directed-graph.d.ts +5 -9
- package/dist/data-structures/types/directed-graph.js +7 -0
- package/dist/data-structures/types/heap.d.ts +2 -2
- package/dist/data-structures/types/index.d.ts +0 -1
- package/dist/data-structures/types/index.js +0 -1
- package/dist/data-structures/types/navigator.d.ts +2 -2
- package/dist/data-structures/types/priority-queue.d.ts +2 -2
- package/dist/data-structures/types/tree-multiset.d.ts +3 -4
- package/docs/assets/search.js +1 -1
- package/docs/classes/AVLTree.html +288 -287
- package/docs/classes/AVLTreeNode.html +106 -63
- package/docs/classes/AaTree.html +14 -12
- package/docs/classes/AbstractEdge.html +68 -34
- package/docs/classes/AbstractGraph.html +219 -114
- package/docs/classes/AbstractVertex.html +71 -30
- package/docs/classes/ArrayDeque.html +27 -25
- package/docs/classes/BST.html +279 -273
- package/docs/classes/BSTNode.html +106 -57
- package/docs/classes/BTree.html +14 -12
- package/docs/classes/BinaryIndexedTree.html +22 -20
- package/docs/classes/BinaryTree.html +283 -277
- package/docs/classes/BinaryTreeNode.html +111 -63
- package/docs/classes/Character.html +17 -15
- package/docs/classes/CoordinateMap.html +22 -20
- package/docs/classes/CoordinateSet.html +23 -21
- package/docs/classes/Deque.html +47 -45
- package/docs/classes/DirectedEdge.html +74 -41
- package/docs/classes/DirectedGraph.html +444 -208
- package/docs/classes/DirectedVertex.html +63 -36
- package/docs/classes/DoublyLinkedList.html +52 -50
- package/docs/classes/DoublyLinkedListNode.html +24 -22
- package/docs/classes/HashTable.html +14 -12
- package/docs/classes/Heap.html +29 -27
- package/docs/classes/HeapItem.html +21 -19
- package/docs/classes/Matrix2D.html +29 -27
- package/docs/classes/MatrixNTI2D.html +17 -15
- package/docs/classes/MaxHeap.html +29 -27
- package/docs/classes/MaxPriorityQueue.html +67 -60
- package/docs/classes/MinHeap.html +29 -27
- package/docs/classes/MinPriorityQueue.html +67 -60
- package/docs/classes/Navigator.html +24 -22
- package/docs/classes/ObjectDeque.html +62 -50
- package/docs/classes/Pair.html +14 -12
- package/docs/classes/PriorityQueue.html +62 -55
- package/docs/classes/Queue.html +29 -27
- package/docs/classes/SegmentTree.html +30 -28
- package/docs/classes/SegmentTreeNode.html +33 -31
- package/docs/classes/SinglyLinkedList.html +49 -47
- package/docs/classes/SinglyLinkedListNode.html +21 -19
- package/docs/classes/SkipLinkedList.html +14 -12
- package/docs/classes/SplayTree.html +14 -12
- package/docs/classes/Stack.html +27 -25
- package/docs/classes/TreeMap.html +14 -12
- package/docs/classes/TreeMultiSet.html +277 -270
- package/docs/classes/TreeNode.html +29 -27
- package/docs/classes/TreeSet.html +14 -12
- package/docs/classes/Trie.html +26 -24
- package/docs/classes/TrieNode.html +24 -22
- package/docs/classes/TwoThreeTree.html +14 -12
- package/docs/classes/UndirectedEdge.html +70 -51
- package/docs/classes/UndirectedGraph.html +344 -161
- package/docs/classes/UndirectedVertex.html +63 -36
- package/docs/classes/Vector2D.html +41 -39
- package/docs/enums/CP.html +17 -15
- package/docs/enums/FamilyPosition.html +17 -15
- package/docs/enums/LoopType.html +16 -14
- package/docs/{interfaces/AVLTreeDeleted.html → enums/TopologicalProperty.html} +43 -43
- package/docs/index.html +195 -68
- package/docs/interfaces/{HeapOptions.html → IBinaryTree.html} +39 -32
- package/docs/interfaces/IBinaryTreeNode.html +383 -0
- package/docs/interfaces/IDirectedGraph.html +20 -18
- package/docs/interfaces/IGraph.html +118 -88
- package/docs/interfaces/{PriorityQueueOptions.html → IUNDirectedGraph.html} +22 -53
- package/docs/modules.html +25 -21
- package/docs/types/{ToThunkFn.html → AVLTreeDeleted.html} +27 -21
- package/docs/types/BSTComparator.html +14 -12
- package/docs/types/BSTDeletedResult.html +19 -17
- package/docs/types/BinaryTreeDeleted.html +19 -17
- package/docs/types/BinaryTreeNodeId.html +14 -12
- package/docs/types/BinaryTreeNodePropertyName.html +14 -12
- package/docs/types/DFSOrderPattern.html +14 -12
- package/docs/types/DijkstraResult.html +14 -12
- package/docs/types/Direction.html +14 -12
- package/docs/types/{TrlFn.html → EdgeId.html} +18 -29
- package/docs/types/{TrlAsyncFn.html → HeapOptions.html} +29 -19
- package/docs/types/NavigatorParams.html +164 -0
- package/docs/types/NodeOrPropertyName.html +14 -12
- package/docs/types/PriorityQueueComparator.html +14 -12
- package/docs/types/PriorityQueueDFSOrderPattern.html +14 -12
- package/docs/types/{SpecifyOptional.html → PriorityQueueOptions.html} +28 -19
- package/docs/types/RecursiveAVLTreeNode.html +135 -0
- package/docs/types/{Thunk.html → RecursiveBSTNode.html} +23 -24
- package/docs/types/RecursiveBinaryTreeNode.html +135 -0
- package/docs/types/ResultByProperty.html +17 -15
- package/docs/types/ResultsByProperty.html +17 -15
- package/docs/types/SegmentTreeNodeVal.html +14 -12
- package/docs/types/TopologicalStatus.html +14 -12
- package/docs/types/TreeMultiSetDeletedResult.html +19 -17
- package/docs/types/Turning.html +14 -12
- package/docs/types/VertexId.html +14 -12
- package/notes/note.md +8 -1
- package/package.json +10 -2
- package/docs/classes/RBTree.html +0 -153
- package/docs/interfaces/NavigatorParams.html +0 -200
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import {arrayRemove} from '../../utils';
|
|
9
|
+
import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
|
|
10
|
+
import type {VertexId} from '../types';
|
|
11
|
+
|
|
12
|
+
export class UndirectedVertex<T = number> extends AbstractVertex<T> {
|
|
13
|
+
/**
|
|
14
|
+
* The constructor function initializes a vertex with an optional value.
|
|
15
|
+
* @param {VertexId} id - The `id` parameter is the identifier for the vertex. It is of type `VertexId`, which is
|
|
16
|
+
* typically a unique identifier for each vertex in a graph.
|
|
17
|
+
* @param {T} [val] - The "val" parameter is an optional parameter of type T. It is used to initialize the value of the
|
|
18
|
+
* vertex. If no value is provided, the vertex will be initialized with a default value.
|
|
19
|
+
*/
|
|
20
|
+
constructor(id: VertexId, val?: T) {
|
|
21
|
+
super(id, val);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// _createVertex(id: VertexId, val?: T): T {
|
|
25
|
+
// return new T(id, val);
|
|
26
|
+
// }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class UndirectedEdge<T = number> extends AbstractEdge<T> {
|
|
30
|
+
/**
|
|
31
|
+
* The constructor function initializes an instance of a class with two vertex IDs, an optional weight, and an optional
|
|
32
|
+
* value.
|
|
33
|
+
* @param {VertexId} v1 - The parameter `v1` is of type `VertexId` and represents the first vertex in the edge.
|
|
34
|
+
* @param {VertexId} v2 - The parameter `v2` is a `VertexId`, which represents the identifier of the second vertex in a
|
|
35
|
+
* graph edge.
|
|
36
|
+
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
37
|
+
* @param {T} [val] - The "val" parameter is an optional parameter of type T. It represents the value associated with
|
|
38
|
+
* the edge.
|
|
39
|
+
*/
|
|
40
|
+
constructor(v1: VertexId, v2: VertexId, weight?: number, val?: T) {
|
|
41
|
+
super(weight, val);
|
|
42
|
+
this._vertices = [v1, v2];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private _vertices: [VertexId, VertexId];
|
|
46
|
+
|
|
47
|
+
get vertices() {
|
|
48
|
+
return this._vertices;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
set vertices(v: [VertexId, VertexId]) {
|
|
52
|
+
this._vertices = v;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// _createEdge(src: VertexId, dest: VertexId, weight?: number, val?: T): T {
|
|
56
|
+
// if (weight === undefined || weight === null) weight = 1;
|
|
57
|
+
// return new UndirectedEdge(src, dest, weight, val);
|
|
58
|
+
// }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class UndirectedGraph<V extends UndirectedVertex<any>, E extends UndirectedEdge<any>> extends AbstractGraph<V, E> {
|
|
62
|
+
|
|
63
|
+
private readonly _vertexConstructor: new (id: VertexId, val?: V['val']) => V;
|
|
64
|
+
private readonly _edgeConstructor: new (src: VertexId, dest: VertexId, weight?: number, val?: E['val']) => E;
|
|
65
|
+
|
|
66
|
+
constructor(vertexConstructor: new (id: VertexId, val?: V['val']) => V, edgeConstructor: new (src: VertexId, dest: VertexId, weight?: number, val?: E['val']) => E) {
|
|
67
|
+
super();
|
|
68
|
+
this._vertexConstructor = vertexConstructor;
|
|
69
|
+
this._edgeConstructor = edgeConstructor;
|
|
70
|
+
this._edges = new Map<V, E[]>();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
protected _edges: Map<V, E[]>;
|
|
74
|
+
|
|
75
|
+
get edges(): Map<V, E[]> {
|
|
76
|
+
return this._edges;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
81
|
+
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
82
|
+
* @param id
|
|
83
|
+
* @param val
|
|
84
|
+
*/
|
|
85
|
+
_createVertex(id: VertexId, val?: V['val']): V {
|
|
86
|
+
return new this._vertexConstructor(id, val);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
91
|
+
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
92
|
+
* @param src
|
|
93
|
+
* @param dest
|
|
94
|
+
* @param weight
|
|
95
|
+
* @param val
|
|
96
|
+
*/
|
|
97
|
+
_createEdge(src: VertexId, dest: VertexId, weight?: number, val?: E['val']): E {
|
|
98
|
+
if (weight === undefined || weight === null) weight = 1;
|
|
99
|
+
return new this._edgeConstructor(src, dest, weight, val);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The function `getEdge` returns the first undirected edge that connects two given vertices, or null if no such edge
|
|
104
|
+
* exists.
|
|
105
|
+
* @param {V | null | VertexId} v1 - The parameter `v1` represents either an `V`
|
|
106
|
+
* object, `null`, or a `VertexId`. It is used to specify one of the vertices of the edge.
|
|
107
|
+
* @param {V | null | VertexId} v2 - The parameter `v2` represents either an `UndirectedVertex`
|
|
108
|
+
* object or a `VertexId` (identifier) of an undirected vertex.
|
|
109
|
+
* @returns an instance of `E` or `null`.
|
|
110
|
+
*/
|
|
111
|
+
getEdge(v1: V | null | VertexId, v2: V | null | VertexId): E | null {
|
|
112
|
+
let edges: E[] | undefined = [];
|
|
113
|
+
|
|
114
|
+
if (v1 !== null && v2 !== null) {
|
|
115
|
+
const vertex1: V | null = this._getVertex(v1);
|
|
116
|
+
const vertex2: V | null = this._getVertex(v2);
|
|
117
|
+
|
|
118
|
+
if (vertex1 && vertex2) {
|
|
119
|
+
edges = this._edges.get(vertex1)?.filter(e => e.vertices.includes(vertex2.id));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return edges ? edges[0] || null : null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The function adds an undirected edge to a graph by updating the adjacency list.
|
|
128
|
+
* @param edge - An object representing an undirected edge in a graph. It has a property called "vertices" which is an
|
|
129
|
+
* array of two vertices connected by the edge.
|
|
130
|
+
* @returns a boolean value.
|
|
131
|
+
*/
|
|
132
|
+
addEdge(edge: E): boolean {
|
|
133
|
+
for (const end of edge.vertices) {
|
|
134
|
+
const endVertex = this._getVertex(end);
|
|
135
|
+
if (endVertex === null) return false;
|
|
136
|
+
if (endVertex) {
|
|
137
|
+
const edges = this._edges.get(endVertex);
|
|
138
|
+
if (edges) {
|
|
139
|
+
edges.push(edge);
|
|
140
|
+
} else {
|
|
141
|
+
this._edges.set(endVertex, [edge]);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The function removes an edge between two vertices in an undirected graph.
|
|
150
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents either an `V` object or
|
|
151
|
+
* a `VertexId`. It is used to specify one of the vertices of the edge that needs to be removed.
|
|
152
|
+
* @param {V | VertexId} v2 - The parameter `v2` represents either an instance of the
|
|
153
|
+
* `UndirectedVertex` class or a `VertexId`. It is used to identify the second vertex of the edge that needs to be
|
|
154
|
+
* removed.
|
|
155
|
+
* @returns The function `removeEdgeBetween` returns an `E` object if an edge is successfully removed
|
|
156
|
+
* between the two vertices `v1` and `v2`. If either `v1` or `v2` is not found in the graph, or if there is no edge
|
|
157
|
+
* between them, the function returns `null`.
|
|
158
|
+
*/
|
|
159
|
+
removeEdgeBetween(v1: V | VertexId, v2: V | VertexId): E | null {
|
|
160
|
+
|
|
161
|
+
const vertex1: V | null = this._getVertex(v1);
|
|
162
|
+
const vertex2: V | null = this._getVertex(v2);
|
|
163
|
+
|
|
164
|
+
if (!vertex1 || !vertex2) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const v1Edges = this._edges.get(vertex1);
|
|
169
|
+
let removed: E | null = null;
|
|
170
|
+
if (v1Edges) {
|
|
171
|
+
removed = arrayRemove<E>(v1Edges, (e: E) => e.vertices.includes(vertex2.id))[0] || null;
|
|
172
|
+
}
|
|
173
|
+
const v2Edges = this._edges.get(vertex2);
|
|
174
|
+
if (v2Edges) {
|
|
175
|
+
arrayRemove<E>(v2Edges, (e: E) => e.vertices.includes(vertex1.id));
|
|
176
|
+
}
|
|
177
|
+
return removed;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The removeEdge function removes an edge between two vertices in an undirected graph.
|
|
182
|
+
* @param edge - An object representing an undirected edge. It has a property called "vertices" which is an array
|
|
183
|
+
* containing the two vertices connected by the edge.
|
|
184
|
+
* @returns The method is returning an UndirectedEdge object or null.
|
|
185
|
+
*/
|
|
186
|
+
removeEdge(edge: E): E | null {
|
|
187
|
+
return this.removeEdgeBetween(edge.vertices[0], edge.vertices[1]);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The function "degreeOf" returns the degree of a given vertex in an undirected graph.
|
|
192
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or an
|
|
193
|
+
* `V`.
|
|
194
|
+
* @returns the degree of the vertex.
|
|
195
|
+
*/
|
|
196
|
+
degreeOf(vertexOrId: VertexId | V): number {
|
|
197
|
+
const vertex = this._getVertex(vertexOrId);
|
|
198
|
+
if (vertex) {
|
|
199
|
+
return this._edges.get(vertex)?.length || 0;
|
|
200
|
+
} else {
|
|
201
|
+
return 0;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* The function "edgesOf" returns an array of undirected edges connected to a given vertex or vertex ID.
|
|
207
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or an
|
|
208
|
+
* `V`.
|
|
209
|
+
* @returns an array of UndirectedEdge objects.
|
|
210
|
+
*/
|
|
211
|
+
edgesOf(vertexOrId: VertexId | V): E[] {
|
|
212
|
+
const vertex = this._getVertex(vertexOrId);
|
|
213
|
+
if (vertex) {
|
|
214
|
+
return this._edges.get(vertex) || [];
|
|
215
|
+
} else {
|
|
216
|
+
return [];
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* The function "edgeSet" returns an array of unique undirected edges from a set of edges.
|
|
222
|
+
* @returns The method `edgeSet()` returns an array of `E` objects.
|
|
223
|
+
*/
|
|
224
|
+
edgeSet(): E[] {
|
|
225
|
+
const edgeSet: Set<E> = new Set();
|
|
226
|
+
this._edges.forEach(edges => {
|
|
227
|
+
edges.forEach(edge => {
|
|
228
|
+
edgeSet.add(edge);
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
return [...edgeSet];
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* The function "getEdgesOf" returns an array of undirected edges connected to a given vertex or vertex ID.
|
|
236
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either an
|
|
237
|
+
* `V` object or a `VertexId`.
|
|
238
|
+
* @returns The function `getEdgesOf` returns an array of `E` objects.
|
|
239
|
+
*/
|
|
240
|
+
getEdgesOf(vertexOrId: V | VertexId): E[] {
|
|
241
|
+
const vertex = this._getVertex(vertexOrId);
|
|
242
|
+
if (!vertex) {
|
|
243
|
+
return [];
|
|
244
|
+
}
|
|
245
|
+
return this._edges.get(vertex) || [];
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* The function `getNeighbors` returns an array of neighboring vertices of a given vertex in an undirected graph.
|
|
250
|
+
* @param {V | VertexId} vertexOrId - The `vertexOrId` parameter can be either an
|
|
251
|
+
* `V` object or a `VertexId`. It represents the vertex for which we want to find the neighbors.
|
|
252
|
+
* @returns an array of UndirectedVertex objects.
|
|
253
|
+
*/
|
|
254
|
+
getNeighbors(vertexOrId: V | VertexId): V[] {
|
|
255
|
+
const neighbors: V[] = [];
|
|
256
|
+
const vertex = this._getVertex(vertexOrId);
|
|
257
|
+
if (vertex) {
|
|
258
|
+
const neighborEdges = this.getEdgesOf(vertex);
|
|
259
|
+
for (const edge of neighborEdges) {
|
|
260
|
+
const neighbor = this._getVertex(edge.vertices.filter(e => e !== vertex.id)[0]);
|
|
261
|
+
if (neighbor) {
|
|
262
|
+
neighbors.push(neighbor);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return neighbors;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* The function "getEndsOfEdge" returns the two vertices that form the ends of a given undirected edge, or null if the
|
|
271
|
+
* edge does not exist in the graph.
|
|
272
|
+
* @param edge - An object representing an undirected edge in a graph. It has a property called "vertices" which is an
|
|
273
|
+
* array containing two vertices that the edge connects.
|
|
274
|
+
* @returns The function `getEndsOfEdge` returns an array containing the two ends of the given `edge` if the edge
|
|
275
|
+
* exists in the graph. If the edge does not exist, it returns `null`.
|
|
276
|
+
*/
|
|
277
|
+
getEndsOfEdge(edge: E): [V, V] | null {
|
|
278
|
+
if (!this.hasEdge(edge.vertices[0], edge.vertices[1])) {
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
const v1 = this._getVertex(edge.vertices[0]);
|
|
282
|
+
const v2 = this._getVertex(edge.vertices[1]);
|
|
283
|
+
if (v1 && v2) {
|
|
284
|
+
return [v1, v2];
|
|
285
|
+
} else {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
protected _setEdges(v: Map<V, E[]>) {
|
|
291
|
+
this._edges = v;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
export class CoordinateMap<V> extends Map<any, V> {
|
|
9
|
+
constructor(joint?: string) {
|
|
10
|
+
super();
|
|
11
|
+
if (joint !== undefined) this._joint = joint;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
protected _joint: string = '_';
|
|
15
|
+
|
|
16
|
+
get joint(): string {
|
|
17
|
+
return this._joint;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The "has" function overrides the base class's "has" function and checks if a key exists in the map by joining the
|
|
22
|
+
* key array with a specified delimiter.
|
|
23
|
+
* @param {number[]} key - The parameter "key" is an array of numbers.
|
|
24
|
+
* @returns The `has` method is being overridden to return the result of calling the `has` method of the superclass
|
|
25
|
+
* (`super.has`) with the `key` array joined together using the `_joint` property.
|
|
26
|
+
*/
|
|
27
|
+
override has(key: number[]) {
|
|
28
|
+
return super.has(key.join(this._joint));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The function overrides the set method of a Map object to convert the key from an array to a string using a specified
|
|
33
|
+
* delimiter before calling the original set method.
|
|
34
|
+
* @param {number[]} key - The key parameter is an array of numbers.
|
|
35
|
+
* @param {V} value - The value parameter is the value that you want to associate with the specified key.
|
|
36
|
+
* @returns The `set` method is returning the result of calling the `set` method of the superclass
|
|
37
|
+
* (`super.set(key.join(this._joint), value)`).
|
|
38
|
+
*/
|
|
39
|
+
override set(key: number[], value: V) {
|
|
40
|
+
return super.set(key.join(this._joint), value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The function overrides the get method to join the key array with a specified joint and then calls the super get
|
|
45
|
+
* method.
|
|
46
|
+
* @param {number[]} key - An array of numbers
|
|
47
|
+
* @returns The code is returning the value associated with the specified key in the map.
|
|
48
|
+
*/
|
|
49
|
+
override get(key: number[]) {
|
|
50
|
+
return super.get(key.join(this._joint));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The function overrides the delete method and joins the key array using a specified joint character before calling
|
|
55
|
+
* the super delete method.
|
|
56
|
+
* @param {number[]} key - An array of numbers that represents the key to be deleted.
|
|
57
|
+
* @returns The `delete` method is returning the result of calling the `delete` method on the superclass, with the
|
|
58
|
+
* `key` array joined together using the `_joint` property.
|
|
59
|
+
*/
|
|
60
|
+
override delete(key: number[]) {
|
|
61
|
+
return super.delete(key.join(this._joint));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
protected _setJoint(v: string) {
|
|
65
|
+
this._joint = v;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
export class CoordinateSet extends Set<any> {
|
|
9
|
+
constructor(joint?: string) {
|
|
10
|
+
super();
|
|
11
|
+
if (joint !== undefined) this._joint = joint;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
protected _joint: string = '_';
|
|
15
|
+
|
|
16
|
+
get joint(): string {
|
|
17
|
+
return this._joint;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The "has" function overrides the "has" method of the superclass and checks if a value exists in an array after
|
|
22
|
+
* joining its elements with a specified separator.
|
|
23
|
+
* @param {number[]} value - The parameter "value" is an array of numbers.
|
|
24
|
+
* @returns The overridden `has` method is returning the result of calling the `has` method of the superclass, passing
|
|
25
|
+
* in the joined value as an argument.
|
|
26
|
+
*/
|
|
27
|
+
override has(value: number[]) {
|
|
28
|
+
return super.has(value.join(this._joint));
|
|
29
|
+
}
|
|
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
|
+
override add(value: number[]) {
|
|
39
|
+
return super.add(value.join(this._joint));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The function overrides the delete method and deletes an element from a Set by joining the elements of the input
|
|
44
|
+
* array with a specified joint and then calling the delete method of the parent class.
|
|
45
|
+
* @param {number[]} value - An array of numbers
|
|
46
|
+
* @returns The `delete` method is returning the result of calling the `delete` method of the superclass, with the
|
|
47
|
+
* `value` array joined together using the `_joint` property.
|
|
48
|
+
*/
|
|
49
|
+
override delete(value: number[]) {
|
|
50
|
+
return super.delete(value.join(this._joint));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protected _setJoint(v: string) {
|
|
54
|
+
this._joint = v;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import {PriorityQueue} from '../priority-queue';
|
|
9
|
+
import type {HeapOptions} from '../types';
|
|
10
|
+
|
|
11
|
+
export class HeapItem<T = number> {
|
|
12
|
+
|
|
13
|
+
constructor(priority: number = NaN, val: T | null = null) {
|
|
14
|
+
this._val = val;
|
|
15
|
+
this._priority = priority;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
private _priority: number;
|
|
19
|
+
|
|
20
|
+
get priority(): number {
|
|
21
|
+
return this._priority;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
set priority(value: number) {
|
|
25
|
+
this._priority = value;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private _val: T | null;
|
|
29
|
+
|
|
30
|
+
get val(): T | null {
|
|
31
|
+
return this._val;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
set val(value: T | null) {
|
|
35
|
+
this._val = value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export abstract class Heap<T = number> {
|
|
40
|
+
/**
|
|
41
|
+
* The function is a constructor for a class that initializes a priority callback function based on the
|
|
42
|
+
* options provided.
|
|
43
|
+
* @param [options] - An optional object that contains configuration options for the Heap.
|
|
44
|
+
*/
|
|
45
|
+
protected constructor(options?: HeapOptions<T>) {
|
|
46
|
+
if (options) {
|
|
47
|
+
const {priority} = options;
|
|
48
|
+
if (priority !== undefined && typeof priority !== 'function') {
|
|
49
|
+
throw new Error('.constructor expects a valid priority function');
|
|
50
|
+
}
|
|
51
|
+
this._priorityCb = priority || ((el) => +el);
|
|
52
|
+
} else {
|
|
53
|
+
this._priorityCb = (el) => +el;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
protected abstract _pq: PriorityQueue<HeapItem<T>>;
|
|
58
|
+
|
|
59
|
+
get pq() {
|
|
60
|
+
return this._pq;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
protected _priorityCb: (val: T) => number;
|
|
64
|
+
get priorityCb() {
|
|
65
|
+
return this._priorityCb;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The function returns the size of a priority queue.
|
|
70
|
+
* @returns The size of the priority queue.
|
|
71
|
+
*/
|
|
72
|
+
get size(): number {
|
|
73
|
+
return this._pq.size;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The function checks if a priority queue is empty.
|
|
78
|
+
* @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
|
|
79
|
+
*/
|
|
80
|
+
isEmpty(): boolean {
|
|
81
|
+
return this._pq.size < 1;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The `peek` function returns the top item in the priority queue without removing it.
|
|
86
|
+
* @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an val with the highest priority in the queue
|
|
87
|
+
*/
|
|
88
|
+
peek(): HeapItem<T> | null {
|
|
89
|
+
return this._pq.peek();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The `peekLast` function returns the last item in the heap.
|
|
94
|
+
* @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
|
|
95
|
+
*/
|
|
96
|
+
peekLast(): HeapItem<T> | null {
|
|
97
|
+
return this._pq.leaf();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The `add` function adds an val to a priority queue with an optional priority value.
|
|
102
|
+
* @param {T} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
|
|
103
|
+
* type.
|
|
104
|
+
* @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
|
|
105
|
+
* val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
|
|
106
|
+
* the value of `val`. If the `val` parameter is not a number, then the
|
|
107
|
+
* @returns The `add` method returns the instance of the `Heap` class.
|
|
108
|
+
* @throws {Error} if priority is not a valid number
|
|
109
|
+
*/
|
|
110
|
+
add(val: T, priority?: number): Heap<T> {
|
|
111
|
+
if (typeof val === 'number') {
|
|
112
|
+
priority = val;
|
|
113
|
+
} else {
|
|
114
|
+
if (priority === undefined) {
|
|
115
|
+
throw new Error('.add expects a numeric priority');
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (priority && Number.isNaN(+priority)) {
|
|
120
|
+
throw new Error('.add expects a numeric priority');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (Number.isNaN(+priority) && Number.isNaN(this._priorityCb(val))) {
|
|
124
|
+
throw new Error(
|
|
125
|
+
'.add expects a numeric priority '
|
|
126
|
+
+ 'or a constructor callback that returns a number'
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const _priority = !Number.isNaN(+priority) ? priority : this._priorityCb(val);
|
|
131
|
+
this._pq.add(new HeapItem<T>(_priority, val));
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 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
|
|
137
|
+
* @returns either a HeapItem<T> object or null.
|
|
138
|
+
*/
|
|
139
|
+
poll(): HeapItem<T> | null {
|
|
140
|
+
const top = this._pq.poll();
|
|
141
|
+
if (!top) {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
return top;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The function checks if a given node or value exists in the priority queue.
|
|
149
|
+
* @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
|
|
150
|
+
* @returns a boolean value.
|
|
151
|
+
*/
|
|
152
|
+
has(node: T | HeapItem<T>): boolean {
|
|
153
|
+
if (node instanceof HeapItem) {
|
|
154
|
+
return this.pq.getNodes().includes(node);
|
|
155
|
+
} else {
|
|
156
|
+
return this.pq.getNodes().findIndex(item => {
|
|
157
|
+
return item.val === node;
|
|
158
|
+
}) !== -1;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The `toArray` function returns an array of `HeapItem<T>` objects.
|
|
164
|
+
* @returns An array of HeapItem<T> objects.Returns a sorted list of vals
|
|
165
|
+
*/
|
|
166
|
+
toArray(): HeapItem<T>[] {
|
|
167
|
+
return this._pq.toArray();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* The clear function clears the priority queue.
|
|
172
|
+
*/
|
|
173
|
+
clear(): void {
|
|
174
|
+
this._pq.clear();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {Heap, HeapItem} from './heap';
|
|
10
|
+
import {PriorityQueue} from '../priority-queue';
|
|
11
|
+
import type {HeapOptions} from '../types';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @class MaxHeap
|
|
15
|
+
* @extends Heap
|
|
16
|
+
*/
|
|
17
|
+
export class MaxHeap<T = number> extends Heap<T> {
|
|
18
|
+
protected _pq: PriorityQueue<HeapItem<T>>;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The constructor initializes a PriorityQueue with a custom comparator function.
|
|
22
|
+
* @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
|
|
23
|
+
* type `HeapOptions<T>`, which is a generic type that represents the options for the heap.
|
|
24
|
+
*/
|
|
25
|
+
constructor(options?: HeapOptions<T>) {
|
|
26
|
+
super(options);
|
|
27
|
+
this._pq = new PriorityQueue<HeapItem<T>>({
|
|
28
|
+
comparator: (a, b) => b.priority - a.priority
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|