data-structure-typed 1.49.0 → 1.49.2
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/CHANGELOG.md +1 -1
- package/README.md +16 -16
- package/README_zh-CN.md +2 -2
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +457 -22
- package/dist/cjs/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/cjs/data-structures/base/iterable-base.js +21 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +10 -12
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +3 -4
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +58 -58
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +6 -6
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +15 -15
- package/dist/cjs/data-structures/binary-tree/bst.js +3 -3
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +11 -11
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +5 -6
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +14 -14
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +3 -3
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +9 -3
- package/dist/cjs/data-structures/graph/abstract-graph.js +27 -31
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +8 -1
- package/dist/cjs/data-structures/graph/directed-graph.js +1 -8
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +8 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js +1 -8
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +22 -10
- package/dist/cjs/data-structures/hash/hash-map.js +28 -16
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +2 -2
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +20 -38
- package/dist/cjs/data-structures/heap/heap.js +22 -42
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +11 -1
- package/dist/cjs/data-structures/heap/max-heap.js +10 -7
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +11 -1
- package/dist/cjs/data-structures/heap/min-heap.js +10 -7
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +95 -95
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +132 -136
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +18 -23
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +42 -49
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +0 -7
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +0 -7
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +9 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -7
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +76 -80
- package/dist/cjs/data-structures/queue/deque.js +106 -122
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +30 -16
- package/dist/cjs/data-structures/queue/queue.js +31 -24
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +17 -8
- package/dist/cjs/data-structures/stack/stack.js +9 -9
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +14 -5
- package/dist/cjs/data-structures/trie/trie.js +13 -13
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +4 -4
- package/dist/cjs/types/common.d.ts +32 -8
- package/dist/cjs/types/common.js +22 -1
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +0 -22
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/mjs/data-structures/base/iterable-base.js +21 -0
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +10 -12
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +3 -4
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +58 -58
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +6 -6
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +15 -15
- package/dist/mjs/data-structures/binary-tree/bst.js +3 -3
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +11 -11
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +5 -6
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +14 -14
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +3 -3
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +9 -3
- package/dist/mjs/data-structures/graph/abstract-graph.js +27 -31
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +8 -1
- package/dist/mjs/data-structures/graph/directed-graph.js +1 -8
- package/dist/mjs/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +8 -1
- package/dist/mjs/data-structures/graph/undirected-graph.js +1 -8
- package/dist/mjs/data-structures/hash/hash-map.d.ts +22 -10
- package/dist/mjs/data-structures/hash/hash-map.js +27 -15
- package/dist/mjs/data-structures/hash/hash-table.d.ts +2 -2
- package/dist/mjs/data-structures/heap/heap.d.ts +20 -38
- package/dist/mjs/data-structures/heap/heap.js +23 -43
- package/dist/mjs/data-structures/heap/max-heap.d.ts +11 -1
- package/dist/mjs/data-structures/heap/max-heap.js +9 -6
- package/dist/mjs/data-structures/heap/min-heap.d.ts +11 -1
- package/dist/mjs/data-structures/heap/min-heap.js +9 -6
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +95 -95
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +132 -136
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +18 -23
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +43 -50
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +0 -7
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +0 -7
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +9 -1
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +7 -6
- package/dist/mjs/data-structures/queue/deque.d.ts +76 -80
- package/dist/mjs/data-structures/queue/deque.js +106 -122
- package/dist/mjs/data-structures/queue/queue.d.ts +30 -16
- package/dist/mjs/data-structures/queue/queue.js +31 -24
- package/dist/mjs/data-structures/stack/stack.d.ts +17 -8
- package/dist/mjs/data-structures/stack/stack.js +10 -10
- package/dist/mjs/data-structures/trie/trie.d.ts +14 -5
- package/dist/mjs/data-structures/trie/trie.js +14 -14
- package/dist/mjs/interfaces/binary-tree.d.ts +4 -4
- package/dist/mjs/types/common.d.ts +32 -8
- package/dist/mjs/types/common.js +21 -0
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.js +1 -21
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/umd/data-structure-typed.js +533 -558
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/base/iterable-base.ts +24 -0
- package/src/data-structures/binary-tree/avl-tree.ts +14 -14
- package/src/data-structures/binary-tree/binary-tree.ts +74 -77
- package/src/data-structures/binary-tree/bst.ts +18 -17
- package/src/data-structures/binary-tree/rb-tree.ts +17 -18
- package/src/data-structures/binary-tree/tree-multimap.ts +22 -20
- package/src/data-structures/graph/abstract-graph.ts +35 -25
- package/src/data-structures/graph/directed-graph.ts +2 -2
- package/src/data-structures/graph/map-graph.ts +1 -1
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +40 -24
- package/src/data-structures/hash/hash-table.ts +3 -3
- package/src/data-structures/heap/heap.ts +33 -60
- package/src/data-structures/heap/max-heap.ts +11 -2
- package/src/data-structures/heap/min-heap.ts +11 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +147 -145
- package/src/data-structures/linked-list/singly-linked-list.ts +52 -52
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +9 -2
- package/src/data-structures/queue/deque.ts +129 -144
- package/src/data-structures/queue/queue.ts +37 -26
- package/src/data-structures/stack/stack.ts +20 -14
- package/src/data-structures/trie/trie.ts +18 -13
- package/src/interfaces/binary-tree.ts +5 -5
- package/src/types/common.ts +37 -12
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -26
- package/src/types/data-structures/binary-tree/bst.ts +0 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
- package/test/performance/data-structures/comparison/comparison.test.ts +6 -6
- package/test/performance/data-structures/heap/heap.test.ts +2 -2
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +2 -2
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +2 -2
- package/test/unit/data-structures/graph/directed-graph.test.ts +21 -1
- package/test/unit/data-structures/graph/undirected-graph.test.ts +39 -1
- package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
- package/test/unit/data-structures/heap/heap.test.ts +1 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -30
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +21 -21
- package/test/unit/data-structures/linked-list/skip-list.test.ts +2 -2
- package/test/unit/data-structures/queue/deque.test.ts +5 -5
- package/test/unit/data-structures/queue/queue.test.ts +4 -4
- package/test/unit/data-structures/trie/trie.test.ts +1 -1
|
@@ -1,11 +1,4 @@
|
|
|
1
1
|
import { IterableElementBase } from "../base";
|
|
2
|
-
/**
|
|
3
|
-
* data-structure-typed
|
|
4
|
-
*
|
|
5
|
-
* @author Tyler Zeng
|
|
6
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
7
|
-
* @license MIT License
|
|
8
|
-
*/
|
|
9
2
|
export class SinglyLinkedListNode {
|
|
10
3
|
value;
|
|
11
4
|
next;
|
|
@@ -27,7 +20,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
27
20
|
super();
|
|
28
21
|
this._head = undefined;
|
|
29
22
|
this._tail = undefined;
|
|
30
|
-
this.
|
|
23
|
+
this._size = 0;
|
|
31
24
|
if (elements) {
|
|
32
25
|
for (const el of elements)
|
|
33
26
|
this.push(el);
|
|
@@ -41,9 +34,9 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
41
34
|
get tail() {
|
|
42
35
|
return this._tail;
|
|
43
36
|
}
|
|
44
|
-
|
|
45
|
-
get
|
|
46
|
-
return this.
|
|
37
|
+
_size;
|
|
38
|
+
get size() {
|
|
39
|
+
return this._size;
|
|
47
40
|
}
|
|
48
41
|
/**
|
|
49
42
|
* Time Complexity: O(n) - Linear time, where n is the length of the input array, as it performs a loop to push each element into the linked list.
|
|
@@ -87,7 +80,8 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
87
80
|
this.tail.next = newNode;
|
|
88
81
|
this._tail = newNode;
|
|
89
82
|
}
|
|
90
|
-
this.
|
|
83
|
+
this._size++;
|
|
84
|
+
return true;
|
|
91
85
|
}
|
|
92
86
|
/**
|
|
93
87
|
* Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
|
|
@@ -102,7 +96,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
102
96
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
103
97
|
*/
|
|
104
98
|
addLast(value) {
|
|
105
|
-
this.push(value);
|
|
99
|
+
return this.push(value);
|
|
106
100
|
}
|
|
107
101
|
/**
|
|
108
102
|
* Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
|
|
@@ -124,7 +118,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
124
118
|
const value = this.head.value;
|
|
125
119
|
this._head = undefined;
|
|
126
120
|
this._tail = undefined;
|
|
127
|
-
this.
|
|
121
|
+
this._size--;
|
|
128
122
|
return value;
|
|
129
123
|
}
|
|
130
124
|
let current = this.head;
|
|
@@ -134,7 +128,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
134
128
|
const value = this.tail.value;
|
|
135
129
|
current.next = undefined;
|
|
136
130
|
this._tail = current;
|
|
137
|
-
this.
|
|
131
|
+
this._size--;
|
|
138
132
|
return value;
|
|
139
133
|
}
|
|
140
134
|
/**
|
|
@@ -169,7 +163,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
169
163
|
return undefined;
|
|
170
164
|
const removedNode = this.head;
|
|
171
165
|
this._head = this.head.next;
|
|
172
|
-
this.
|
|
166
|
+
this._size--;
|
|
173
167
|
return removedNode.value;
|
|
174
168
|
}
|
|
175
169
|
/**
|
|
@@ -208,7 +202,8 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
208
202
|
newNode.next = this.head;
|
|
209
203
|
this._head = newNode;
|
|
210
204
|
}
|
|
211
|
-
this.
|
|
205
|
+
this._size++;
|
|
206
|
+
return true;
|
|
212
207
|
}
|
|
213
208
|
/**
|
|
214
209
|
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
|
|
@@ -223,7 +218,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
223
218
|
* linked list.
|
|
224
219
|
*/
|
|
225
220
|
addFirst(value) {
|
|
226
|
-
this.unshift(value);
|
|
221
|
+
return this.unshift(value);
|
|
227
222
|
}
|
|
228
223
|
/**
|
|
229
224
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
@@ -240,7 +235,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
240
235
|
* `undefined` if the index is out of bounds.
|
|
241
236
|
*/
|
|
242
237
|
getAt(index) {
|
|
243
|
-
if (index < 0 || index >= this.
|
|
238
|
+
if (index < 0 || index >= this.size)
|
|
244
239
|
return undefined;
|
|
245
240
|
let current = this.head;
|
|
246
241
|
for (let i = 0; i < index; i++) {
|
|
@@ -284,17 +279,21 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
284
279
|
* bounds.
|
|
285
280
|
*/
|
|
286
281
|
deleteAt(index) {
|
|
287
|
-
if (index < 0 || index >= this.
|
|
288
|
-
return
|
|
289
|
-
if (index === 0)
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
282
|
+
if (index < 0 || index >= this.size)
|
|
283
|
+
return false;
|
|
284
|
+
if (index === 0) {
|
|
285
|
+
this.shift();
|
|
286
|
+
return true;
|
|
287
|
+
}
|
|
288
|
+
if (index === this.size - 1) {
|
|
289
|
+
this.pop();
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
293
292
|
const prevNode = this.getNodeAt(index - 1);
|
|
294
293
|
const removedNode = prevNode.next;
|
|
295
294
|
prevNode.next = removedNode.next;
|
|
296
|
-
this.
|
|
297
|
-
return
|
|
295
|
+
this._size--;
|
|
296
|
+
return true;
|
|
298
297
|
}
|
|
299
298
|
/**
|
|
300
299
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
@@ -335,7 +334,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
335
334
|
this._tail = prev;
|
|
336
335
|
}
|
|
337
336
|
}
|
|
338
|
-
this.
|
|
337
|
+
this._size--;
|
|
339
338
|
return true;
|
|
340
339
|
}
|
|
341
340
|
prev = current;
|
|
@@ -351,7 +350,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
351
350
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
352
351
|
* Space Complexity: O(1) - Constant space.
|
|
353
352
|
*
|
|
354
|
-
* The `
|
|
353
|
+
* The `addAt` function inserts a value at a specified index in a singly linked list.
|
|
355
354
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
356
355
|
* linked list. It is of type number.
|
|
357
356
|
* @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
|
|
@@ -359,14 +358,14 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
359
358
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
360
359
|
* if the index is out of bounds.
|
|
361
360
|
*/
|
|
362
|
-
|
|
363
|
-
if (index < 0 || index > this.
|
|
361
|
+
addAt(index, value) {
|
|
362
|
+
if (index < 0 || index > this.size)
|
|
364
363
|
return false;
|
|
365
364
|
if (index === 0) {
|
|
366
365
|
this.unshift(value);
|
|
367
366
|
return true;
|
|
368
367
|
}
|
|
369
|
-
if (index === this.
|
|
368
|
+
if (index === this.size) {
|
|
370
369
|
this.push(value);
|
|
371
370
|
return true;
|
|
372
371
|
}
|
|
@@ -374,7 +373,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
374
373
|
const prevNode = this.getNodeAt(index - 1);
|
|
375
374
|
newNode.next = prevNode.next;
|
|
376
375
|
prevNode.next = newNode;
|
|
377
|
-
this.
|
|
376
|
+
this._size++;
|
|
378
377
|
return true;
|
|
379
378
|
}
|
|
380
379
|
/**
|
|
@@ -383,7 +382,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
383
382
|
* @returns A boolean value indicating whether the length of the object is equal to 0.
|
|
384
383
|
*/
|
|
385
384
|
isEmpty() {
|
|
386
|
-
return this.
|
|
385
|
+
return this.size === 0;
|
|
387
386
|
}
|
|
388
387
|
/**
|
|
389
388
|
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
|
|
@@ -391,7 +390,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
391
390
|
clear() {
|
|
392
391
|
this._head = undefined;
|
|
393
392
|
this._tail = undefined;
|
|
394
|
-
this.
|
|
393
|
+
this._size = 0;
|
|
395
394
|
}
|
|
396
395
|
/**
|
|
397
396
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to traverse the entire list to convert it to an array.
|
|
@@ -426,7 +425,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
426
425
|
*/
|
|
427
426
|
reverse() {
|
|
428
427
|
if (!this.head || this.head === this.tail)
|
|
429
|
-
return;
|
|
428
|
+
return this;
|
|
430
429
|
let prev = undefined;
|
|
431
430
|
let current = this.head;
|
|
432
431
|
let next = undefined;
|
|
@@ -437,6 +436,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
437
436
|
current = next;
|
|
438
437
|
}
|
|
439
438
|
[this._head, this._tail] = [this.tail, this.head];
|
|
439
|
+
return this;
|
|
440
440
|
}
|
|
441
441
|
/**
|
|
442
442
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
@@ -519,14 +519,14 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
519
519
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
520
520
|
* Space Complexity: O(1) - Constant space.
|
|
521
521
|
*
|
|
522
|
-
* The `
|
|
522
|
+
* The `addBefore` function inserts a new value before an existing value in a singly linked list.
|
|
523
523
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
|
|
524
524
|
* new value before. It can be either the value itself or a node containing the value in the linked list.
|
|
525
525
|
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
|
|
526
|
-
* @returns The method `
|
|
526
|
+
* @returns The method `addBefore` returns a boolean value. It returns `true` if the new value was successfully
|
|
527
527
|
* inserted before the existing value, and `false` otherwise.
|
|
528
528
|
*/
|
|
529
|
-
|
|
529
|
+
addBefore(existingValueOrNode, newValue) {
|
|
530
530
|
if (!this.head)
|
|
531
531
|
return false;
|
|
532
532
|
let existingValue;
|
|
@@ -546,7 +546,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
546
546
|
const newNode = new SinglyLinkedListNode(newValue);
|
|
547
547
|
newNode.next = current.next;
|
|
548
548
|
current.next = newNode;
|
|
549
|
-
this.
|
|
549
|
+
this._size++;
|
|
550
550
|
return true;
|
|
551
551
|
}
|
|
552
552
|
current = current.next;
|
|
@@ -561,14 +561,14 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
561
561
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
562
562
|
* Space Complexity: O(1) - Constant space.
|
|
563
563
|
*
|
|
564
|
-
* The `
|
|
564
|
+
* The `addAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
565
565
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
|
|
566
566
|
* the new value will be inserted. It can be either the value of the existing node or the existing node itself.
|
|
567
567
|
* @param {E} newValue - The value that you want to insert into the linked list after the existing value or node.
|
|
568
568
|
* @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
|
|
569
569
|
* existing value or node, and false if the existing value or node was not found in the linked list.
|
|
570
570
|
*/
|
|
571
|
-
|
|
571
|
+
addAfter(existingValueOrNode, newValue) {
|
|
572
572
|
let existingNode;
|
|
573
573
|
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
574
574
|
existingNode = existingValueOrNode;
|
|
@@ -583,7 +583,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
583
583
|
if (existingNode === this.tail) {
|
|
584
584
|
this._tail = newNode;
|
|
585
585
|
}
|
|
586
|
-
this.
|
|
586
|
+
this._size++;
|
|
587
587
|
return true;
|
|
588
588
|
}
|
|
589
589
|
return false;
|
|
@@ -670,13 +670,6 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
670
670
|
}
|
|
671
671
|
return mappedList;
|
|
672
672
|
}
|
|
673
|
-
/**
|
|
674
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
675
|
-
* Space Complexity: O(n)
|
|
676
|
-
*/
|
|
677
|
-
print() {
|
|
678
|
-
console.log([...this]);
|
|
679
|
-
}
|
|
680
673
|
*_getIterator() {
|
|
681
674
|
let current = this.head;
|
|
682
675
|
while (current) {
|
|
@@ -90,7 +90,7 @@ export declare class SkipList<K, V> {
|
|
|
90
90
|
* Get the value of the first element (the smallest element) in the Skip List.
|
|
91
91
|
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
92
92
|
*/
|
|
93
|
-
|
|
93
|
+
get first(): V | undefined;
|
|
94
94
|
/**
|
|
95
95
|
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
96
96
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
@@ -102,7 +102,7 @@ export declare class SkipList<K, V> {
|
|
|
102
102
|
* Get the value of the last element (the largest element) in the Skip List.
|
|
103
103
|
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
104
104
|
*/
|
|
105
|
-
|
|
105
|
+
get last(): V | undefined;
|
|
106
106
|
/**
|
|
107
107
|
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
108
108
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
@@ -161,7 +161,7 @@ export class SkipList {
|
|
|
161
161
|
* Get the value of the first element (the smallest element) in the Skip List.
|
|
162
162
|
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
163
163
|
*/
|
|
164
|
-
|
|
164
|
+
get first() {
|
|
165
165
|
const firstNode = this.head.forward[0];
|
|
166
166
|
return firstNode ? firstNode.value : undefined;
|
|
167
167
|
}
|
|
@@ -176,7 +176,7 @@ export class SkipList {
|
|
|
176
176
|
* Get the value of the last element (the largest element) in the Skip List.
|
|
177
177
|
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
178
178
|
*/
|
|
179
|
-
|
|
179
|
+
get last() {
|
|
180
180
|
let current = this.head;
|
|
181
181
|
for (let i = this.level - 1; i >= 0; i--) {
|
|
182
182
|
while (current.forward[i]) {
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { PriorityQueue } from './priority-queue';
|
|
9
8
|
import type { PriorityQueueOptions } from '../../types';
|
|
9
|
+
import { PriorityQueue } from './priority-queue';
|
|
10
10
|
export declare class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
11
11
|
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
|
|
12
12
|
}
|
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Kirk Qi
|
|
5
|
-
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
1
|
import { PriorityQueue } from './priority-queue';
|
|
9
2
|
export class MaxPriorityQueue extends PriorityQueue {
|
|
10
3
|
constructor(elements, options = {
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { PriorityQueue } from './priority-queue';
|
|
9
8
|
import type { PriorityQueueOptions } from '../../types';
|
|
9
|
+
import { PriorityQueue } from './priority-queue';
|
|
10
10
|
export declare class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
11
11
|
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
|
|
12
12
|
}
|
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Kirk Qi
|
|
5
|
-
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
1
|
import { PriorityQueue } from './priority-queue';
|
|
9
2
|
export class MinPriorityQueue extends PriorityQueue {
|
|
10
3
|
constructor(elements, options = {
|
|
@@ -5,8 +5,16 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
+
import type { PriorityQueueOptions } from '../../types';
|
|
8
9
|
import { Heap } from '../heap';
|
|
9
|
-
|
|
10
|
+
/**
|
|
11
|
+
* 1. Element Priority: In a PriorityQueue, elements are sorted according to their priority. Each dequeue (element removal) operation removes the element with the highest priority. The priority can be determined based on the natural ordering of the elements or through a provided comparator (Comparator).
|
|
12
|
+
* 2. Heap-Based Implementation: PriorityQueue is typically implemented using a binary heap, allowing both insertion and removal operations to be completed in O(log n) time, where n is the number of elements in the queue.
|
|
13
|
+
* 3. Task Scheduling: In systems where tasks need to be processed based on the urgency of tasks rather than the order of arrival.
|
|
14
|
+
* 4. Dijkstra's Algorithm: In shortest path algorithms for graphs, used to select the next shortest edge to visit.
|
|
15
|
+
* 5. Huffman Coding: Used to select the smallest node combination when constructing a Huffman tree.
|
|
16
|
+
* 6. Kth Largest Element in a Data Stream: Used to maintain a min-heap of size K for quickly finding the Kth largest element in stream data
|
|
17
|
+
*/
|
|
10
18
|
export declare class PriorityQueue<E = any> extends Heap<E> {
|
|
11
19
|
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
|
|
12
20
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
import { Heap } from '../heap';
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
3
|
+
* 1. Element Priority: In a PriorityQueue, elements are sorted according to their priority. Each dequeue (element removal) operation removes the element with the highest priority. The priority can be determined based on the natural ordering of the elements or through a provided comparator (Comparator).
|
|
4
|
+
* 2. Heap-Based Implementation: PriorityQueue is typically implemented using a binary heap, allowing both insertion and removal operations to be completed in O(log n) time, where n is the number of elements in the queue.
|
|
5
|
+
* 3. Task Scheduling: In systems where tasks need to be processed based on the urgency of tasks rather than the order of arrival.
|
|
6
|
+
* 4. Dijkstra's Algorithm: In shortest path algorithms for graphs, used to select the next shortest edge to visit.
|
|
7
|
+
* 5. Huffman Coding: Used to select the smallest node combination when constructing a Huffman tree.
|
|
8
|
+
* 6. Kth Largest Element in a Data Stream: Used to maintain a min-heap of size K for quickly finding the Kth largest element in stream data
|
|
7
9
|
*/
|
|
8
|
-
import { Heap } from '../heap';
|
|
9
10
|
export class PriorityQueue extends Heap {
|
|
10
11
|
constructor(elements, options) {
|
|
11
12
|
super(elements, options);
|
|
@@ -5,13 +5,14 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { ElementCallback, IterableWithSizeOrLength } from "../../types";
|
|
8
|
+
import type { ElementCallback, IterableWithSizeOrLength } from "../../types";
|
|
9
9
|
import { IterableElementBase } from "../base";
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
11
|
+
* 1. Operations at Both Ends: Supports adding and removing elements at both the front and back of the queue. This allows it to be used as a stack (last in, first out) and a queue (first in, first out).
|
|
12
|
+
* 2. Efficient Random Access: Being based on an array, it offers fast random access capability, allowing constant time access to any element.
|
|
13
|
+
* 3. Continuous Memory Allocation: Since it is based on an array, all elements are stored contiguously in memory, which can bring cache friendliness and efficient memory access.
|
|
14
|
+
* 4. Efficiency: Adding and removing elements at both ends of a deque is usually very fast. However, when the dynamic array needs to expand, it may involve copying the entire array to a larger one, and this operation has a time complexity of O(n).
|
|
15
|
+
* 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
|
|
15
16
|
*/
|
|
16
17
|
export declare class Deque<E> extends IterableElementBase<E> {
|
|
17
18
|
protected _bucketFirst: number;
|
|
@@ -41,68 +42,6 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
41
42
|
*/
|
|
42
43
|
get first(): E | undefined;
|
|
43
44
|
get last(): E | undefined;
|
|
44
|
-
/**
|
|
45
|
-
* Time Complexity: O(1) - Removes the last element.
|
|
46
|
-
* Space Complexity: O(1) - Operates in-place.
|
|
47
|
-
*/
|
|
48
|
-
isEmpty(): boolean;
|
|
49
|
-
/**
|
|
50
|
-
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
51
|
-
* Space Complexity: O(n) - Due to potential resizing.
|
|
52
|
-
*/
|
|
53
|
-
/**
|
|
54
|
-
* Time Complexity: O(1)
|
|
55
|
-
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
|
|
56
|
-
*
|
|
57
|
-
* The addLast function adds an element to the end of an array.
|
|
58
|
-
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
59
|
-
* data structure.
|
|
60
|
-
*/
|
|
61
|
-
addLast(element: E): void;
|
|
62
|
-
/**
|
|
63
|
-
* Time Complexity: O(1) - Removes the first element.
|
|
64
|
-
* Space Complexity: O(1) - In-place operation.
|
|
65
|
-
*/
|
|
66
|
-
/**
|
|
67
|
-
* Time Complexity: O(1) - Removes the last element.
|
|
68
|
-
* Space Complexity: O(1) - Operates in-place.
|
|
69
|
-
*
|
|
70
|
-
* The function "pollLast" removes and returns the last element of an array.
|
|
71
|
-
* @returns The last element of the array is being returned.
|
|
72
|
-
*/
|
|
73
|
-
pollLast(): E | undefined;
|
|
74
|
-
/**
|
|
75
|
-
* Time Complexity: O(1).
|
|
76
|
-
* Space Complexity: O(n) - Due to potential resizing.
|
|
77
|
-
*
|
|
78
|
-
* The "addFirst" function adds an element to the beginning of an array.
|
|
79
|
-
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
80
|
-
* beginning of the data structure.
|
|
81
|
-
*/
|
|
82
|
-
addFirst(element: E): void;
|
|
83
|
-
/**
|
|
84
|
-
* Time Complexity: O(1) - Removes the first element.
|
|
85
|
-
* Space Complexity: O(1) - In-place operation.
|
|
86
|
-
*
|
|
87
|
-
* The function "pollFirst" removes and returns the first element of an array.
|
|
88
|
-
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
89
|
-
* from the beginning. If the array is empty, it will return `undefined`.
|
|
90
|
-
*/
|
|
91
|
-
pollFirst(): E | undefined;
|
|
92
|
-
/**
|
|
93
|
-
* The clear() function resets the state of the object by initializing all variables to their default
|
|
94
|
-
* values.
|
|
95
|
-
*/
|
|
96
|
-
clear(): void;
|
|
97
|
-
/**
|
|
98
|
-
* The below function is a generator that yields elements from a collection one by one.
|
|
99
|
-
*/
|
|
100
|
-
begin(): Generator<E>;
|
|
101
|
-
/**
|
|
102
|
-
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
103
|
-
* the last element.
|
|
104
|
-
*/
|
|
105
|
-
reverseBegin(): Generator<E>;
|
|
106
45
|
/**
|
|
107
46
|
* Time Complexity - Amortized O(1) (possible reallocation)
|
|
108
47
|
* Space Complexity - O(n) (due to potential resizing).
|
|
@@ -116,7 +55,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
116
55
|
* structure.
|
|
117
56
|
* @returns The size of the data structure after the element has been pushed.
|
|
118
57
|
*/
|
|
119
|
-
push(element: E):
|
|
58
|
+
push(element: E): boolean;
|
|
120
59
|
/**
|
|
121
60
|
* Time Complexity: O(1)
|
|
122
61
|
* Space Complexity: O(1)
|
|
@@ -144,7 +83,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
144
83
|
* beginning of the data structure.
|
|
145
84
|
* @returns The size of the data structure after the element has been added.
|
|
146
85
|
*/
|
|
147
|
-
unshift(element: E):
|
|
86
|
+
unshift(element: E): boolean;
|
|
148
87
|
/**
|
|
149
88
|
* Time Complexity: O(1)
|
|
150
89
|
* Space Complexity: O(1)
|
|
@@ -159,6 +98,25 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
159
98
|
* returned.
|
|
160
99
|
*/
|
|
161
100
|
shift(): E | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* Time Complexity: O(1) - Removes the last element.
|
|
103
|
+
* Space Complexity: O(1) - Operates in-place.
|
|
104
|
+
*/
|
|
105
|
+
isEmpty(): boolean;
|
|
106
|
+
/**
|
|
107
|
+
* The clear() function resets the state of the object by initializing all variables to their default
|
|
108
|
+
* values.
|
|
109
|
+
*/
|
|
110
|
+
clear(): void;
|
|
111
|
+
/**
|
|
112
|
+
* The below function is a generator that yields elements from a collection one by one.
|
|
113
|
+
*/
|
|
114
|
+
begin(): Generator<E>;
|
|
115
|
+
/**
|
|
116
|
+
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
117
|
+
* the last element.
|
|
118
|
+
*/
|
|
119
|
+
reverseBegin(): Generator<E>;
|
|
162
120
|
/**
|
|
163
121
|
* Time Complexity: O(1)
|
|
164
122
|
* Space Complexity: O(1)
|
|
@@ -188,7 +146,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
188
146
|
* @param {E} element - The `element` parameter is the value that you want to set at the specified
|
|
189
147
|
* position in the data structure.
|
|
190
148
|
*/
|
|
191
|
-
setAt(pos: number, element: E):
|
|
149
|
+
setAt(pos: number, element: E): boolean;
|
|
192
150
|
/**
|
|
193
151
|
* Time Complexity: O(n)
|
|
194
152
|
* Space Complexity: O(n)
|
|
@@ -197,7 +155,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
197
155
|
* Time Complexity: O(n)
|
|
198
156
|
* Space Complexity: O(n)
|
|
199
157
|
*
|
|
200
|
-
* The `
|
|
158
|
+
* The `addAt` function inserts one or more elements at a specified position in an array-like data
|
|
201
159
|
* structure.
|
|
202
160
|
* @param {number} pos - The `pos` parameter represents the position at which the element(s) should
|
|
203
161
|
* be inserted. It is of type `number`.
|
|
@@ -208,7 +166,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
208
166
|
* will be inserted once. However, you can provide a different value for `num` if you want
|
|
209
167
|
* @returns The size of the array after the insertion is being returned.
|
|
210
168
|
*/
|
|
211
|
-
|
|
169
|
+
addAt(pos: number, element: E, num?: number): boolean;
|
|
212
170
|
/**
|
|
213
171
|
* Time Complexity: O(1)
|
|
214
172
|
* Space Complexity: O(1)
|
|
@@ -239,7 +197,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
239
197
|
* the index of the element to be deleted.
|
|
240
198
|
* @returns The size of the data structure after the deletion operation is performed.
|
|
241
199
|
*/
|
|
242
|
-
deleteAt(pos: number):
|
|
200
|
+
deleteAt(pos: number): boolean;
|
|
243
201
|
/**
|
|
244
202
|
* Time Complexity: O(n)
|
|
245
203
|
* Space Complexity: O(1)
|
|
@@ -254,7 +212,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
254
212
|
* the data structure.
|
|
255
213
|
* @returns The size of the data structure after the element has been deleted.
|
|
256
214
|
*/
|
|
257
|
-
delete(element: E):
|
|
215
|
+
delete(element: E): boolean;
|
|
258
216
|
/**
|
|
259
217
|
* Time Complexity: O(n)
|
|
260
218
|
* Space Complexity: O(1)
|
|
@@ -281,7 +239,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
281
239
|
* the number of unique elements.
|
|
282
240
|
* @returns The size of the modified array is being returned.
|
|
283
241
|
*/
|
|
284
|
-
unique():
|
|
242
|
+
unique(): this;
|
|
285
243
|
/**
|
|
286
244
|
* Time Complexity: O(n log n)
|
|
287
245
|
* Space Complexity: O(n)
|
|
@@ -294,7 +252,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
294
252
|
* @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
|
|
295
253
|
* `y` of type `E` and returns a number. The comparator function is used to determine the order of
|
|
296
254
|
* the elements in the sorted array.
|
|
297
|
-
* @returns
|
|
255
|
+
* @returns Deque<E>
|
|
298
256
|
*/
|
|
299
257
|
sort(comparator?: (x: E, y: E) => number): this;
|
|
300
258
|
/**
|
|
@@ -395,10 +353,48 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
395
353
|
*/
|
|
396
354
|
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Deque<T>;
|
|
397
355
|
/**
|
|
398
|
-
* Time Complexity: O(n)
|
|
399
|
-
* Space Complexity: O(n)
|
|
356
|
+
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
357
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
358
|
+
*/
|
|
359
|
+
/**
|
|
360
|
+
* Time Complexity: O(1)
|
|
361
|
+
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
|
|
362
|
+
*
|
|
363
|
+
* The addLast function adds an element to the end of an array.
|
|
364
|
+
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
365
|
+
* data structure.
|
|
366
|
+
*/
|
|
367
|
+
addLast(element: E): boolean;
|
|
368
|
+
/**
|
|
369
|
+
* Time Complexity: O(1) - Removes the first element.
|
|
370
|
+
* Space Complexity: O(1) - In-place operation.
|
|
371
|
+
*/
|
|
372
|
+
/**
|
|
373
|
+
* Time Complexity: O(1) - Removes the last element.
|
|
374
|
+
* Space Complexity: O(1) - Operates in-place.
|
|
375
|
+
*
|
|
376
|
+
* The function "pollLast" removes and returns the last element of an array.
|
|
377
|
+
* @returns The last element of the array is being returned.
|
|
378
|
+
*/
|
|
379
|
+
pollLast(): E | undefined;
|
|
380
|
+
/**
|
|
381
|
+
* Time Complexity: O(1).
|
|
382
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
383
|
+
*
|
|
384
|
+
* The "addFirst" function adds an element to the beginning of an array.
|
|
385
|
+
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
386
|
+
* beginning of the data structure.
|
|
400
387
|
*/
|
|
401
|
-
|
|
388
|
+
addFirst(element: E): boolean;
|
|
389
|
+
/**
|
|
390
|
+
* Time Complexity: O(1) - Removes the first element.
|
|
391
|
+
* Space Complexity: O(1) - In-place operation.
|
|
392
|
+
*
|
|
393
|
+
* The function "pollFirst" removes and returns the first element of an array.
|
|
394
|
+
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
395
|
+
* from the beginning. If the array is empty, it will return `undefined`.
|
|
396
|
+
*/
|
|
397
|
+
pollFirst(): E | undefined;
|
|
402
398
|
/**
|
|
403
399
|
* Time Complexity: O(n)
|
|
404
400
|
* Space Complexity: O(1)
|
|
@@ -406,7 +402,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
406
402
|
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
407
403
|
* object to be iterated over using a for...of loop.
|
|
408
404
|
*/
|
|
409
|
-
protected _getIterator():
|
|
405
|
+
protected _getIterator(): IterableIterator<E>;
|
|
410
406
|
/**
|
|
411
407
|
* Time Complexity: O(n)
|
|
412
408
|
* Space Complexity: O(n)
|