queue-typed 1.49.0 → 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 (88) 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/binary-tree/avl-tree.d.ts +10 -12
  4. package/dist/data-structures/binary-tree/avl-tree.js +3 -4
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +58 -58
  6. package/dist/data-structures/binary-tree/binary-tree.js +6 -6
  7. package/dist/data-structures/binary-tree/bst.d.ts +15 -15
  8. package/dist/data-structures/binary-tree/bst.js +3 -3
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -11
  10. package/dist/data-structures/binary-tree/rb-tree.js +5 -6
  11. package/dist/data-structures/binary-tree/tree-multimap.d.ts +14 -14
  12. package/dist/data-structures/binary-tree/tree-multimap.js +3 -3
  13. package/dist/data-structures/graph/abstract-graph.d.ts +9 -3
  14. package/dist/data-structures/graph/abstract-graph.js +27 -31
  15. package/dist/data-structures/graph/directed-graph.d.ts +8 -1
  16. package/dist/data-structures/graph/directed-graph.js +1 -8
  17. package/dist/data-structures/graph/map-graph.d.ts +1 -1
  18. package/dist/data-structures/graph/undirected-graph.d.ts +8 -1
  19. package/dist/data-structures/graph/undirected-graph.js +1 -8
  20. package/dist/data-structures/hash/hash-map.d.ts +22 -10
  21. package/dist/data-structures/hash/hash-map.js +28 -16
  22. package/dist/data-structures/hash/hash-table.d.ts +2 -2
  23. package/dist/data-structures/heap/heap.d.ts +20 -38
  24. package/dist/data-structures/heap/heap.js +22 -42
  25. package/dist/data-structures/heap/max-heap.d.ts +11 -1
  26. package/dist/data-structures/heap/max-heap.js +10 -7
  27. package/dist/data-structures/heap/min-heap.d.ts +11 -1
  28. package/dist/data-structures/heap/min-heap.js +10 -7
  29. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +95 -95
  30. package/dist/data-structures/linked-list/doubly-linked-list.js +132 -136
  31. package/dist/data-structures/linked-list/singly-linked-list.d.ts +18 -23
  32. package/dist/data-structures/linked-list/singly-linked-list.js +42 -49
  33. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  34. package/dist/data-structures/linked-list/skip-linked-list.js +2 -2
  35. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
  36. package/dist/data-structures/priority-queue/max-priority-queue.js +0 -7
  37. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
  38. package/dist/data-structures/priority-queue/min-priority-queue.js +0 -7
  39. package/dist/data-structures/priority-queue/priority-queue.d.ts +9 -1
  40. package/dist/data-structures/priority-queue/priority-queue.js +8 -7
  41. package/dist/data-structures/queue/deque.d.ts +76 -80
  42. package/dist/data-structures/queue/deque.js +106 -122
  43. package/dist/data-structures/queue/queue.d.ts +30 -16
  44. package/dist/data-structures/queue/queue.js +31 -24
  45. package/dist/data-structures/stack/stack.d.ts +17 -8
  46. package/dist/data-structures/stack/stack.js +9 -9
  47. package/dist/data-structures/trie/trie.d.ts +14 -5
  48. package/dist/data-structures/trie/trie.js +13 -13
  49. package/dist/interfaces/binary-tree.d.ts +4 -4
  50. package/dist/types/common.d.ts +32 -8
  51. package/dist/types/common.js +22 -1
  52. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
  53. package/dist/types/data-structures/binary-tree/binary-tree.js +0 -22
  54. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
  55. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
  56. package/package.json +2 -2
  57. package/src/data-structures/base/iterable-base.ts +24 -0
  58. package/src/data-structures/binary-tree/avl-tree.ts +14 -14
  59. package/src/data-structures/binary-tree/binary-tree.ts +74 -77
  60. package/src/data-structures/binary-tree/bst.ts +18 -17
  61. package/src/data-structures/binary-tree/rb-tree.ts +17 -18
  62. package/src/data-structures/binary-tree/tree-multimap.ts +22 -20
  63. package/src/data-structures/graph/abstract-graph.ts +35 -25
  64. package/src/data-structures/graph/directed-graph.ts +2 -2
  65. package/src/data-structures/graph/map-graph.ts +1 -1
  66. package/src/data-structures/graph/undirected-graph.ts +2 -2
  67. package/src/data-structures/hash/hash-map.ts +40 -24
  68. package/src/data-structures/hash/hash-table.ts +3 -3
  69. package/src/data-structures/heap/heap.ts +33 -60
  70. package/src/data-structures/heap/max-heap.ts +11 -2
  71. package/src/data-structures/heap/min-heap.ts +11 -2
  72. package/src/data-structures/linked-list/doubly-linked-list.ts +147 -145
  73. package/src/data-structures/linked-list/singly-linked-list.ts +52 -52
  74. package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  77. package/src/data-structures/priority-queue/priority-queue.ts +9 -2
  78. package/src/data-structures/queue/deque.ts +129 -144
  79. package/src/data-structures/queue/queue.ts +37 -26
  80. package/src/data-structures/stack/stack.ts +20 -14
  81. package/src/data-structures/trie/trie.ts +18 -13
  82. package/src/interfaces/binary-tree.ts +5 -5
  83. package/src/types/common.ts +37 -12
  84. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -1
  85. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -26
  86. package/src/types/data-structures/binary-tree/bst.ts +0 -1
  87. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
  88. package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
