priority-queue-typed 1.49.1 → 1.49.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) 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/hash/hash-map.d.ts +9 -9
  4. package/dist/data-structures/hash/hash-map.js +16 -15
  5. package/dist/data-structures/heap/heap.d.ts +6 -35
  6. package/dist/data-structures/heap/heap.js +10 -42
  7. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +87 -93
  8. package/dist/data-structures/linked-list/doubly-linked-list.js +126 -129
  9. package/dist/data-structures/linked-list/singly-linked-list.d.ts +16 -21
  10. package/dist/data-structures/linked-list/singly-linked-list.js +42 -42
  11. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  12. package/dist/data-structures/linked-list/skip-linked-list.js +2 -2
  13. package/dist/data-structures/queue/deque.d.ts +70 -75
  14. package/dist/data-structures/queue/deque.js +100 -110
  15. package/dist/data-structures/queue/queue.d.ts +13 -14
  16. package/dist/data-structures/queue/queue.js +15 -18
  17. package/dist/data-structures/stack/stack.d.ts +2 -3
  18. package/dist/data-structures/stack/stack.js +2 -5
  19. package/dist/data-structures/trie/trie.d.ts +1 -2
  20. package/dist/data-structures/trie/trie.js +2 -5
  21. package/package.json +1 -1
  22. package/src/data-structures/base/iterable-base.ts +24 -0
  23. package/src/data-structures/hash/hash-map.ts +27 -28
  24. package/src/data-structures/heap/heap.ts +19 -57
  25. package/src/data-structures/linked-list/doubly-linked-list.ts +138 -142
  26. package/src/data-structures/linked-list/singly-linked-list.ts +49 -49
  27. package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
  28. package/src/data-structures/queue/deque.ts +122 -135
  29. package/src/data-structures/queue/queue.ts +19 -23
  30. package/src/data-structures/stack/stack.ts +4 -8
  31. package/src/data-structures/trie/trie.ts +5 -9
