min-heap-typed 1.49.1 → 1.49.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +11 -0
  2. package/dist/data-structures/base/iterable-base.js +21 -0
  3. package/dist/data-structures/graph/abstract-graph.d.ts +7 -7
  4. package/dist/data-structures/graph/abstract-graph.js +43 -12
  5. package/dist/data-structures/graph/directed-graph.d.ts +2 -2
  6. package/dist/data-structures/graph/directed-graph.js +2 -2
  7. package/dist/data-structures/graph/undirected-graph.d.ts +1 -1
  8. package/dist/data-structures/graph/undirected-graph.js +1 -1
  9. package/dist/data-structures/hash/hash-map.d.ts +9 -9
  10. package/dist/data-structures/hash/hash-map.js +16 -15
  11. package/dist/data-structures/heap/heap.d.ts +6 -35
  12. package/dist/data-structures/heap/heap.js +10 -42
  13. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
  14. package/dist/data-structures/linked-list/doubly-linked-list.js +143 -146
  15. package/dist/data-structures/linked-list/singly-linked-list.d.ts +16 -21
  16. package/dist/data-structures/linked-list/singly-linked-list.js +42 -42
  17. package/dist/data-structures/linked-list/skip-linked-list.d.ts +25 -25
  18. package/dist/data-structures/linked-list/skip-linked-list.js +36 -36
  19. package/dist/data-structures/queue/deque.d.ts +70 -75
  20. package/dist/data-structures/queue/deque.js +100 -110
  21. package/dist/data-structures/queue/queue.d.ts +37 -38
  22. package/dist/data-structures/queue/queue.js +46 -49
  23. package/dist/data-structures/stack/stack.d.ts +2 -3
  24. package/dist/data-structures/stack/stack.js +2 -5
  25. package/dist/data-structures/trie/trie.d.ts +1 -2
  26. package/dist/data-structures/trie/trie.js +2 -5
  27. package/package.json +2 -2
  28. package/src/data-structures/base/iterable-base.ts +24 -0
  29. package/src/data-structures/graph/abstract-graph.ts +55 -14
  30. package/src/data-structures/graph/directed-graph.ts +3 -2
  31. package/src/data-structures/graph/undirected-graph.ts +1 -1
  32. package/src/data-structures/hash/hash-map.ts +27 -28
  33. package/src/data-structures/heap/heap.ts +19 -57
  34. package/src/data-structures/linked-list/doubly-linked-list.ts +157 -161
  35. package/src/data-structures/linked-list/singly-linked-list.ts +49 -49
  36. package/src/data-structures/linked-list/skip-linked-list.ts +40 -40
  37. package/src/data-structures/queue/deque.ts +122 -135
  38. package/src/data-structures/queue/queue.ts +54 -58
  39. package/src/data-structures/stack/stack.ts +4 -8
  40. package/src/data-structures/trie/trie.ts +5 -9
@@ -22,7 +22,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
22
22
  super();
23
23
  this._head = undefined;
24
24
  this._tail = undefined;
