min-heap-typed 1.42.8 → 1.43.0
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/binary-tree/avl-tree.d.ts +88 -23
- package/dist/data-structures/binary-tree/avl-tree.js +88 -23
- package/dist/data-structures/binary-tree/binary-tree.d.ts +180 -74
- package/dist/data-structures/binary-tree/binary-tree.js +415 -236
- package/dist/data-structures/binary-tree/bst.d.ts +121 -66
- package/dist/data-structures/binary-tree/bst.js +121 -67
- package/dist/data-structures/binary-tree/rb-tree.d.ts +72 -5
- package/dist/data-structures/binary-tree/rb-tree.js +95 -18
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +82 -43
- package/dist/data-structures/binary-tree/tree-multimap.js +82 -43
- package/dist/data-structures/graph/abstract-graph.d.ts +139 -36
- package/dist/data-structures/graph/abstract-graph.js +147 -36
- package/dist/data-structures/graph/directed-graph.d.ts +126 -0
- package/dist/data-structures/graph/directed-graph.js +126 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/data-structures/graph/undirected-graph.js +63 -0
- package/dist/data-structures/heap/heap.d.ts +175 -12
- package/dist/data-structures/heap/heap.js +175 -12
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +203 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +182 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +182 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +64 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +64 -0
- package/dist/data-structures/queue/deque.d.ts +113 -3
- package/dist/data-structures/queue/deque.js +113 -3
- package/dist/data-structures/queue/queue.d.ts +87 -0
- package/dist/data-structures/queue/queue.js +87 -0
- package/dist/data-structures/stack/stack.d.ts +42 -0
- package/dist/data-structures/stack/stack.js +42 -0
- package/dist/data-structures/trie/trie.d.ts +76 -0
- package/dist/data-structures/trie/trie.js +76 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +97 -23
- package/src/data-structures/binary-tree/binary-tree.ts +465 -256
- package/src/data-structures/binary-tree/bst.ts +130 -68
- package/src/data-structures/binary-tree/rb-tree.ts +106 -19
- package/src/data-structures/binary-tree/tree-multimap.ts +88 -44
- package/src/data-structures/graph/abstract-graph.ts +133 -7
- package/src/data-structures/graph/directed-graph.ts +145 -1
- package/src/data-structures/graph/undirected-graph.ts +72 -0
- package/src/data-structures/heap/heap.ts +201 -12
- package/src/data-structures/linked-list/doubly-linked-list.ts +232 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +208 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +74 -0
- package/src/data-structures/queue/deque.ts +127 -3
- package/src/data-structures/queue/queue.ts +99 -0
- package/src/data-structures/stack/stack.ts +48 -0
- package/src/data-structures/trie/trie.ts +87 -4
|
@@ -39,6 +39,13 @@ class SinglyLinkedList {
|
|
|
39
39
|
return this._length;
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
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.
|
|
43
|
+
* Space Complexity: O(n) - Linear space, as it creates a new node for each element in the array.
|
|
44
|
+
*/
|
|
45
|
+
/**
|
|
46
|
+
* 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.
|
|
47
|
+
* Space Complexity: O(n) - Linear space, as it creates a new node for each element in the array.
|
|
48
|
+
*
|
|
42
49
|
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
43
50
|
* array.
|
|
44
51
|
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
@@ -52,6 +59,13 @@ class SinglyLinkedList {
|
|
|
52
59
|
return singlyLinkedList;
|
|
53
60
|
}
|
|
54
61
|
/**
|
|
62
|
+
* Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
|
|
63
|
+
* Space Complexity: O(1) - Constant space, as it only creates a new node.
|
|
64
|
+
*/
|
|
65
|
+
/**
|
|
66
|
+
* Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
|
|
67
|
+
* Space Complexity: O(1) - Constant space, as it only creates a new node.
|
|
68
|
+
*
|
|
55
69
|
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
56
70
|
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
57
71
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
@@ -69,6 +83,13 @@ class SinglyLinkedList {
|
|
|
69
83
|
this._length++;
|
|
70
84
|
}
|
|
71
85
|
/**
|
|
86
|
+
* Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
|
|
87
|
+
* Space Complexity: O(1) - Constant space, as it only creates a new node.
|
|
88
|
+
*/
|
|
89
|
+
/**
|
|
90
|
+
* Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
|
|
91
|
+
* Space Complexity: O(1) - Constant space, as it only creates a new node.
|
|
92
|
+
*
|
|
72
93
|
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
73
94
|
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
74
95
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
@@ -77,6 +98,13 @@ class SinglyLinkedList {
|
|
|
77
98
|
this.push(value);
|
|
78
99
|
}
|
|
79
100
|
/**
|
|
101
|
+
* Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
|
|
102
|
+
* Space Complexity: O(1) - Constant space.
|
|
103
|
+
*/
|
|
104
|
+
/**
|
|
105
|
+
* Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
|
|
106
|
+
* Space Complexity: O(1) - Constant space.
|
|
107
|
+
*
|
|
80
108
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
81
109
|
* pointers accordingly.
|
|
82
110
|
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
@@ -103,6 +131,13 @@ class SinglyLinkedList {
|
|
|
103
131
|
return value;
|
|
104
132
|
}
|
|
105
133
|
/**
|
|
134
|
+
* Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
|
|
135
|
+
* Space Complexity: O(1) - Constant space.
|
|
136
|
+
*/
|
|
137
|
+
/**
|
|
138
|
+
* Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
|
|
139
|
+
* Space Complexity: O(1) - Constant space.
|
|
140
|
+
*
|
|
106
141
|
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
107
142
|
* pointers accordingly.
|
|
108
143
|
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
@@ -112,6 +147,13 @@ class SinglyLinkedList {
|
|
|
112
147
|
return this.pop();
|
|
113
148
|
}
|
|
114
149
|
/**
|
|
150
|
+
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
|
|
151
|
+
* Space Complexity: O(1) - Constant space.
|
|
152
|
+
*/
|
|
153
|
+
/**
|
|
154
|
+
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
|
|
155
|
+
* Space Complexity: O(1) - Constant space.
|
|
156
|
+
*
|
|
115
157
|
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
116
158
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
117
159
|
*/
|
|
@@ -124,6 +166,13 @@ class SinglyLinkedList {
|
|
|
124
166
|
return removedNode.value;
|
|
125
167
|
}
|
|
126
168
|
/**
|
|
169
|
+
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
|
|
170
|
+
* Space Complexity: O(1) - Constant space.
|
|
171
|
+
*/
|
|
172
|
+
/**
|
|
173
|
+
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
|
|
174
|
+
* Space Complexity: O(1) - Constant space.
|
|
175
|
+
*
|
|
127
176
|
* The `popFirst()` function removes and returns the value of the first node in a linked list.
|
|
128
177
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
129
178
|
*/
|
|
@@ -131,6 +180,13 @@ class SinglyLinkedList {
|
|
|
131
180
|
return this.shift();
|
|
132
181
|
}
|
|
133
182
|
/**
|
|
183
|
+
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
|
|
184
|
+
* Space Complexity: O(1) - Constant space.
|
|
185
|
+
*/
|
|
186
|
+
/**
|
|
187
|
+
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
|
|
188
|
+
* Space Complexity: O(1) - Constant space.
|
|
189
|
+
*
|
|
134
190
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
135
191
|
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
136
192
|
* linked list.
|
|
@@ -148,6 +204,13 @@ class SinglyLinkedList {
|
|
|
148
204
|
this._length++;
|
|
149
205
|
}
|
|
150
206
|
/**
|
|
207
|
+
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
|
|
208
|
+
* Space Complexity: O(1) - Constant space.
|
|
209
|
+
*/
|
|
210
|
+
/**
|
|
211
|
+
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
|
|
212
|
+
* Space Complexity: O(1) - Constant space.
|
|
213
|
+
*
|
|
151
214
|
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
152
215
|
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
153
216
|
* linked list.
|
|
@@ -156,6 +219,13 @@ class SinglyLinkedList {
|
|
|
156
219
|
this.unshift(value);
|
|
157
220
|
}
|
|
158
221
|
/**
|
|
222
|
+
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
223
|
+
* Space Complexity: O(1) - Constant space.
|
|
224
|
+
*/
|
|
225
|
+
/**
|
|
226
|
+
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
227
|
+
* Space Complexity: O(1) - Constant space.
|
|
228
|
+
*
|
|
159
229
|
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
160
230
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
161
231
|
* retrieve from the list.
|
|
@@ -172,6 +242,13 @@ class SinglyLinkedList {
|
|
|
172
242
|
return current.value;
|
|
173
243
|
}
|
|
174
244
|
/**
|
|
245
|
+
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
246
|
+
* Space Complexity: O(1) - Constant space.
|
|
247
|
+
*/
|
|
248
|
+
/**
|
|
249
|
+
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
250
|
+
* Space Complexity: O(1) - Constant space.
|
|
251
|
+
*
|
|
175
252
|
* The function `getNodeAt` returns the node at a given index in a singly linked list.
|
|
176
253
|
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
177
254
|
* retrieve from the linked list. It indicates the zero-based index of the node we want to access.
|
|
@@ -186,6 +263,13 @@ class SinglyLinkedList {
|
|
|
186
263
|
return current;
|
|
187
264
|
}
|
|
188
265
|
/**
|
|
266
|
+
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
267
|
+
* Space Complexity: O(1) - Constant space.
|
|
268
|
+
*/
|
|
269
|
+
/**
|
|
270
|
+
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
271
|
+
* Space Complexity: O(1) - Constant space.
|
|
272
|
+
*
|
|
189
273
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
190
274
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
191
275
|
* data structure. It is of type number.
|
|
@@ -206,6 +290,13 @@ class SinglyLinkedList {
|
|
|
206
290
|
return removedNode.value;
|
|
207
291
|
}
|
|
208
292
|
/**
|
|
293
|
+
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
294
|
+
* Space Complexity: O(1) - Constant space.
|
|
295
|
+
*/
|
|
296
|
+
/**
|
|
297
|
+
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
298
|
+
* Space Complexity: O(1) - Constant space.
|
|
299
|
+
*
|
|
209
300
|
* The delete function removes a node with a specific value from a singly linked list.
|
|
210
301
|
* @param {E | SinglyLinkedListNode<E>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `E`
|
|
211
302
|
* or a `SinglyLinkedListNode<E>` object.
|
|
@@ -246,6 +337,13 @@ class SinglyLinkedList {
|
|
|
246
337
|
return false;
|
|
247
338
|
}
|
|
248
339
|
/**
|
|
340
|
+
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
341
|
+
* Space Complexity: O(1) - Constant space.
|
|
342
|
+
*/
|
|
343
|
+
/**
|
|
344
|
+
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
345
|
+
* Space Complexity: O(1) - Constant space.
|
|
346
|
+
*
|
|
249
347
|
* The `insertAt` function inserts a value at a specified index in a singly linked list.
|
|
250
348
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
251
349
|
* linked list. It is of type number.
|
|
@@ -289,6 +387,13 @@ class SinglyLinkedList {
|
|
|
289
387
|
this._length = 0;
|
|
290
388
|
}
|
|
291
389
|
/**
|
|
390
|
+
* 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.
|
|
391
|
+
* Space Complexity: O(n) - Linear space, as it creates an array with the same length as the list.
|
|
392
|
+
*/
|
|
393
|
+
/**
|
|
394
|
+
* 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.
|
|
395
|
+
* Space Complexity: O(n) - Linear space, as it creates an array with the same length as the list.
|
|
396
|
+
*
|
|
292
397
|
* The `toArray` function converts a linked list into an array.
|
|
293
398
|
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
294
399
|
*/
|
|
@@ -302,6 +407,13 @@ class SinglyLinkedList {
|
|
|
302
407
|
return array;
|
|
303
408
|
}
|
|
304
409
|
/**
|
|
410
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
411
|
+
* Space Complexity: O(1) - Constant space.
|
|
412
|
+
*/
|
|
413
|
+
/**
|
|
414
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
415
|
+
* Space Complexity: O(1) - Constant space.
|
|
416
|
+
*
|
|
305
417
|
* The `reverse` function reverses the order of the nodes in a singly linked list.
|
|
306
418
|
* @returns The reverse() method does not return anything. It has a return type of void.
|
|
307
419
|
*/
|
|
@@ -320,6 +432,13 @@ class SinglyLinkedList {
|
|
|
320
432
|
[this._head, this._tail] = [this.tail, this.head];
|
|
321
433
|
}
|
|
322
434
|
/**
|
|
435
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
436
|
+
* Space Complexity: O(1) - Constant space.
|
|
437
|
+
*/
|
|
438
|
+
/**
|
|
439
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
440
|
+
* Space Complexity: O(1) - Constant space.
|
|
441
|
+
*
|
|
323
442
|
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
|
324
443
|
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
325
444
|
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
@@ -337,6 +456,13 @@ class SinglyLinkedList {
|
|
|
337
456
|
return null;
|
|
338
457
|
}
|
|
339
458
|
/**
|
|
459
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
460
|
+
* Space Complexity: O(1) - Constant space.
|
|
461
|
+
*/
|
|
462
|
+
/**
|
|
463
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
464
|
+
* Space Complexity: O(1) - Constant space.
|
|
465
|
+
*
|
|
340
466
|
* The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
|
|
341
467
|
* @param {E} value - The value parameter is the value that you want to find the index of in the linked list.
|
|
342
468
|
* @returns The method is returning the index of the first occurrence of the specified value in the linked list. If the
|
|
@@ -355,6 +481,13 @@ class SinglyLinkedList {
|
|
|
355
481
|
return -1;
|
|
356
482
|
}
|
|
357
483
|
/**
|
|
484
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
485
|
+
* Space Complexity: O(1) - Constant space.
|
|
486
|
+
*/
|
|
487
|
+
/**
|
|
488
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
489
|
+
* Space Complexity: O(1) - Constant space.
|
|
490
|
+
*
|
|
358
491
|
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
|
|
359
492
|
* null.
|
|
360
493
|
* @param {E} value - The value parameter is the value that we want to search for in the linked list.
|
|
@@ -372,6 +505,13 @@ class SinglyLinkedList {
|
|
|
372
505
|
return null;
|
|
373
506
|
}
|
|
374
507
|
/**
|
|
508
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
509
|
+
* Space Complexity: O(1) - Constant space.
|
|
510
|
+
*/
|
|
511
|
+
/**
|
|
512
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
513
|
+
* Space Complexity: O(1) - Constant space.
|
|
514
|
+
*
|
|
375
515
|
* The `insertBefore` function inserts a new value before an existing value in a singly linked list.
|
|
376
516
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
|
|
377
517
|
* new value before. It can be either the value itself or a node containing the value in the linked list.
|
|
@@ -407,6 +547,13 @@ class SinglyLinkedList {
|
|
|
407
547
|
return false;
|
|
408
548
|
}
|
|
409
549
|
/**
|
|
550
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
551
|
+
* Space Complexity: O(1) - Constant space.
|
|
552
|
+
*/
|
|
553
|
+
/**
|
|
554
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
555
|
+
* Space Complexity: O(1) - Constant space.
|
|
556
|
+
*
|
|
410
557
|
* The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
411
558
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
|
|
412
559
|
* the new value will be inserted. It can be either the value of the existing node or the existing node itself.
|
|
@@ -435,6 +582,13 @@ class SinglyLinkedList {
|
|
|
435
582
|
return false;
|
|
436
583
|
}
|
|
437
584
|
/**
|
|
585
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
586
|
+
* Space Complexity: O(1) - Constant space.
|
|
587
|
+
*/
|
|
588
|
+
/**
|
|
589
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
590
|
+
* Space Complexity: O(1) - Constant space.
|
|
591
|
+
*
|
|
438
592
|
* The function counts the number of occurrences of a given value in a linked list.
|
|
439
593
|
* @param {E} value - The value parameter is the value that you want to count the occurrences of in the linked list.
|
|
440
594
|
* @returns The count of occurrences of the given value in the linked list.
|
|
@@ -451,6 +605,13 @@ class SinglyLinkedList {
|
|
|
451
605
|
return count;
|
|
452
606
|
}
|
|
453
607
|
/**
|
|
608
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
609
|
+
* Space Complexity: O(1) - Constant space.
|
|
610
|
+
*/
|
|
611
|
+
/**
|
|
612
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
613
|
+
* Space Complexity: O(1) - Constant space.
|
|
614
|
+
*
|
|
454
615
|
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
455
616
|
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
456
617
|
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
@@ -466,6 +627,13 @@ class SinglyLinkedList {
|
|
|
466
627
|
}
|
|
467
628
|
}
|
|
468
629
|
/**
|
|
630
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
|
|
631
|
+
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
|
|
632
|
+
*/
|
|
633
|
+
/**
|
|
634
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
|
|
635
|
+
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
|
|
636
|
+
*
|
|
469
637
|
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
470
638
|
* SinglyLinkedList with the transformed values.
|
|
471
639
|
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
@@ -483,6 +651,13 @@ class SinglyLinkedList {
|
|
|
483
651
|
return mappedList;
|
|
484
652
|
}
|
|
485
653
|
/**
|
|
654
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
|
|
655
|
+
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
|
|
656
|
+
*/
|
|
657
|
+
/**
|
|
658
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
|
|
659
|
+
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
|
|
660
|
+
*
|
|
486
661
|
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
487
662
|
* elements that satisfy the given callback function.
|
|
488
663
|
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
|
|
@@ -501,6 +676,13 @@ class SinglyLinkedList {
|
|
|
501
676
|
return filteredList;
|
|
502
677
|
}
|
|
503
678
|
/**
|
|
679
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
|
|
680
|
+
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
|
|
681
|
+
*/
|
|
682
|
+
/**
|
|
683
|
+
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
|
|
684
|
+
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
|
|
685
|
+
*
|
|
504
686
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
505
687
|
* single value.
|
|
506
688
|
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
@@ -29,6 +29,13 @@ export declare class SkipList<K, V> {
|
|
|
29
29
|
protected _probability: number;
|
|
30
30
|
get probability(): number;
|
|
31
31
|
/**
|
|
32
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
33
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
34
|
+
*/
|
|
35
|
+
/**
|
|
36
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
37
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
38
|
+
*
|
|
32
39
|
* The add function adds a new node with a given key and value to a Skip List data structure.
|
|
33
40
|
* @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
|
|
34
41
|
* @param {V} value - The "value" parameter represents the value associated with the key that is being added to the Skip
|
|
@@ -36,14 +43,36 @@ export declare class SkipList<K, V> {
|
|
|
36
43
|
*/
|
|
37
44
|
add(key: K, value: V): void;
|
|
38
45
|
/**
|
|
46
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
47
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
48
|
+
*/
|
|
49
|
+
/**
|
|
50
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
51
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
52
|
+
*
|
|
39
53
|
* The function `get` retrieves the value associated with a given key from a skip list data structure.
|
|
40
54
|
* @param {K} key - The `key` parameter is the key of the element that we want to retrieve from the data structure.
|
|
41
55
|
* @returns The method `get(key: K)` returns the value associated with the given key if it exists in the data structure,
|
|
42
56
|
* otherwise it returns `undefined`.
|
|
43
57
|
*/
|
|
44
58
|
get(key: K): V | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
61
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
62
|
+
*/
|
|
63
|
+
/**
|
|
64
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
65
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
66
|
+
*/
|
|
45
67
|
has(key: K): boolean;
|
|
46
68
|
/**
|
|
69
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
70
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
71
|
+
*/
|
|
72
|
+
/**
|
|
73
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
74
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
75
|
+
*
|
|
47
76
|
* The `delete` function removes a node with a specific key from a Skip List data structure.
|
|
48
77
|
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
|
|
49
78
|
* @returns The `delete` method returns a boolean value. It returns `true` if the key was successfully removed from the
|
|
@@ -51,28 +80,63 @@ export declare class SkipList<K, V> {
|
|
|
51
80
|
*/
|
|
52
81
|
delete(key: K): boolean;
|
|
53
82
|
/**
|
|
83
|
+
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
84
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
85
|
+
*/
|
|
86
|
+
/**
|
|
87
|
+
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
88
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
89
|
+
*
|
|
54
90
|
* Get the value of the first element (the smallest element) in the Skip List.
|
|
55
91
|
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
56
92
|
*/
|
|
57
93
|
getFirst(): V | undefined;
|
|
58
94
|
/**
|
|
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
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
97
|
+
*/
|
|
98
|
+
/**
|
|
99
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
100
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
101
|
+
*
|
|
59
102
|
* Get the value of the last element (the largest element) in the Skip List.
|
|
60
103
|
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
61
104
|
*/
|
|
62
105
|
getLast(): V | undefined;
|
|
63
106
|
/**
|
|
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
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
109
|
+
*/
|
|
110
|
+
/**
|
|
111
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
112
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
113
|
+
*
|
|
64
114
|
* Get the value of the first element in the Skip List that is greater than the given key.
|
|
65
115
|
* @param key - the given key.
|
|
66
116
|
* @returns The value of the first element greater than the given key, or undefined if there is no such element.
|
|
67
117
|
*/
|
|
68
118
|
higher(key: K): V | undefined;
|
|
69
119
|
/**
|
|
120
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
121
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
122
|
+
*/
|
|
123
|
+
/**
|
|
124
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
125
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
126
|
+
*
|
|
70
127
|
* Get the value of the last element in the Skip List that is less than the given key.
|
|
71
128
|
* @param key - the given key.
|
|
72
129
|
* @returns The value of the last element less than the given key, or undefined if there is no such element.
|
|
73
130
|
*/
|
|
74
131
|
lower(key: K): V | undefined;
|
|
75
132
|
/**
|
|
133
|
+
* Time Complexity: O(maxLevel) - where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case.
|
|
134
|
+
* Space Complexity: O(1) - constant space.
|
|
135
|
+
*/
|
|
136
|
+
/**
|
|
137
|
+
* Time Complexity: O(maxLevel) - where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case.
|
|
138
|
+
* Space Complexity: O(1) - constant space.
|
|
139
|
+
*
|
|
76
140
|
* The function "_randomLevel" generates a random level based on a given probability and maximum level.
|
|
77
141
|
* @returns the level, which is a number.
|
|
78
142
|
*/
|
|
@@ -43,6 +43,13 @@ class SkipList {
|
|
|
43
43
|
return this._probability;
|
|
44
44
|
}
|
|
45
45
|
/**
|
|
46
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
47
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
48
|
+
*/
|
|
49
|
+
/**
|
|
50
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
51
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
52
|
+
*
|
|
46
53
|
* The add function adds a new node with a given key and value to a Skip List data structure.
|
|
47
54
|
* @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
|
|
48
55
|
* @param {V} value - The "value" parameter represents the value associated with the key that is being added to the Skip
|
|
@@ -67,6 +74,13 @@ class SkipList {
|
|
|
67
74
|
}
|
|
68
75
|
}
|
|
69
76
|
/**
|
|
77
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
78
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
79
|
+
*/
|
|
80
|
+
/**
|
|
81
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
82
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
83
|
+
*
|
|
70
84
|
* The function `get` retrieves the value associated with a given key from a skip list data structure.
|
|
71
85
|
* @param {K} key - The `key` parameter is the key of the element that we want to retrieve from the data structure.
|
|
72
86
|
* @returns The method `get(key: K)` returns the value associated with the given key if it exists in the data structure,
|
|
@@ -85,10 +99,25 @@ class SkipList {
|
|
|
85
99
|
}
|
|
86
100
|
return undefined;
|
|
87
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
104
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
105
|
+
*/
|
|
106
|
+
/**
|
|
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
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
109
|
+
*/
|
|
88
110
|
has(key) {
|
|
89
111
|
return this.get(key) !== undefined;
|
|
90
112
|
}
|
|
91
113
|
/**
|
|
114
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
115
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
116
|
+
*/
|
|
117
|
+
/**
|
|
118
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
119
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
120
|
+
*
|
|
92
121
|
* The `delete` function removes a node with a specific key from a Skip List data structure.
|
|
93
122
|
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
|
|
94
123
|
* @returns The `delete` method returns a boolean value. It returns `true` if the key was successfully removed from the
|
|
@@ -119,6 +148,13 @@ class SkipList {
|
|
|
119
148
|
return false;
|
|
120
149
|
}
|
|
121
150
|
/**
|
|
151
|
+
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
152
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
153
|
+
*/
|
|
154
|
+
/**
|
|
155
|
+
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
156
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
157
|
+
*
|
|
122
158
|
* Get the value of the first element (the smallest element) in the Skip List.
|
|
123
159
|
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
124
160
|
*/
|
|
@@ -127,6 +163,13 @@ class SkipList {
|
|
|
127
163
|
return firstNode ? firstNode.value : undefined;
|
|
128
164
|
}
|
|
129
165
|
/**
|
|
166
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
167
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
168
|
+
*/
|
|
169
|
+
/**
|
|
170
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
171
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
172
|
+
*
|
|
130
173
|
* Get the value of the last element (the largest element) in the Skip List.
|
|
131
174
|
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
132
175
|
*/
|
|
@@ -140,6 +183,13 @@ class SkipList {
|
|
|
140
183
|
return current.value;
|
|
141
184
|
}
|
|
142
185
|
/**
|
|
186
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
187
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
188
|
+
*/
|
|
189
|
+
/**
|
|
190
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
191
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
192
|
+
*
|
|
143
193
|
* Get the value of the first element in the Skip List that is greater than the given key.
|
|
144
194
|
* @param key - the given key.
|
|
145
195
|
* @returns The value of the first element greater than the given key, or undefined if there is no such element.
|
|
@@ -155,6 +205,13 @@ class SkipList {
|
|
|
155
205
|
return nextNode ? nextNode.value : undefined;
|
|
156
206
|
}
|
|
157
207
|
/**
|
|
208
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
209
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
210
|
+
*/
|
|
211
|
+
/**
|
|
212
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
213
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
214
|
+
*
|
|
158
215
|
* Get the value of the last element in the Skip List that is less than the given key.
|
|
159
216
|
* @param key - the given key.
|
|
160
217
|
* @returns The value of the last element less than the given key, or undefined if there is no such element.
|
|
@@ -173,6 +230,13 @@ class SkipList {
|
|
|
173
230
|
return lastLess ? lastLess.value : undefined;
|
|
174
231
|
}
|
|
175
232
|
/**
|
|
233
|
+
* Time Complexity: O(maxLevel) - where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case.
|
|
234
|
+
* Space Complexity: O(1) - constant space.
|
|
235
|
+
*/
|
|
236
|
+
/**
|
|
237
|
+
* Time Complexity: O(maxLevel) - where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case.
|
|
238
|
+
* Space Complexity: O(1) - constant space.
|
|
239
|
+
*
|
|
176
240
|
* The function "_randomLevel" generates a random level based on a given probability and maximum level.
|
|
177
241
|
* @returns the level, which is a number.
|
|
178
242
|
*/
|