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