avl-tree-typed 1.49.1 → 1.49.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 +11 -0
- package/dist/data-structures/base/iterable-base.js +21 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +7 -7
- package/dist/data-structures/graph/abstract-graph.js +43 -12
- package/dist/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/data-structures/graph/directed-graph.js +2 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +1 -1
- package/dist/data-structures/graph/undirected-graph.js +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +9 -9
- package/dist/data-structures/hash/hash-map.js +16 -15
- package/dist/data-structures/heap/heap.d.ts +6 -35
- package/dist/data-structures/heap/heap.js +10 -42
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
- package/dist/data-structures/linked-list/doubly-linked-list.js +143 -146
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +16 -21
- package/dist/data-structures/linked-list/singly-linked-list.js +42 -42
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +25 -25
- package/dist/data-structures/linked-list/skip-linked-list.js +36 -36
- package/dist/data-structures/queue/deque.d.ts +70 -75
- package/dist/data-structures/queue/deque.js +100 -110
- package/dist/data-structures/queue/queue.d.ts +37 -38
- package/dist/data-structures/queue/queue.js +46 -49
- package/dist/data-structures/stack/stack.d.ts +2 -3
- package/dist/data-structures/stack/stack.js +2 -5
- package/dist/data-structures/trie/trie.d.ts +1 -2
- package/dist/data-structures/trie/trie.js +2 -5
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +24 -0
- package/src/data-structures/graph/abstract-graph.ts +55 -14
- package/src/data-structures/graph/directed-graph.ts +3 -2
- package/src/data-structures/graph/undirected-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +27 -28
- package/src/data-structures/heap/heap.ts +19 -57
- package/src/data-structures/linked-list/doubly-linked-list.ts +157 -161
- package/src/data-structures/linked-list/singly-linked-list.ts +49 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +40 -40
- package/src/data-structures/queue/deque.ts +122 -135
- package/src/data-structures/queue/queue.ts +54 -58
- package/src/data-structures/stack/stack.ts +4 -8
- package/src/data-structures/trie/trie.ts +5 -9
|
@@ -83,106 +83,6 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
83
83
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
/**
|
|
87
|
-
* Time Complexity: O(1) - Removes the last element.
|
|
88
|
-
* Space Complexity: O(1) - Operates in-place.
|
|
89
|
-
*/
|
|
90
|
-
|
|
91
|
-
isEmpty() {
|
|
92
|
-
return this.size === 0;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
97
|
-
* Space Complexity: O(n) - Due to potential resizing.
|
|
98
|
-
*/
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Time Complexity: O(1)
|
|
102
|
-
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
|
|
103
|
-
*
|
|
104
|
-
* The addLast function adds an element to the end of an array.
|
|
105
|
-
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
106
|
-
* data structure.
|
|
107
|
-
*/
|
|
108
|
-
addLast(element: E): void {
|
|
109
|
-
this.push(element);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Time Complexity: O(1) - Removes the first element.
|
|
114
|
-
* Space Complexity: O(1) - In-place operation.
|
|
115
|
-
*/
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Time Complexity: O(1) - Removes the last element.
|
|
119
|
-
* Space Complexity: O(1) - Operates in-place.
|
|
120
|
-
*
|
|
121
|
-
* The function "pollLast" removes and returns the last element of an array.
|
|
122
|
-
* @returns The last element of the array is being returned.
|
|
123
|
-
*/
|
|
124
|
-
pollLast(): E | undefined {
|
|
125
|
-
return this.pop();
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Time Complexity: O(1).
|
|
130
|
-
* Space Complexity: O(n) - Due to potential resizing.
|
|
131
|
-
*
|
|
132
|
-
* The "addFirst" function adds an element to the beginning of an array.
|
|
133
|
-
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
134
|
-
* beginning of the data structure.
|
|
135
|
-
*/
|
|
136
|
-
addFirst(element: E): void {
|
|
137
|
-
this.unshift(element);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Time Complexity: O(1) - Removes the first element.
|
|
142
|
-
* Space Complexity: O(1) - In-place operation.
|
|
143
|
-
*
|
|
144
|
-
* The function "pollFirst" removes and returns the first element of an array.
|
|
145
|
-
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
146
|
-
* from the beginning. If the array is empty, it will return `undefined`.
|
|
147
|
-
*/
|
|
148
|
-
pollFirst(): E | undefined {
|
|
149
|
-
return this.shift();
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* The clear() function resets the state of the object by initializing all variables to their default
|
|
154
|
-
* values.
|
|
155
|
-
*/
|
|
156
|
-
clear() {
|
|
157
|
-
this._buckets = [new Array(this._bucketSize)];
|
|
158
|
-
this._bucketCount = 1;
|
|
159
|
-
this._bucketFirst = this._bucketLast = this._size = 0;
|
|
160
|
-
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* The below function is a generator that yields elements from a collection one by one.
|
|
165
|
-
*/
|
|
166
|
-
* begin(): Generator<E> {
|
|
167
|
-
let index = 0;
|
|
168
|
-
while (index < this.size) {
|
|
169
|
-
yield this.getAt(index);
|
|
170
|
-
index++;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
176
|
-
* the last element.
|
|
177
|
-
*/
|
|
178
|
-
* reverseBegin(): Generator<E> {
|
|
179
|
-
let index = this.size - 1;
|
|
180
|
-
while (index >= 0) {
|
|
181
|
-
yield this.getAt(index);
|
|
182
|
-
index--;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
86
|
/**
|
|
187
87
|
* Time Complexity - Amortized O(1) (possible reallocation)
|
|
188
88
|
* Space Complexity - O(n) (due to potential resizing).
|
|
@@ -197,7 +97,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
197
97
|
* structure.
|
|
198
98
|
* @returns The size of the data structure after the element has been pushed.
|
|
199
99
|
*/
|
|
200
|
-
push(element: E) {
|
|
100
|
+
push(element: E): boolean {
|
|
201
101
|
if (this.size) {
|
|
202
102
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
203
103
|
this._lastInBucket += 1;
|
|
@@ -215,7 +115,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
215
115
|
}
|
|
216
116
|
this._size += 1;
|
|
217
117
|
this._buckets[this._bucketLast][this._lastInBucket] = element;
|
|
218
|
-
return
|
|
118
|
+
return true;
|
|
219
119
|
}
|
|
220
120
|
|
|
221
121
|
/**
|
|
@@ -231,7 +131,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
231
131
|
* internal state variables accordingly.
|
|
232
132
|
* @returns The element that was removed from the data structure is being returned.
|
|
233
133
|
*/
|
|
234
|
-
pop() {
|
|
134
|
+
pop(): E | undefined {
|
|
235
135
|
if (this.size === 0) return;
|
|
236
136
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
237
137
|
if (this.size !== 1) {
|
|
@@ -264,7 +164,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
264
164
|
* beginning of the data structure.
|
|
265
165
|
* @returns The size of the data structure after the element has been added.
|
|
266
166
|
*/
|
|
267
|
-
unshift(element: E) {
|
|
167
|
+
unshift(element: E): boolean {
|
|
268
168
|
if (this.size) {
|
|
269
169
|
if (this._firstInBucket > 0) {
|
|
270
170
|
this._firstInBucket -= 1;
|
|
@@ -282,10 +182,9 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
282
182
|
}
|
|
283
183
|
this._size += 1;
|
|
284
184
|
this._buckets[this._bucketFirst][this._firstInBucket] = element;
|
|
285
|
-
return
|
|
185
|
+
return true;
|
|
286
186
|
}
|
|
287
187
|
|
|
288
|
-
|
|
289
188
|
/**
|
|
290
189
|
* Time Complexity: O(1)
|
|
291
190
|
* Space Complexity: O(1)
|
|
@@ -300,7 +199,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
300
199
|
* @returns The element that is being removed from the beginning of the data structure is being
|
|
301
200
|
* returned.
|
|
302
201
|
*/
|
|
303
|
-
shift() {
|
|
202
|
+
shift(): E | undefined {
|
|
304
203
|
if (this.size === 0) return;
|
|
305
204
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
306
205
|
if (this.size !== 1) {
|
|
@@ -318,6 +217,49 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
318
217
|
return element;
|
|
319
218
|
}
|
|
320
219
|
|
|
220
|
+
/**
|
|
221
|
+
* Time Complexity: O(1) - Removes the last element.
|
|
222
|
+
* Space Complexity: O(1) - Operates in-place.
|
|
223
|
+
*/
|
|
224
|
+
|
|
225
|
+
isEmpty(): boolean {
|
|
226
|
+
return this.size === 0;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* The clear() function resets the state of the object by initializing all variables to their default
|
|
231
|
+
* values.
|
|
232
|
+
*/
|
|
233
|
+
clear(): void {
|
|
234
|
+
this._buckets = [new Array(this._bucketSize)];
|
|
235
|
+
this._bucketCount = 1;
|
|
236
|
+
this._bucketFirst = this._bucketLast = this._size = 0;
|
|
237
|
+
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* The below function is a generator that yields elements from a collection one by one.
|
|
242
|
+
*/
|
|
243
|
+
* begin(): Generator<E> {
|
|
244
|
+
let index = 0;
|
|
245
|
+
while (index < this.size) {
|
|
246
|
+
yield this.getAt(index);
|
|
247
|
+
index++;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
253
|
+
* the last element.
|
|
254
|
+
*/
|
|
255
|
+
* reverseBegin(): Generator<E> {
|
|
256
|
+
let index = this.size - 1;
|
|
257
|
+
while (index >= 0) {
|
|
258
|
+
yield this.getAt(index);
|
|
259
|
+
index--;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
321
263
|
|
|
322
264
|
/**
|
|
323
265
|
* Time Complexity: O(1)
|
|
@@ -359,13 +301,14 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
359
301
|
* @param {E} element - The `element` parameter is the value that you want to set at the specified
|
|
360
302
|
* position in the data structure.
|
|
361
303
|
*/
|
|
362
|
-
setAt(pos: number, element: E) {
|
|
304
|
+
setAt(pos: number, element: E): boolean {
|
|
363
305
|
rangeCheck(pos, 0, this.size - 1);
|
|
364
306
|
const {
|
|
365
307
|
bucketIndex,
|
|
366
308
|
indexInBucket
|
|
367
309
|
} = this._getBucketAndPosition(pos);
|
|
368
310
|
this._buckets[bucketIndex][indexInBucket] = element;
|
|
311
|
+
return true;
|
|
369
312
|
}
|
|
370
313
|
|
|
371
314
|
/**
|
|
@@ -377,7 +320,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
377
320
|
* Time Complexity: O(n)
|
|
378
321
|
* Space Complexity: O(n)
|
|
379
322
|
*
|
|
380
|
-
* The `
|
|
323
|
+
* The `addAt` function inserts one or more elements at a specified position in an array-like data
|
|
381
324
|
* structure.
|
|
382
325
|
* @param {number} pos - The `pos` parameter represents the position at which the element(s) should
|
|
383
326
|
* be inserted. It is of type `number`.
|
|
@@ -388,7 +331,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
388
331
|
* will be inserted once. However, you can provide a different value for `num` if you want
|
|
389
332
|
* @returns The size of the array after the insertion is being returned.
|
|
390
333
|
*/
|
|
391
|
-
|
|
334
|
+
addAt(pos: number, element: E, num = 1): boolean {
|
|
392
335
|
const length = this.size;
|
|
393
336
|
rangeCheck(pos, 0, length);
|
|
394
337
|
if (pos === 0) {
|
|
@@ -404,7 +347,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
404
347
|
for (let i = 0; i < num; ++i) this.push(element);
|
|
405
348
|
for (let i = 0; i < arr.length; ++i) this.push(arr[i]);
|
|
406
349
|
}
|
|
407
|
-
return
|
|
350
|
+
return true;
|
|
408
351
|
}
|
|
409
352
|
|
|
410
353
|
/**
|
|
@@ -422,7 +365,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
422
365
|
* cut. It is a number that indicates the index of the character where the cut should be made.
|
|
423
366
|
* @returns The method is returning the updated size of the data structure.
|
|
424
367
|
*/
|
|
425
|
-
cut(pos: number) {
|
|
368
|
+
cut(pos: number): number {
|
|
426
369
|
if (pos < 0) {
|
|
427
370
|
this.clear();
|
|
428
371
|
return 0;
|
|
@@ -453,7 +396,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
453
396
|
* the index of the element to be deleted.
|
|
454
397
|
* @returns The size of the data structure after the deletion operation is performed.
|
|
455
398
|
*/
|
|
456
|
-
deleteAt(pos: number) {
|
|
399
|
+
deleteAt(pos: number): boolean {
|
|
457
400
|
rangeCheck(pos, 0, this.size - 1);
|
|
458
401
|
if (pos === 0) this.shift();
|
|
459
402
|
else if (pos === this.size - 1) this.pop();
|
|
@@ -474,7 +417,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
474
417
|
}
|
|
475
418
|
this.pop();
|
|
476
419
|
}
|
|
477
|
-
return
|
|
420
|
+
return true;
|
|
478
421
|
}
|
|
479
422
|
|
|
480
423
|
/**
|
|
@@ -492,9 +435,9 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
492
435
|
* the data structure.
|
|
493
436
|
* @returns The size of the data structure after the element has been deleted.
|
|
494
437
|
*/
|
|
495
|
-
delete(element: E) {
|
|
438
|
+
delete(element: E): boolean {
|
|
496
439
|
const size = this.size;
|
|
497
|
-
if (size === 0) return
|
|
440
|
+
if (size === 0) return false;
|
|
498
441
|
let i = 0;
|
|
499
442
|
let index = 0;
|
|
500
443
|
while (i < size) {
|
|
@@ -506,7 +449,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
506
449
|
i += 1;
|
|
507
450
|
}
|
|
508
451
|
this.cut(index - 1);
|
|
509
|
-
return
|
|
452
|
+
return true;
|
|
510
453
|
}
|
|
511
454
|
|
|
512
455
|
/**
|
|
@@ -523,7 +466,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
523
466
|
* @returns The reverse() method is returning the object itself (this) after performing the reverse
|
|
524
467
|
* operation on the buckets and updating the relevant properties.
|
|
525
468
|
*/
|
|
526
|
-
reverse() {
|
|
469
|
+
reverse(): this {
|
|
527
470
|
this._buckets.reverse().forEach(function (bucket) {
|
|
528
471
|
bucket.reverse();
|
|
529
472
|
});
|
|
@@ -548,9 +491,9 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
548
491
|
* the number of unique elements.
|
|
549
492
|
* @returns The size of the modified array is being returned.
|
|
550
493
|
*/
|
|
551
|
-
unique() {
|
|
494
|
+
unique(): this {
|
|
552
495
|
if (this.size <= 1) {
|
|
553
|
-
return this
|
|
496
|
+
return this;
|
|
554
497
|
}
|
|
555
498
|
let index = 1;
|
|
556
499
|
let prev = this.getAt(0);
|
|
@@ -562,7 +505,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
562
505
|
}
|
|
563
506
|
}
|
|
564
507
|
this.cut(index - 1);
|
|
565
|
-
return this
|
|
508
|
+
return this;
|
|
566
509
|
}
|
|
567
510
|
|
|
568
511
|
/**
|
|
@@ -578,9 +521,9 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
578
521
|
* @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
|
|
579
522
|
* `y` of type `E` and returns a number. The comparator function is used to determine the order of
|
|
580
523
|
* the elements in the sorted array.
|
|
581
|
-
* @returns
|
|
524
|
+
* @returns Deque<E>
|
|
582
525
|
*/
|
|
583
|
-
sort(comparator?: (x: E, y: E) => number) {
|
|
526
|
+
sort(comparator?: (x: E, y: E) => number): this {
|
|
584
527
|
const arr: E[] = [];
|
|
585
528
|
for (let i = 0; i < this.size; ++i) {
|
|
586
529
|
arr.push(this.getAt(i));
|
|
@@ -606,7 +549,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
606
549
|
* @returns Nothing is being returned. The function is using the `return` statement to exit early if
|
|
607
550
|
* `this.size` is 0, but it does not return any value.
|
|
608
551
|
*/
|
|
609
|
-
shrinkToFit() {
|
|
552
|
+
shrinkToFit(): void {
|
|
610
553
|
if (this.size === 0) return;
|
|
611
554
|
const newBuckets = [];
|
|
612
555
|
if (this._bucketFirst === this._bucketLast) return;
|
|
@@ -650,7 +593,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
650
593
|
return element;
|
|
651
594
|
}
|
|
652
595
|
}
|
|
653
|
-
return
|
|
596
|
+
return;
|
|
654
597
|
}
|
|
655
598
|
|
|
656
599
|
/**
|
|
@@ -691,11 +634,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
691
634
|
* @returns The `toArray()` method is returning an array of elements of type `E`.
|
|
692
635
|
*/
|
|
693
636
|
toArray(): E[] {
|
|
694
|
-
|
|
695
|
-
for (let i = 0; i < this.size; ++i) {
|
|
696
|
-
arr.push(this.getAt(i));
|
|
697
|
-
}
|
|
698
|
-
return arr;
|
|
637
|
+
return [...this];
|
|
699
638
|
}
|
|
700
639
|
|
|
701
640
|
/**
|
|
@@ -758,12 +697,60 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
758
697
|
}
|
|
759
698
|
|
|
760
699
|
/**
|
|
761
|
-
* Time Complexity: O(n)
|
|
762
|
-
* Space Complexity: O(n)
|
|
700
|
+
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
701
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
763
702
|
*/
|
|
764
703
|
|
|
765
|
-
|
|
766
|
-
|
|
704
|
+
/**
|
|
705
|
+
* Time Complexity: O(1)
|
|
706
|
+
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
|
|
707
|
+
*
|
|
708
|
+
* The addLast function adds an element to the end of an array.
|
|
709
|
+
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
710
|
+
* data structure.
|
|
711
|
+
*/
|
|
712
|
+
addLast(element: E): boolean {
|
|
713
|
+
return this.push(element);
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
/**
|
|
717
|
+
* Time Complexity: O(1) - Removes the first element.
|
|
718
|
+
* Space Complexity: O(1) - In-place operation.
|
|
719
|
+
*/
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* Time Complexity: O(1) - Removes the last element.
|
|
723
|
+
* Space Complexity: O(1) - Operates in-place.
|
|
724
|
+
*
|
|
725
|
+
* The function "pollLast" removes and returns the last element of an array.
|
|
726
|
+
* @returns The last element of the array is being returned.
|
|
727
|
+
*/
|
|
728
|
+
pollLast(): E | undefined {
|
|
729
|
+
return this.pop();
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
/**
|
|
733
|
+
* Time Complexity: O(1).
|
|
734
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
735
|
+
*
|
|
736
|
+
* The "addFirst" function adds an element to the beginning of an array.
|
|
737
|
+
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
738
|
+
* beginning of the data structure.
|
|
739
|
+
*/
|
|
740
|
+
addFirst(element: E): boolean {
|
|
741
|
+
return this.unshift(element);
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* Time Complexity: O(1) - Removes the first element.
|
|
746
|
+
* Space Complexity: O(1) - In-place operation.
|
|
747
|
+
*
|
|
748
|
+
* The function "pollFirst" removes and returns the first element of an array.
|
|
749
|
+
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
750
|
+
* from the beginning. If the array is empty, it will return `undefined`.
|
|
751
|
+
*/
|
|
752
|
+
pollFirst(): E | undefined {
|
|
753
|
+
return this.shift();
|
|
767
754
|
}
|
|
768
755
|
|
|
769
756
|
/**
|
|
@@ -773,7 +760,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
773
760
|
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
774
761
|
* object to be iterated over using a for...of loop.
|
|
775
762
|
*/
|
|
776
|
-
protected* _getIterator() {
|
|
763
|
+
protected* _getIterator(): IterableIterator<E> {
|
|
777
764
|
for (let i = 0; i < this.size; ++i) {
|
|
778
765
|
yield this.getAt(i);
|
|
779
766
|
}
|
|
@@ -49,6 +49,40 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
49
49
|
return this.nodes.length - this.offset;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
54
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
55
|
+
*
|
|
56
|
+
* The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
57
|
+
* @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
58
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
59
|
+
*/
|
|
60
|
+
get first(): E | undefined {
|
|
61
|
+
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Time Complexity: O(1) - constant time as it adds an element to the end of the array.
|
|
66
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
71
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
72
|
+
*
|
|
73
|
+
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
74
|
+
* @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
75
|
+
* array is empty, it returns `undefined`.
|
|
76
|
+
*/
|
|
77
|
+
get last(): E | undefined {
|
|
78
|
+
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
|
|
83
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
84
|
+
*/
|
|
85
|
+
|
|
52
86
|
/**
|
|
53
87
|
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
54
88
|
* @public
|
|
@@ -62,7 +96,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
62
96
|
}
|
|
63
97
|
|
|
64
98
|
/**
|
|
65
|
-
* Time Complexity: O(1) - constant time as it
|
|
99
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
66
100
|
* Space Complexity: O(1) - no additional space is used.
|
|
67
101
|
*/
|
|
68
102
|
|
|
@@ -74,13 +108,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
74
108
|
* @param {E} element - The `element` parameter represents the element that you want to add to the queue.
|
|
75
109
|
* @returns The `add` method is returning a `Queue<E>` object.
|
|
76
110
|
*/
|
|
77
|
-
push(element: E):
|
|
111
|
+
push(element: E): boolean {
|
|
78
112
|
this.nodes.push(element);
|
|
79
|
-
return
|
|
113
|
+
return true;
|
|
80
114
|
}
|
|
81
115
|
|
|
82
116
|
/**
|
|
83
|
-
* Time Complexity: O(
|
|
117
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
84
118
|
* Space Complexity: O(1) - no additional space is used.
|
|
85
119
|
*/
|
|
86
120
|
|
|
@@ -95,7 +129,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
95
129
|
shift(): E | undefined {
|
|
96
130
|
if (this.size === 0) return undefined;
|
|
97
131
|
|
|
98
|
-
const first = this.
|
|
132
|
+
const first = this.first;
|
|
99
133
|
this._offset += 1;
|
|
100
134
|
|
|
101
135
|
if (this.offset * 2 < this.nodes.length) return first;
|
|
@@ -107,23 +141,6 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
107
141
|
return first;
|
|
108
142
|
}
|
|
109
143
|
|
|
110
|
-
/**
|
|
111
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
112
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
113
|
-
*/
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
117
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
118
|
-
*
|
|
119
|
-
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
120
|
-
* @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
121
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
122
|
-
*/
|
|
123
|
-
getFirst(): E | undefined {
|
|
124
|
-
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
144
|
/**
|
|
128
145
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
129
146
|
* Space Complexity: O(1) - no additional space is used.
|
|
@@ -138,24 +155,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
138
155
|
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
139
156
|
*/
|
|
140
157
|
peek(): E | undefined {
|
|
141
|
-
return this.
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
146
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
147
|
-
*/
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
151
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
152
|
-
*
|
|
153
|
-
* The `getLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
154
|
-
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
155
|
-
* array is empty, it returns `undefined`.
|
|
156
|
-
*/
|
|
157
|
-
getLast(): E | undefined {
|
|
158
|
-
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
158
|
+
return this.first;
|
|
159
159
|
}
|
|
160
160
|
|
|
161
161
|
/**
|
|
@@ -172,7 +172,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
172
172
|
* array is empty, it returns `undefined`.
|
|
173
173
|
*/
|
|
174
174
|
peekLast(): E | undefined {
|
|
175
|
-
return this.
|
|
175
|
+
return this.last;
|
|
176
176
|
}
|
|
177
177
|
|
|
178
178
|
/**
|
|
@@ -187,7 +187,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
187
187
|
* The enqueue function adds a value to the end of a queue.
|
|
188
188
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
189
189
|
*/
|
|
190
|
-
enqueue(value: E) {
|
|
190
|
+
enqueue(value: E): boolean {
|
|
191
191
|
return this.push(value);
|
|
192
192
|
}
|
|
193
193
|
|
|
@@ -278,10 +278,6 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
278
278
|
return new Queue(this.nodes.slice(this.offset));
|
|
279
279
|
}
|
|
280
280
|
|
|
281
|
-
print(): void {
|
|
282
|
-
console.log([...this]);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
281
|
/**
|
|
286
282
|
* Time Complexity: O(n)
|
|
287
283
|
* Space Complexity: O(n)
|
|
@@ -348,7 +344,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
348
344
|
* Space Complexity: O(n)
|
|
349
345
|
*/
|
|
350
346
|
|
|
351
|
-
protected* _getIterator() {
|
|
347
|
+
protected* _getIterator(): IterableIterator<E> {
|
|
352
348
|
for (const item of this.nodes) {
|
|
353
349
|
yield item;
|
|
354
350
|
}
|
|
@@ -362,12 +358,20 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
362
358
|
* 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
|
|
363
359
|
*/
|
|
364
360
|
export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
361
|
+
/**
|
|
362
|
+
* The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
363
|
+
* @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
364
|
+
*/
|
|
365
|
+
get first(): E | undefined {
|
|
366
|
+
return this.head?.value;
|
|
367
|
+
}
|
|
368
|
+
|
|
365
369
|
/**
|
|
366
370
|
* The enqueue function adds a value to the end of an array.
|
|
367
371
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
368
372
|
*/
|
|
369
|
-
enqueue(value: E) {
|
|
370
|
-
this.push(value);
|
|
373
|
+
enqueue(value: E): boolean {
|
|
374
|
+
return this.push(value);
|
|
371
375
|
}
|
|
372
376
|
|
|
373
377
|
/**
|
|
@@ -378,19 +382,11 @@ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
|
378
382
|
return this.shift();
|
|
379
383
|
}
|
|
380
384
|
|
|
381
|
-
/**
|
|
382
|
-
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
383
|
-
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
384
|
-
*/
|
|
385
|
-
getFirst(): E | undefined {
|
|
386
|
-
return this.head?.value;
|
|
387
|
-
}
|
|
388
|
-
|
|
389
385
|
/**
|
|
390
386
|
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
391
387
|
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
392
388
|
*/
|
|
393
389
|
peek(): E | undefined {
|
|
394
|
-
return this.
|
|
390
|
+
return this.first;
|
|
395
391
|
}
|
|
396
392
|
}
|
|
@@ -104,9 +104,9 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
104
104
|
* @param {E} element - The parameter "element" is of type E, which means it can be any data type.
|
|
105
105
|
* @returns The `push` method is returning the updated `Stack<E>` object.
|
|
106
106
|
*/
|
|
107
|
-
push(element: E):
|
|
107
|
+
push(element: E): boolean {
|
|
108
108
|
this.elements.push(element);
|
|
109
|
-
return
|
|
109
|
+
return true;
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
/**
|
|
@@ -123,7 +123,7 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
123
123
|
* array is empty, it returns `undefined`.
|
|
124
124
|
*/
|
|
125
125
|
pop(): E | undefined {
|
|
126
|
-
if (this.isEmpty()) return
|
|
126
|
+
if (this.isEmpty()) return;
|
|
127
127
|
|
|
128
128
|
return this.elements.pop();
|
|
129
129
|
}
|
|
@@ -228,15 +228,11 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
228
228
|
return newStack;
|
|
229
229
|
}
|
|
230
230
|
|
|
231
|
-
print(): void {
|
|
232
|
-
console.log([...this]);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
231
|
/**
|
|
236
232
|
* Custom iterator for the Stack class.
|
|
237
233
|
* @returns An iterator object.
|
|
238
234
|
*/
|
|
239
|
-
protected* _getIterator() {
|
|
235
|
+
protected* _getIterator(): IterableIterator<E> {
|
|
240
236
|
for (let i = 0; i < this.elements.length; i++) {
|
|
241
237
|
yield this.elements[i];
|
|
242
238
|
}
|