@@ -2,13 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
4
4
  const base_1 = require("../base");
5
- /**
6
- * data-structure-typed
7
- *
8
- * @author Tyler Zeng
9
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
10
- * @license MIT License
11
- */
12
5
  class SinglyLinkedListNode {
13
6
  /**
14
7
  * The constructor function initializes an instance of a class with a given value and sets the next property to undefined.
@@ -29,7 +22,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
29
22
  super();
30
23
  this._head = undefined;
31
24
  this._tail = undefined;
32
- this._length = 0;
25
+ this._size = 0;
33
26
  if (elements) {
34
27
  for (const el of elements)
35
28
  this.push(el);
@@ -41,8 +34,8 @@ class SinglyLinkedList extends base_1.IterableElementBase {
41
34
  get tail() {
42
35
  return this._tail;
43
36
  }
44
- get length() {
45
- return this._length;
37
+ get size() {
38
+ return this._size;
46
39
  }
47
40
  /**
48
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.
@@ -86,7 +79,8 @@ class SinglyLinkedList extends base_1.IterableElementBase {
86
79
  this.tail.next = newNode;
87
80
  this._tail = newNode;
88
81
  }
89
- this._length++;
82
+ this._size++;
83
+ return true;
90
84
  }
91
85
  /**
92
86
  * Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
@@ -101,7 +95,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
101
95
  * any type (E) as specified in the generic type declaration of the class or function.
102
96
  */
103
97
  addLast(value) {
104
- this.push(value);
98
+ return this.push(value);
105
99
  }
106
100
  /**
107
101
  * Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
@@ -123,7 +117,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
123
117
  const value = this.head.value;
124
118
  this._head = undefined;
125
119
  this._tail = undefined;
126
- this._length--;
120
+ this._size--;
127
121
  return value;
128
122
  }
129
123
  let current = this.head;
@@ -133,7 +127,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
133
127
  const value = this.tail.value;
134
128
  current.next = undefined;
135
129
  this._tail = current;
136
- this._length--;
130
+ this._size--;
137
131
  return value;
138
132
  }
139
133
  /**
@@ -168,7 +162,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
168
162
  return undefined;
169
163
  const removedNode = this.head;
170
164
  this._head = this.head.next;
171
- this._length--;
165
+ this._size--;
172
166
  return removedNode.value;
173
167
  }
174
168
  /**
@@ -207,7 +201,8 @@ class SinglyLinkedList extends base_1.IterableElementBase {
207
201
  newNode.next = this.head;
208
202
  this._head = newNode;
209
203
  }
210
- this._length++;
204
+ this._size++;
205
+ return true;
211
206
  }
212
207
  /**
213
208
  * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
@@ -222,7 +217,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
222
217
  * linked list.
223
218
  */
224
219
  addFirst(value) {
225
- this.unshift(value);
220
+ return this.unshift(value);
226
221
  }
227
222
  /**
228
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.
@@ -239,7 +234,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
239
234
  * `undefined` if the index is out of bounds.
240
235
  */
241
236
  getAt(index) {
242
- if (index < 0 || index >= this.length)
237
+ if (index < 0 || index >= this.size)
243
238
  return undefined;
244
239
  let current = this.head;
245
240
  for (let i = 0; i < index; i++) {
@@ -283,17 +278,21 @@ class SinglyLinkedList extends base_1.IterableElementBase {
283
278
  * bounds.
284
279
  */
285
280
  deleteAt(index) {
286
- if (index < 0 || index >= this.length)
287
- return undefined;
288
- if (index === 0)
289
- return this.shift();
290
- if (index === this.length - 1)
291
- 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
+ }
292
291
  const prevNode = this.getNodeAt(index - 1);
293
292
  const removedNode = prevNode.next;
294
293
  prevNode.next = removedNode.next;
295
- this._length--;
296
- return removedNode.value;
294
+ this._size--;
295
+ return true;
297
296
  }
298
297
  /**
299
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.
@@ -334,7 +333,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
334
333
  this._tail = prev;
335
334
  }
336
335
  }
337
- this._length--;
336
+ this._size--;
338
337
  return true;
339
338
  }
340
339
  prev = current;
@@ -350,7 +349,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
350
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.
351
350
  * Space Complexity: O(1) - Constant space.
352
351
  *
353
- * 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.
354
353
  * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
355
354
  * linked list. It is of type number.
356
355
  * @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
@@ -358,14 +357,14 @@ class SinglyLinkedList extends base_1.IterableElementBase {
358
357
  * @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
359
358
  * if the index is out of bounds.
360
359
  */
361
- insertAt(index, value) {
362
- if (index < 0 || index > this.length)
360
+ addAt(index, value) {
361
+ if (index < 0 || index > this.size)
363
362
  return false;
364
363
  if (index === 0) {
365
364
  this.unshift(value);
366
365
  return true;
367
366
  }
368
- if (index === this.length) {
367
+ if (index === this.size) {
369
368
  this.push(value);
370
369
  return true;
371
370
  }
@@ -373,7 +372,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
373
372
  const prevNode = this.getNodeAt(index - 1);
374
373
  newNode.next = prevNode.next;
375
374
  prevNode.next = newNode;
376
- this._length++;
375
+ this._size++;
377
376
  return true;
378
377
  }
379
378
  /**
@@ -382,7 +381,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
382
381
  * @returns A boolean value indicating whether the length of the object is equal to 0.
383
382
  */
384
383
  isEmpty() {
385
- return this.length === 0;
384
+ return this.size === 0;
386
385
  }
387
386
  /**
388
387
  * The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
@@ -390,7 +389,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
390
389
  clear() {
391
390
  this._head = undefined;
392
391
  this._tail = undefined;
393
- this._length = 0;
392
+ this._size = 0;
394
393
  }
395
394
  /**
396
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.
@@ -425,7 +424,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
425
424
  */
426
425
  reverse() {
427
426
  if (!this.head || this.head === this.tail)
428
- return;
427
+ return this;
429
428
  let prev = undefined;
430
429
  let current = this.head;
431
430
  let next = undefined;
@@ -436,6 +435,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
436
435
  current = next;
437
436
  }
438
437
  [this._head, this._tail] = [this.tail, this.head];
438
+ return this;
439
439
  }
440
440
  /**
441
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.
@@ -518,14 +518,14 @@ class SinglyLinkedList extends base_1.IterableElementBase {
518
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.
519
519
  * Space Complexity: O(1) - Constant space.
520
520
  *
521
- * 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.
522
522
  * @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
523
523
  * new value before. It can be either the value itself or a node containing the value in the linked list.
524
524
  * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
525
- * @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
526
526
  * inserted before the existing value, and `false` otherwise.
527
527
  */
528
- insertBefore(existingValueOrNode, newValue) {
528
+ addBefore(existingValueOrNode, newValue) {
529
529
  if (!this.head)
530
530
  return false;
531
531
  let existingValue;
@@ -545,7 +545,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
545
545
  const newNode = new SinglyLinkedListNode(newValue);
546
546
  newNode.next = current.next;
547
547
  current.next = newNode;
548
- this._length++;
548
+ this._size++;
549
549
  return true;
550
550
  }
551
551
  current = current.next;
@@ -560,14 +560,14 @@ class SinglyLinkedList extends base_1.IterableElementBase {
560
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.
561
561
  * Space Complexity: O(1) - Constant space.
562
562
  *
563
- * 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.
564
564
  * @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
565
565
  * the new value will be inserted. It can be either the value of the existing node or the existing node itself.
566
566
  * @param {E} newValue - The value that you want to insert into the linked list after the existing value or node.
567
567
  * @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
568
568
  * existing value or node, and false if the existing value or node was not found in the linked list.
569
569
  */
570
- insertAfter(existingValueOrNode, newValue) {
570
+ addAfter(existingValueOrNode, newValue) {
571
571
  let existingNode;
572
572
  if (existingValueOrNode instanceof SinglyLinkedListNode) {
573
573
  existingNode = existingValueOrNode;
@@ -582,7 +582,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
582
582
  if (existingNode === this.tail) {
583
583
  this._tail = newNode;
584
584
  }
585
- this._length++;
585
+ this._size++;
586
586
  return true;
587
587
  }
588
588
  return false;
@@ -669,13 +669,6 @@ class SinglyLinkedList extends base_1.IterableElementBase {
669
669
  }
670
670
  return mappedList;
671
671
  }
672
- /**
673
- * Time Complexity: O(n), where n is the number of elements in the linked list.
674
- * Space Complexity: O(n)
675
- */
676
- print() {
677
- console.log([...this]);
678
- }
679
672
  *_getIterator() {
680
673
  let current = this.head;
681
674
  while (current) {
@@ -90,7 +90,7 @@ export declare class SkipList<K, V> {
90
90
  * Get the value of the first element (the smallest element) in the Skip List.
91
91
  * @returns The value of the first element, or undefined if the Skip List is empty.
92
92
  */
93
- getFirst(): V | undefined;
93
+ get first(): V | undefined;
94
94
  /**
95
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
96
  * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
@@ -102,7 +102,7 @@ export declare class SkipList<K, V> {
102
102
  * Get the value of the last element (the largest element) in the Skip List.
103
103
  * @returns The value of the last element, or undefined if the Skip List is empty.
104
104
  */
105
- getLast(): V | undefined;
105
+ get last(): V | undefined;
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.
@@ -158,7 +158,7 @@ class SkipList {
158
158
  * Get the value of the first element (the smallest element) in the Skip List.
159
159
  * @returns The value of the first element, or undefined if the Skip List is empty.
160
160
  */
161
- getFirst() {
161
+ get first() {
162
162
  const firstNode = this.head.forward[0];
163
163
  return firstNode ? firstNode.value : undefined;
164
164
  }
@@ -173,7 +173,7 @@ class SkipList {
173
173
  * Get the value of the last element (the largest element) in the Skip List.
174
174
  * @returns The value of the last element, or undefined if the Skip List is empty.
175
175
  */
176
- getLast() {
176
+ get last() {
177
177
  let current = this.head;
178
178
  for (let i = this.level - 1; i >= 0; i--) {
179
179
  while (current.forward[i]) {
@@ -5,8 +5,8 @@
5
5
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { PriorityQueue } from './priority-queue';
9
8
  import type { PriorityQueueOptions } from '../../types';
9
+ import { PriorityQueue } from './priority-queue';
10
10
  export declare class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
11
11
  constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
12
12
  }
@@ -1,13 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MaxPriorityQueue = void 0;
4
- /**
5
- * data-structure-typed
6
- *
7
- * @author Kirk Qi
8
- * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
9
- * @license MIT License
10
- */
11
4
  const priority_queue_1 = require("./priority-queue");
12
5
  class MaxPriorityQueue extends priority_queue_1.PriorityQueue {
13
6
  constructor(elements, options = {
@@ -5,8 +5,8 @@
5
5
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { PriorityQueue } from './priority-queue';
9
8
  import type { PriorityQueueOptions } from '../../types';
9
+ import { PriorityQueue } from './priority-queue';
10
10
  export declare class MinPriorityQueue<E = any> extends PriorityQueue<E> {
11
11
  constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
12
12
  }
@@ -1,13 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MinPriorityQueue = void 0;
4
- /**
5
- * data-structure-typed
6
- *
7
- * @author Kirk Qi
8
- * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
9
- * @license MIT License
10
- */
11
4
  const priority_queue_1 = require("./priority-queue");
12
5
  class MinPriorityQueue extends priority_queue_1.PriorityQueue {
13
6
  constructor(elements, options = {
@@ -5,8 +5,16 @@
5
5
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
+ import type { PriorityQueueOptions } from '../../types';
8
9
  import { Heap } from '../heap';
9
- import { PriorityQueueOptions } from '../../types';
10
+ /**
11
+ * 1. Element Priority: In a PriorityQueue, elements are sorted according to their priority. Each dequeue (element removal) operation removes the element with the highest priority. The priority can be determined based on the natural ordering of the elements or through a provided comparator (Comparator).
12
+ * 2. Heap-Based Implementation: PriorityQueue is typically implemented using a binary heap, allowing both insertion and removal operations to be completed in O(log n) time, where n is the number of elements in the queue.
13
+ * 3. Task Scheduling: In systems where tasks need to be processed based on the urgency of tasks rather than the order of arrival.
14
+ * 4. Dijkstra's Algorithm: In shortest path algorithms for graphs, used to select the next shortest edge to visit.
15
+ * 5. Huffman Coding: Used to select the smallest node combination when constructing a Huffman tree.
16
+ * 6. Kth Largest Element in a Data Stream: Used to maintain a min-heap of size K for quickly finding the Kth largest element in stream data
17
+ */
10
18
  export declare class PriorityQueue<E = any> extends Heap<E> {
11
19
  constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
12
20
  }
@@ -1,14 +1,15 @@
1
1
  "use strict";
2
- /**
3
- * data-structure-typed
4
- *
5
- * @author Kirk Qi
6
- * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
7
- * @license MIT License
8
- */
9
2
  Object.defineProperty(exports, "__esModule", { value: true });
10
3
  exports.PriorityQueue = void 0;
11
4
  const heap_1 = require("../heap");
5
+ /**
6
+ * 1. Element Priority: In a PriorityQueue, elements are sorted according to their priority. Each dequeue (element removal) operation removes the element with the highest priority. The priority can be determined based on the natural ordering of the elements or through a provided comparator (Comparator).
7
+ * 2. Heap-Based Implementation: PriorityQueue is typically implemented using a binary heap, allowing both insertion and removal operations to be completed in O(log n) time, where n is the number of elements in the queue.
8
+ * 3. Task Scheduling: In systems where tasks need to be processed based on the urgency of tasks rather than the order of arrival.
9
+ * 4. Dijkstra's Algorithm: In shortest path algorithms for graphs, used to select the next shortest edge to visit.
10
+ * 5. Huffman Coding: Used to select the smallest node combination when constructing a Huffman tree.
11
+ * 6. Kth Largest Element in a Data Stream: Used to maintain a min-heap of size K for quickly finding the Kth largest element in stream data
12
+ */
12
13
  class PriorityQueue extends heap_1.Heap {
13
14
  constructor(elements, options) {
14
15
  super(elements, options);
@@ -5,13 +5,14 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { ElementCallback, IterableWithSizeOrLength } from "../../types";
8
+ import type { ElementCallback, IterableWithSizeOrLength } from "../../types";
9
9
  import { IterableElementBase } from "../base";
10
10
  /**
11
- * Deque can provide random access with O(1) time complexity
12
- * Deque is usually more compact and efficient in memory usage because it does not require additional space to store pointers.
13
- * Deque may experience performance jitter, but DoublyLinkedList will not
14
- * Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
11
+ * 1. Operations at Both Ends: Supports adding and removing elements at both the front and back of the queue. This allows it to be used as a stack (last in, first out) and a queue (first in, first out).
12
+ * 2. Efficient Random Access: Being based on an array, it offers fast random access capability, allowing constant time access to any element.
13
+ * 3. Continuous Memory Allocation: Since it is based on an array, all elements are stored contiguously in memory, which can bring cache friendliness and efficient memory access.
14
+ * 4. Efficiency: Adding and removing elements at both ends of a deque is usually very fast. However, when the dynamic array needs to expand, it may involve copying the entire array to a larger one, and this operation has a time complexity of O(n).
15
+ * 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
15
16
  */
16
17
  export declare class Deque<E> extends IterableElementBase<E> {
17
18
  protected _bucketFirst: number;
@@ -41,68 +42,6 @@ export declare class Deque<E> extends IterableElementBase<E> {
41
42
  */
42
43
  get first(): E | undefined;
43
44
  get last(): E | undefined;
44
- /**
45
- * Time Complexity: O(1) - Removes the last element.
46
- * Space Complexity: O(1) - Operates in-place.
47
- */
48
- isEmpty(): boolean;
49
- /**
50
- * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
51
- * Space Complexity: O(n) - Due to potential resizing.
52
- */
53
- /**
54
- * Time Complexity: O(1)
55
- * Space Complexity: O(n) - In worst case, resizing doubles the array size.
56
- *
57
- * The addLast function adds an element to the end of an array.
58
- * @param {E} element - The element parameter represents the element that you want to add to the end of the
59
- * data structure.
60
- */
61
- addLast(element: E): void;
62
- /**
63
- * Time Complexity: O(1) - Removes the first element.
64
- * Space Complexity: O(1) - In-place operation.
65
- */
66
- /**
67
- * Time Complexity: O(1) - Removes the last element.
68
- * Space Complexity: O(1) - Operates in-place.
69
- *
70
- * The function "pollLast" removes and returns the last element of an array.
71
- * @returns The last element of the array is being returned.
72
- */
73
- pollLast(): E | undefined;
74
- /**
75
- * Time Complexity: O(1).
76
- * Space Complexity: O(n) - Due to potential resizing.
77
- *
78
- * The "addFirst" function adds an element to the beginning of an array.
79
- * @param {E} element - The parameter "element" represents the element that you want to add to the
80
- * beginning of the data structure.
81
- */
82
- addFirst(element: E): void;
83
- /**
84
- * Time Complexity: O(1) - Removes the first element.
85
- * Space Complexity: O(1) - In-place operation.
86
- *
87
- * The function "pollFirst" removes and returns the first element of an array.
88
- * @returns The method `pollFirst()` is returning the first element of the array after removing it
89
- * from the beginning. If the array is empty, it will return `undefined`.
90
- */
91
- pollFirst(): E | undefined;
92
- /**
93
- * The clear() function resets the state of the object by initializing all variables to their default
94
- * values.
95
- */
96
- clear(): void;
97
- /**
98
- * The below function is a generator that yields elements from a collection one by one.
99
- */
100
- begin(): Generator<E>;
101
- /**
102
- * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
103
- * the last element.
104
- */
105
- reverseBegin(): Generator<E>;
106
45
  /**
107
46
  * Time Complexity - Amortized O(1) (possible reallocation)
108
47
  * Space Complexity - O(n) (due to potential resizing).
@@ -116,7 +55,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
116
55
  * structure.
117
56
  * @returns The size of the data structure after the element has been pushed.
118
57
  */
119
- push(element: E): number;
58
+ push(element: E): boolean;
120
59
  /**
121
60
  * Time Complexity: O(1)
122
61
  * Space Complexity: O(1)
@@ -144,7 +83,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
144
83
  * beginning of the data structure.
145
84
  * @returns The size of the data structure after the element has been added.
146
85
  */
147
- unshift(element: E): number;
86
+ unshift(element: E): boolean;
148
87
  /**
149
88
  * Time Complexity: O(1)
150
89
  * Space Complexity: O(1)
@@ -159,6 +98,25 @@ export declare class Deque<E> extends IterableElementBase<E> {
159
98
  * returned.
160
99
  */
161
100
  shift(): E | undefined;
101
+ /**
102
+ * Time Complexity: O(1) - Removes the last element.
103
+ * Space Complexity: O(1) - Operates in-place.
104
+ */
105
+ isEmpty(): boolean;
106
+ /**
107
+ * The clear() function resets the state of the object by initializing all variables to their default
108
+ * values.
109
+ */
110
+ clear(): void;
111
+ /**
112
+ * The below function is a generator that yields elements from a collection one by one.
113
+ */
114
+ begin(): Generator<E>;
115
+ /**
116
+ * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
117
+ * the last element.
118
+ */
119
+ reverseBegin(): Generator<E>;
162
120
  /**
163
121
  * Time Complexity: O(1)
164
122
  * Space Complexity: O(1)
@@ -188,7 +146,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
188
146
  * @param {E} element - The `element` parameter is the value that you want to set at the specified
189
147
  * position in the data structure.
190
148
  */
191
- setAt(pos: number, element: E): void;
149
+ setAt(pos: number, element: E): boolean;
192
150
  /**
193
151
  * Time Complexity: O(n)
194
152
  * Space Complexity: O(n)
@@ -197,7 +155,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
197
155
  * Time Complexity: O(n)
198
156
  * Space Complexity: O(n)
199
157
  *
200
- * The `insertAt` function inserts one or more elements at a specified position in an array-like data
158
+ * The `addAt` function inserts one or more elements at a specified position in an array-like data
201
159
  * structure.
202
160
  * @param {number} pos - The `pos` parameter represents the position at which the element(s) should
203
161
  * be inserted. It is of type `number`.
@@ -208,7 +166,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
208
166
  * will be inserted once. However, you can provide a different value for `num` if you want
209
167
  * @returns The size of the array after the insertion is being returned.
210
168
  */
211
- insertAt(pos: number, element: E, num?: number): number;
169
+ addAt(pos: number, element: E, num?: number): boolean;
212
170
  /**
213
171
  * Time Complexity: O(1)
214
172
  * Space Complexity: O(1)
@@ -239,7 +197,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
239
197
  * the index of the element to be deleted.
240
198
  * @returns The size of the data structure after the deletion operation is performed.
241
199
  */
242
- deleteAt(pos: number): number;
200
+ deleteAt(pos: number): boolean;
243
201
  /**
244
202
  * Time Complexity: O(n)
245
203
  * Space Complexity: O(1)
@@ -254,7 +212,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
254
212
  * the data structure.
255
213
  * @returns The size of the data structure after the element has been deleted.
256
214
  */
257
- delete(element: E): number;
215
+ delete(element: E): boolean;
258
216
  /**
259
217
  * Time Complexity: O(n)
260
218
  * Space Complexity: O(1)
@@ -281,7 +239,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
281
239
  * the number of unique elements.
282
240
  * @returns The size of the modified array is being returned.
283
241
  */
284
- unique(): number;
242
+ unique(): this;
285
243
  /**
286
244
  * Time Complexity: O(n log n)
287
245
  * Space Complexity: O(n)
@@ -294,7 +252,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
294
252
  * @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
295
253
  * `y` of type `E` and returns a number. The comparator function is used to determine the order of
296
254
  * the elements in the sorted array.
297
- * @returns The method is returning the sorted instance of the object on which the method is called.
255
+ * @returns Deque<E>
298
256
  */
299
257
  sort(comparator?: (x: E, y: E) => number): this;
300
258
  /**
@@ -395,10 +353,48 @@ export declare class Deque<E> extends IterableElementBase<E> {
395
353
  */
396
354
  map<T>(callback: ElementCallback<E, T>, thisArg?: any): Deque<T>;
397
355
  /**
398
- * Time Complexity: O(n)
399
- * Space Complexity: O(n)
356
+ * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
357
+ * Space Complexity: O(n) - Due to potential resizing.
358
+ */
359
+ /**
360
+ * Time Complexity: O(1)
361
+ * Space Complexity: O(n) - In worst case, resizing doubles the array size.
362
+ *
363
+ * The addLast function adds an element to the end of an array.
364
+ * @param {E} element - The element parameter represents the element that you want to add to the end of the
365
+ * data structure.
366
+ */
367
+ addLast(element: E): boolean;
368
+ /**
369
+ * Time Complexity: O(1) - Removes the first element.
370
+ * Space Complexity: O(1) - In-place operation.
371
+ */
372
+ /**
373
+ * Time Complexity: O(1) - Removes the last element.
374
+ * Space Complexity: O(1) - Operates in-place.
375
+ *
376
+ * The function "pollLast" removes and returns the last element of an array.
377
+ * @returns The last element of the array is being returned.
378
+ */
379
+ pollLast(): E | undefined;
380
+ /**
381
+ * Time Complexity: O(1).
382
+ * Space Complexity: O(n) - Due to potential resizing.
383
+ *
384
+ * The "addFirst" function adds an element to the beginning of an array.
385
+ * @param {E} element - The parameter "element" represents the element that you want to add to the
386
+ * beginning of the data structure.
400
387
  */
401
- print(): void;
388
+ addFirst(element: E): boolean;
389
+ /**
390
+ * Time Complexity: O(1) - Removes the first element.
391
+ * Space Complexity: O(1) - In-place operation.
392
+ *
393
+ * The function "pollFirst" removes and returns the first element of an array.
394
+ * @returns The method `pollFirst()` is returning the first element of the array after removing it
395
+ * from the beginning. If the array is empty, it will return `undefined`.
396
+ */
397
+ pollFirst(): E | undefined;
402
398
  /**
403
399
  * Time Complexity: O(n)
404
400
  * Space Complexity: O(1)
@@ -406,7 +402,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
406
402
  * The above function is an implementation of the iterator protocol in TypeScript, allowing the
407
403
  * object to be iterated over using a for...of loop.
408
404
  */
409
- protected _getIterator(): Generator<E, void, unknown>;
405
+ protected _getIterator(): IterableIterator<E>;
410
406
  /**
411
407
  * Time Complexity: O(n)
412
408
  * Space Complexity: O(n)