data-structure-typed 1.50.4 → 1.50.5
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 +116 -55
- package/SPECIFICATION.md +2 -2
- package/SPECIFICATION_zh-CN.md +81 -0
- package/{SPONSOR-zh-CN.md → SPONSOR_zh-CN.md} +1 -1
- package/benchmark/report.html +24 -24
- package/benchmark/report.json +242 -242
- package/dist/cjs/data-structures/base/iterable-base.d.ts +10 -8
- package/dist/cjs/data-structures/base/iterable-base.js +8 -12
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +14 -76
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +16 -86
- 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 +27 -69
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +35 -79
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +0 -53
- package/dist/cjs/data-structures/queue/deque.js +0 -61
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +0 -70
- package/dist/cjs/data-structures/queue/queue.js +0 -87
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +10 -8
- package/dist/mjs/data-structures/base/iterable-base.js +8 -12
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/mjs/data-structures/graph/abstract-graph.js +3 -0
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +14 -76
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +16 -86
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +27 -69
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +33 -79
- package/dist/mjs/data-structures/queue/deque.d.ts +0 -53
- package/dist/mjs/data-structures/queue/deque.js +0 -61
- package/dist/mjs/data-structures/queue/queue.d.ts +0 -70
- package/dist/mjs/data-structures/queue/queue.js +0 -86
- package/dist/umd/data-structure-typed.js +62 -325
- 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 +14 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -0
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +16 -94
- package/src/data-structures/linked-list/singly-linked-list.ts +35 -87
- package/src/data-structures/queue/deque.ts +0 -67
- package/src/data-structures/queue/queue.ts +0 -98
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +3 -3
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +3 -3
- package/test/performance/data-structures/hash/hash-map.test.ts +6 -6
- package/test/performance/data-structures/heap/heap.test.ts +14 -14
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +11 -6
- package/test/performance/data-structures/queue/deque.test.ts +8 -8
- package/test/performance/data-structures/queue/queue.test.ts +5 -12
- package/test/performance/reportor.ts +43 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +6 -6
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +10 -10
- package/test/unit/data-structures/linked-list/skip-list.test.ts +4 -4
- package/test/unit/data-structures/queue/deque.test.ts +26 -26
- package/test/unit/data-structures/queue/queue.test.ts +20 -20
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.50.
|
|
3
|
+
"version": "1.50.5",
|
|
4
4
|
"description": "Javascript Data Structure. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
@@ -6,6 +6,8 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
6
6
|
* Space Complexity: O(1)
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
+
abstract get size(): number;
|
|
10
|
+
|
|
9
11
|
/**
|
|
10
12
|
* Time Complexity: O(n)
|
|
11
13
|
* Space Complexity: O(1)
|
|
@@ -125,6 +127,11 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
125
127
|
return false;
|
|
126
128
|
}
|
|
127
129
|
|
|
130
|
+
/**
|
|
131
|
+
* Time Complexity: O(n)
|
|
132
|
+
* Space Complexity: O(1)
|
|
133
|
+
*/
|
|
134
|
+
|
|
128
135
|
/**
|
|
129
136
|
* Time Complexity: O(n)
|
|
130
137
|
* Space Complexity: O(1)
|
|
@@ -248,11 +255,6 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
248
255
|
return;
|
|
249
256
|
}
|
|
250
257
|
|
|
251
|
-
/**
|
|
252
|
-
* Time Complexity: O(n)
|
|
253
|
-
* Space Complexity: O(1)
|
|
254
|
-
*/
|
|
255
|
-
|
|
256
258
|
/**
|
|
257
259
|
* Time Complexity: O(n)
|
|
258
260
|
* Space Complexity: O(1)
|
|
@@ -301,6 +303,8 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
301
303
|
}
|
|
302
304
|
|
|
303
305
|
export abstract class IterableElementBase<E = any, C = any> {
|
|
306
|
+
abstract get size(): number;
|
|
307
|
+
|
|
304
308
|
/**
|
|
305
309
|
* Time Complexity: O(n)
|
|
306
310
|
* Space Complexity: O(1)
|
|
@@ -362,6 +366,11 @@ export abstract class IterableElementBase<E = any, C = any> {
|
|
|
362
366
|
return true;
|
|
363
367
|
}
|
|
364
368
|
|
|
369
|
+
/**
|
|
370
|
+
* Time Complexity: O(n)
|
|
371
|
+
* Space Complexity: O(1)
|
|
372
|
+
*/
|
|
373
|
+
|
|
365
374
|
/**
|
|
366
375
|
* Time Complexity: O(n)
|
|
367
376
|
* Space Complexity: O(1)
|
|
@@ -445,11 +454,6 @@ export abstract class IterableElementBase<E = any, C = any> {
|
|
|
445
454
|
return;
|
|
446
455
|
}
|
|
447
456
|
|
|
448
|
-
/**
|
|
449
|
-
* Time Complexity: O(n)
|
|
450
|
-
* Space Complexity: O(1)
|
|
451
|
-
*/
|
|
452
|
-
|
|
453
457
|
/**
|
|
454
458
|
* Time Complexity: O(n)
|
|
455
459
|
* Space Complexity: O(1)
|
|
@@ -82,6 +82,10 @@ export abstract class AbstractGraph<
|
|
|
82
82
|
this._vertexMap = v;
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
+
get size(): number {
|
|
86
|
+
return this._vertexMap.size;
|
|
87
|
+
}
|
|
88
|
+
|
|
85
89
|
/**
|
|
86
90
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
87
91
|
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
@@ -200,7 +200,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
200
200
|
* @param element - the element to check.
|
|
201
201
|
* @returns Returns true if the specified element is contained; otherwise, returns false.
|
|
202
202
|
*/
|
|
203
|
-
has(element: E): boolean {
|
|
203
|
+
override has(element: E): boolean {
|
|
204
204
|
return this.elements.includes(element);
|
|
205
205
|
}
|
|
206
206
|
|
|
@@ -194,14 +194,13 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
194
194
|
*/
|
|
195
195
|
|
|
196
196
|
/**
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
* The push
|
|
201
|
-
* @param {E} value - The value to be added to the linked list.
|
|
197
|
+
* The push function adds a new element to the end of a doubly linked list.
|
|
198
|
+
* @param {E} element - The "element" parameter represents the value that you want to add to the
|
|
199
|
+
* doubly linked list.
|
|
200
|
+
* @returns The `push` method is returning a boolean value, `true`.
|
|
202
201
|
*/
|
|
203
|
-
push(
|
|
204
|
-
const newNode = new DoublyLinkedListNode(
|
|
202
|
+
push(element: E): boolean {
|
|
203
|
+
const newNode = new DoublyLinkedListNode(element);
|
|
205
204
|
if (!this.head) {
|
|
206
205
|
this._head = newNode;
|
|
207
206
|
this._tail = newNode;
|
|
@@ -220,12 +219,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
220
219
|
*/
|
|
221
220
|
|
|
222
221
|
/**
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
|
|
227
|
-
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
228
|
-
* list is empty, it returns undefined.
|
|
222
|
+
* The `pop()` function removes and returns the value of the last element in a linked list.
|
|
223
|
+
* @returns The method is returning the value of the removed node.
|
|
229
224
|
*/
|
|
230
225
|
pop(): E | undefined {
|
|
231
226
|
if (!this.tail) return undefined;
|
|
@@ -247,12 +242,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
247
242
|
*/
|
|
248
243
|
|
|
249
244
|
/**
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
* The `shift()` function removes and returns the value of the first node in a doubly linked list.
|
|
254
|
-
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
255
|
-
* list.
|
|
245
|
+
* The `shift()` function removes and returns the value of the first element in a doubly linked list.
|
|
246
|
+
* @returns The value of the removed node.
|
|
256
247
|
*/
|
|
257
248
|
shift(): E | undefined {
|
|
258
249
|
if (!this.head) return undefined;
|
|
@@ -274,15 +265,13 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
274
265
|
*/
|
|
275
266
|
|
|
276
267
|
/**
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
* The unshift
|
|
281
|
-
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
282
|
-
* doubly linked list.
|
|
268
|
+
* The unshift function adds a new element to the beginning of a doubly linked list.
|
|
269
|
+
* @param {E} element - The "element" parameter represents the value of the element that you want to
|
|
270
|
+
* add to the beginning of the doubly linked list.
|
|
271
|
+
* @returns The `unshift` method is returning a boolean value, `true`.
|
|
283
272
|
*/
|
|
284
|
-
unshift(
|
|
285
|
-
const newNode = new DoublyLinkedListNode(
|
|
273
|
+
unshift(element: E): boolean {
|
|
274
|
+
const newNode = new DoublyLinkedListNode(element);
|
|
286
275
|
if (!this.head) {
|
|
287
276
|
this._head = newNode;
|
|
288
277
|
this._tail = newNode;
|
|
@@ -811,73 +800,6 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
811
800
|
return mappedList;
|
|
812
801
|
}
|
|
813
802
|
|
|
814
|
-
/**
|
|
815
|
-
* Time Complexity: O(1)
|
|
816
|
-
* Space Complexity: O(1)
|
|
817
|
-
*/
|
|
818
|
-
|
|
819
|
-
/**
|
|
820
|
-
* Time Complexity: O(1)
|
|
821
|
-
* Space Complexity: O(1)
|
|
822
|
-
*
|
|
823
|
-
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
824
|
-
* @param {E} value - The value to be added to the linked list.
|
|
825
|
-
*/
|
|
826
|
-
addLast(value: E): boolean {
|
|
827
|
-
return this.push(value);
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
/**
|
|
831
|
-
* Time Complexity: O(1)
|
|
832
|
-
* Space Complexity: O(1)
|
|
833
|
-
*/
|
|
834
|
-
|
|
835
|
-
/**
|
|
836
|
-
* Time Complexity: O(1)
|
|
837
|
-
* Space Complexity: O(1)
|
|
838
|
-
*
|
|
839
|
-
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
840
|
-
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
841
|
-
* list is empty, it returns undefined.
|
|
842
|
-
*/
|
|
843
|
-
pollLast(): E | undefined {
|
|
844
|
-
return this.pop();
|
|
845
|
-
}
|
|
846
|
-
|
|
847
|
-
/**
|
|
848
|
-
* Time Complexity: O(1)
|
|
849
|
-
* Space Complexity: O(1)
|
|
850
|
-
*/
|
|
851
|
-
|
|
852
|
-
/**
|
|
853
|
-
* Time Complexity: O(1)
|
|
854
|
-
* Space Complexity: O(1)
|
|
855
|
-
*
|
|
856
|
-
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
857
|
-
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
858
|
-
* list.
|
|
859
|
-
*/
|
|
860
|
-
pollFirst(): E | undefined {
|
|
861
|
-
return this.shift();
|
|
862
|
-
}
|
|
863
|
-
|
|
864
|
-
/**
|
|
865
|
-
* Time Complexity: O(1)
|
|
866
|
-
* Space Complexity: O(1)
|
|
867
|
-
*/
|
|
868
|
-
|
|
869
|
-
/**
|
|
870
|
-
* Time Complexity: O(1)
|
|
871
|
-
* Space Complexity: O(1)
|
|
872
|
-
*
|
|
873
|
-
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
874
|
-
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
875
|
-
* doubly linked list.
|
|
876
|
-
*/
|
|
877
|
-
addFirst(value: E): void {
|
|
878
|
-
this.unshift(value);
|
|
879
|
-
}
|
|
880
|
-
|
|
881
803
|
/**
|
|
882
804
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
883
805
|
*/
|
|
@@ -93,6 +93,24 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
93
93
|
return this._tail;
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
/**
|
|
97
|
+
* The above function returns the value of the first element in a linked list, or undefined if the
|
|
98
|
+
* list is empty.
|
|
99
|
+
* @returns The value of the first node in the linked list, or undefined if the linked list is empty.
|
|
100
|
+
*/
|
|
101
|
+
get first(): E | undefined {
|
|
102
|
+
return this.head?.value;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The function returns the value of the last element in a linked list, or undefined if the list is
|
|
107
|
+
* empty.
|
|
108
|
+
* @returns The value of the last node in the linked list, or undefined if the linked list is empty.
|
|
109
|
+
*/
|
|
110
|
+
get last(): E | undefined {
|
|
111
|
+
return this.tail?.value;
|
|
112
|
+
}
|
|
113
|
+
|
|
96
114
|
protected _size: number = 0;
|
|
97
115
|
|
|
98
116
|
/**
|
|
@@ -130,20 +148,19 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
130
148
|
/**
|
|
131
149
|
* Time Complexity: O(1)
|
|
132
150
|
* Space Complexity: O(1)
|
|
133
|
-
* Constant time, as it involves basic pointer adjustments.
|
|
134
|
-
* Constant space, as it only creates a new node.
|
|
135
151
|
*/
|
|
136
152
|
|
|
137
153
|
/**
|
|
138
154
|
* Time Complexity: O(1)
|
|
139
155
|
* Space Complexity: O(1)
|
|
140
156
|
*
|
|
141
|
-
* The
|
|
142
|
-
* @param {E}
|
|
143
|
-
*
|
|
157
|
+
* The push function adds a new element to the end of a singly linked list.
|
|
158
|
+
* @param {E} element - The "element" parameter represents the value of the element that you want to
|
|
159
|
+
* add to the linked list.
|
|
160
|
+
* @returns The `push` method is returning a boolean value, `true`.
|
|
144
161
|
*/
|
|
145
|
-
push(
|
|
146
|
-
const newNode = new SinglyLinkedListNode(
|
|
162
|
+
push(element: E): boolean {
|
|
163
|
+
const newNode = new SinglyLinkedListNode(element);
|
|
147
164
|
if (!this.head) {
|
|
148
165
|
this._head = newNode;
|
|
149
166
|
this._tail = newNode;
|
|
@@ -155,23 +172,6 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
155
172
|
return true;
|
|
156
173
|
}
|
|
157
174
|
|
|
158
|
-
/**
|
|
159
|
-
* Time Complexity: O(1)
|
|
160
|
-
* Space Complexity: O(1)
|
|
161
|
-
*/
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Time Complexity: O(1)
|
|
165
|
-
* Space Complexity: O(1)
|
|
166
|
-
*
|
|
167
|
-
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
168
|
-
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
169
|
-
* any type (E) as specified in the generic type declaration of the class or function.
|
|
170
|
-
*/
|
|
171
|
-
addLast(value: E): boolean {
|
|
172
|
-
return this.push(value);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
175
|
/**
|
|
176
176
|
* Time Complexity: O(n)
|
|
177
177
|
* Space Complexity: O(1)
|
|
@@ -182,10 +182,9 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
182
182
|
* Time Complexity: O(n)
|
|
183
183
|
* Space Complexity: O(1)
|
|
184
184
|
*
|
|
185
|
-
* The `pop
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
* the linked list is empty, it returns `undefined`.
|
|
185
|
+
* The `pop` function removes and returns the value of the last element in a linked list.
|
|
186
|
+
* @returns The method is returning the value of the element that is being popped from the end of the
|
|
187
|
+
* list.
|
|
189
188
|
*/
|
|
190
189
|
pop(): E | undefined {
|
|
191
190
|
if (!this.head) return undefined;
|
|
@@ -208,24 +207,6 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
208
207
|
return value;
|
|
209
208
|
}
|
|
210
209
|
|
|
211
|
-
/**
|
|
212
|
-
* Time Complexity: O(n)
|
|
213
|
-
* Space Complexity: O(1)
|
|
214
|
-
*/
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Time Complexity: O(n)
|
|
218
|
-
* Space Complexity: O(1)
|
|
219
|
-
*
|
|
220
|
-
* The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
221
|
-
* pointers accordingly.
|
|
222
|
-
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
223
|
-
* the linked list is empty, it returns `undefined`.
|
|
224
|
-
*/
|
|
225
|
-
pollLast(): E | undefined {
|
|
226
|
-
return this.pop();
|
|
227
|
-
}
|
|
228
|
-
|
|
229
210
|
/**
|
|
230
211
|
* Time Complexity: O(1)
|
|
231
212
|
* Space Complexity: O(1)
|
|
@@ -235,8 +216,8 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
235
216
|
* Time Complexity: O(1)
|
|
236
217
|
* Space Complexity: O(1)
|
|
237
218
|
*
|
|
238
|
-
* The `shift()` function removes and returns the value of the first
|
|
239
|
-
* @returns The value of the
|
|
219
|
+
* The `shift()` function removes and returns the value of the first element in a linked list.
|
|
220
|
+
* @returns The value of the removed node.
|
|
240
221
|
*/
|
|
241
222
|
shift(): E | undefined {
|
|
242
223
|
if (!this.head) return undefined;
|
|
@@ -255,28 +236,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
255
236
|
* Time Complexity: O(1)
|
|
256
237
|
* Space Complexity: O(1)
|
|
257
238
|
*
|
|
258
|
-
* The
|
|
259
|
-
* @
|
|
239
|
+
* The unshift function adds a new element to the beginning of a singly linked list.
|
|
240
|
+
* @param {E} element - The "element" parameter represents the value of the element that you want to
|
|
241
|
+
* add to the beginning of the singly linked list.
|
|
242
|
+
* @returns The `unshift` method is returning a boolean value, `true`.
|
|
260
243
|
*/
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Time Complexity: O(1)
|
|
267
|
-
* Space Complexity: O(1)
|
|
268
|
-
*/
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* Time Complexity: O(1)
|
|
272
|
-
* Space Complexity: O(1)
|
|
273
|
-
*
|
|
274
|
-
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
275
|
-
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
276
|
-
* linked list.
|
|
277
|
-
*/
|
|
278
|
-
unshift(value: E): boolean {
|
|
279
|
-
const newNode = new SinglyLinkedListNode(value);
|
|
244
|
+
unshift(element: E): boolean {
|
|
245
|
+
const newNode = new SinglyLinkedListNode(element);
|
|
280
246
|
if (!this.head) {
|
|
281
247
|
this._head = newNode;
|
|
282
248
|
this._tail = newNode;
|
|
@@ -288,27 +254,9 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
288
254
|
return true;
|
|
289
255
|
}
|
|
290
256
|
|
|
291
|
-
/**
|
|
292
|
-
* Time Complexity: O(1)
|
|
293
|
-
* Space Complexity: O(1)
|
|
294
|
-
*/
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Time Complexity: O(1)
|
|
298
|
-
* Space Complexity: O(1)
|
|
299
|
-
*
|
|
300
|
-
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
301
|
-
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
302
|
-
* linked list.
|
|
303
|
-
*/
|
|
304
|
-
addFirst(value: E): boolean {
|
|
305
|
-
return this.unshift(value);
|
|
306
|
-
}
|
|
307
|
-
|
|
308
257
|
/**
|
|
309
258
|
* Time Complexity: O(n)
|
|
310
259
|
* Space Complexity: O(1)
|
|
311
|
-
* Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
312
260
|
*/
|
|
313
261
|
|
|
314
262
|
/**
|
|
@@ -811,73 +811,6 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
811
811
|
return newDeque;
|
|
812
812
|
}
|
|
813
813
|
|
|
814
|
-
/**
|
|
815
|
-
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
816
|
-
* Space Complexity: O(n) - Due to potential resizing.
|
|
817
|
-
*/
|
|
818
|
-
|
|
819
|
-
/**
|
|
820
|
-
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
821
|
-
* Space Complexity: O(n) - Due to potential resizing.
|
|
822
|
-
*
|
|
823
|
-
* The addLast function adds an element to the end of an array.
|
|
824
|
-
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
825
|
-
* data structure.
|
|
826
|
-
*/
|
|
827
|
-
addLast(element: E): boolean {
|
|
828
|
-
return this.push(element);
|
|
829
|
-
}
|
|
830
|
-
|
|
831
|
-
/**
|
|
832
|
-
* Time Complexity: O(1)
|
|
833
|
-
* Space Complexity: O(1)
|
|
834
|
-
*/
|
|
835
|
-
|
|
836
|
-
/**
|
|
837
|
-
* Time Complexity: O(1)
|
|
838
|
-
* Space Complexity: O(1)
|
|
839
|
-
*
|
|
840
|
-
* The function "pollLast" removes and returns the last element of an array.
|
|
841
|
-
* @returns The last element of the array is being returned.
|
|
842
|
-
*/
|
|
843
|
-
pollLast(): E | undefined {
|
|
844
|
-
return this.pop();
|
|
845
|
-
}
|
|
846
|
-
|
|
847
|
-
/**
|
|
848
|
-
* Time Complexity: O(1)
|
|
849
|
-
* Space Complexity: O(1)
|
|
850
|
-
* /
|
|
851
|
-
|
|
852
|
-
/**
|
|
853
|
-
* Time Complexity: O(1)
|
|
854
|
-
* Space Complexity: O(1)
|
|
855
|
-
*
|
|
856
|
-
* The "addFirst" function adds an element to the beginning of an array.
|
|
857
|
-
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
858
|
-
* beginning of the data structure.
|
|
859
|
-
*/
|
|
860
|
-
addFirst(element: E): boolean {
|
|
861
|
-
return this.unshift(element);
|
|
862
|
-
}
|
|
863
|
-
|
|
864
|
-
/**
|
|
865
|
-
* Time Complexity: O(1)
|
|
866
|
-
* Space Complexity: O(1)
|
|
867
|
-
* /
|
|
868
|
-
|
|
869
|
-
/**
|
|
870
|
-
* Time Complexity: O(1)
|
|
871
|
-
* Space Complexity: O(1)
|
|
872
|
-
*
|
|
873
|
-
* The function "pollFirst" removes and returns the first element of an array.
|
|
874
|
-
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
875
|
-
* from the beginning. If the array is empty, it will return `undefined`.
|
|
876
|
-
*/
|
|
877
|
-
pollFirst(): E | undefined {
|
|
878
|
-
return this.shift();
|
|
879
|
-
}
|
|
880
|
-
|
|
881
814
|
/**
|
|
882
815
|
* Time Complexity: O(n)
|
|
883
816
|
* Space Complexity: O(1)
|
|
@@ -178,72 +178,6 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
178
178
|
return spliced.length === 1;
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
-
/**
|
|
182
|
-
* Time Complexity: O(1)
|
|
183
|
-
* Space Complexity: O(1)
|
|
184
|
-
*/
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Time Complexity: O(1)
|
|
188
|
-
* Space Complexity: O(1)
|
|
189
|
-
*
|
|
190
|
-
* The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
|
|
191
|
-
* @returns The `peek()` method returns the first element of the data structure, represented by the `_elements` array at
|
|
192
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
193
|
-
*/
|
|
194
|
-
peek(): E | undefined {
|
|
195
|
-
return this.first;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Time Complexity: O(1)
|
|
200
|
-
* Space Complexity: O(1)
|
|
201
|
-
*/
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Time Complexity: O(1)
|
|
205
|
-
* Space Complexity: O(1)
|
|
206
|
-
*
|
|
207
|
-
* The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
208
|
-
* @returns The method `peekLast()` returns the last element of the `_elements` array if the array is not empty. If the
|
|
209
|
-
* array is empty, it returns `undefined`.
|
|
210
|
-
*/
|
|
211
|
-
peekLast(): E | undefined {
|
|
212
|
-
return this.last;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Time Complexity: O(1)
|
|
217
|
-
* Space Complexity: O(1)
|
|
218
|
-
*/
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Time Complexity: O(1)
|
|
222
|
-
* Space Complexity: O(1)
|
|
223
|
-
*
|
|
224
|
-
* The enqueue function adds a value to the end of a queue.
|
|
225
|
-
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
226
|
-
*/
|
|
227
|
-
enqueue(value: E): boolean {
|
|
228
|
-
return this.push(value);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Time Complexity: O(1)
|
|
233
|
-
* Space Complexity: O(1)
|
|
234
|
-
*/
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Time Complexity: O(1)
|
|
238
|
-
* Space Complexity: O(1)
|
|
239
|
-
*
|
|
240
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
241
|
-
* @returns The method is returning a value of type E or undefined.
|
|
242
|
-
*/
|
|
243
|
-
dequeue(): E | undefined {
|
|
244
|
-
return this.shift();
|
|
245
|
-
}
|
|
246
|
-
|
|
247
181
|
/**
|
|
248
182
|
* Time Complexity: O(1)
|
|
249
183
|
* Space Complexity: O(1)
|
|
@@ -411,38 +345,6 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
411
345
|
* 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.
|
|
412
346
|
*/
|
|
413
347
|
export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
414
|
-
/**
|
|
415
|
-
* The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
416
|
-
* @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
417
|
-
*/
|
|
418
|
-
get first(): E | undefined {
|
|
419
|
-
return this.head?.value;
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
/**
|
|
423
|
-
* The enqueue function adds a value to the end of an array.
|
|
424
|
-
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
425
|
-
*/
|
|
426
|
-
enqueue(value: E): boolean {
|
|
427
|
-
return this.push(value);
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
/**
|
|
431
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
432
|
-
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
433
|
-
*/
|
|
434
|
-
dequeue(): E | undefined {
|
|
435
|
-
return this.shift();
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
/**
|
|
439
|
-
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
440
|
-
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
441
|
-
*/
|
|
442
|
-
peek(): E | undefined {
|
|
443
|
-
return this.first;
|
|
444
|
-
}
|
|
445
|
-
|
|
446
348
|
/**
|
|
447
349
|
* Time Complexity: O(n)
|
|
448
350
|
* Space Complexity: O(n)
|
|
@@ -12,6 +12,9 @@ suite
|
|
|
12
12
|
avl.clear();
|
|
13
13
|
for (let i = 0; i < arr.length; i++) avl.add(arr[i]);
|
|
14
14
|
})
|
|
15
|
+
.add(`${TEN_THOUSAND.toLocaleString()} get`, () => {
|
|
16
|
+
for (let i = 0; i < arr.length; i++) avl.get(arr[i]);
|
|
17
|
+
})
|
|
15
18
|
.add(`${TEN_THOUSAND.toLocaleString()} add & delete randomly`, () => {
|
|
16
19
|
avl.clear();
|
|
17
20
|
for (let i = 0; i < arr.length; i++) avl.add(arr[i]);
|
|
@@ -20,9 +23,6 @@ suite
|
|
|
20
23
|
.add(`${TEN_THOUSAND.toLocaleString()} addMany`, () => {
|
|
21
24
|
avl.clear();
|
|
22
25
|
avl.addMany(arr);
|
|
23
|
-
})
|
|
24
|
-
.add(`${TEN_THOUSAND.toLocaleString()} get`, () => {
|
|
25
|
-
for (let i = 0; i < arr.length; i++) avl.get(arr[i]);
|
|
26
26
|
});
|
|
27
27
|
|
|
28
28
|
export { suite };
|
|
@@ -25,13 +25,13 @@ suite
|
|
|
25
25
|
avlTree.clear();
|
|
26
26
|
for (let i = 0; i < arr.length; i++) avlTree.add(arr[i]);
|
|
27
27
|
})
|
|
28
|
+
.add(`${TEN_THOUSAND.toLocaleString()} AVLTree get`, () => {
|
|
29
|
+
for (let i = 0; i < arr.length; i++) avlTree.get(arr[i]);
|
|
30
|
+
})
|
|
28
31
|
.add(`${TEN_THOUSAND.toLocaleString()} AVLTree add & delete randomly`, () => {
|
|
29
32
|
avlTree.clear();
|
|
30
33
|
for (let i = 0; i < arr.length; i++) avlTree.add(arr[i]);
|
|
31
34
|
for (let i = 0; i < arr.length; i++) avlTree.delete(arr[i]);
|
|
32
|
-
})
|
|
33
|
-
.add(`${TEN_THOUSAND.toLocaleString()} AVLTree get`, () => {
|
|
34
|
-
for (let i = 0; i < arr.length; i++) avlTree.get(arr[i]);
|
|
35
35
|
});
|
|
36
36
|
|
|
37
37
|
export { suite };
|