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
|
@@ -38,6 +38,11 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
exports.PriorityQueue = void 0;
|
|
40
40
|
var PriorityQueue = /** @class */ (function () {
|
|
41
|
+
/**
|
|
42
|
+
* The constructor initializes a priority queue with the given options, including an array of nodes and a comparator
|
|
43
|
+
* function.
|
|
44
|
+
* @param options - The `options` parameter is an object that contains the following properties:
|
|
45
|
+
*/
|
|
41
46
|
function PriorityQueue(options) {
|
|
42
47
|
this.nodes = [];
|
|
43
48
|
this._comparator = function (a, b) {
|
|
@@ -59,21 +64,49 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
59
64
|
enumerable: false,
|
|
60
65
|
configurable: true
|
|
61
66
|
});
|
|
67
|
+
/**
|
|
68
|
+
* The `heapify` function creates a new PriorityQueue instance and fixes the heap property.
|
|
69
|
+
* @param options - The "options" parameter is an object that contains the configuration options for the PriorityQueue.
|
|
70
|
+
* It can include properties such as "comparator" which specifies the comparison function used to order the elements in
|
|
71
|
+
* the priority queue, and "initialValues" which is an array of initial values to be added to the priority
|
|
72
|
+
* @returns a new instance of the PriorityQueue class after performing the heapify operation on it.
|
|
73
|
+
*/
|
|
62
74
|
PriorityQueue.heapify = function (options) {
|
|
63
75
|
var heap = new PriorityQueue(options);
|
|
64
76
|
heap._fix();
|
|
65
77
|
return heap;
|
|
66
78
|
};
|
|
79
|
+
/**
|
|
80
|
+
* The function checks if a priority queue is valid by creating a new priority queue with a fix option and then calling
|
|
81
|
+
* the isValid method.
|
|
82
|
+
* @param options - An object containing options for creating a priority queue. The options object should have the
|
|
83
|
+
* following properties:
|
|
84
|
+
* @returns the result of calling the `isValid()` method on a new instance of the `PriorityQueue` class.
|
|
85
|
+
*/
|
|
67
86
|
PriorityQueue.isPriorityQueueified = function (options) {
|
|
68
87
|
return new PriorityQueue(__assign(__assign({}, options), { isFix: true })).isValid();
|
|
69
88
|
};
|
|
89
|
+
/**
|
|
90
|
+
* The "offer" function adds a node to the heap and ensures that the heap property is maintained.
|
|
91
|
+
* @param {T} node - The parameter "node" is of type T, which means it can be any data type. It represents the node
|
|
92
|
+
* that needs to be added to the heap.
|
|
93
|
+
*/
|
|
70
94
|
PriorityQueue.prototype.offer = function (node) {
|
|
71
95
|
this.nodes.push(node);
|
|
72
96
|
this._heapifyUp(this.size - 1);
|
|
73
97
|
};
|
|
98
|
+
/**
|
|
99
|
+
* The `peek` function returns the first element of the `nodes` array if it exists, otherwise it returns `null`.
|
|
100
|
+
* @returns The `peek()` function is returning the first element (`T`) of the `nodes` array if the `size` is not zero.
|
|
101
|
+
* Otherwise, it returns `null`.
|
|
102
|
+
*/
|
|
74
103
|
PriorityQueue.prototype.peek = function () {
|
|
75
104
|
return this.size ? this.nodes[0] : null;
|
|
76
105
|
};
|
|
106
|
+
/**
|
|
107
|
+
* The `poll` function removes and returns the top element from a heap data structure.
|
|
108
|
+
* @returns The `poll()` method returns a value of type `T` or `null`.
|
|
109
|
+
*/
|
|
77
110
|
PriorityQueue.prototype.poll = function () {
|
|
78
111
|
var _a, _b;
|
|
79
112
|
var res = null;
|
|
@@ -87,23 +120,51 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
87
120
|
}
|
|
88
121
|
return res;
|
|
89
122
|
};
|
|
123
|
+
/**
|
|
124
|
+
* The `leaf` function returns the last element in the `nodes` array or `null` if the array is empty.
|
|
125
|
+
* @returns The method `leaf()` is returning the last element (`T`) in the `nodes` array if it exists. If the array is
|
|
126
|
+
* empty or the last element is `null`, then it returns `null`.
|
|
127
|
+
*/
|
|
90
128
|
PriorityQueue.prototype.leaf = function () {
|
|
91
129
|
var _a;
|
|
92
130
|
return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : null;
|
|
93
131
|
};
|
|
132
|
+
/**
|
|
133
|
+
* The function checks if the size of an object is equal to zero and returns a boolean value indicating whether the
|
|
134
|
+
* object is empty or not.
|
|
135
|
+
* @returns The method `isEmpty()` is returning a boolean value indicating whether the size of the object is equal to
|
|
136
|
+
* 0.
|
|
137
|
+
*/
|
|
94
138
|
PriorityQueue.prototype.isEmpty = function () {
|
|
95
139
|
return this.size === 0;
|
|
96
140
|
};
|
|
141
|
+
/**
|
|
142
|
+
* The clear function clears the nodes array.
|
|
143
|
+
*/
|
|
97
144
|
PriorityQueue.prototype.clear = function () {
|
|
98
145
|
this.nodes = [];
|
|
99
146
|
};
|
|
147
|
+
/**
|
|
148
|
+
* The toArray function returns an array containing all the elements in the nodes property.
|
|
149
|
+
* @returns An array of type T, which is the elements of the nodes property.
|
|
150
|
+
*/
|
|
100
151
|
PriorityQueue.prototype.toArray = function () {
|
|
101
152
|
return __spreadArray([], __read(this.nodes), false);
|
|
102
153
|
};
|
|
154
|
+
/**
|
|
155
|
+
* The `clone` function returns a new instance of the `PriorityQueue` class with the same nodes and comparator as the
|
|
156
|
+
* original instance.
|
|
157
|
+
* @returns The `clone()` method is returning a new instance of the `PriorityQueue` class with the same `nodes` and
|
|
158
|
+
* `comparator` properties as the original instance.
|
|
159
|
+
*/
|
|
103
160
|
PriorityQueue.prototype.clone = function () {
|
|
104
161
|
return new PriorityQueue({ nodes: this.nodes, comparator: this._comparator });
|
|
105
162
|
};
|
|
106
163
|
// --- start additional methods ---
|
|
164
|
+
/**
|
|
165
|
+
* The `isValid` function recursively checks if a binary tree satisfies a certain condition.
|
|
166
|
+
* @returns The function `isValid()` returns a boolean value.
|
|
167
|
+
*/
|
|
107
168
|
PriorityQueue.prototype.isValid = function () {
|
|
108
169
|
var _this = this;
|
|
109
170
|
var isValidRecursive = function (parentIndex) {
|
|
@@ -125,6 +186,10 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
125
186
|
};
|
|
126
187
|
return isValidRecursive(0);
|
|
127
188
|
};
|
|
189
|
+
/**
|
|
190
|
+
* The function sorts the elements in a data structure and returns them in ascending order.
|
|
191
|
+
* @returns The `sort()` function is returning an array of type `T[]`.
|
|
192
|
+
*/
|
|
128
193
|
PriorityQueue.prototype.sort = function () {
|
|
129
194
|
var visitedNode = [];
|
|
130
195
|
while (this.size !== 0) {
|
|
@@ -134,6 +199,13 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
134
199
|
}
|
|
135
200
|
return visitedNode;
|
|
136
201
|
};
|
|
202
|
+
/**
|
|
203
|
+
* The DFS function performs a depth-first search traversal on a binary tree and returns an array of visited nodes
|
|
204
|
+
* based on the specified traversal order.
|
|
205
|
+
* @param {PriorityQueueDFSOrderPattern} dfsMode - The dfsMode parameter is a string that specifies the order in which
|
|
206
|
+
* the nodes should be visited during the Depth-First Search (DFS) traversal. It can have one of the following values:
|
|
207
|
+
* @returns an array of type `(T | null)[]`.
|
|
208
|
+
*/
|
|
137
209
|
PriorityQueue.prototype.DFS = function (dfsMode) {
|
|
138
210
|
var _this = this;
|
|
139
211
|
var visitedNode = [];
|
|
@@ -162,26 +234,66 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
162
234
|
this._isValidIndex(0) && traverse(0);
|
|
163
235
|
return visitedNode;
|
|
164
236
|
};
|
|
237
|
+
/**
|
|
238
|
+
* The function compares two numbers using a custom comparator function.
|
|
239
|
+
* @param {number} a - The parameter "a" is a number that represents the index of a node in an array.
|
|
240
|
+
* @param {number} b - The parameter "b" is a number.
|
|
241
|
+
* @returns the result of the comparison between the elements at indices `a` and `b` in the `nodes` array. The
|
|
242
|
+
* comparison is done using the `_comparator` function, and if the result is greater than 0, `true` is returned,
|
|
243
|
+
* indicating that the element at index `a` is greater than the element at index `b`.
|
|
244
|
+
*/
|
|
165
245
|
PriorityQueue.prototype._compare = function (a, b) {
|
|
166
246
|
return this._comparator(this.nodes[a], this.nodes[b]) > 0;
|
|
167
247
|
};
|
|
248
|
+
/**
|
|
249
|
+
* The function swaps two elements in an array.
|
|
250
|
+
* @param {number} a - The parameter "a" is a number that represents the index of an element in an array.
|
|
251
|
+
* @param {number} b - The parameter "b" is a number.
|
|
252
|
+
*/
|
|
168
253
|
PriorityQueue.prototype._swap = function (a, b) {
|
|
169
254
|
var temp = this.nodes[a];
|
|
170
255
|
this.nodes[a] = this.nodes[b];
|
|
171
256
|
this.nodes[b] = temp;
|
|
172
257
|
};
|
|
258
|
+
/**
|
|
259
|
+
* The function checks if a given index is valid within an array.
|
|
260
|
+
* @param {number} index - The parameter "index" is of type number and represents the index value that needs to be
|
|
261
|
+
* checked for validity.
|
|
262
|
+
* @returns A boolean value indicating whether the given index is valid or not.
|
|
263
|
+
*/
|
|
173
264
|
PriorityQueue.prototype._isValidIndex = function (index) {
|
|
174
265
|
return index > -1 && index < this.nodes.length;
|
|
175
266
|
};
|
|
267
|
+
/**
|
|
268
|
+
* The function returns the index of the parent node given the index of a child node in a binary tree.
|
|
269
|
+
* @param {number} child - The "child" parameter is a number representing the index of a child node in a binary tree.
|
|
270
|
+
* @returns the parent of the given child node.
|
|
271
|
+
*/
|
|
176
272
|
PriorityQueue.prototype._getParent = function (child) {
|
|
177
273
|
return Math.floor((child - 1) / 2);
|
|
178
274
|
};
|
|
275
|
+
/**
|
|
276
|
+
* The function returns the index of the left child node in a binary tree given the index of its parent node.
|
|
277
|
+
* @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
|
|
278
|
+
* @returns the left child of a given parent node in a binary tree.
|
|
279
|
+
*/
|
|
179
280
|
PriorityQueue.prototype._getLeft = function (parent) {
|
|
180
281
|
return (2 * parent) + 1;
|
|
181
282
|
};
|
|
283
|
+
/**
|
|
284
|
+
* The function returns the index of the right child node in a binary tree given the index of its parent node.
|
|
285
|
+
* @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
|
|
286
|
+
* @returns the right child of a given parent node in a binary tree.
|
|
287
|
+
*/
|
|
182
288
|
PriorityQueue.prototype._getRight = function (parent) {
|
|
183
289
|
return (2 * parent) + 2;
|
|
184
290
|
};
|
|
291
|
+
/**
|
|
292
|
+
* The function returns the index of the smallest child node of a given parent node.
|
|
293
|
+
* @param {number} parent - The parent parameter is a number that represents the index of the parent node in a binary
|
|
294
|
+
* tree.
|
|
295
|
+
* @returns the minimum value between the parent node and its left and right child nodes.
|
|
296
|
+
*/
|
|
185
297
|
PriorityQueue.prototype._getComparedChild = function (parent) {
|
|
186
298
|
var min = parent;
|
|
187
299
|
var left = this._getLeft(parent), right = this._getRight(parent);
|
|
@@ -193,6 +305,11 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
193
305
|
}
|
|
194
306
|
return min;
|
|
195
307
|
};
|
|
308
|
+
/**
|
|
309
|
+
* The function `_heapifyUp` is used to maintain the heap property by moving an element up the heap until it is in the
|
|
310
|
+
* correct position.
|
|
311
|
+
* @param {number} start - The start parameter is the index of the element that needs to be moved up in the heap.
|
|
312
|
+
*/
|
|
196
313
|
PriorityQueue.prototype._heapifyUp = function (start) {
|
|
197
314
|
while (start > 0 && this._compare(this._getParent(start), start)) {
|
|
198
315
|
var parent = this._getParent(start);
|
|
@@ -200,6 +317,11 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
200
317
|
start = parent;
|
|
201
318
|
}
|
|
202
319
|
};
|
|
320
|
+
/**
|
|
321
|
+
* The function performs a heapify operation by comparing and swapping elements in a binary heap.
|
|
322
|
+
* @param {number} start - The start parameter is the index of the element in the heap from where the heapifyDown
|
|
323
|
+
* operation should start.
|
|
324
|
+
*/
|
|
203
325
|
PriorityQueue.prototype._heapifyDown = function (start) {
|
|
204
326
|
var min = this._getComparedChild(start);
|
|
205
327
|
while (this._compare(start, min)) {
|
|
@@ -208,6 +330,10 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
208
330
|
min = this._getComparedChild(start);
|
|
209
331
|
}
|
|
210
332
|
};
|
|
333
|
+
/**
|
|
334
|
+
* The _fix function performs a heapify operation on the elements of the heap starting from the middle and moving
|
|
335
|
+
* towards the root.
|
|
336
|
+
*/
|
|
211
337
|
PriorityQueue.prototype._fix = function () {
|
|
212
338
|
for (var i = Math.floor(this.size / 2); i > -1; i--)
|
|
213
339
|
this._heapifyDown(i);
|
|
@@ -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
|
export declare class Deque<T> extends DoublyLinkedList<T> {
|
|
3
7
|
}
|
|
@@ -23,15 +27,80 @@ export declare class ObjectDeque<T> {
|
|
|
23
27
|
export declare class ArrayDeque<T> {
|
|
24
28
|
protected _nodes: T[];
|
|
25
29
|
get size(): number;
|
|
30
|
+
/**
|
|
31
|
+
* The function "offerLast" adds a value to the end of an array.
|
|
32
|
+
* @param {T} value - The value parameter represents the value that you want to add to the end of the array.
|
|
33
|
+
* @returns The return value is the new length of the array after the value has been added.
|
|
34
|
+
*/
|
|
26
35
|
offerLast(value: T): number;
|
|
36
|
+
/**
|
|
37
|
+
* The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
38
|
+
* @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
39
|
+
*/
|
|
27
40
|
pollLast(): T | null;
|
|
41
|
+
/**
|
|
42
|
+
* The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
43
|
+
* @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
44
|
+
* empty.
|
|
45
|
+
*/
|
|
28
46
|
pollFirst(): T | null;
|
|
47
|
+
/**
|
|
48
|
+
* The function "offerFirst" adds a value to the beginning of an array.
|
|
49
|
+
* @param {T} value - The value parameter represents the value that you want to add to the beginning of the array.
|
|
50
|
+
* @returns The return value of the `offerFirst` function is the new length of the array `_nodes` after adding the
|
|
51
|
+
* `value` at the beginning.
|
|
52
|
+
*/
|
|
29
53
|
offerFirst(value: T): number;
|
|
54
|
+
/**
|
|
55
|
+
* The `peekFirst` function returns the first element of an array or null if the array is empty.
|
|
56
|
+
* @returns The function `peekFirst()` is returning the first element (`T`) of the `_nodes` array. If the array is
|
|
57
|
+
* empty, it will return `null`.
|
|
58
|
+
*/
|
|
30
59
|
peekFirst(): T | null;
|
|
60
|
+
/**
|
|
61
|
+
* The `peekLast` function returns the last element of an array or null if the array is empty.
|
|
62
|
+
* @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
63
|
+
*/
|
|
31
64
|
peekLast(): T | null;
|
|
65
|
+
/**
|
|
66
|
+
* The get function returns the element at the specified index in an array, or null if the index is out of bounds.
|
|
67
|
+
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
68
|
+
* retrieve from the array.
|
|
69
|
+
* @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
|
|
70
|
+
* will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
|
|
71
|
+
*/
|
|
32
72
|
get(index: number): T | null;
|
|
73
|
+
/**
|
|
74
|
+
* The set function assigns a value to a specific index in an array.
|
|
75
|
+
* @param {number} index - The index parameter is a number that represents the position of the element in the array
|
|
76
|
+
* that you want to set a new value for.
|
|
77
|
+
* @param {T} value - The value parameter represents the new value that you want to set at the specified index in the
|
|
78
|
+
* _nodes array.
|
|
79
|
+
* @returns The value that is being set at the specified index in the `_nodes` array.
|
|
80
|
+
*/
|
|
33
81
|
set(index: number, value: T): T;
|
|
82
|
+
/**
|
|
83
|
+
* The insert function adds a value at a specified index in an array.
|
|
84
|
+
* @param {number} index - The index parameter specifies the position at which the value should be inserted in the
|
|
85
|
+
* array. It is a number that represents the index of the array where the value should be inserted. The index starts
|
|
86
|
+
* from 0, so the first element of the array has an index of 0, the second element has
|
|
87
|
+
* @param {T} value - The value parameter represents the value that you want to insert into the array at the specified
|
|
88
|
+
* index.
|
|
89
|
+
* @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
|
|
90
|
+
* are being removed, an empty array will be returned.
|
|
91
|
+
*/
|
|
34
92
|
insert(index: number, value: T): T[];
|
|
93
|
+
/**
|
|
94
|
+
* The remove function removes an element from an array at a specified index.
|
|
95
|
+
* @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
|
|
96
|
+
* is a number that represents the index of the element to be removed.
|
|
97
|
+
* @returns The method is returning an array containing the removed element.
|
|
98
|
+
*/
|
|
35
99
|
remove(index: number): T[];
|
|
100
|
+
/**
|
|
101
|
+
* The function checks if an array called "_nodes" is empty.
|
|
102
|
+
* @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
|
|
103
|
+
* is 0, indicating that the array is empty. Otherwise, it returns `false`.
|
|
104
|
+
*/
|
|
36
105
|
isEmpty(): boolean;
|
|
37
106
|
}
|
|
@@ -16,6 +16,10 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
16
16
|
})();
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.ArrayDeque = exports.ObjectDeque = exports.Deque = void 0;
|
|
19
|
+
/**
|
|
20
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
21
|
+
* @license MIT
|
|
22
|
+
*/
|
|
19
23
|
var linked_list_1 = require("../linked-list");
|
|
20
24
|
// O(n) time complexity of obtaining the value
|
|
21
25
|
// O(1) time complexity of adding at the beginning and the end
|
|
@@ -115,41 +119,106 @@ var ArrayDeque = /** @class */ (function () {
|
|
|
115
119
|
enumerable: false,
|
|
116
120
|
configurable: true
|
|
117
121
|
});
|
|
122
|
+
/**
|
|
123
|
+
* The function "offerLast" adds a value to the end of an array.
|
|
124
|
+
* @param {T} value - The value parameter represents the value that you want to add to the end of the array.
|
|
125
|
+
* @returns The return value is the new length of the array after the value has been added.
|
|
126
|
+
*/
|
|
118
127
|
ArrayDeque.prototype.offerLast = function (value) {
|
|
119
128
|
return this._nodes.push(value);
|
|
120
129
|
};
|
|
130
|
+
/**
|
|
131
|
+
* The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
132
|
+
* @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
133
|
+
*/
|
|
121
134
|
ArrayDeque.prototype.pollLast = function () {
|
|
122
135
|
var _a;
|
|
123
136
|
return (_a = this._nodes.pop()) !== null && _a !== void 0 ? _a : null;
|
|
124
137
|
};
|
|
138
|
+
/**
|
|
139
|
+
* The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
140
|
+
* @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
141
|
+
* empty.
|
|
142
|
+
*/
|
|
125
143
|
ArrayDeque.prototype.pollFirst = function () {
|
|
126
144
|
var _a;
|
|
127
145
|
return (_a = this._nodes.shift()) !== null && _a !== void 0 ? _a : null;
|
|
128
146
|
};
|
|
147
|
+
/**
|
|
148
|
+
* The function "offerFirst" adds a value to the beginning of an array.
|
|
149
|
+
* @param {T} value - The value parameter represents the value that you want to add to the beginning of the array.
|
|
150
|
+
* @returns The return value of the `offerFirst` function is the new length of the array `_nodes` after adding the
|
|
151
|
+
* `value` at the beginning.
|
|
152
|
+
*/
|
|
129
153
|
ArrayDeque.prototype.offerFirst = function (value) {
|
|
130
154
|
return this._nodes.unshift(value);
|
|
131
155
|
};
|
|
156
|
+
/**
|
|
157
|
+
* The `peekFirst` function returns the first element of an array or null if the array is empty.
|
|
158
|
+
* @returns The function `peekFirst()` is returning the first element (`T`) of the `_nodes` array. If the array is
|
|
159
|
+
* empty, it will return `null`.
|
|
160
|
+
*/
|
|
132
161
|
ArrayDeque.prototype.peekFirst = function () {
|
|
133
162
|
var _a;
|
|
134
163
|
return (_a = this._nodes[0]) !== null && _a !== void 0 ? _a : null;
|
|
135
164
|
};
|
|
165
|
+
/**
|
|
166
|
+
* The `peekLast` function returns the last element of an array or null if the array is empty.
|
|
167
|
+
* @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
168
|
+
*/
|
|
136
169
|
ArrayDeque.prototype.peekLast = function () {
|
|
137
170
|
var _a;
|
|
138
171
|
return (_a = this._nodes[this._nodes.length - 1]) !== null && _a !== void 0 ? _a : null;
|
|
139
172
|
};
|
|
173
|
+
/**
|
|
174
|
+
* The get function returns the element at the specified index in an array, or null if the index is out of bounds.
|
|
175
|
+
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
176
|
+
* retrieve from the array.
|
|
177
|
+
* @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
|
|
178
|
+
* will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
|
|
179
|
+
*/
|
|
140
180
|
ArrayDeque.prototype.get = function (index) {
|
|
141
181
|
var _a;
|
|
142
182
|
return (_a = this._nodes[index]) !== null && _a !== void 0 ? _a : null;
|
|
143
183
|
};
|
|
184
|
+
/**
|
|
185
|
+
* The set function assigns a value to a specific index in an array.
|
|
186
|
+
* @param {number} index - The index parameter is a number that represents the position of the element in the array
|
|
187
|
+
* that you want to set a new value for.
|
|
188
|
+
* @param {T} value - The value parameter represents the new value that you want to set at the specified index in the
|
|
189
|
+
* _nodes array.
|
|
190
|
+
* @returns The value that is being set at the specified index in the `_nodes` array.
|
|
191
|
+
*/
|
|
144
192
|
ArrayDeque.prototype.set = function (index, value) {
|
|
145
193
|
return this._nodes[index] = value;
|
|
146
194
|
};
|
|
195
|
+
/**
|
|
196
|
+
* The insert function adds a value at a specified index in an array.
|
|
197
|
+
* @param {number} index - The index parameter specifies the position at which the value should be inserted in the
|
|
198
|
+
* array. It is a number that represents the index of the array where the value should be inserted. The index starts
|
|
199
|
+
* from 0, so the first element of the array has an index of 0, the second element has
|
|
200
|
+
* @param {T} value - The value parameter represents the value that you want to insert into the array at the specified
|
|
201
|
+
* index.
|
|
202
|
+
* @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
|
|
203
|
+
* are being removed, an empty array will be returned.
|
|
204
|
+
*/
|
|
147
205
|
ArrayDeque.prototype.insert = function (index, value) {
|
|
148
206
|
return this._nodes.splice(index, 0, value);
|
|
149
207
|
};
|
|
208
|
+
/**
|
|
209
|
+
* The remove function removes an element from an array at a specified index.
|
|
210
|
+
* @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
|
|
211
|
+
* is a number that represents the index of the element to be removed.
|
|
212
|
+
* @returns The method is returning an array containing the removed element.
|
|
213
|
+
*/
|
|
150
214
|
ArrayDeque.prototype.remove = function (index) {
|
|
151
215
|
return this._nodes.splice(index, 1);
|
|
152
216
|
};
|
|
217
|
+
/**
|
|
218
|
+
* The function checks if an array called "_nodes" is empty.
|
|
219
|
+
* @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
|
|
220
|
+
* is 0, indicating that the array is empty. Otherwise, it returns `false`.
|
|
221
|
+
*/
|
|
153
222
|
ArrayDeque.prototype.isEmpty = function () {
|
|
154
223
|
return this._nodes.length === 0;
|
|
155
224
|
};
|
|
@@ -1,75 +1,73 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license MIT
|
|
3
|
-
* @copyright
|
|
3
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
6
|
export declare class Queue<T> {
|
|
7
7
|
protected _nodes: T[];
|
|
8
8
|
protected _offset: number;
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
* @param {
|
|
10
|
+
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
11
|
+
* @param {T[]} [elements] - The `elements` parameter is an optional array of elements of type `T`. If provided, it
|
|
12
|
+
* will be used to initialize the `_nodes` property of the class. If not provided, the `_nodes` property will be
|
|
13
|
+
* initialized as an empty array.
|
|
12
14
|
*/
|
|
13
15
|
constructor(elements?: T[]);
|
|
14
16
|
/**
|
|
15
|
-
* Creates a queue from an existing array.
|
|
17
|
+
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
16
18
|
* @public
|
|
17
19
|
* @static
|
|
18
|
-
* @param {
|
|
19
|
-
* @
|
|
20
|
+
* @param {T[]} elements - The "elements" parameter is an array of elements of type T.
|
|
21
|
+
* @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
|
|
22
|
+
* array.
|
|
20
23
|
*/
|
|
21
24
|
static fromArray<T>(elements: T[]): Queue<T>;
|
|
22
25
|
/**
|
|
23
|
-
* Adds an element at the back of the queue.
|
|
24
|
-
* @
|
|
25
|
-
* @
|
|
26
|
+
* 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.
|
|
27
|
+
* @param {T} element - The `element` parameter represents the element that you want to add to the queue.
|
|
28
|
+
* @returns The `offer` method is returning a `Queue<T>` object.
|
|
26
29
|
*/
|
|
27
30
|
offer(element: T): Queue<T>;
|
|
28
31
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* @returns
|
|
32
|
+
* The `poll` function removes and returns the first element in the queue, and adjusts the internal data structure if
|
|
33
|
+
* necessary to optimize performance.
|
|
34
|
+
* @returns The function `poll()` returns either the first element in the queue or `null` if the queue is empty.
|
|
32
35
|
*/
|
|
33
36
|
poll(): T | null;
|
|
34
37
|
/**
|
|
35
|
-
*
|
|
36
|
-
* @
|
|
37
|
-
*
|
|
38
|
+
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
39
|
+
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
40
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
38
41
|
*/
|
|
39
42
|
peek(): T | null;
|
|
40
43
|
/**
|
|
41
|
-
*
|
|
42
|
-
* @
|
|
43
|
-
*
|
|
44
|
+
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
45
|
+
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
46
|
+
* array is empty, it returns `null`.
|
|
44
47
|
*/
|
|
45
48
|
peekLast(): T | null;
|
|
46
49
|
/**
|
|
47
|
-
*
|
|
48
|
-
* @
|
|
49
|
-
* @returns {number}
|
|
50
|
+
* The size function returns the number of elements in an array.
|
|
51
|
+
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
50
52
|
*/
|
|
51
53
|
size(): number;
|
|
52
54
|
/**
|
|
53
|
-
*
|
|
54
|
-
* @
|
|
55
|
-
* @returns {boolean}
|
|
55
|
+
* The function checks if a data structure is empty by comparing its size to zero.
|
|
56
|
+
* @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
|
|
56
57
|
*/
|
|
57
58
|
isEmpty(): boolean;
|
|
58
59
|
/**
|
|
59
|
-
*
|
|
60
|
-
* @
|
|
61
|
-
* @returns {array}
|
|
60
|
+
* The toArray() function returns an array of elements from the current offset to the end of the _nodes array.
|
|
61
|
+
* @returns An array of type T is being returned.
|
|
62
62
|
*/
|
|
63
63
|
toArray(): T[];
|
|
64
64
|
/**
|
|
65
|
-
*
|
|
66
|
-
* @public
|
|
65
|
+
* The clear function resets the nodes array and offset to their initial values.
|
|
67
66
|
*/
|
|
68
67
|
clear(): void;
|
|
69
68
|
/**
|
|
70
|
-
*
|
|
71
|
-
* @
|
|
72
|
-
* @return {Queue}
|
|
69
|
+
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
|
|
70
|
+
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
73
71
|
*/
|
|
74
72
|
clone(): Queue<T>;
|
|
75
73
|
}
|