min-heap-typed 1.50.1 → 1.50.3
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/dist/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/data-structures/base/iterable-base.js +143 -7
- package/dist/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/data-structures/binary-tree/avl-tree.js +101 -72
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/data-structures/binary-tree/bst.js +68 -76
- package/dist/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/data-structures/binary-tree/tree-multimap.js +102 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/data-structures/graph/abstract-graph.js +3 -189
- package/dist/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/data-structures/graph/directed-graph.js +131 -0
- package/dist/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/data-structures/graph/map-graph.js +14 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/data-structures/graph/undirected-graph.js +151 -18
- package/dist/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/data-structures/hash/hash-map.js +347 -78
- package/dist/data-structures/heap/heap.d.ts +95 -25
- package/dist/data-structures/heap/heap.js +95 -26
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/data-structures/linked-list/doubly-linked-list.js +141 -77
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/data-structures/linked-list/singly-linked-list.js +164 -115
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/data-structures/matrix/matrix.js +50 -11
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/data-structures/queue/deque.d.ts +139 -35
- package/dist/data-structures/queue/deque.js +200 -62
- package/dist/data-structures/queue/queue.d.ts +103 -49
- package/dist/data-structures/queue/queue.js +111 -49
- package/dist/data-structures/stack/stack.d.ts +51 -21
- package/dist/data-structures/stack/stack.js +58 -22
- package/dist/data-structures/tree/tree.d.ts +57 -3
- package/dist/data-structures/tree/tree.js +77 -11
- package/dist/data-structures/trie/trie.d.ts +135 -34
- package/dist/data-structures/trie/trie.js +153 -33
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
|
@@ -32,12 +32,20 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
32
32
|
|
|
33
33
|
protected _elements: E[] = [];
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* The elements function returns the elements of this set.
|
|
37
|
+
* @return An array of the elements in the stack
|
|
38
|
+
*/
|
|
35
39
|
get elements(): E[] {
|
|
36
40
|
return this._elements;
|
|
37
41
|
}
|
|
38
42
|
|
|
39
43
|
protected _offset: number = 0;
|
|
40
44
|
|
|
45
|
+
/**
|
|
46
|
+
* The offset function returns the offset of the current page.
|
|
47
|
+
* @return The value of the protected variable _offset
|
|
48
|
+
*/
|
|
41
49
|
get offset(): number {
|
|
42
50
|
return this._offset;
|
|
43
51
|
}
|
|
@@ -51,8 +59,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
51
59
|
}
|
|
52
60
|
|
|
53
61
|
/**
|
|
54
|
-
* Time Complexity: O(1)
|
|
55
|
-
* Space Complexity: O(1)
|
|
62
|
+
* Time Complexity: O(1)
|
|
63
|
+
* Space Complexity: O(1)
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Time Complexity: O(1)
|
|
68
|
+
* Space Complexity: O(1)
|
|
56
69
|
*
|
|
57
70
|
* The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
|
|
58
71
|
* @returns The `get first()` method returns the first element of the data structure, represented by the `_elements` array at
|
|
@@ -63,13 +76,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
63
76
|
}
|
|
64
77
|
|
|
65
78
|
/**
|
|
66
|
-
* Time Complexity: O(1)
|
|
67
|
-
* Space Complexity: O(1)
|
|
79
|
+
* Time Complexity: O(1)
|
|
80
|
+
* Space Complexity: O(1)
|
|
68
81
|
*/
|
|
69
82
|
|
|
70
83
|
/**
|
|
71
|
-
* Time Complexity: O(1)
|
|
72
|
-
* Space Complexity: O(1)
|
|
84
|
+
* Time Complexity: O(1)
|
|
85
|
+
* Space Complexity: O(1)
|
|
73
86
|
*
|
|
74
87
|
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
75
88
|
* @returns The method `get last()` returns the last element of the `_elements` array if the array is not empty. If the
|
|
@@ -80,11 +93,14 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
80
93
|
}
|
|
81
94
|
|
|
82
95
|
/**
|
|
83
|
-
* Time Complexity: O(n)
|
|
84
|
-
* Space Complexity: O(
|
|
96
|
+
* Time Complexity: O(n)
|
|
97
|
+
* Space Complexity: O(n)
|
|
85
98
|
*/
|
|
86
99
|
|
|
87
100
|
/**
|
|
101
|
+
* Time Complexity: O(n)
|
|
102
|
+
* Space Complexity: O(n)
|
|
103
|
+
*
|
|
88
104
|
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
89
105
|
* @public
|
|
90
106
|
* @static
|
|
@@ -97,13 +113,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
97
113
|
}
|
|
98
114
|
|
|
99
115
|
/**
|
|
100
|
-
* Time Complexity: O(1)
|
|
101
|
-
* Space Complexity: O(1)
|
|
116
|
+
* Time Complexity: O(1)
|
|
117
|
+
* Space Complexity: O(1)
|
|
102
118
|
*/
|
|
103
119
|
|
|
104
120
|
/**
|
|
105
|
-
* Time Complexity: O(1)
|
|
106
|
-
* Space Complexity: O(1)
|
|
121
|
+
* Time Complexity: O(1)
|
|
122
|
+
* Space Complexity: O(1)
|
|
107
123
|
*
|
|
108
124
|
* The push function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
|
|
109
125
|
* @param {E} element - The `element` parameter represents the element that you want to add to the queue.
|
|
@@ -115,13 +131,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
115
131
|
}
|
|
116
132
|
|
|
117
133
|
/**
|
|
118
|
-
* Time Complexity: O(1)
|
|
119
|
-
* Space Complexity: O(1)
|
|
134
|
+
* Time Complexity: O(1)
|
|
135
|
+
* Space Complexity: O(1)
|
|
120
136
|
*/
|
|
121
137
|
|
|
122
138
|
/**
|
|
123
|
-
* Time Complexity: O(
|
|
124
|
-
* Space Complexity: O(1)
|
|
139
|
+
* Time Complexity: O(1)
|
|
140
|
+
* Space Complexity: O(1)
|
|
125
141
|
*
|
|
126
142
|
* The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
|
|
127
143
|
* necessary to optimize performance.
|
|
@@ -143,13 +159,33 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
143
159
|
}
|
|
144
160
|
|
|
145
161
|
/**
|
|
146
|
-
*
|
|
147
|
-
*
|
|
162
|
+
* The delete function removes an element from the list.
|
|
163
|
+
* @param element: E Specify the element to be deleted
|
|
164
|
+
* @return A boolean value indicating whether the element was successfully deleted or not
|
|
165
|
+
*/
|
|
166
|
+
delete(element: E): boolean {
|
|
167
|
+
const index = this.elements.indexOf(element);
|
|
168
|
+
return this.deleteAt(index);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* The deleteAt function deletes the element at a given index.
|
|
173
|
+
* @param index: number Determine the index of the element to be deleted
|
|
174
|
+
* @return A boolean value
|
|
175
|
+
*/
|
|
176
|
+
deleteAt(index: number): boolean {
|
|
177
|
+
const spliced = this.elements.splice(index, 1);
|
|
178
|
+
return spliced.length === 1;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Time Complexity: O(1)
|
|
183
|
+
* Space Complexity: O(1)
|
|
148
184
|
*/
|
|
149
185
|
|
|
150
186
|
/**
|
|
151
|
-
* Time Complexity: O(1)
|
|
152
|
-
* Space Complexity: O(1)
|
|
187
|
+
* Time Complexity: O(1)
|
|
188
|
+
* Space Complexity: O(1)
|
|
153
189
|
*
|
|
154
190
|
* The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
|
|
155
191
|
* @returns The `peek()` method returns the first element of the data structure, represented by the `_elements` array at
|
|
@@ -160,13 +196,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
160
196
|
}
|
|
161
197
|
|
|
162
198
|
/**
|
|
163
|
-
* Time Complexity: O(1)
|
|
164
|
-
* Space Complexity: O(1)
|
|
199
|
+
* Time Complexity: O(1)
|
|
200
|
+
* Space Complexity: O(1)
|
|
165
201
|
*/
|
|
166
202
|
|
|
167
203
|
/**
|
|
168
|
-
* Time Complexity: O(1)
|
|
169
|
-
* Space Complexity: O(1)
|
|
204
|
+
* Time Complexity: O(1)
|
|
205
|
+
* Space Complexity: O(1)
|
|
170
206
|
*
|
|
171
207
|
* The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
172
208
|
* @returns The method `peekLast()` returns the last element of the `_elements` array if the array is not empty. If the
|
|
@@ -177,13 +213,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
177
213
|
}
|
|
178
214
|
|
|
179
215
|
/**
|
|
180
|
-
* Time Complexity: O(1)
|
|
181
|
-
* Space Complexity: O(1)
|
|
216
|
+
* Time Complexity: O(1)
|
|
217
|
+
* Space Complexity: O(1)
|
|
182
218
|
*/
|
|
183
219
|
|
|
184
220
|
/**
|
|
185
|
-
* Time Complexity: O(1)
|
|
186
|
-
* Space Complexity: O(1)
|
|
221
|
+
* Time Complexity: O(1)
|
|
222
|
+
* Space Complexity: O(1)
|
|
187
223
|
*
|
|
188
224
|
* The enqueue function adds a value to the end of a queue.
|
|
189
225
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -193,13 +229,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
193
229
|
}
|
|
194
230
|
|
|
195
231
|
/**
|
|
196
|
-
* Time Complexity: O(
|
|
197
|
-
* Space Complexity: O(1)
|
|
232
|
+
* Time Complexity: O(1)
|
|
233
|
+
* Space Complexity: O(1)
|
|
198
234
|
*/
|
|
199
235
|
|
|
200
236
|
/**
|
|
201
|
-
* Time Complexity: O(
|
|
202
|
-
* Space Complexity: O(1)
|
|
237
|
+
* Time Complexity: O(1)
|
|
238
|
+
* Space Complexity: O(1)
|
|
203
239
|
*
|
|
204
240
|
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
205
241
|
* @returns The method is returning a value of type E or undefined.
|
|
@@ -209,28 +245,28 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
209
245
|
}
|
|
210
246
|
|
|
211
247
|
/**
|
|
212
|
-
* Time Complexity: O(1)
|
|
213
|
-
* Space Complexity: O(1)
|
|
248
|
+
* Time Complexity: O(1)
|
|
249
|
+
* Space Complexity: O(1)
|
|
214
250
|
*/
|
|
215
251
|
|
|
216
252
|
/**
|
|
217
|
-
* Time Complexity: O(1)
|
|
218
|
-
* Space Complexity: O(1)
|
|
253
|
+
* Time Complexity: O(1)
|
|
254
|
+
* Space Complexity: O(1)
|
|
219
255
|
*
|
|
220
256
|
* @param index
|
|
221
257
|
*/
|
|
222
|
-
|
|
258
|
+
at(index: number): E | undefined {
|
|
223
259
|
return this.elements[index];
|
|
224
260
|
}
|
|
225
261
|
|
|
226
262
|
/**
|
|
227
|
-
* Time Complexity: O(1)
|
|
228
|
-
* Space Complexity: O(1)
|
|
263
|
+
* Time Complexity: O(1)
|
|
264
|
+
* Space Complexity: O(1)
|
|
229
265
|
*/
|
|
230
266
|
|
|
231
267
|
/**
|
|
232
|
-
* Time Complexity: O(1)
|
|
233
|
-
* Space Complexity: O(1)
|
|
268
|
+
* Time Complexity: O(1)
|
|
269
|
+
* Space Complexity: O(1)
|
|
234
270
|
*
|
|
235
271
|
* The function checks if a data structure is empty by comparing its size to zero.
|
|
236
272
|
* @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
|
|
@@ -240,13 +276,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
240
276
|
}
|
|
241
277
|
|
|
242
278
|
/**
|
|
243
|
-
* Time Complexity: O(1)
|
|
244
|
-
* Space Complexity: O(n)
|
|
279
|
+
* Time Complexity: O(1)
|
|
280
|
+
* Space Complexity: O(n)
|
|
245
281
|
*/
|
|
246
282
|
|
|
247
283
|
/**
|
|
248
|
-
* Time Complexity: O(1)
|
|
249
|
-
* Space Complexity: O(n)
|
|
284
|
+
* Time Complexity: O(1)
|
|
285
|
+
* Space Complexity: O(n)
|
|
250
286
|
*
|
|
251
287
|
* The toArray() function returns an array of elements from the current offset to the end of the _elements array.
|
|
252
288
|
* @returns An array of type E is being returned.
|
|
@@ -256,6 +292,14 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
256
292
|
}
|
|
257
293
|
|
|
258
294
|
/**
|
|
295
|
+
* Time Complexity: O(1)
|
|
296
|
+
* Space Complexity: O(1)
|
|
297
|
+
*/
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Time Complexity: O(1)
|
|
301
|
+
* Space Complexity: O(1)
|
|
302
|
+
*
|
|
259
303
|
* The clear function resets the elements array and offset to their initial values.
|
|
260
304
|
*/
|
|
261
305
|
clear(): void {
|
|
@@ -264,13 +308,14 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
264
308
|
}
|
|
265
309
|
|
|
266
310
|
/**
|
|
267
|
-
* Time Complexity: O(n)
|
|
268
|
-
* Space Complexity: O(n)
|
|
311
|
+
* Time Complexity: O(n)
|
|
312
|
+
* Space Complexity: O(n)
|
|
313
|
+
* where n is the number of elements in the queue. It creates a shallow copy of the internal array. the space required is proportional to the number of elements in the queue.
|
|
269
314
|
*/
|
|
270
315
|
|
|
271
316
|
/**
|
|
272
|
-
* Time Complexity: O(n)
|
|
273
|
-
* Space Complexity: O(n)
|
|
317
|
+
* Time Complexity: O(n)
|
|
318
|
+
* Space Complexity: O(n)
|
|
274
319
|
*
|
|
275
320
|
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
|
|
276
321
|
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
@@ -316,6 +361,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
316
361
|
* Time Complexity: O(n)
|
|
317
362
|
* Space Complexity: O(n)
|
|
318
363
|
*/
|
|
364
|
+
|
|
319
365
|
/**
|
|
320
366
|
* Time Complexity: O(n)
|
|
321
367
|
* Space Complexity: O(n)
|
|
@@ -345,6 +391,12 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
345
391
|
* Space Complexity: O(n)
|
|
346
392
|
*/
|
|
347
393
|
|
|
394
|
+
/**
|
|
395
|
+
* Time Complexity: O(n)
|
|
396
|
+
* Space Complexity: O(n)
|
|
397
|
+
*
|
|
398
|
+
* The function `_getIterator` returns an iterable iterator for the elements in the class.
|
|
399
|
+
*/
|
|
348
400
|
protected* _getIterator(): IterableIterator<E> {
|
|
349
401
|
for (const item of this.elements) {
|
|
350
402
|
yield item;
|
|
@@ -390,4 +442,21 @@ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
|
390
442
|
peek(): E | undefined {
|
|
391
443
|
return this.first;
|
|
392
444
|
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Time Complexity: O(n)
|
|
448
|
+
* Space Complexity: O(n)
|
|
449
|
+
*/
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Time Complexity: O(n)
|
|
453
|
+
* Space Complexity: O(n)
|
|
454
|
+
* The `clone` function returns a new instance of the `LinkedListQueue` class with the same values as
|
|
455
|
+
* the current instance.
|
|
456
|
+
* @returns The `clone()` method is returning a new instance of `LinkedListQueue` with the same
|
|
457
|
+
* values as the original `LinkedListQueue`.
|
|
458
|
+
*/
|
|
459
|
+
clone(): LinkedListQueue<E> {
|
|
460
|
+
return new LinkedListQueue<E>(this.values());
|
|
461
|
+
}
|
|
393
462
|
}
|
|
@@ -32,15 +32,14 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
32
32
|
|
|
33
33
|
protected _elements: E[] = [];
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* The elements function returns the elements of this set.
|
|
37
|
+
* @return An array of elements
|
|
38
|
+
*/
|
|
35
39
|
get elements(): E[] {
|
|
36
40
|
return this._elements;
|
|
37
41
|
}
|
|
38
42
|
|
|
39
|
-
/**
|
|
40
|
-
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
|
|
41
|
-
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
42
|
-
*/
|
|
43
|
-
|
|
44
43
|
/**
|
|
45
44
|
* The size() function returns the number of elements in an array.
|
|
46
45
|
* @returns The size of the elements array.
|
|
@@ -50,8 +49,13 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
50
49
|
}
|
|
51
50
|
|
|
52
51
|
/**
|
|
53
|
-
* Time Complexity: O(n)
|
|
54
|
-
* Space Complexity: O(n)
|
|
52
|
+
* Time Complexity: O(n)
|
|
53
|
+
* Space Complexity: O(n)
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Time Complexity: O(n)
|
|
58
|
+
* Space Complexity: O(n)
|
|
55
59
|
*
|
|
56
60
|
* The function "fromArray" creates a new Stack object from an array of elements.
|
|
57
61
|
* @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
|
|
@@ -71,13 +75,13 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
71
75
|
}
|
|
72
76
|
|
|
73
77
|
/**
|
|
74
|
-
* Time Complexity: O(1)
|
|
75
|
-
* Space Complexity: O(1)
|
|
78
|
+
* Time Complexity: O(1)
|
|
79
|
+
* Space Complexity: O(1)
|
|
76
80
|
*/
|
|
77
81
|
|
|
78
82
|
/**
|
|
79
|
-
* Time Complexity: O(1)
|
|
80
|
-
* Space Complexity: O(1)
|
|
83
|
+
* Time Complexity: O(1)
|
|
84
|
+
* Space Complexity: O(1)
|
|
81
85
|
*
|
|
82
86
|
* The `peek` function returns the last element of an array, or undefined if the array is empty.
|
|
83
87
|
* @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
|
|
@@ -89,13 +93,13 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
89
93
|
}
|
|
90
94
|
|
|
91
95
|
/**
|
|
92
|
-
* Time Complexity: O(1)
|
|
93
|
-
* Space Complexity: O(1)
|
|
96
|
+
* Time Complexity: O(1)
|
|
97
|
+
* Space Complexity: O(1)
|
|
94
98
|
*/
|
|
95
99
|
|
|
96
100
|
/**
|
|
97
|
-
* Time Complexity: O(1)
|
|
98
|
-
* Space Complexity: O(1)
|
|
101
|
+
* Time Complexity: O(1)
|
|
102
|
+
* Space Complexity: O(1)
|
|
99
103
|
*
|
|
100
104
|
* The push function adds an element to the stack and returns the updated stack.
|
|
101
105
|
* @param {E} element - The parameter "element" is of type E, which means it can be any data type.
|
|
@@ -107,13 +111,13 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
107
111
|
}
|
|
108
112
|
|
|
109
113
|
/**
|
|
110
|
-
* Time Complexity: O(1)
|
|
111
|
-
* Space Complexity: O(1)
|
|
114
|
+
* Time Complexity: O(1)
|
|
115
|
+
* Space Complexity: O(1)
|
|
112
116
|
*/
|
|
113
117
|
|
|
114
118
|
/**
|
|
115
|
-
* Time Complexity: O(1)
|
|
116
|
-
* Space Complexity: O(1)
|
|
119
|
+
* Time Complexity: O(1)
|
|
120
|
+
* Space Complexity: O(1)
|
|
117
121
|
*
|
|
118
122
|
* The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
|
|
119
123
|
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
@@ -125,6 +129,26 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
125
129
|
return this.elements.pop();
|
|
126
130
|
}
|
|
127
131
|
|
|
132
|
+
/**
|
|
133
|
+
* The delete function removes an element from the stack.
|
|
134
|
+
* @param element: E Specify the element to be deleted
|
|
135
|
+
* @return A boolean value indicating whether the element was successfully deleted or not
|
|
136
|
+
*/
|
|
137
|
+
delete(element: E): boolean {
|
|
138
|
+
const index = this.elements.indexOf(element);
|
|
139
|
+
return this.deleteAt(index);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* The deleteAt function deletes the element at a given index.
|
|
144
|
+
* @param index: number Determine the index of the element to be deleted
|
|
145
|
+
* @return A boolean value
|
|
146
|
+
*/
|
|
147
|
+
deleteAt(index: number): boolean {
|
|
148
|
+
const spliced = this.elements.splice(index, 1);
|
|
149
|
+
return spliced.length === 1;
|
|
150
|
+
}
|
|
151
|
+
|
|
128
152
|
/**
|
|
129
153
|
* Time Complexity: O(n)
|
|
130
154
|
* Space Complexity: O(n)
|
|
@@ -142,6 +166,14 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
142
166
|
}
|
|
143
167
|
|
|
144
168
|
/**
|
|
169
|
+
* Time Complexity: O(1)
|
|
170
|
+
* Space Complexity: O(1)
|
|
171
|
+
*/
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Time Complexity: O(1)
|
|
175
|
+
* Space Complexity: O(1)
|
|
176
|
+
*
|
|
145
177
|
* The clear function clears the elements array.
|
|
146
178
|
*/
|
|
147
179
|
clear(): void {
|
|
@@ -149,13 +181,13 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
149
181
|
}
|
|
150
182
|
|
|
151
183
|
/**
|
|
152
|
-
* Time Complexity: O(n)
|
|
153
|
-
* Space Complexity: O(n)
|
|
184
|
+
* Time Complexity: O(n)
|
|
185
|
+
* Space Complexity: O(n)
|
|
154
186
|
*/
|
|
155
187
|
|
|
156
188
|
/**
|
|
157
|
-
* Time Complexity: O(n)
|
|
158
|
-
* Space Complexity: O(n)
|
|
189
|
+
* Time Complexity: O(n)
|
|
190
|
+
* Space Complexity: O(n)
|
|
159
191
|
*
|
|
160
192
|
* The `clone()` function returns a new `Stack` object with the same elements as the original stack.
|
|
161
193
|
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
@@ -226,6 +258,14 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
226
258
|
}
|
|
227
259
|
|
|
228
260
|
/**
|
|
261
|
+
* Time Complexity: O(n)
|
|
262
|
+
* Space Complexity: O(n)
|
|
263
|
+
*/
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Time Complexity: O(n)
|
|
267
|
+
* Space Complexity: O(n)
|
|
268
|
+
*
|
|
229
269
|
* Custom iterator for the Stack class.
|
|
230
270
|
* @returns An iterator object.
|
|
231
271
|
*/
|
|
@@ -1,25 +1,99 @@
|
|
|
1
1
|
export class TreeNode<V = any> {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
/**
|
|
3
|
+
* The constructor function initializes a TreeNode object with a key, optional value, and optional
|
|
4
|
+
* children.
|
|
5
|
+
* @param {string} key - A string representing the key of the tree node.
|
|
6
|
+
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
7
|
+
* value associated with the node. If no value is provided, it defaults to `undefined`.
|
|
8
|
+
* @param {TreeNode<V>[]} [children] - The `children` parameter is an optional array of `TreeNode<V>`
|
|
9
|
+
* objects. It represents the child nodes of the current node. If no children are provided, the
|
|
10
|
+
* default value is an empty array.
|
|
11
|
+
*/
|
|
6
12
|
constructor(key: string, value?: V, children?: TreeNode<V>[]) {
|
|
7
|
-
this.
|
|
8
|
-
this.
|
|
9
|
-
this.
|
|
13
|
+
this._key = key;
|
|
14
|
+
this._value = value || undefined;
|
|
15
|
+
this._children = children || [];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
protected _key: string;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The function returns the value of the protected variable _key.
|
|
22
|
+
* @returns The value of the `_key` property, which is a string.
|
|
23
|
+
*/
|
|
24
|
+
get key(): string {
|
|
25
|
+
return this._key;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The above function sets the value of a protected variable called "key".
|
|
30
|
+
* @param {string} value - The value parameter is a string that represents the value to be assigned
|
|
31
|
+
* to the key.
|
|
32
|
+
*/
|
|
33
|
+
set key(value: string) {
|
|
34
|
+
this._key = value;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
protected _value?: V | undefined;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The function returns the value stored in a variable, or undefined if the variable is empty.
|
|
41
|
+
* @returns The value of the variable `_value` is being returned.
|
|
42
|
+
*/
|
|
43
|
+
get value(): V | undefined {
|
|
44
|
+
return this._value;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The function sets the value of a variable.
|
|
49
|
+
* @param {V | undefined} value - The parameter "value" is of type "V | undefined", which means it
|
|
50
|
+
* can accept a value of type "V" or it can be undefined.
|
|
51
|
+
*/
|
|
52
|
+
set value(value: V | undefined) {
|
|
53
|
+
this._value = value;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
protected _children?: TreeNode<V>[] | undefined;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The function returns an array of TreeNode objects or undefined.
|
|
60
|
+
* @returns The `children` property is being returned. It is of type `TreeNode<V>[] | undefined`,
|
|
61
|
+
* which means it can either be an array of `TreeNode<V>` objects or `undefined`.
|
|
62
|
+
*/
|
|
63
|
+
get children(): TreeNode<V>[] | undefined {
|
|
64
|
+
return this._children;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The function sets the value of the children property of a TreeNode object.
|
|
69
|
+
* @param {TreeNode<V>[] | undefined} value - The value parameter is of type TreeNode<V>[] |
|
|
70
|
+
* undefined. This means that it can accept an array of TreeNode objects or undefined.
|
|
71
|
+
*/
|
|
72
|
+
set children(value: TreeNode<V>[] | undefined) {
|
|
73
|
+
this._children = value;
|
|
10
74
|
}
|
|
11
75
|
|
|
76
|
+
/**
|
|
77
|
+
* The function `addChildren` adds one or more child nodes to the current node.
|
|
78
|
+
* @param {TreeNode<V> | TreeNode<V>[]} children - The `children` parameter can be either a single
|
|
79
|
+
* `TreeNode<V>` object or an array of `TreeNode<V>` objects.
|
|
80
|
+
*/
|
|
12
81
|
addChildren(children: TreeNode<V> | TreeNode<V>[]) {
|
|
13
|
-
if (!this.
|
|
14
|
-
this.
|
|
82
|
+
if (!this._children) {
|
|
83
|
+
this._children = [];
|
|
15
84
|
}
|
|
16
85
|
if (children instanceof TreeNode) {
|
|
17
|
-
this.
|
|
86
|
+
this._children.push(children);
|
|
18
87
|
} else {
|
|
19
|
-
this.
|
|
88
|
+
this._children = this._children.concat(children);
|
|
20
89
|
}
|
|
21
90
|
}
|
|
22
91
|
|
|
92
|
+
/**
|
|
93
|
+
* The function `getHeight()` calculates the maximum depth of a tree structure by performing a
|
|
94
|
+
* breadth-first search.
|
|
95
|
+
* @returns the maximum depth or height of the tree.
|
|
96
|
+
*/
|
|
23
97
|
getHeight() {
|
|
24
98
|
let maxDepth = 0;
|
|
25
99
|
if (this) {
|
|
@@ -27,10 +101,10 @@ export class TreeNode<V = any> {
|
|
|
27
101
|
if (level > maxDepth) {
|
|
28
102
|
maxDepth = level;
|
|
29
103
|
}
|
|
30
|
-
const {
|
|
31
|
-
if (
|
|
32
|
-
for (let i = 0, len =
|
|
33
|
-
bfs(
|
|
104
|
+
const { _children } = node;
|
|
105
|
+
if (_children) {
|
|
106
|
+
for (let i = 0, len = _children.length; i < len; i++) {
|
|
107
|
+
bfs(_children[i], level + 1);
|
|
34
108
|
}
|
|
35
109
|
}
|
|
36
110
|
};
|