25
- this._length = 0;
25
+ this._size = 0;
26
26
  if (elements) {
27
27
  for (const el of elements)
28
28
  this.push(el);
@@ -34,8 +34,8 @@ class SinglyLinkedList extends base_1.IterableElementBase {
34
34
  get tail() {
35
35
  return this._tail;
36
36
  }
37
- get length() {
38
- return this._length;
37
+ get size() {
38
+ return this._size;
39
39
  }
40
40
  /**
41
41
  * 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.
@@ -79,7 +79,8 @@ class SinglyLinkedList extends base_1.IterableElementBase {
79
79
  this.tail.next = newNode;
80
80
  this._tail = newNode;
81
81
  }
82
- this._length++;
82
+ this._size++;
83
+ return true;
83
84
  }
84
85
  /**
85
86
  * Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
@@ -94,7 +95,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
94
95
  * any type (E) as specified in the generic type declaration of the class or function.
95
96
  */
96
97
  addLast(value) {
97
- this.push(value);
98
+ return this.push(value);
98
99
  }
99
100
  /**
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.
@@ -116,7 +117,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
116
117
  const value = this.head.value;
117
118
  this._head = undefined;
118
119
  this._tail = undefined;
119
- this._length--;
120
+ this._size--;
120
121
  return value;
121
122
  }
122
123
  let current = this.head;
@@ -126,7 +127,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
126
127
  const value = this.tail.value;
127
128
  current.next = undefined;
128
129
  this._tail = current;
129
- this._length--;
130
+ this._size--;
130
131
  return value;
131
132
  }
132
133
  /**
@@ -161,7 +162,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
161
162
  return undefined;
162
163
  const removedNode = this.head;
163
164
  this._head = this.head.next;
164
- this._length--;
165
+ this._size--;
165
166
  return removedNode.value;
166
167
  }
167
168
  /**
@@ -200,7 +201,8 @@ class SinglyLinkedList extends base_1.IterableElementBase {
200
201
  newNode.next = this.head;
201
202
  this._head = newNode;
202
203
  }
203
- this._length++;
204
+ this._size++;
205
+ return true;
204
206
  }
205
207
  /**
206
208
  * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
@@ -215,7 +217,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
215
217
  * linked list.
216
218
  */
217
219
  addFirst(value) {
218
- this.unshift(value);
220
+ return this.unshift(value);
219
221
  }
220
222
  /**
221
223
  * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
@@ -232,7 +234,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
232
234
  * `undefined` if the index is out of bounds.
233
235
  */
234
236
  getAt(index) {
235
- if (index < 0 || index >= this.length)
237
+ if (index < 0 || index >= this.size)
236
238
  return undefined;
237
239
  let current = this.head;
238
240
  for (let i = 0; i < index; i++) {
@@ -276,17 +278,21 @@ class SinglyLinkedList extends base_1.IterableElementBase {
276
278
  * bounds.
277
279
  */
278
280
  deleteAt(index) {
279
- if (index < 0 || index >= this.length)
280
- return undefined;
281
- if (index === 0)
282
- return this.shift();
283
- if (index === this.length - 1)
284
- return this.pop();
281
+ if (index < 0 || index >= this.size)
282
+ return false;
283
+ if (index === 0) {
284
+ this.shift();
285
+ return true;
286
+ }
287
+ if (index === this.size - 1) {
288
+ this.pop();
289
+ return true;
290
+ }
285
291
  const prevNode = this.getNodeAt(index - 1);
286
292
  const removedNode = prevNode.next;
287
293
  prevNode.next = removedNode.next;
288
- this._length--;
289
- return removedNode.value;
294
+ this._size--;
295
+ return true;
290
296
  }
291
297
  /**
292
298
  * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
@@ -327,7 +333,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
327
333
  this._tail = prev;
328
334
  }
329
335
  }
330
- this._length--;
336
+ this._size--;
331
337
  return true;
332
338
  }
333
339
  prev = current;
@@ -343,7 +349,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
343
349
  * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
344
350
  * Space Complexity: O(1) - Constant space.
345
351
  *
346
- * The `insertAt` function inserts a value at a specified index in a singly linked list.
352
+ * The `addAt` function inserts a value at a specified index in a singly linked list.
347
353
  * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
348
354
  * linked list. It is of type number.
349
355
  * @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
@@ -351,14 +357,14 @@ class SinglyLinkedList extends base_1.IterableElementBase {
351
357
  * @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
352
358
  * if the index is out of bounds.
353
359
  */
354
- insertAt(index, value) {
355
- if (index < 0 || index > this.length)
360
+ addAt(index, value) {
361
+ if (index < 0 || index > this.size)
356
362
  return false;
357
363
  if (index === 0) {
358
364
  this.unshift(value);
359
365
  return true;
360
366
  }
361
- if (index === this.length) {
367
+ if (index === this.size) {
362
368
  this.push(value);
363
369
  return true;
364
370
  }
@@ -366,7 +372,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
366
372
  const prevNode = this.getNodeAt(index - 1);
367
373
  newNode.next = prevNode.next;
368
374
  prevNode.next = newNode;
369
- this._length++;
375
+ this._size++;
370
376
  return true;
371
377
  }
372
378
  /**
@@ -375,7 +381,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
375
381
  * @returns A boolean value indicating whether the length of the object is equal to 0.
376
382
  */
377
383
  isEmpty() {
378
- return this.length === 0;
384
+ return this.size === 0;
379
385
  }
380
386
  /**
381
387
  * The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
@@ -383,7 +389,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
383
389
  clear() {
384
390
  this._head = undefined;
385
391
  this._tail = undefined;
386
- this._length = 0;
392
+ this._size = 0;
387
393
  }
388
394
  /**
389
395
  * 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.
@@ -418,7 +424,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
418
424
  */
419
425
  reverse() {
420
426
  if (!this.head || this.head === this.tail)
421
- return;
427
+ return this;
422
428
  let prev = undefined;
423
429
  let current = this.head;
424
430
  let next = undefined;
@@ -429,6 +435,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
429
435
  current = next;
430
436
  }
431
437
  [this._head, this._tail] = [this.tail, this.head];
438
+ return this;
432
439
  }
433
440
  /**
434
441
  * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
@@ -511,14 +518,14 @@ class SinglyLinkedList extends base_1.IterableElementBase {
511
518
  * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
512
519
  * Space Complexity: O(1) - Constant space.
513
520
  *
514
- * The `insertBefore` function inserts a new value before an existing value in a singly linked list.
521
+ * The `addBefore` function inserts a new value before an existing value in a singly linked list.
515
522
  * @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
516
523
  * new value before. It can be either the value itself or a node containing the value in the linked list.
517
524
  * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
518
- * @returns The method `insertBefore` returns a boolean value. It returns `true` if the new value was successfully
525
+ * @returns The method `addBefore` returns a boolean value. It returns `true` if the new value was successfully
519
526
  * inserted before the existing value, and `false` otherwise.
520
527
  */
521
- insertBefore(existingValueOrNode, newValue) {
528
+ addBefore(existingValueOrNode, newValue) {
522
529
  if (!this.head)
523
530
  return false;
524
531
  let existingValue;
@@ -538,7 +545,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
538
545
  const newNode = new SinglyLinkedListNode(newValue);
539
546
  newNode.next = current.next;
540
547
  current.next = newNode;
541
- this._length++;
548
+ this._size++;
542
549
  return true;
543
550
  }
544
551
  current = current.next;
@@ -553,14 +560,14 @@ class SinglyLinkedList extends base_1.IterableElementBase {
553
560
  * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
554
561
  * Space Complexity: O(1) - Constant space.
555
562
  *
556
- * The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
563
+ * The `addAfter` function inserts a new node with a given value after an existing node in a singly linked list.
557
564
  * @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
558
565
  * the new value will be inserted. It can be either the value of the existing node or the existing node itself.
559
566
  * @param {E} newValue - The value that you want to insert into the linked list after the existing value or node.
560
567
  * @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
561
568
  * existing value or node, and false if the existing value or node was not found in the linked list.
562
569
  */
563
- insertAfter(existingValueOrNode, newValue) {
570
+ addAfter(existingValueOrNode, newValue) {
564
571
  let existingNode;
565
572
  if (existingValueOrNode instanceof SinglyLinkedListNode) {
566
573
  existingNode = existingValueOrNode;
@@ -575,7 +582,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
575
582
  if (existingNode === this.tail) {
576
583
  this._tail = newNode;
577
584
  }
578
- this._length++;
585
+ this._size++;
579
586
  return true;
580
587
  }
581
588
  return false;
@@ -662,13 +669,6 @@ class SinglyLinkedList extends base_1.IterableElementBase {
662
669
  }
663
670
  return mappedList;
664
671
  }
665
- /**
666
- * Time Complexity: O(n), where n is the number of elements in the linked list.
667
- * Space Complexity: O(n)
668
- */
669
- print() {
670
- console.log([...this]);
671
- }
672
672
  *_getIterator() {
673
673
  let current = this.head;
674
674
  while (current) {
@@ -33,15 +33,13 @@ export declare class SkipList<K, V> {
33
33
  * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
34
34
  */
35
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.
36
+ * Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
37
37
  * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
38
38
  *
39
- * The add function adds a new node with a given key and value to a Skip List data structure.
40
- * @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
41
- * @param {V} value - The "value" parameter represents the value associated with the key that is being added to the Skip
42
- * List.
39
+ * Get the value of the first element (the smallest element) in the Skip List.
40
+ * @returns The value of the first element, or undefined if the Skip List is empty.
43
41
  */
44
- add(key: K, value: V): void;
42
+ get first(): V | undefined;
45
43
  /**
46
44
  * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
47
45
  * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
@@ -50,12 +48,10 @@ export declare class SkipList<K, V> {
50
48
  * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
51
49
  * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
52
50
  *
53
- * The function `get` retrieves the value associated with a given key from a skip list data structure.
54
- * @param {K} key - The `key` parameter is the key of the element that we want to retrieve from the data structure.
55
- * @returns The method `get(key: K)` returns the value associated with the given key if it exists in the data structure,
56
- * otherwise it returns `undefined`.
51
+ * Get the value of the last element (the largest element) in the Skip List.
52
+ * @returns The value of the last element, or undefined if the Skip List is empty.
57
53
  */
58
- get(key: K): V | undefined;
54
+ get last(): V | undefined;
59
55
  /**
60
56
  * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
61
57
  * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
@@ -63,8 +59,13 @@ export declare class SkipList<K, V> {
63
59
  /**
64
60
  * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
65
61
  * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
62
+ *
63
+ * The add function adds a new node with a given key and value to a Skip List data structure.
64
+ * @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
65
+ * @param {V} value - The "value" parameter represents the value associated with the key that is being added to the Skip
66
+ * List.
66
67
  */
67
- has(key: K): boolean;
68
+ add(key: K, value: V): void;
68
69
  /**
69
70
  * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
70
71
  * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
@@ -73,24 +74,21 @@ export declare class SkipList<K, V> {
73
74
  * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
74
75
  * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
75
76
  *
76
- * The `delete` function removes a node with a specific key from a Skip List data structure.
77
- * @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
78
- * @returns The `delete` method returns a boolean value. It returns `true` if the key was successfully removed from the
79
- * skip list, and `false` if the key was not found in the skip list.
77
+ * The function `get` retrieves the value associated with a given key from a skip list data structure.
78
+ * @param {K} key - The `key` parameter is the key of the element that we want to retrieve from the data structure.
79
+ * @returns The method `get(key: K)` returns the value associated with the given key if it exists in the data structure,
80
+ * otherwise it returns `undefined`.
80
81
  */
81
- delete(key: K): boolean;
82
+ get(key: K): V | undefined;
82
83
  /**
83
84
  * Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
84
85
  * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
85
86
  */
86
87
  /**
87
- * Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
88
+ * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
88
89
  * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
89
- *
90
- * Get the value of the first element (the smallest element) in the Skip List.
91
- * @returns The value of the first element, or undefined if the Skip List is empty.
92
90
  */
93
- getFirst(): V | undefined;
91
+ has(key: K): boolean;
94
92
  /**
95
93
  * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
96
94
  * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
@@ -99,10 +97,12 @@ export declare class SkipList<K, V> {
99
97
  * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
100
98
  * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
101
99
  *
102
- * Get the value of the last element (the largest element) in the Skip List.
103
- * @returns The value of the last element, or undefined if the Skip List is empty.
100
+ * The `delete` function removes a node with a specific key from a Skip List data structure.
101
+ * @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
102
+ * @returns The `delete` method returns a boolean value. It returns `true` if the key was successfully removed from the
103
+ * skip list, and `false` if the key was not found in the skip list.
104
104
  */
105
- getLast(): V | undefined;
105
+ delete(key: K): boolean;
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.
@@ -42,6 +42,41 @@ class SkipList {
42
42
  get probability() {
43
43
  return this._probability;
44
44
  }
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(1) - 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
+ *
53
+ * Get the value of the first element (the smallest element) in the Skip List.
54
+ * @returns The value of the first element, or undefined if the Skip List is empty.
55
+ */
56
+ get first() {
57
+ const firstNode = this.head.forward[0];
58
+ return firstNode ? firstNode.value : undefined;
59
+ }
60
+ /**
61
+ * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
62
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
63
+ */
64
+ /**
65
+ * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
66
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
67
+ *
68
+ * Get the value of the last element (the largest element) in the Skip List.
69
+ * @returns The value of the last element, or undefined if the Skip List is empty.
70
+ */
71
+ get last() {
72
+ let current = this.head;
73
+ for (let i = this.level - 1; i >= 0; i--) {
74
+ while (current.forward[i]) {
75
+ current = current.forward[i];
76
+ }
77
+ }
78
+ return current.value;
79
+ }
45
80
  /**
46
81
  * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
47
82
  * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
@@ -100,7 +135,7 @@ class SkipList {
100
135
  return undefined;
101
136
  }
102
137
  /**
103
- * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
138
+ * Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
104
139
  * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
105
140
  */
106
141
  /**
@@ -147,41 +182,6 @@ class SkipList {
147
182
  }
148
183
  return false;
149
184
  }
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
- *
158
- * Get the value of the first element (the smallest element) in the Skip List.
159
- * @returns The value of the first element, or undefined if the Skip List is empty.
160
- */
161
- getFirst() {
162
- const firstNode = this.head.forward[0];
163
- return firstNode ? firstNode.value : undefined;
164
- }
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
- *
173
- * Get the value of the last element (the largest element) in the Skip List.
174
- * @returns The value of the last element, or undefined if the Skip List is empty.
175
- */
176
- getLast() {
177
- let current = this.head;
178
- for (let i = this.level - 1; i >= 0; i--) {
179
- while (current.forward[i]) {
180
- current = current.forward[i];
181
- }
182
- }
183
- return current.value;
184
- }
185
185
  /**
186
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
187
  * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.