min-heap-typed 1.50.2 → 1.50.4
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 +6 -0
- package/dist/data-structures/binary-tree/{tree-multimap.d.ts → avl-tree-multi-map.d.ts} +43 -10
- package/dist/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +49 -11
- package/dist/data-structures/binary-tree/avl-tree.d.ts +29 -1
- package/dist/data-structures/binary-tree/avl-tree.js +33 -1
- 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 +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +46 -13
- package/dist/data-structures/binary-tree/bst.js +51 -20
- package/dist/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/data-structures/binary-tree/index.js +2 -1
- package/dist/data-structures/binary-tree/rb-tree.d.ts +54 -2
- package/dist/data-structures/binary-tree/rb-tree.js +90 -24
- 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-multi-map.d.ts +200 -0
- package/dist/data-structures/binary-tree/tree-multi-map.js +399 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -78
- package/dist/data-structures/graph/abstract-graph.js +0 -189
- package/dist/data-structures/graph/directed-graph.d.ts +59 -0
- package/dist/data-structures/graph/directed-graph.js +105 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +60 -7
- package/dist/data-structures/graph/undirected-graph.js +126 -18
- package/dist/data-structures/hash/hash-map.d.ts +143 -23
- package/dist/data-structures/hash/hash-map.js +196 -62
- package/dist/data-structures/heap/heap.d.ts +29 -19
- package/dist/data-structures/heap/heap.js +29 -20
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
- package/dist/data-structures/linked-list/doubly-linked-list.js +83 -25
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +26 -3
- package/dist/data-structures/linked-list/singly-linked-list.js +34 -3
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/data-structures/matrix/matrix.js +1 -1
- 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 +95 -21
- package/dist/data-structures/queue/deque.js +100 -16
- package/dist/data-structures/queue/queue.d.ts +65 -45
- package/dist/data-structures/queue/queue.js +65 -45
- package/dist/data-structures/stack/stack.d.ts +36 -22
- package/dist/data-structures/stack/stack.js +36 -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 +100 -36
- package/dist/data-structures/trie/trie.js +115 -36
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/index.js +2 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +2 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +12 -0
- package/src/data-structures/binary-tree/{tree-multimap.ts → avl-tree-multi-map.ts} +59 -20
- package/src/data-structures/binary-tree/avl-tree.ts +37 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/data-structures/binary-tree/bst.ts +51 -19
- package/src/data-structures/binary-tree/index.ts +2 -1
- package/src/data-structures/binary-tree/rb-tree.ts +99 -28
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multi-map.ts +463 -0
- package/src/data-structures/graph/abstract-graph.ts +0 -211
- package/src/data-structures/graph/directed-graph.ts +122 -0
- package/src/data-structures/graph/undirected-graph.ts +143 -19
- package/src/data-structures/hash/hash-map.ts +228 -76
- package/src/data-structures/heap/heap.ts +31 -20
- package/src/data-structures/linked-list/doubly-linked-list.ts +96 -29
- package/src/data-structures/linked-list/singly-linked-list.ts +42 -6
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +1 -1
- 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 +118 -22
- package/src/data-structures/queue/queue.ts +68 -45
- package/src/data-structures/stack/stack.ts +39 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +131 -40
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +8 -0
- package/src/types/data-structures/binary-tree/index.ts +2 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +8 -0
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +0 -5
- package/src/types/data-structures/binary-tree/tree-multimap.ts +0 -8
- /package/dist/types/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +0 -0
|
@@ -17,13 +17,6 @@ import { calcMinUnitsRequired, rangeCheck } from '../../utils';
|
|
|
17
17
|
* 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
|
|
18
18
|
*/
|
|
19
19
|
export class Deque<E> extends IterableElementBase<E> {
|
|
20
|
-
protected _bucketFirst = 0;
|
|
21
|
-
protected _firstInBucket = 0;
|
|
22
|
-
protected _bucketLast = 0;
|
|
23
|
-
protected _lastInBucket = 0;
|
|
24
|
-
protected _bucketCount = 0;
|
|
25
|
-
protected readonly _bucketSize: number = 1 << 12;
|
|
26
|
-
|
|
27
20
|
/**
|
|
28
21
|
* The constructor initializes a Deque object with an optional iterable of elements and options.
|
|
29
22
|
* @param elements - An iterable object (such as an array or a Set) that contains the initial
|
|
@@ -65,6 +58,8 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
65
58
|
}
|
|
66
59
|
}
|
|
67
60
|
|
|
61
|
+
protected _bucketSize: number = 1 << 12;
|
|
62
|
+
|
|
68
63
|
/**
|
|
69
64
|
* The bucketSize function returns the size of the bucket.
|
|
70
65
|
*
|
|
@@ -74,11 +69,62 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
74
69
|
return this._bucketSize;
|
|
75
70
|
}
|
|
76
71
|
|
|
72
|
+
protected _bucketFirst = 0;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The function returns the value of the protected variable `_bucketFirst`.
|
|
76
|
+
* @returns The value of the `_bucketFirst` property.
|
|
77
|
+
*/
|
|
78
|
+
get bucketFirst(): number {
|
|
79
|
+
return this._bucketFirst;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
protected _firstInBucket = 0;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The function returns the value of the protected variable _firstInBucket.
|
|
86
|
+
* @returns The method is returning the value of the variable `_firstInBucket`, which is of type
|
|
87
|
+
* `number`.
|
|
88
|
+
*/
|
|
89
|
+
get firstInBucket(): number {
|
|
90
|
+
return this._firstInBucket;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
protected _bucketLast = 0;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The function returns the value of the protected variable `_bucketLast`.
|
|
97
|
+
* @returns The value of the `_bucketLast` property, which is a number.
|
|
98
|
+
*/
|
|
99
|
+
get bucketLast(): number {
|
|
100
|
+
return this._bucketLast;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
protected _lastInBucket = 0;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The function returns the value of the protected variable _lastInBucket.
|
|
107
|
+
* @returns The method is returning the value of the variable `_lastInBucket`, which is of type
|
|
108
|
+
* `number`.
|
|
109
|
+
*/
|
|
110
|
+
get lastInBucket(): number {
|
|
111
|
+
return this._lastInBucket;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
protected _bucketCount = 0;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The function returns the number of buckets.
|
|
118
|
+
* @returns The number of buckets.
|
|
119
|
+
*/
|
|
120
|
+
get bucketCount(): number {
|
|
121
|
+
return this._bucketCount;
|
|
122
|
+
}
|
|
123
|
+
|
|
77
124
|
protected _buckets: E[][] = [];
|
|
78
125
|
|
|
79
126
|
/**
|
|
80
127
|
* The buckets function returns the buckets property of the object.
|
|
81
|
-
*
|
|
82
128
|
* @return The buckets property
|
|
83
129
|
*/
|
|
84
130
|
get buckets() {
|
|
@@ -243,15 +289,30 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
243
289
|
}
|
|
244
290
|
|
|
245
291
|
/**
|
|
246
|
-
* Time Complexity: O(1)
|
|
247
|
-
* Space Complexity: O(1)
|
|
292
|
+
* Time Complexity: O(1)
|
|
293
|
+
* Space Complexity: O(1)
|
|
248
294
|
*/
|
|
249
295
|
|
|
296
|
+
/**
|
|
297
|
+
* Time Complexity: O(1)
|
|
298
|
+
* Space Complexity: O(1)
|
|
299
|
+
*
|
|
300
|
+
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
301
|
+
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
302
|
+
*/
|
|
250
303
|
isEmpty(): boolean {
|
|
251
304
|
return this.size === 0;
|
|
252
305
|
}
|
|
253
306
|
|
|
254
307
|
/**
|
|
308
|
+
* Time Complexity: O(1)
|
|
309
|
+
* Space Complexity: O(1)
|
|
310
|
+
*/
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Time Complexity: O(1)
|
|
314
|
+
* Space Complexity: O(1)
|
|
315
|
+
*
|
|
255
316
|
* The clear() function resets the state of the object by initializing all variables to their default
|
|
256
317
|
* values.
|
|
257
318
|
*/
|
|
@@ -405,6 +466,26 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
405
466
|
}
|
|
406
467
|
}
|
|
407
468
|
|
|
469
|
+
/**
|
|
470
|
+
* Time Complexity: O(1)
|
|
471
|
+
* Space Complexity: O(1) or O(n)
|
|
472
|
+
*/
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Time Complexity: O(1)
|
|
476
|
+
* Space Complexity: O(1) or O(n)
|
|
477
|
+
*
|
|
478
|
+
* The `cutRest` function cuts the elements from a specified position in a deque and returns a new
|
|
479
|
+
* deque with the cut elements.
|
|
480
|
+
* @param {number} pos - The `pos` parameter represents the position from which to cut the Deque. It
|
|
481
|
+
* is a number that indicates the index of the element in the Deque where the cut should start.
|
|
482
|
+
* @param [isCutSelf=false] - isCutSelf is a boolean parameter that determines whether the original
|
|
483
|
+
* Deque should be modified or a new Deque should be created. If isCutSelf is true, the original
|
|
484
|
+
* Deque will be modified by cutting off elements starting from the specified position. If isCutSelf
|
|
485
|
+
* is false, a new De
|
|
486
|
+
* @returns The function `cutRest` returns either the modified original deque (`this`) or a new deque
|
|
487
|
+
* (`newDeque`) depending on the value of the `isCutSelf` parameter.
|
|
488
|
+
*/
|
|
408
489
|
cutRest(pos: number, isCutSelf = false): Deque<E> {
|
|
409
490
|
if (isCutSelf) {
|
|
410
491
|
if (pos < 0) {
|
|
@@ -429,12 +510,12 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
429
510
|
|
|
430
511
|
/**
|
|
431
512
|
* Time Complexity: O(n)
|
|
432
|
-
* Space Complexity: O(1)
|
|
513
|
+
* Space Complexity: O(1) or O(n)
|
|
433
514
|
*/
|
|
434
515
|
|
|
435
516
|
/**
|
|
436
517
|
* Time Complexity: O(n)
|
|
437
|
-
* Space Complexity: O(1)
|
|
518
|
+
* Space Complexity: O(1) or O(n)
|
|
438
519
|
*
|
|
439
520
|
* The `deleteAt` function removes an element at a specified position in an array-like data
|
|
440
521
|
* structure.
|
|
@@ -736,8 +817,8 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
736
817
|
*/
|
|
737
818
|
|
|
738
819
|
/**
|
|
739
|
-
* Time Complexity: O(1)
|
|
740
|
-
* Space Complexity: O(n) -
|
|
820
|
+
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
821
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
741
822
|
*
|
|
742
823
|
* The addLast function adds an element to the end of an array.
|
|
743
824
|
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
@@ -748,13 +829,13 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
748
829
|
}
|
|
749
830
|
|
|
750
831
|
/**
|
|
751
|
-
* Time Complexity: O(1)
|
|
752
|
-
* Space Complexity: O(1)
|
|
832
|
+
* Time Complexity: O(1)
|
|
833
|
+
* Space Complexity: O(1)
|
|
753
834
|
*/
|
|
754
835
|
|
|
755
836
|
/**
|
|
756
|
-
* Time Complexity: O(1)
|
|
757
|
-
* Space Complexity: O(1)
|
|
837
|
+
* Time Complexity: O(1)
|
|
838
|
+
* Space Complexity: O(1)
|
|
758
839
|
*
|
|
759
840
|
* The function "pollLast" removes and returns the last element of an array.
|
|
760
841
|
* @returns The last element of the array is being returned.
|
|
@@ -764,8 +845,13 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
764
845
|
}
|
|
765
846
|
|
|
766
847
|
/**
|
|
767
|
-
* Time Complexity: O(1)
|
|
768
|
-
* Space Complexity: O(
|
|
848
|
+
* Time Complexity: O(1)
|
|
849
|
+
* Space Complexity: O(1)
|
|
850
|
+
* /
|
|
851
|
+
|
|
852
|
+
/**
|
|
853
|
+
* Time Complexity: O(1)
|
|
854
|
+
* Space Complexity: O(1)
|
|
769
855
|
*
|
|
770
856
|
* The "addFirst" function adds an element to the beginning of an array.
|
|
771
857
|
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
@@ -776,8 +862,13 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
776
862
|
}
|
|
777
863
|
|
|
778
864
|
/**
|
|
779
|
-
* Time Complexity: O(1)
|
|
780
|
-
* Space Complexity: O(1)
|
|
865
|
+
* Time Complexity: O(1)
|
|
866
|
+
* Space Complexity: O(1)
|
|
867
|
+
* /
|
|
868
|
+
|
|
869
|
+
/**
|
|
870
|
+
* Time Complexity: O(1)
|
|
871
|
+
* Space Complexity: O(1)
|
|
781
872
|
*
|
|
782
873
|
* The function "pollFirst" removes and returns the first element of an array.
|
|
783
874
|
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
@@ -788,6 +879,11 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
788
879
|
}
|
|
789
880
|
|
|
790
881
|
/**
|
|
882
|
+
* Time Complexity: O(n)
|
|
883
|
+
* Space Complexity: O(1)
|
|
884
|
+
* /
|
|
885
|
+
|
|
886
|
+
/**
|
|
791
887
|
* Time Complexity: O(n)
|
|
792
888
|
* Space Complexity: O(1)
|
|
793
889
|
*
|
|
@@ -44,7 +44,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
46
|
* The offset function returns the offset of the current page.
|
|
47
|
-
* @return The value of the
|
|
47
|
+
* @return The value of the protected variable _offset
|
|
48
48
|
*/
|
|
49
49
|
get offset(): number {
|
|
50
50
|
return this._offset;
|
|
@@ -59,8 +59,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
/**
|
|
62
|
-
* Time Complexity: O(1)
|
|
63
|
-
* 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)
|
|
64
69
|
*
|
|
65
70
|
* The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
|
|
66
71
|
* @returns The `get first()` method returns the first element of the data structure, represented by the `_elements` array at
|
|
@@ -71,13 +76,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
71
76
|
}
|
|
72
77
|
|
|
73
78
|
/**
|
|
74
|
-
* Time Complexity: O(1)
|
|
75
|
-
* Space Complexity: O(1)
|
|
79
|
+
* Time Complexity: O(1)
|
|
80
|
+
* Space Complexity: O(1)
|
|
76
81
|
*/
|
|
77
82
|
|
|
78
83
|
/**
|
|
79
|
-
* Time Complexity: O(1)
|
|
80
|
-
* Space Complexity: O(1)
|
|
84
|
+
* Time Complexity: O(1)
|
|
85
|
+
* Space Complexity: O(1)
|
|
81
86
|
*
|
|
82
87
|
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
83
88
|
* @returns The method `get last()` returns the last element of the `_elements` array if the array is not empty. If the
|
|
@@ -88,11 +93,14 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
88
93
|
}
|
|
89
94
|
|
|
90
95
|
/**
|
|
91
|
-
* Time Complexity: O(n)
|
|
92
|
-
* Space Complexity: O(
|
|
96
|
+
* Time Complexity: O(n)
|
|
97
|
+
* Space Complexity: O(n)
|
|
93
98
|
*/
|
|
94
99
|
|
|
95
100
|
/**
|
|
101
|
+
* Time Complexity: O(n)
|
|
102
|
+
* Space Complexity: O(n)
|
|
103
|
+
*
|
|
96
104
|
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
97
105
|
* @public
|
|
98
106
|
* @static
|
|
@@ -105,13 +113,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
105
113
|
}
|
|
106
114
|
|
|
107
115
|
/**
|
|
108
|
-
* Time Complexity: O(1)
|
|
109
|
-
* Space Complexity: O(1)
|
|
116
|
+
* Time Complexity: O(1)
|
|
117
|
+
* Space Complexity: O(1)
|
|
110
118
|
*/
|
|
111
119
|
|
|
112
120
|
/**
|
|
113
|
-
* Time Complexity: O(1)
|
|
114
|
-
* Space Complexity: O(1)
|
|
121
|
+
* Time Complexity: O(1)
|
|
122
|
+
* Space Complexity: O(1)
|
|
115
123
|
*
|
|
116
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.
|
|
117
125
|
* @param {E} element - The `element` parameter represents the element that you want to add to the queue.
|
|
@@ -123,13 +131,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
123
131
|
}
|
|
124
132
|
|
|
125
133
|
/**
|
|
126
|
-
* Time Complexity: O(1)
|
|
127
|
-
* Space Complexity: O(1)
|
|
134
|
+
* Time Complexity: O(1)
|
|
135
|
+
* Space Complexity: O(1)
|
|
128
136
|
*/
|
|
129
137
|
|
|
130
138
|
/**
|
|
131
|
-
* Time Complexity: O(
|
|
132
|
-
* Space Complexity: O(1)
|
|
139
|
+
* Time Complexity: O(1)
|
|
140
|
+
* Space Complexity: O(1)
|
|
133
141
|
*
|
|
134
142
|
* The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
|
|
135
143
|
* necessary to optimize performance.
|
|
@@ -171,13 +179,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
171
179
|
}
|
|
172
180
|
|
|
173
181
|
/**
|
|
174
|
-
* Time Complexity: O(1)
|
|
175
|
-
* Space Complexity: O(1)
|
|
182
|
+
* Time Complexity: O(1)
|
|
183
|
+
* Space Complexity: O(1)
|
|
176
184
|
*/
|
|
177
185
|
|
|
178
186
|
/**
|
|
179
|
-
* Time Complexity: O(1)
|
|
180
|
-
* Space Complexity: O(1)
|
|
187
|
+
* Time Complexity: O(1)
|
|
188
|
+
* Space Complexity: O(1)
|
|
181
189
|
*
|
|
182
190
|
* The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
|
|
183
191
|
* @returns The `peek()` method returns the first element of the data structure, represented by the `_elements` array at
|
|
@@ -188,13 +196,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
188
196
|
}
|
|
189
197
|
|
|
190
198
|
/**
|
|
191
|
-
* Time Complexity: O(1)
|
|
192
|
-
* Space Complexity: O(1)
|
|
199
|
+
* Time Complexity: O(1)
|
|
200
|
+
* Space Complexity: O(1)
|
|
193
201
|
*/
|
|
194
202
|
|
|
195
203
|
/**
|
|
196
|
-
* Time Complexity: O(1)
|
|
197
|
-
* Space Complexity: O(1)
|
|
204
|
+
* Time Complexity: O(1)
|
|
205
|
+
* Space Complexity: O(1)
|
|
198
206
|
*
|
|
199
207
|
* The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
200
208
|
* @returns The method `peekLast()` returns the last element of the `_elements` array if the array is not empty. If the
|
|
@@ -205,13 +213,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
205
213
|
}
|
|
206
214
|
|
|
207
215
|
/**
|
|
208
|
-
* Time Complexity: O(1)
|
|
209
|
-
* Space Complexity: O(1)
|
|
216
|
+
* Time Complexity: O(1)
|
|
217
|
+
* Space Complexity: O(1)
|
|
210
218
|
*/
|
|
211
219
|
|
|
212
220
|
/**
|
|
213
|
-
* Time Complexity: O(1)
|
|
214
|
-
* Space Complexity: O(1)
|
|
221
|
+
* Time Complexity: O(1)
|
|
222
|
+
* Space Complexity: O(1)
|
|
215
223
|
*
|
|
216
224
|
* The enqueue function adds a value to the end of a queue.
|
|
217
225
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -221,13 +229,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
221
229
|
}
|
|
222
230
|
|
|
223
231
|
/**
|
|
224
|
-
* Time Complexity: O(
|
|
225
|
-
* Space Complexity: O(1)
|
|
232
|
+
* Time Complexity: O(1)
|
|
233
|
+
* Space Complexity: O(1)
|
|
226
234
|
*/
|
|
227
235
|
|
|
228
236
|
/**
|
|
229
|
-
* Time Complexity: O(
|
|
230
|
-
* Space Complexity: O(1)
|
|
237
|
+
* Time Complexity: O(1)
|
|
238
|
+
* Space Complexity: O(1)
|
|
231
239
|
*
|
|
232
240
|
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
233
241
|
* @returns The method is returning a value of type E or undefined.
|
|
@@ -237,13 +245,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
237
245
|
}
|
|
238
246
|
|
|
239
247
|
/**
|
|
240
|
-
* Time Complexity: O(1)
|
|
241
|
-
* Space Complexity: O(1)
|
|
248
|
+
* Time Complexity: O(1)
|
|
249
|
+
* Space Complexity: O(1)
|
|
242
250
|
*/
|
|
243
251
|
|
|
244
252
|
/**
|
|
245
|
-
* Time Complexity: O(1)
|
|
246
|
-
* Space Complexity: O(1)
|
|
253
|
+
* Time Complexity: O(1)
|
|
254
|
+
* Space Complexity: O(1)
|
|
247
255
|
*
|
|
248
256
|
* @param index
|
|
249
257
|
*/
|
|
@@ -252,13 +260,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
252
260
|
}
|
|
253
261
|
|
|
254
262
|
/**
|
|
255
|
-
* Time Complexity: O(1)
|
|
256
|
-
* Space Complexity: O(1)
|
|
263
|
+
* Time Complexity: O(1)
|
|
264
|
+
* Space Complexity: O(1)
|
|
257
265
|
*/
|
|
258
266
|
|
|
259
267
|
/**
|
|
260
|
-
* Time Complexity: O(1)
|
|
261
|
-
* Space Complexity: O(1)
|
|
268
|
+
* Time Complexity: O(1)
|
|
269
|
+
* Space Complexity: O(1)
|
|
262
270
|
*
|
|
263
271
|
* The function checks if a data structure is empty by comparing its size to zero.
|
|
264
272
|
* @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
|
|
@@ -268,13 +276,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
268
276
|
}
|
|
269
277
|
|
|
270
278
|
/**
|
|
271
|
-
* Time Complexity: O(1)
|
|
272
|
-
* Space Complexity: O(n)
|
|
279
|
+
* Time Complexity: O(1)
|
|
280
|
+
* Space Complexity: O(n)
|
|
273
281
|
*/
|
|
274
282
|
|
|
275
283
|
/**
|
|
276
|
-
* Time Complexity: O(1)
|
|
277
|
-
* Space Complexity: O(n)
|
|
284
|
+
* Time Complexity: O(1)
|
|
285
|
+
* Space Complexity: O(n)
|
|
278
286
|
*
|
|
279
287
|
* The toArray() function returns an array of elements from the current offset to the end of the _elements array.
|
|
280
288
|
* @returns An array of type E is being returned.
|
|
@@ -284,6 +292,14 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
284
292
|
}
|
|
285
293
|
|
|
286
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
|
+
*
|
|
287
303
|
* The clear function resets the elements array and offset to their initial values.
|
|
288
304
|
*/
|
|
289
305
|
clear(): void {
|
|
@@ -345,6 +361,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
345
361
|
* Time Complexity: O(n)
|
|
346
362
|
* Space Complexity: O(n)
|
|
347
363
|
*/
|
|
364
|
+
|
|
348
365
|
/**
|
|
349
366
|
* Time Complexity: O(n)
|
|
350
367
|
* Space Complexity: O(n)
|
|
@@ -374,6 +391,12 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
374
391
|
* Space Complexity: O(n)
|
|
375
392
|
*/
|
|
376
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
|
+
*/
|
|
377
400
|
protected* _getIterator(): IterableIterator<E> {
|
|
378
401
|
for (const item of this.elements) {
|
|
379
402
|
yield item;
|
|
@@ -40,11 +40,6 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
40
40
|
return this._elements;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
/**
|
|
44
|
-
* 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.
|
|
45
|
-
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
46
|
-
*/
|
|
47
|
-
|
|
48
43
|
/**
|
|
49
44
|
* The size() function returns the number of elements in an array.
|
|
50
45
|
* @returns The size of the elements array.
|
|
@@ -54,8 +49,13 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
54
49
|
}
|
|
55
50
|
|
|
56
51
|
/**
|
|
57
|
-
* Time Complexity: O(n)
|
|
58
|
-
* 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)
|
|
59
59
|
*
|
|
60
60
|
* The function "fromArray" creates a new Stack object from an array of elements.
|
|
61
61
|
* @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
|
|
@@ -75,13 +75,13 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
/**
|
|
78
|
-
* Time Complexity: O(1)
|
|
79
|
-
* Space Complexity: O(1)
|
|
78
|
+
* Time Complexity: O(1)
|
|
79
|
+
* Space Complexity: O(1)
|
|
80
80
|
*/
|
|
81
81
|
|
|
82
82
|
/**
|
|
83
|
-
* Time Complexity: O(1)
|
|
84
|
-
* Space Complexity: O(1)
|
|
83
|
+
* Time Complexity: O(1)
|
|
84
|
+
* Space Complexity: O(1)
|
|
85
85
|
*
|
|
86
86
|
* The `peek` function returns the last element of an array, or undefined if the array is empty.
|
|
87
87
|
* @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
|
|
@@ -93,13 +93,13 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
/**
|
|
96
|
-
* Time Complexity: O(1)
|
|
97
|
-
* Space Complexity: O(1)
|
|
96
|
+
* Time Complexity: O(1)
|
|
97
|
+
* Space Complexity: O(1)
|
|
98
98
|
*/
|
|
99
99
|
|
|
100
100
|
/**
|
|
101
|
-
* Time Complexity: O(1)
|
|
102
|
-
* Space Complexity: O(1)
|
|
101
|
+
* Time Complexity: O(1)
|
|
102
|
+
* Space Complexity: O(1)
|
|
103
103
|
*
|
|
104
104
|
* The push function adds an element to the stack and returns the updated stack.
|
|
105
105
|
* @param {E} element - The parameter "element" is of type E, which means it can be any data type.
|
|
@@ -111,13 +111,13 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
/**
|
|
114
|
-
* Time Complexity: O(1)
|
|
115
|
-
* Space Complexity: O(1)
|
|
114
|
+
* Time Complexity: O(1)
|
|
115
|
+
* Space Complexity: O(1)
|
|
116
116
|
*/
|
|
117
117
|
|
|
118
118
|
/**
|
|
119
|
-
* Time Complexity: O(1)
|
|
120
|
-
* Space Complexity: O(1)
|
|
119
|
+
* Time Complexity: O(1)
|
|
120
|
+
* Space Complexity: O(1)
|
|
121
121
|
*
|
|
122
122
|
* The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
|
|
123
123
|
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
@@ -166,6 +166,14 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
166
166
|
}
|
|
167
167
|
|
|
168
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
|
+
*
|
|
169
177
|
* The clear function clears the elements array.
|
|
170
178
|
*/
|
|
171
179
|
clear(): void {
|
|
@@ -173,13 +181,13 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
173
181
|
}
|
|
174
182
|
|
|
175
183
|
/**
|
|
176
|
-
* Time Complexity: O(n)
|
|
177
|
-
* Space Complexity: O(n)
|
|
184
|
+
* Time Complexity: O(n)
|
|
185
|
+
* Space Complexity: O(n)
|
|
178
186
|
*/
|
|
179
187
|
|
|
180
188
|
/**
|
|
181
|
-
* Time Complexity: O(n)
|
|
182
|
-
* Space Complexity: O(n)
|
|
189
|
+
* Time Complexity: O(n)
|
|
190
|
+
* Space Complexity: O(n)
|
|
183
191
|
*
|
|
184
192
|
* The `clone()` function returns a new `Stack` object with the same elements as the original stack.
|
|
185
193
|
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
@@ -250,6 +258,14 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
250
258
|
}
|
|
251
259
|
|
|
252
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
|
+
*
|
|
253
269
|
* Custom iterator for the Stack class.
|
|
254
270
|
* @returns An iterator object.
|
|
255
271
|
*/
|