@@ -33,13 +33,13 @@ export class DoublyLinkedListNode<E = any> {
33
33
  */
34
34
  export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
35
35
  /**
36
- * The constructor initializes the linked list with an empty head, tail, and length.
36
+ * The constructor initializes the linked list with an empty head, tail, and size.
37
37
  */
38
38
  constructor(elements?: Iterable<E>) {
39
39
  super();
40
40
  this._head = undefined;
41
41
  this._tail = undefined;
42
- this._length = 0;
42
+ this._size = 0;
43
43
  if (elements) {
44
44
  for (const el of elements) {
45
45
  this.push(el);
@@ -59,23 +59,19 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
59
59
  return this._tail;
60
60
  }
61
61
 
62
- protected _length: number;
63
-
64
- get length(): number {
65
- return this._length;
66
- }
62
+ protected _size: number;
67
63
 
68
64
  get size(): number {
69
- return this.length;
65
+ return this._size;
70
66
  }
71
67
 
72
68
  /**
73
- * Time Complexity: O(n), where n is the length of the input array.
69
+ * Time Complexity: O(n), where n is the size of the input array.
74
70
  * Space Complexity: O(n)
75
71
  */
76
72
 
77
73
  /**
78
- * Time Complexity: O(n), where n is the length of the input array.
74
+ * Time Complexity: O(n), where n is the size of the input array.
79
75
  * Space Complexity: O(n)
80
76
  *
81
77
  * The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
@@ -103,7 +99,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
103
99
  * The push function adds a new node with the given value to the end of the doubly linked list.
104
100
  * @param {E} value - The value to be added to the linked list.
105
101
  */
106
- push(value: E): void {
102
+ push(value: E): boolean {
107
103
  const newNode = new DoublyLinkedListNode(value);
108
104
  if (!this.head) {
109
105
  this._head = newNode;
@@ -113,23 +109,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
113
109
  this.tail!.next = newNode;
114
110
  this._tail = newNode;
115
111
  }
116
- this._length++;
117
- }
118
-
119
- /**
120
- * Time Complexity: O(1)
121
- * Space Complexity: O(1)
122
- */
123
-
124
- /**
125
- * Time Complexity: O(1)
126
- * Space Complexity: O(1)
127
- *
128
- * The addLast function adds a new node with the given value to the end of the doubly linked list.
129
- * @param {E} value - The value to be added to the linked list.
130
- */
131
- addLast(value: E): void {
132
- this.push(value);
112
+ this._size++;
113
+ return true;
133
114
  }
134
115
 
135
116
  /**
@@ -155,27 +136,10 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
155
136
  this._tail = removedNode.prev;
156
137
  this.tail!.next = undefined;
157
138
  }
158
- this._length--;
139
+ this._size--;
159
140
  return removedNode.value;
160
141
  }
161
142
 
162
- /**
163
- * Time Complexity: O(1)
164
- * Space Complexity: O(1)
165
- */
166
-
167
- /**
168
- * Time Complexity: O(1)
169
- * Space Complexity: O(1)
170
- *
171
- * The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
172
- * @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
173
- * list is empty, it returns undefined.
174
- */
175
- pollLast(): E | undefined {
176
- return this.pop();
177
- }
178
-
179
143
  /**
180
144
  * Time Complexity: O(1)
181
145
  * Space Complexity: O(1)
@@ -199,27 +163,10 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
199
163
  this._head = removedNode.next;
200
164
  this.head!.prev = undefined;
201
165
  }
202
- this._length--;
166
+ this._size--;
203
167
  return removedNode.value;
204
168
  }
205
169
 
206
- /**
207
- * Time Complexity: O(1)
208
- * Space Complexity: O(1)
209
- */
210
-
211
- /**
212
- * Time Complexity: O(1)
213
- * Space Complexity: O(1)
214
- *
215
- * The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
216
- * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
217
- * list.
218
- */
219
- pollFirst(): E | undefined {
220
- return this.shift();
221
- }
222
-
223
170
  /**
224
171
  * Time Complexity: O(1)
225
172
  * Space Complexity: O(1)
@@ -233,7 +180,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
233
180
  * @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
234
181
  * doubly linked list.
235
182
  */
236
- unshift(value: E): void {
183
+ unshift(value: E): boolean {
237
184
  const newNode = new DoublyLinkedListNode(value);
238
185
  if (!this.head) {
239
186
  this._head = newNode;
@@ -243,56 +190,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
243
190
  this.head!.prev = newNode;
244
191
  this._head = newNode;
245
192
  }
246
- this._length++;
247
- }
248
-
249
- /**
250
- * Time Complexity: O(1)
251
- * Space Complexity: O(1)
252
- */
253
-
254
- /**
255
- * Time Complexity: O(1)
256
- * Space Complexity: O(1)
257
- *
258
- * The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
259
- * @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
260
- * doubly linked list.
261
- */
262
- addFirst(value: E): void {
263
- this.unshift(value);
264
- }
265
-
266
- /**
267
- * Time Complexity: O(n), where n is the number of elements in the linked list.
268
- * Space Complexity: O(1)
269
- */
270
-
271
- /**
272
- * Time Complexity: O(n), where n is the number of elements in the linked list.
273
- * Space Complexity: O(1)
274
- *
275
- * The `getFirst` function returns the first node in a doubly linked list, or undefined if the list is empty.
276
- * @returns The method `getFirst()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
277
- */
278
- getFirst(): E | undefined {
279
- return this.head?.value;
280
- }
281
-
282
- /**
283
- * Time Complexity: O(n), where n is the number of elements in the linked list.
284
- * Space Complexity: O(1)
285
- */
286
-
287
- /**
288
- * Time Complexity: O(n), where n is the number of elements in the linked list.
289
- * Space Complexity: O(1)
290
- *
291
- * The `getLast` function returns the last node in a doubly linked list, or undefined if the list is empty.
292
- * @returns The method `getLast()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
293
- */
294
- getLast(): E | undefined {
295
- return this.tail?.value;
193
+ this._size++;
194
+ return true;
296
195
  }
297
196
 
298
197
  /**
@@ -311,7 +210,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
311
210
  * or the linked list is empty, it will return undefined.
312
211
  */
313
212
  getAt(index: number): E | undefined {
314
- if (index < 0 || index >= this.length) return undefined;
213
+ if (index < 0 || index >= this.size) return undefined;
315
214
  let current = this.head;
316
215
  for (let i = 0; i < index; i++) {
317
216
  current = current!.next;
@@ -336,7 +235,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
336
235
  * valid range of the linked list, otherwise it returns `undefined`.
337
236
  */
338
237
  getNodeAt(index: number): DoublyLinkedListNode<E> | undefined {
339
- if (index < 0 || index >= this.length) return undefined;
238
+ if (index < 0 || index >= this.size) return undefined;
340
239
  let current = this.head;
341
240
  for (let i = 0; i < index; i++) {
342
241
  current = current!.next;
@@ -389,13 +288,13 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
389
288
  * @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
390
289
  * if the index is out of bounds.
391
290
  */
392
- insertAt(index: number, value: E): boolean {
393
- if (index < 0 || index > this.length) return false;
291
+ addAt(index: number, value: E): boolean {
292
+ if (index < 0 || index > this.size) return false;
394
293
  if (index === 0) {
395
294
  this.unshift(value);
396
295
  return true;
397
296
  }
398
- if (index === this.length) {
297
+ if (index === this.size) {
399
298
  this.push(value);
400
299
  return true;
401
300
  }
@@ -407,7 +306,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
407
306
  newNode.next = nextNode;
408
307
  prevNode!.next = newNode;
409
308
  nextNode!.prev = newNode;
410
- this._length++;
309
+ this._size++;
411
310
  return true;
412
311
  }
413
312
 
@@ -420,7 +319,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
420
319
  * Time Complexity: O(n), where n is the number of elements in the linked list.
421
320
  * Space Complexity: O(1)
422
321
  *
423
- * The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
322
+ * The `addBefore` function inserts a new value before an existing value or node in a doubly linked list.
424
323
  * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
425
324
  * before which the new value will be inserted. It can be either the value of the existing node or the existing node
426
325
  * itself.
@@ -429,7 +328,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
429
328
  * @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
430
329
  * insertion fails.
431
330
  */
432
- insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
331
+ addBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
433
332
  let existingNode;
434
333
 
435
334
  if (existingValueOrNode instanceof DoublyLinkedListNode) {
@@ -449,7 +348,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
449
348
  if (existingNode === this.head) {
450
349
  this._head = newNode;
451
350
  }
452
- this._length++;
351
+ this._size++;
453
352
  return true;
454
353
  }
455
354
 
@@ -465,7 +364,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
465
364
  * Time Complexity: O(n), where n is the number of elements in the linked list.
466
365
  * Space Complexity: O(1)
467
366
  *
468
- * The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
367
+ * The `addAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
469
368
  * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
470
369
  * after which the new value will be inserted. It can be either the value of the existing node or the existing node
471
370
  * itself.
@@ -473,7 +372,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
473
372
  * @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
474
373
  * existing value or node is not found in the doubly linked list.
475
374
  */
476
- insertAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
375
+ addAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
477
376
  let existingNode;
478
377
 
479
378
  if (existingValueOrNode instanceof DoublyLinkedListNode) {
@@ -493,7 +392,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
493
392
  if (existingNode === this.tail) {
494
393
  this._tail = newNode;
495
394
  }
496
- this._length++;
395
+ this._size++;
497
396
  return true;
498
397
  }
499
398
 
@@ -515,18 +414,24 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
515
414
  * @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
516
415
  * bounds.
517
416
  */
518
- deleteAt(index: number): E | undefined {
519
- if (index < 0 || index >= this.length) return undefined;
520
- if (index === 0) return this.shift();
521
- if (index === this.length - 1) return this.pop();
417
+ deleteAt(index: number): boolean {
418
+ if (index < 0 || index >= this.size) return false;
419
+ if (index === 0) {
420
+ this.shift();
421
+ return true;
422
+ }
423
+ if (index === this.size - 1) {
424
+ this.pop();
425
+ return true;
426
+ }
522
427
 
523
428
  const removedNode = this.getNodeAt(index);
524
429
  const prevNode = removedNode!.prev;
525
430
  const nextNode = removedNode!.next;
526
431
  prevNode!.next = nextNode;
527
432
  nextNode!.prev = prevNode;
528
- this._length--;
529
- return removedNode!.value;
433
+ this._size--;
434
+ return true;
530
435
  }
531
436
 
532
437
  /**
@@ -563,7 +468,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
563
468
  const nextNode = node.next;
564
469
  prevNode!.next = nextNode;
565
470
  nextNode!.prev = prevNode;
566
- this._length--;
471
+ this._size--;
567
472
  }
568
473
  return true;
569
474
  }
@@ -571,20 +476,20 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
571
476
  }
572
477
 
573
478
  /**
574
- * The function checks if a variable has a length greater than zero and returns a boolean value.
479
+ * The function checks if a variable has a size greater than zero and returns a boolean value.
575
480
  * @returns A boolean value is being returned.
576
481
  */
577
482
  isEmpty(): boolean {
578
- return this.length === 0;
483
+ return this.size === 0;
579
484
  }
580
485
 
581
486
  /**
582
- * The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
487
+ * The `clear` function resets the linked list by setting the head, tail, and size to undefined and 0 respectively.
583
488
  */
584
489
  clear(): void {
585
490
  this._head = undefined;
586
491
  this._tail = undefined;
587
- this._length = 0;
492
+ this._size = 0;
588
493
  }
589
494
 
590
495
  /**
@@ -679,7 +584,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
679
584
  *
680
585
  * The `reverse` function reverses the order of the elements in a doubly linked list.
681
586
  */
682
- reverse(): void {
587
+ reverse(): this {
683
588
  let current = this.head;
684
589
  [this._head, this._tail] = [this.tail, this.head];
685
590
  while (current) {
@@ -687,6 +592,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
687
592
  [current.prev, current.next] = [current.next, current.prev];
688
593
  current = next;
689
594
  }
595
+ return this;
690
596
  }
691
597
 
692
598
  /**
@@ -800,13 +706,103 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
800
706
  return mappedList;
801
707
  }
802
708
 
709
+ /**
710
+ * Time Complexity: O(1)
711
+ * Space Complexity: O(1)
712
+ */
713
+
714
+ /**
715
+ * Time Complexity: O(1)
716
+ * Space Complexity: O(1)
717
+ *
718
+ * The addLast function adds a new node with the given value to the end of the doubly linked list.
719
+ * @param {E} value - The value to be added to the linked list.
720
+ */
721
+ addLast(value: E): boolean {
722
+ return this.push(value);
723
+ }
724
+
725
+ /**
726
+ * Time Complexity: O(1)
727
+ * Space Complexity: O(1)
728
+ */
729
+
730
+ /**
731
+ * Time Complexity: O(1)
732
+ * Space Complexity: O(1)
733
+ *
734
+ * The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
735
+ * @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
736
+ * list is empty, it returns undefined.
737
+ */
738
+ pollLast(): E | undefined {
739
+ return this.pop();
740
+ }
741
+
742
+ /**
743
+ * Time Complexity: O(1)
744
+ * Space Complexity: O(1)
745
+ */
746
+
747
+ /**
748
+ * Time Complexity: O(1)
749
+ * Space Complexity: O(1)
750
+ *
751
+ * The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
752
+ * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
753
+ * list.
754
+ */
755
+ pollFirst(): E | undefined {
756
+ return this.shift();
757
+ }
758
+
759
+ /**
760
+ * Time Complexity: O(1)
761
+ * Space Complexity: O(1)
762
+ */
763
+
764
+ /**
765
+ * Time Complexity: O(1)
766
+ * Space Complexity: O(1)
767
+ *
768
+ * The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
769
+ * @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
770
+ * doubly linked list.
771
+ */
772
+ addFirst(value: E): void {
773
+ this.unshift(value);
774
+ }
775
+
803
776
  /**
804
777
  * Time Complexity: O(n), where n is the number of elements in the linked list.
805
- * Space Complexity: O(n)
778
+ * Space Complexity: O(1)
779
+ */
780
+
781
+ /**
782
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
783
+ * Space Complexity: O(1)
784
+ *
785
+ * The `get first` function returns the first node in a doubly linked list, or undefined if the list is empty.
786
+ * @returns The method `get first()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
806
787
  */
788
+ get first(): E | undefined {
789
+ return this.head?.value;
790
+ }
807
791
 
808
- print(): void {
809
- console.log([...this]);
792
+ /**
793
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
794
+ * Space Complexity: O(1)
795
+ */
796
+
797
+ /**
798
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
799
+ * Space Complexity: O(1)
800
+ *
801
+ * The `get last` function returns the last node in a doubly linked list, or undefined if the list is empty.
802
+ * @returns The method `get last()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
803
+ */
804
+ get last(): E | undefined {
805
+ return this.tail?.value;
810
806
  }
811
807
 
812
808
  /**