data-structure-typed 0.9.16 → 1.12.10
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/.dependency-cruiser.js +449 -0
- package/.idea/data-structure-typed.iml +2 -0
- package/README.md +102 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +57 -0
- package/dist/data-structures/binary-tree/avl-tree.js +57 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +27 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +27 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +215 -9
- package/dist/data-structures/binary-tree/binary-tree.js +283 -15
- package/dist/data-structures/binary-tree/bst.d.ts +16 -0
- package/dist/data-structures/binary-tree/bst.js +12 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +4 -0
- package/dist/data-structures/binary-tree/segment-tree.js +4 -0
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +4 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +4 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +163 -13
- package/dist/data-structures/graph/abstract-graph.js +166 -12
- package/dist/data-structures/graph/directed-graph.d.ts +111 -1
- package/dist/data-structures/graph/directed-graph.js +122 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/data-structures/graph/undirected-graph.js +67 -0
- package/dist/data-structures/hash/coordinate-map.d.ts +32 -0
- package/dist/data-structures/hash/coordinate-map.js +32 -0
- package/dist/data-structures/hash/coordinate-set.d.ts +25 -0
- package/dist/data-structures/hash/coordinate-set.js +25 -0
- package/dist/data-structures/heap/heap.d.ts +26 -31
- package/dist/data-structures/heap/heap.js +23 -32
- package/dist/data-structures/heap/max-heap.d.ts +6 -1
- package/dist/data-structures/heap/max-heap.js +6 -1
- package/dist/data-structures/heap/min-heap.d.ts +7 -1
- package/dist/data-structures/heap/min-heap.js +7 -1
- package/dist/data-structures/index.d.ts +0 -1
- package/dist/data-structures/index.js +0 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -6
- package/dist/data-structures/linked-list/doubly-linked-list.js +45 -8
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +11 -8
- package/dist/data-structures/linked-list/singly-linked-list.js +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +9 -0
- package/dist/data-structures/matrix/matrix.js +11 -0
- package/dist/data-structures/matrix/matrix2d.d.ts +82 -2
- package/dist/data-structures/matrix/matrix2d.js +82 -2
- package/dist/data-structures/matrix/navigator.d.ts +32 -0
- package/dist/data-structures/matrix/navigator.js +28 -0
- package/dist/data-structures/matrix/vector2d.d.ts +139 -15
- package/dist/data-structures/matrix/vector2d.js +139 -15
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +8 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +8 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +130 -0
- package/dist/data-structures/priority-queue/priority-queue.js +126 -0
- package/dist/data-structures/queue/deque.d.ts +69 -0
- package/dist/data-structures/queue/deque.js +69 -0
- package/dist/data-structures/queue/queue.d.ts +30 -32
- package/dist/data-structures/queue/queue.js +30 -32
- package/dist/data-structures/stack/stack.d.ts +26 -31
- package/dist/data-structures/stack/stack.js +26 -31
- package/dist/data-structures/trie/trie.d.ts +26 -6
- package/dist/data-structures/trie/trie.js +26 -7
- package/dist/data-structures/types/avl-tree.d.ts +1 -1
- package/dist/data-structures/types/binary-tree.d.ts +1 -1
- package/dist/data-structures/types/index.d.ts +1 -1
- package/dist/data-structures/types/index.js +1 -1
- package/dist/data-structures/types/singly-linked-list.d.ts +1 -5
- package/dist/data-structures/types/tree-multiset.d.ts +1 -1
- package/dist/{data-structures → utils}/trampoline.d.ts +5 -7
- package/dist/utils/types/index.d.ts +1 -0
- package/dist/utils/types/index.js +17 -0
- package/dist/{data-structures → utils}/types/utils.d.ts +8 -0
- package/dist/{data-structures → utils}/types/utils.js +1 -0
- package/dist/utils/utils.d.ts +1 -1
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +85 -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 +2046 -0
- package/docs/classes/AVLTreeNode.html +423 -0
- package/docs/classes/AaTree.html +117 -0
- package/docs/classes/AbstractEdge.html +198 -0
- package/docs/classes/AbstractGraph.html +891 -0
- package/docs/classes/AbstractVertex.html +164 -0
- package/docs/classes/ArrayDeque.html +384 -0
- package/docs/classes/BST.html +1893 -0
- package/docs/classes/BSTNode.html +425 -0
- package/docs/classes/BTree.html +117 -0
- package/docs/classes/BinaryIndexedTree.html +244 -0
- package/docs/classes/BinaryTree.html +1754 -0
- package/docs/classes/BinaryTreeNode.html +396 -0
- package/docs/classes/Character.html +165 -0
- package/docs/classes/CoordinateMap.html +394 -0
- package/docs/classes/CoordinateSet.html +355 -0
- package/docs/classes/Deque.html +617 -0
- package/docs/classes/DirectedEdge.html +247 -0
- package/docs/classes/DirectedGraph.html +1207 -0
- package/docs/classes/DirectedVertex.html +154 -0
- package/docs/classes/DoublyLinkedList.html +619 -0
- package/docs/classes/DoublyLinkedListNode.html +160 -0
- package/docs/classes/Heap.html +315 -0
- package/docs/classes/Matrix2D.html +447 -0
- package/docs/classes/MatrixNTI2D.html +181 -0
- package/docs/classes/MaxHeap.html +325 -0
- package/docs/classes/MaxPriorityQueue.html +668 -0
- package/docs/classes/MinHeap.html +326 -0
- package/docs/classes/MinPriorityQueue.html +668 -0
- package/docs/classes/Navigator.html +285 -0
- package/docs/classes/ObjectDeque.html +289 -0
- package/docs/classes/PriorityQueue.html +643 -0
- package/docs/classes/Queue.html +337 -0
- package/docs/classes/RBTree.html +117 -0
- package/docs/classes/SegmentTree.html +234 -0
- package/docs/classes/SegmentTreeNode.html +302 -0
- package/docs/classes/SinglyLinkedList.html +1035 -0
- package/docs/classes/SinglyLinkedListNode.html +304 -0
- package/docs/classes/SplayTree.html +117 -0
- package/docs/classes/Stack.html +313 -0
- package/docs/classes/TreeMultiSet.html +1897 -0
- package/docs/classes/Trie.html +317 -0
- package/docs/classes/TrieNode.html +221 -0
- package/docs/classes/TwoThreeTree.html +117 -0
- package/docs/classes/UndirectedEdge.html +220 -0
- package/docs/classes/UndirectedGraph.html +1006 -0
- package/docs/classes/UndirectedVertex.html +154 -0
- package/docs/classes/Vector2D.html +746 -0
- package/docs/enums/CP.html +126 -0
- package/docs/enums/FamilyPosition.html +126 -0
- package/docs/enums/LoopType.html +119 -0
- package/docs/index.html +288 -0
- package/docs/modules.html +146 -0
- package/jest.config.js +5 -0
- package/package.json +19 -11
- package/rename_clear_files.sh +29 -0
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/data-structures/binary-tree/avl-tree.ts +57 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +27 -0
- package/src/data-structures/binary-tree/binary-tree.ts +288 -18
- package/src/data-structures/binary-tree/bst.ts +16 -1
- package/src/data-structures/binary-tree/segment-tree.ts +5 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +4 -0
- package/src/data-structures/diagrams/README.md +5 -7
- package/src/data-structures/graph/abstract-graph.ts +167 -15
- 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.webp +0 -0
- package/src/data-structures/graph/directed-graph.ts +127 -5
- package/src/data-structures/graph/undirected-graph.ts +67 -1
- package/src/data-structures/hash/coordinate-map.ts +32 -0
- package/src/data-structures/hash/coordinate-set.ts +25 -0
- package/src/data-structures/heap/heap.ts +27 -32
- package/src/data-structures/heap/max-heap.ts +6 -1
- package/src/data-structures/heap/min-heap.ts +7 -1
- package/src/data-structures/index.ts +0 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +49 -8
- package/src/data-structures/linked-list/singly-linked-list.ts +39 -8
- package/src/data-structures/matrix/matrix.ts +11 -0
- package/src/data-structures/matrix/matrix2d.ts +82 -2
- package/src/data-structures/matrix/navigator.ts +32 -0
- package/src/data-structures/matrix/vector2d.ts +139 -15
- package/src/data-structures/priority-queue/max-priority-queue.ts +8 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +8 -0
- package/src/data-structures/priority-queue/priority-queue.ts +130 -0
- package/src/data-structures/queue/deque.ts +69 -0
- package/src/data-structures/queue/queue.ts +30 -32
- package/src/data-structures/stack/stack.ts +26 -31
- package/src/data-structures/trie/trie.ts +26 -7
- package/src/data-structures/types/avl-tree.ts +1 -1
- package/src/data-structures/types/binary-tree.ts +1 -1
- package/src/data-structures/types/bst.ts +1 -1
- package/src/data-structures/types/index.ts +1 -1
- package/src/data-structures/types/navigator.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +1 -15
- package/src/data-structures/types/tree-multiset.ts +1 -1
- package/src/{data-structures → utils}/trampoline.ts +6 -6
- package/src/utils/types/index.ts +1 -0
- package/src/{data-structures → utils}/types/utils.ts +7 -0
- package/src/utils/utils.ts +12 -2
- package/tests/unit/data-structures/binary-tree/bst.test.ts +185 -0
- package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
- package/tests/unit/data-structures/graph/directed-graph.test.ts +71 -0
- package/tests/unit/data-structures/graph/index.ts +3 -0
- package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
- 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/dist/{data-structures → utils}/trampoline.js +0 -0
|
@@ -1,8 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
1
5
|
import type {PriorityQueueComparator, PriorityQueueDFSOrderPattern, PriorityQueueOptions} from '../types';
|
|
2
6
|
|
|
3
7
|
export class PriorityQueue<T = number> {
|
|
4
8
|
protected nodes: T[] = [];
|
|
5
9
|
|
|
10
|
+
/**
|
|
11
|
+
* The constructor initializes a priority queue with the given options, including an array of nodes and a comparator
|
|
12
|
+
* function.
|
|
13
|
+
* @param options - The `options` parameter is an object that contains the following properties:
|
|
14
|
+
*/
|
|
6
15
|
constructor(options: PriorityQueueOptions<T>) {
|
|
7
16
|
const {nodes, comparator, isFix = true} = options;
|
|
8
17
|
this._comparator = comparator;
|
|
@@ -18,25 +27,53 @@ export class PriorityQueue<T = number> {
|
|
|
18
27
|
return this.nodes.length;
|
|
19
28
|
}
|
|
20
29
|
|
|
30
|
+
/**
|
|
31
|
+
* The `heapify` function creates a new PriorityQueue instance and fixes the heap property.
|
|
32
|
+
* @param options - The "options" parameter is an object that contains the configuration options for the PriorityQueue.
|
|
33
|
+
* It can include properties such as "comparator" which specifies the comparison function used to order the elements in
|
|
34
|
+
* the priority queue, and "initialValues" which is an array of initial values to be added to the priority
|
|
35
|
+
* @returns a new instance of the PriorityQueue class after performing the heapify operation on it.
|
|
36
|
+
*/
|
|
21
37
|
static heapify<T>(options: PriorityQueueOptions<T>) {
|
|
22
38
|
const heap = new PriorityQueue(options);
|
|
23
39
|
heap._fix();
|
|
24
40
|
return heap;
|
|
25
41
|
}
|
|
26
42
|
|
|
43
|
+
/**
|
|
44
|
+
* The function checks if a priority queue is valid by creating a new priority queue with a fix option and then calling
|
|
45
|
+
* the isValid method.
|
|
46
|
+
* @param options - An object containing options for creating a priority queue. The options object should have the
|
|
47
|
+
* following properties:
|
|
48
|
+
* @returns the result of calling the `isValid()` method on a new instance of the `PriorityQueue` class.
|
|
49
|
+
*/
|
|
27
50
|
static isPriorityQueueified<T>(options: Omit<PriorityQueueOptions<T>, 'isFix'>) {
|
|
28
51
|
return new PriorityQueue({...options, isFix: true}).isValid();
|
|
29
52
|
}
|
|
30
53
|
|
|
54
|
+
/**
|
|
55
|
+
* The "offer" function adds a node to the heap and ensures that the heap property is maintained.
|
|
56
|
+
* @param {T} node - The parameter "node" is of type T, which means it can be any data type. It represents the node
|
|
57
|
+
* that needs to be added to the heap.
|
|
58
|
+
*/
|
|
31
59
|
offer(node: T) {
|
|
32
60
|
this.nodes.push(node);
|
|
33
61
|
this._heapifyUp(this.size - 1);
|
|
34
62
|
}
|
|
35
63
|
|
|
64
|
+
/**
|
|
65
|
+
* The `peek` function returns the first element of the `nodes` array if it exists, otherwise it returns `null`.
|
|
66
|
+
* @returns The `peek()` function is returning the first element (`T`) of the `nodes` array if the `size` is not zero.
|
|
67
|
+
* Otherwise, it returns `null`.
|
|
68
|
+
*/
|
|
36
69
|
peek(): T | null {
|
|
37
70
|
return this.size ? this.nodes[0] : null;
|
|
38
71
|
}
|
|
39
72
|
|
|
73
|
+
/**
|
|
74
|
+
* The `poll` function removes and returns the top element from a heap data structure.
|
|
75
|
+
* @returns The `poll()` method returns a value of type `T` or `null`.
|
|
76
|
+
*/
|
|
40
77
|
poll(): T | null {
|
|
41
78
|
let res: T | null = null;
|
|
42
79
|
if (this.size > 1) {
|
|
@@ -49,27 +86,55 @@ export class PriorityQueue<T = number> {
|
|
|
49
86
|
return res;
|
|
50
87
|
}
|
|
51
88
|
|
|
89
|
+
/**
|
|
90
|
+
* The `leaf` function returns the last element in the `nodes` array or `null` if the array is empty.
|
|
91
|
+
* @returns The method `leaf()` is returning the last element (`T`) in the `nodes` array if it exists. If the array is
|
|
92
|
+
* empty or the last element is `null`, then it returns `null`.
|
|
93
|
+
*/
|
|
52
94
|
leaf(): T | null {
|
|
53
95
|
return this.nodes[this.size - 1] ?? null;
|
|
54
96
|
}
|
|
55
97
|
|
|
98
|
+
/**
|
|
99
|
+
* The function checks if the size of an object is equal to zero and returns a boolean value indicating whether the
|
|
100
|
+
* object is empty or not.
|
|
101
|
+
* @returns The method `isEmpty()` is returning a boolean value indicating whether the size of the object is equal to
|
|
102
|
+
* 0.
|
|
103
|
+
*/
|
|
56
104
|
isEmpty() {
|
|
57
105
|
return this.size === 0;
|
|
58
106
|
}
|
|
59
107
|
|
|
108
|
+
/**
|
|
109
|
+
* The clear function clears the nodes array.
|
|
110
|
+
*/
|
|
60
111
|
clear() {
|
|
61
112
|
this.nodes = [];
|
|
62
113
|
}
|
|
63
114
|
|
|
115
|
+
/**
|
|
116
|
+
* The toArray function returns an array containing all the elements in the nodes property.
|
|
117
|
+
* @returns An array of type T, which is the elements of the nodes property.
|
|
118
|
+
*/
|
|
64
119
|
toArray(): T[] {
|
|
65
120
|
return [...this.nodes];
|
|
66
121
|
}
|
|
67
122
|
|
|
123
|
+
/**
|
|
124
|
+
* The `clone` function returns a new instance of the `PriorityQueue` class with the same nodes and comparator as the
|
|
125
|
+
* original instance.
|
|
126
|
+
* @returns The `clone()` method is returning a new instance of the `PriorityQueue` class with the same `nodes` and
|
|
127
|
+
* `comparator` properties as the original instance.
|
|
128
|
+
*/
|
|
68
129
|
clone(): PriorityQueue<T> {
|
|
69
130
|
return new PriorityQueue<T>({nodes: this.nodes, comparator: this._comparator});
|
|
70
131
|
}
|
|
71
132
|
|
|
72
133
|
// --- start additional methods ---
|
|
134
|
+
/**
|
|
135
|
+
* The `isValid` function recursively checks if a binary tree satisfies a certain condition.
|
|
136
|
+
* @returns The function `isValid()` returns a boolean value.
|
|
137
|
+
*/
|
|
73
138
|
isValid(): boolean {
|
|
74
139
|
const isValidRecursive = (parentIndex: number): boolean => {
|
|
75
140
|
let isValidLeft = true;
|
|
@@ -93,6 +158,10 @@ export class PriorityQueue<T = number> {
|
|
|
93
158
|
return isValidRecursive(0);
|
|
94
159
|
}
|
|
95
160
|
|
|
161
|
+
/**
|
|
162
|
+
* The function sorts the elements in a data structure and returns them in ascending order.
|
|
163
|
+
* @returns The `sort()` function is returning an array of type `T[]`.
|
|
164
|
+
*/
|
|
96
165
|
sort(): T[] {
|
|
97
166
|
const visitedNode: T[] = [];
|
|
98
167
|
while (this.size !== 0) {
|
|
@@ -102,6 +171,13 @@ export class PriorityQueue<T = number> {
|
|
|
102
171
|
return visitedNode;
|
|
103
172
|
}
|
|
104
173
|
|
|
174
|
+
/**
|
|
175
|
+
* The DFS function performs a depth-first search traversal on a binary tree and returns an array of visited nodes
|
|
176
|
+
* based on the specified traversal order.
|
|
177
|
+
* @param {PriorityQueueDFSOrderPattern} dfsMode - The dfsMode parameter is a string that specifies the order in which
|
|
178
|
+
* the nodes should be visited during the Depth-First Search (DFS) traversal. It can have one of the following values:
|
|
179
|
+
* @returns an array of type `(T | null)[]`.
|
|
180
|
+
*/
|
|
105
181
|
DFS(dfsMode: PriorityQueueDFSOrderPattern): (T | null)[] {
|
|
106
182
|
const visitedNode: (T | null)[] = [];
|
|
107
183
|
|
|
@@ -136,32 +212,72 @@ export class PriorityQueue<T = number> {
|
|
|
136
212
|
return aKey - bKey;
|
|
137
213
|
};
|
|
138
214
|
|
|
215
|
+
/**
|
|
216
|
+
* The function compares two numbers using a custom comparator function.
|
|
217
|
+
* @param {number} a - The parameter "a" is a number that represents the index of a node in an array.
|
|
218
|
+
* @param {number} b - The parameter "b" is a number.
|
|
219
|
+
* @returns the result of the comparison between the elements at indices `a` and `b` in the `nodes` array. The
|
|
220
|
+
* comparison is done using the `_comparator` function, and if the result is greater than 0, `true` is returned,
|
|
221
|
+
* indicating that the element at index `a` is greater than the element at index `b`.
|
|
222
|
+
*/
|
|
139
223
|
protected _compare(a: number, b: number) {
|
|
140
224
|
return this._comparator(this.nodes[a], this.nodes[b]) > 0;
|
|
141
225
|
}
|
|
142
226
|
|
|
227
|
+
/**
|
|
228
|
+
* The function swaps two elements in an array.
|
|
229
|
+
* @param {number} a - The parameter "a" is a number that represents the index of an element in an array.
|
|
230
|
+
* @param {number} b - The parameter "b" is a number.
|
|
231
|
+
*/
|
|
143
232
|
protected _swap(a: number, b: number) {
|
|
144
233
|
const temp = this.nodes[a];
|
|
145
234
|
this.nodes[a] = this.nodes[b];
|
|
146
235
|
this.nodes[b] = temp;
|
|
147
236
|
}
|
|
148
237
|
|
|
238
|
+
/**
|
|
239
|
+
* The function checks if a given index is valid within an array.
|
|
240
|
+
* @param {number} index - The parameter "index" is of type number and represents the index value that needs to be
|
|
241
|
+
* checked for validity.
|
|
242
|
+
* @returns A boolean value indicating whether the given index is valid or not.
|
|
243
|
+
*/
|
|
149
244
|
protected _isValidIndex(index: number): boolean {
|
|
150
245
|
return index > -1 && index < this.nodes.length;
|
|
151
246
|
}
|
|
152
247
|
|
|
248
|
+
/**
|
|
249
|
+
* The function returns the index of the parent node given the index of a child node in a binary tree.
|
|
250
|
+
* @param {number} child - The "child" parameter is a number representing the index of a child node in a binary tree.
|
|
251
|
+
* @returns the parent of the given child node.
|
|
252
|
+
*/
|
|
153
253
|
protected _getParent(child: number): number {
|
|
154
254
|
return Math.floor((child - 1) / 2);
|
|
155
255
|
}
|
|
156
256
|
|
|
257
|
+
/**
|
|
258
|
+
* The function returns the index of the left child node in a binary tree given the index of its parent node.
|
|
259
|
+
* @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
|
|
260
|
+
* @returns the left child of a given parent node in a binary tree.
|
|
261
|
+
*/
|
|
157
262
|
protected _getLeft(parent: number): number {
|
|
158
263
|
return (2 * parent) + 1;
|
|
159
264
|
}
|
|
160
265
|
|
|
266
|
+
/**
|
|
267
|
+
* The function returns the index of the right child node in a binary tree given the index of its parent node.
|
|
268
|
+
* @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
|
|
269
|
+
* @returns the right child of a given parent node in a binary tree.
|
|
270
|
+
*/
|
|
161
271
|
protected _getRight(parent: number): number {
|
|
162
272
|
return (2 * parent) + 2;
|
|
163
273
|
}
|
|
164
274
|
|
|
275
|
+
/**
|
|
276
|
+
* The function returns the index of the smallest child node of a given parent node.
|
|
277
|
+
* @param {number} parent - The parent parameter is a number that represents the index of the parent node in a binary
|
|
278
|
+
* tree.
|
|
279
|
+
* @returns the minimum value between the parent node and its left and right child nodes.
|
|
280
|
+
*/
|
|
165
281
|
protected _getComparedChild(parent: number) {
|
|
166
282
|
let min = parent;
|
|
167
283
|
const left = this._getLeft(parent), right = this._getRight(parent);
|
|
@@ -175,6 +291,11 @@ export class PriorityQueue<T = number> {
|
|
|
175
291
|
return min;
|
|
176
292
|
}
|
|
177
293
|
|
|
294
|
+
/**
|
|
295
|
+
* The function `_heapifyUp` is used to maintain the heap property by moving an element up the heap until it is in the
|
|
296
|
+
* correct position.
|
|
297
|
+
* @param {number} start - The start parameter is the index of the element that needs to be moved up in the heap.
|
|
298
|
+
*/
|
|
178
299
|
protected _heapifyUp(start: number) {
|
|
179
300
|
while (start > 0 && this._compare(this._getParent(start), start)) {
|
|
180
301
|
const parent = this._getParent(start);
|
|
@@ -183,6 +304,11 @@ export class PriorityQueue<T = number> {
|
|
|
183
304
|
}
|
|
184
305
|
}
|
|
185
306
|
|
|
307
|
+
/**
|
|
308
|
+
* The function performs a heapify operation by comparing and swapping elements in a binary heap.
|
|
309
|
+
* @param {number} start - The start parameter is the index of the element in the heap from where the heapifyDown
|
|
310
|
+
* operation should start.
|
|
311
|
+
*/
|
|
186
312
|
protected _heapifyDown(start: number) {
|
|
187
313
|
let min = this._getComparedChild(start);
|
|
188
314
|
while (this._compare(start, min)) {
|
|
@@ -192,6 +318,10 @@ export class PriorityQueue<T = number> {
|
|
|
192
318
|
}
|
|
193
319
|
}
|
|
194
320
|
|
|
321
|
+
/**
|
|
322
|
+
* The _fix function performs a heapify operation on the elements of the heap starting from the middle and moving
|
|
323
|
+
* towards the root.
|
|
324
|
+
*/
|
|
195
325
|
protected _fix() {
|
|
196
326
|
for (let i = Math.floor(this.size / 2); i > -1; i--) this._heapifyDown(i);
|
|
197
327
|
}
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
1
5
|
import {DoublyLinkedList} from '../linked-list';
|
|
2
6
|
|
|
3
7
|
// O(n) time complexity of obtaining the value
|
|
@@ -93,46 +97,111 @@ export class ArrayDeque<T> {
|
|
|
93
97
|
return this._nodes.length;
|
|
94
98
|
}
|
|
95
99
|
|
|
100
|
+
/**
|
|
101
|
+
* The function "offerLast" adds a value to the end of an array.
|
|
102
|
+
* @param {T} value - The value parameter represents the value that you want to add to the end of the array.
|
|
103
|
+
* @returns The return value is the new length of the array after the value has been added.
|
|
104
|
+
*/
|
|
96
105
|
offerLast(value: T) {
|
|
97
106
|
return this._nodes.push(value);
|
|
98
107
|
}
|
|
99
108
|
|
|
109
|
+
/**
|
|
110
|
+
* The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
111
|
+
* @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
112
|
+
*/
|
|
100
113
|
pollLast(): T | null {
|
|
101
114
|
return this._nodes.pop() ?? null;
|
|
102
115
|
}
|
|
103
116
|
|
|
117
|
+
/**
|
|
118
|
+
* The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
119
|
+
* @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
120
|
+
* empty.
|
|
121
|
+
*/
|
|
104
122
|
pollFirst(): T | null {
|
|
105
123
|
return this._nodes.shift() ?? null;
|
|
106
124
|
}
|
|
107
125
|
|
|
126
|
+
/**
|
|
127
|
+
* The function "offerFirst" adds a value to the beginning of an array.
|
|
128
|
+
* @param {T} value - The value parameter represents the value that you want to add to the beginning of the array.
|
|
129
|
+
* @returns The return value of the `offerFirst` function is the new length of the array `_nodes` after adding the
|
|
130
|
+
* `value` at the beginning.
|
|
131
|
+
*/
|
|
108
132
|
offerFirst(value: T) {
|
|
109
133
|
return this._nodes.unshift(value);
|
|
110
134
|
}
|
|
111
135
|
|
|
136
|
+
/**
|
|
137
|
+
* The `peekFirst` function returns the first element of an array or null if the array is empty.
|
|
138
|
+
* @returns The function `peekFirst()` is returning the first element (`T`) of the `_nodes` array. If the array is
|
|
139
|
+
* empty, it will return `null`.
|
|
140
|
+
*/
|
|
112
141
|
peekFirst(): T | null {
|
|
113
142
|
return this._nodes[0] ?? null;
|
|
114
143
|
}
|
|
115
144
|
|
|
145
|
+
/**
|
|
146
|
+
* The `peekLast` function returns the last element of an array or null if the array is empty.
|
|
147
|
+
* @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
148
|
+
*/
|
|
116
149
|
peekLast(): T | null {
|
|
117
150
|
return this._nodes[this._nodes.length - 1] ?? null;
|
|
118
151
|
}
|
|
119
152
|
|
|
153
|
+
/**
|
|
154
|
+
* The get function returns the element at the specified index in an array, or null if the index is out of bounds.
|
|
155
|
+
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
156
|
+
* retrieve from the array.
|
|
157
|
+
* @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
|
|
158
|
+
* will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
|
|
159
|
+
*/
|
|
120
160
|
get(index: number): T | null {
|
|
121
161
|
return this._nodes[index] ?? null;
|
|
122
162
|
}
|
|
123
163
|
|
|
164
|
+
/**
|
|
165
|
+
* The set function assigns a value to a specific index in an array.
|
|
166
|
+
* @param {number} index - The index parameter is a number that represents the position of the element in the array
|
|
167
|
+
* that you want to set a new value for.
|
|
168
|
+
* @param {T} value - The value parameter represents the new value that you want to set at the specified index in the
|
|
169
|
+
* _nodes array.
|
|
170
|
+
* @returns The value that is being set at the specified index in the `_nodes` array.
|
|
171
|
+
*/
|
|
124
172
|
set(index: number, value: T) {
|
|
125
173
|
return this._nodes[index] = value;
|
|
126
174
|
}
|
|
127
175
|
|
|
176
|
+
/**
|
|
177
|
+
* The insert function adds a value at a specified index in an array.
|
|
178
|
+
* @param {number} index - The index parameter specifies the position at which the value should be inserted in the
|
|
179
|
+
* array. It is a number that represents the index of the array where the value should be inserted. The index starts
|
|
180
|
+
* from 0, so the first element of the array has an index of 0, the second element has
|
|
181
|
+
* @param {T} value - The value parameter represents the value that you want to insert into the array at the specified
|
|
182
|
+
* index.
|
|
183
|
+
* @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
|
|
184
|
+
* are being removed, an empty array will be returned.
|
|
185
|
+
*/
|
|
128
186
|
insert(index: number, value: T) {
|
|
129
187
|
return this._nodes.splice(index, 0, value);
|
|
130
188
|
}
|
|
131
189
|
|
|
190
|
+
/**
|
|
191
|
+
* The remove function removes an element from an array at a specified index.
|
|
192
|
+
* @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
|
|
193
|
+
* is a number that represents the index of the element to be removed.
|
|
194
|
+
* @returns The method is returning an array containing the removed element.
|
|
195
|
+
*/
|
|
132
196
|
remove(index: number) {
|
|
133
197
|
return this._nodes.splice(index, 1);
|
|
134
198
|
}
|
|
135
199
|
|
|
200
|
+
/**
|
|
201
|
+
* The function checks if an array called "_nodes" is empty.
|
|
202
|
+
* @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
|
|
203
|
+
* is 0, indicating that the array is empty. Otherwise, it returns `false`.
|
|
204
|
+
*/
|
|
136
205
|
isEmpty() {
|
|
137
206
|
return this._nodes.length === 0;
|
|
138
207
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license MIT
|
|
3
|
-
* @copyright
|
|
3
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
6
|
export class Queue<T> {
|
|
@@ -8,8 +8,10 @@ export class Queue<T> {
|
|
|
8
8
|
protected _offset: number;
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
12
|
-
* @param {
|
|
11
|
+
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
12
|
+
* @param {T[]} [elements] - The `elements` parameter is an optional array of elements of type `T`. If provided, it
|
|
13
|
+
* will be used to initialize the `_nodes` property of the class. If not provided, the `_nodes` property will be
|
|
14
|
+
* initialized as an empty array.
|
|
13
15
|
*/
|
|
14
16
|
constructor(elements?: T[]) {
|
|
15
17
|
this._nodes = elements || [];
|
|
@@ -17,20 +19,21 @@ export class Queue<T> {
|
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
/**
|
|
20
|
-
* Creates a queue from an existing array.
|
|
22
|
+
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
21
23
|
* @public
|
|
22
24
|
* @static
|
|
23
|
-
* @param {
|
|
24
|
-
* @
|
|
25
|
+
* @param {T[]} elements - The "elements" parameter is an array of elements of type T.
|
|
26
|
+
* @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
|
|
27
|
+
* array.
|
|
25
28
|
*/
|
|
26
29
|
static fromArray<T>(elements: T[]): Queue<T> {
|
|
27
30
|
return new Queue(elements);
|
|
28
31
|
}
|
|
29
32
|
|
|
30
33
|
/**
|
|
31
|
-
* Adds an element at the back of the queue.
|
|
32
|
-
* @
|
|
33
|
-
* @
|
|
34
|
+
* The offer function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
|
|
35
|
+
* @param {T} element - The `element` parameter represents the element that you want to add to the queue.
|
|
36
|
+
* @returns The `offer` method is returning a `Queue<T>` object.
|
|
34
37
|
*/
|
|
35
38
|
offer(element: T): Queue<T> {
|
|
36
39
|
this._nodes.push(element);
|
|
@@ -38,9 +41,9 @@ export class Queue<T> {
|
|
|
38
41
|
}
|
|
39
42
|
|
|
40
43
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* @returns
|
|
44
|
+
* The `poll` function removes and returns the first element in the queue, and adjusts the internal data structure if
|
|
45
|
+
* necessary to optimize performance.
|
|
46
|
+
* @returns The function `poll()` returns either the first element in the queue or `null` if the queue is empty.
|
|
44
47
|
*/
|
|
45
48
|
poll(): T | null {
|
|
46
49
|
if (this.size() === 0) return null;
|
|
@@ -58,53 +61,49 @@ export class Queue<T> {
|
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
/**
|
|
61
|
-
*
|
|
62
|
-
* @
|
|
63
|
-
*
|
|
64
|
+
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
65
|
+
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
66
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
64
67
|
*/
|
|
65
68
|
peek(): T | null {
|
|
66
69
|
return this.size() > 0 ? this._nodes[this._offset] : null;
|
|
67
70
|
}
|
|
68
71
|
|
|
69
72
|
/**
|
|
70
|
-
*
|
|
71
|
-
* @
|
|
72
|
-
*
|
|
73
|
+
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
74
|
+
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
75
|
+
* array is empty, it returns `null`.
|
|
73
76
|
*/
|
|
74
77
|
peekLast(): T | null {
|
|
75
78
|
return this.size() > 0 ? this._nodes[this._nodes.length - 1] : null;
|
|
76
79
|
}
|
|
77
80
|
|
|
78
81
|
/**
|
|
79
|
-
*
|
|
80
|
-
* @
|
|
81
|
-
* @returns {number}
|
|
82
|
+
* The size function returns the number of elements in an array.
|
|
83
|
+
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
82
84
|
*/
|
|
83
85
|
size(): number {
|
|
84
86
|
return this._nodes.length - this._offset;
|
|
85
87
|
}
|
|
86
88
|
|
|
87
89
|
/**
|
|
88
|
-
*
|
|
89
|
-
* @
|
|
90
|
-
* @returns {boolean}
|
|
90
|
+
* The function checks if a data structure is empty by comparing its size to zero.
|
|
91
|
+
* @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
|
|
91
92
|
*/
|
|
92
93
|
isEmpty(): boolean {
|
|
93
94
|
return this.size() === 0;
|
|
94
95
|
}
|
|
95
96
|
|
|
96
97
|
/**
|
|
97
|
-
*
|
|
98
|
-
* @
|
|
99
|
-
* @returns {array}
|
|
98
|
+
* The toArray() function returns an array of elements from the current offset to the end of the _nodes array.
|
|
99
|
+
* @returns An array of type T is being returned.
|
|
100
100
|
*/
|
|
101
101
|
toArray(): T[] {
|
|
102
102
|
return this._nodes.slice(this._offset);
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
/**
|
|
106
|
-
*
|
|
107
|
-
* @public
|
|
106
|
+
* The clear function resets the nodes array and offset to their initial values.
|
|
108
107
|
*/
|
|
109
108
|
clear(): void {
|
|
110
109
|
this._nodes = [];
|
|
@@ -112,9 +111,8 @@ export class Queue<T> {
|
|
|
112
111
|
}
|
|
113
112
|
|
|
114
113
|
/**
|
|
115
|
-
*
|
|
116
|
-
* @
|
|
117
|
-
* @return {Queue}
|
|
114
|
+
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
|
|
115
|
+
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
118
116
|
*/
|
|
119
117
|
clone(): Queue<T> {
|
|
120
118
|
return new Queue(this._nodes.slice(this._offset));
|
|
@@ -1,52 +1,50 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license MIT
|
|
3
|
-
* @copyright
|
|
3
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
6
|
export class Stack<T> {
|
|
7
7
|
protected _elements: T[];
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
* @param {
|
|
10
|
+
* The constructor initializes an array of elements, which can be provided as an optional parameter.
|
|
11
|
+
* @param {T[]} [elements] - The `elements` parameter is an optional parameter of type `T[]`, which represents an array
|
|
12
|
+
* of elements of type `T`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
|
|
13
|
+
* is provided and is an array, it is assigned to the `_elements
|
|
12
14
|
*/
|
|
13
15
|
constructor(elements?: T[]) {
|
|
14
16
|
this._elements = Array.isArray(elements) ? elements : [];
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
/**
|
|
18
|
-
*
|
|
19
|
-
* @
|
|
20
|
-
* @
|
|
21
|
-
*
|
|
22
|
-
* @return {Stack}
|
|
20
|
+
* The function "fromArray" creates a new Stack object from an array of elements.
|
|
21
|
+
* @param {T[]} elements - The `elements` parameter is an array of elements of type `T`.
|
|
22
|
+
* @returns {Stack} The method is returning a new instance of the Stack class, initialized with the elements from the input
|
|
23
|
+
* array.
|
|
23
24
|
*/
|
|
24
25
|
static fromArray<T>(elements: T[]): Stack<T> {
|
|
25
26
|
return new Stack(elements);
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
|
-
*
|
|
30
|
-
* @
|
|
31
|
-
* @returns {boolean}
|
|
30
|
+
* The function checks if an array is empty and returns a boolean value.
|
|
31
|
+
* @returns A boolean value indicating whether the `_elements` array is empty or not.
|
|
32
32
|
*/
|
|
33
33
|
isEmpty(): boolean {
|
|
34
34
|
return this._elements.length === 0;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
*
|
|
39
|
-
* @
|
|
40
|
-
* @returns {number}
|
|
38
|
+
* The size() function returns the number of elements in an array.
|
|
39
|
+
* @returns The size of the elements array.
|
|
41
40
|
*/
|
|
42
41
|
size(): number {
|
|
43
42
|
return this._elements.length;
|
|
44
43
|
}
|
|
45
44
|
|
|
46
45
|
/**
|
|
47
|
-
*
|
|
48
|
-
* @
|
|
49
|
-
* @returns {object}
|
|
46
|
+
* The `peek` function returns the last element of an array, or null if the array is empty.
|
|
47
|
+
* @returns The `peek()` function returns the last element of the `_elements` array, or `null` if the array is empty.
|
|
50
48
|
*/
|
|
51
49
|
peek(): T | null {
|
|
52
50
|
if (this.isEmpty()) return null;
|
|
@@ -55,9 +53,9 @@ export class Stack<T> {
|
|
|
55
53
|
}
|
|
56
54
|
|
|
57
55
|
/**
|
|
58
|
-
*
|
|
59
|
-
* @
|
|
60
|
-
* @
|
|
56
|
+
* The push function adds an element to the stack and returns the updated stack.
|
|
57
|
+
* @param {T} element - The parameter "element" is of type T, which means it can be any data type.
|
|
58
|
+
* @returns The `push` method is returning the updated `Stack<T>` object.
|
|
61
59
|
*/
|
|
62
60
|
push(element: T): Stack<T> {
|
|
63
61
|
this._elements.push(element);
|
|
@@ -65,9 +63,9 @@ export class Stack<T> {
|
|
|
65
63
|
}
|
|
66
64
|
|
|
67
65
|
/**
|
|
68
|
-
*
|
|
69
|
-
* @
|
|
70
|
-
*
|
|
66
|
+
* The `pop` function removes and returns the last element from an array, or returns null if the array is empty.
|
|
67
|
+
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
68
|
+
* array is empty, it returns `null`.
|
|
71
69
|
*/
|
|
72
70
|
pop(): T | null {
|
|
73
71
|
if (this.isEmpty()) return null;
|
|
@@ -76,26 +74,23 @@ export class Stack<T> {
|
|
|
76
74
|
}
|
|
77
75
|
|
|
78
76
|
/**
|
|
79
|
-
*
|
|
80
|
-
* @
|
|
81
|
-
* @returns {array}
|
|
77
|
+
* The toArray function returns a copy of the elements in an array.
|
|
78
|
+
* @returns An array of type T.
|
|
82
79
|
*/
|
|
83
80
|
toArray(): T[] {
|
|
84
81
|
return this._elements.slice();
|
|
85
82
|
}
|
|
86
83
|
|
|
87
84
|
/**
|
|
88
|
-
*
|
|
89
|
-
* @public
|
|
85
|
+
* The clear function clears the elements array.
|
|
90
86
|
*/
|
|
91
87
|
clear(): void {
|
|
92
88
|
this._elements = [];
|
|
93
89
|
}
|
|
94
90
|
|
|
95
91
|
/**
|
|
96
|
-
*
|
|
97
|
-
* @
|
|
98
|
-
* @return {Stack}
|
|
92
|
+
* The `clone()` function returns a new `Stack` object with the same elements as the original stack.
|
|
93
|
+
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
99
94
|
*/
|
|
100
95
|
clone(): Stack<T> {
|
|
101
96
|
return new Stack(this._elements.slice());
|