bst-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
@@ -1,20 +1,14 @@
1
1
  "use strict";
2
- /**
3
- * data-structure-typed
4
- *
5
- * @author Tyler Zeng
6
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
7
- * @license MIT License
8
- */
9
2
  Object.defineProperty(exports, "__esModule", { value: true });
10
3
  exports.Deque = void 0;
11
- const utils_1 = require("../../utils");
12
4
  const base_1 = require("../base");
5
+ const utils_1 = require("../../utils");
13
6
  /**
14
- * Deque can provide random access with O(1) time complexity
15
- * Deque is usually more compact and efficient in memory usage because it does not require additional space to store pointers.
16
- * Deque may experience performance jitter, but DoublyLinkedList will not
17
- * Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
7
+ * 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).
8
+ * 2. Efficient Random Access: Being based on an array, it offers fast random access capability, allowing constant time access to any element.
9
+ * 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.
10
+ * 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).
11
+ * 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
18
12
  */
19
13
  class Deque extends base_1.IterableElementBase {
20
14
  /**
@@ -81,95 +75,6 @@ class Deque extends base_1.IterableElementBase {
81
75
  return;
82
76
  return this._buckets[this._bucketLast][this._lastInBucket];
83
77
  }
84
- /**
85
- * Time Complexity: O(1) - Removes the last element.
86
- * Space Complexity: O(1) - Operates in-place.
87
- */
88
- isEmpty() {
89
- return this.size === 0;
90
- }
91
- /**
92
- * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
93
- * Space Complexity: O(n) - Due to potential resizing.
94
- */
95
- /**
96
- * Time Complexity: O(1)
97
- * Space Complexity: O(n) - In worst case, resizing doubles the array size.
98
- *
99
- * The addLast function adds an element to the end of an array.
100
- * @param {E} element - The element parameter represents the element that you want to add to the end of the
101
- * data structure.
102
- */
103
- addLast(element) {
104
- this.push(element);
105
- }
106
- /**
107
- * Time Complexity: O(1) - Removes the first element.
108
- * Space Complexity: O(1) - In-place operation.
109
- */
110
- /**
111
- * Time Complexity: O(1) - Removes the last element.
112
- * Space Complexity: O(1) - Operates in-place.
113
- *
114
- * The function "pollLast" removes and returns the last element of an array.
115
- * @returns The last element of the array is being returned.
116
- */
117
- pollLast() {
118
- return this.pop();
119
- }
120
- /**
121
- * Time Complexity: O(1).
122
- * Space Complexity: O(n) - Due to potential resizing.
123
- *
124
- * The "addFirst" function adds an element to the beginning of an array.
125
- * @param {E} element - The parameter "element" represents the element that you want to add to the
126
- * beginning of the data structure.
127
- */
128
- addFirst(element) {
129
- this.unshift(element);
130
- }
131
- /**
132
- * Time Complexity: O(1) - Removes the first element.
133
- * Space Complexity: O(1) - In-place operation.
134
- *
135
- * The function "pollFirst" removes and returns the first element of an array.
136
- * @returns The method `pollFirst()` is returning the first element of the array after removing it
137
- * from the beginning. If the array is empty, it will return `undefined`.
138
- */
139
- pollFirst() {
140
- return this.shift();
141
- }
142
- /**
143
- * The clear() function resets the state of the object by initializing all variables to their default
144
- * values.
145
- */
146
- clear() {
147
- this._buckets = [new Array(this._bucketSize)];
148
- this._bucketCount = 1;
149
- this._bucketFirst = this._bucketLast = this._size = 0;
150
- this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
151
- }
152
- /**
153
- * The below function is a generator that yields elements from a collection one by one.
154
- */
155
- *begin() {
156
- let index = 0;
157
- while (index < this.size) {
158
- yield this.getAt(index);
159
- index++;
160
- }
161
- }
162
- /**
163
- * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
164
- * the last element.
165
- */
166
- *reverseBegin() {
167
- let index = this.size - 1;
168
- while (index >= 0) {
169
- yield this.getAt(index);
170
- index--;
171
- }
172
- }
173
78
  /**
174
79
  * Time Complexity - Amortized O(1) (possible reallocation)
175
80
  * Space Complexity - O(n) (due to potential resizing).
@@ -202,7 +107,7 @@ class Deque extends base_1.IterableElementBase {
202
107
  }
203
108
  this._size += 1;
204
109
  this._buckets[this._bucketLast][this._lastInBucket] = element;
205
- return this.size;
110
+ return true;
206
111
  }
207
112
  /**
208
113
  * Time Complexity: O(1)
@@ -269,7 +174,7 @@ class Deque extends base_1.IterableElementBase {
269
174
  }
270
175
  this._size += 1;
271
176
  this._buckets[this._bucketFirst][this._firstInBucket] = element;
272
- return this.size;
177
+ return true;
273
178
  }
274
179
  /**
275
180
  * Time Complexity: O(1)
@@ -304,6 +209,44 @@ class Deque extends base_1.IterableElementBase {
304
209
  this._size -= 1;
305
210
  return element;
306
211
  }
212
+ /**
213
+ * Time Complexity: O(1) - Removes the last element.
214
+ * Space Complexity: O(1) - Operates in-place.
215
+ */
216
+ isEmpty() {
217
+ return this.size === 0;
218
+ }
219
+ /**
220
+ * The clear() function resets the state of the object by initializing all variables to their default
221
+ * values.
222
+ */
223
+ clear() {
224
+ this._buckets = [new Array(this._bucketSize)];
225
+ this._bucketCount = 1;
226
+ this._bucketFirst = this._bucketLast = this._size = 0;
227
+ this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
228
+ }
229
+ /**
230
+ * The below function is a generator that yields elements from a collection one by one.
231
+ */
232
+ *begin() {
233
+ let index = 0;
234
+ while (index < this.size) {
235
+ yield this.getAt(index);
236
+ index++;
237
+ }
238
+ }
239
+ /**
240
+ * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
241
+ * the last element.
242
+ */
243
+ *reverseBegin() {
244
+ let index = this.size - 1;
245
+ while (index >= 0) {
246
+ yield this.getAt(index);
247
+ index--;
248
+ }
249
+ }
307
250
  /**
308
251
  * Time Complexity: O(1)
309
252
  * Space Complexity: O(1)
@@ -341,6 +284,7 @@ class Deque extends base_1.IterableElementBase {
341
284
  (0, utils_1.rangeCheck)(pos, 0, this.size - 1);
342
285
  const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
343
286
  this._buckets[bucketIndex][indexInBucket] = element;
287
+ return true;
344
288
  }
345
289
  /**
346
290
  * Time Complexity: O(n)
@@ -350,7 +294,7 @@ class Deque extends base_1.IterableElementBase {
350
294
  * Time Complexity: O(n)
351
295
  * Space Complexity: O(n)
352
296
  *
353
- * The `insertAt` function inserts one or more elements at a specified position in an array-like data
297
+ * The `addAt` function inserts one or more elements at a specified position in an array-like data
354
298
  * structure.
355
299
  * @param {number} pos - The `pos` parameter represents the position at which the element(s) should
356
300
  * be inserted. It is of type `number`.
@@ -361,7 +305,7 @@ class Deque extends base_1.IterableElementBase {
361
305
  * will be inserted once. However, you can provide a different value for `num` if you want
362
306
  * @returns The size of the array after the insertion is being returned.
363
307
  */
364
- insertAt(pos, element, num = 1) {
308
+ addAt(pos, element, num = 1) {
365
309
  const length = this.size;
366
310
  (0, utils_1.rangeCheck)(pos, 0, length);
367
311
  if (pos === 0) {
@@ -383,7 +327,7 @@ class Deque extends base_1.IterableElementBase {
383
327
  for (let i = 0; i < arr.length; ++i)
384
328
  this.push(arr[i]);
385
329
  }
386
- return this.size;
330
+ return true;
387
331
  }
388
332
  /**
389
333
  * Time Complexity: O(1)
@@ -442,7 +386,7 @@ class Deque extends base_1.IterableElementBase {
442
386
  }
443
387
  this.pop();
444
388
  }
445
- return this.size;
389
+ return true;
446
390
  }
447
391
  /**
448
392
  * Time Complexity: O(n)
@@ -461,7 +405,7 @@ class Deque extends base_1.IterableElementBase {
461
405
  delete(element) {
462
406
  const size = this.size;
463
407
  if (size === 0)
464
- return 0;
408
+ return false;
465
409
  let i = 0;
466
410
  let index = 0;
467
411
  while (i < size) {
@@ -473,7 +417,7 @@ class Deque extends base_1.IterableElementBase {
473
417
  i += 1;
474
418
  }
475
419
  this.cut(index - 1);
476
- return this.size;
420
+ return true;
477
421
  }
478
422
  /**
479
423
  * Time Complexity: O(n)
@@ -513,7 +457,7 @@ class Deque extends base_1.IterableElementBase {
513
457
  */
514
458
  unique() {
515
459
  if (this.size <= 1) {
516
- return this.size;
460
+ return this;
517
461
  }
518
462
  let index = 1;
519
463
  let prev = this.getAt(0);
@@ -525,7 +469,7 @@ class Deque extends base_1.IterableElementBase {
525
469
  }
526
470
  }
527
471
  this.cut(index - 1);
528
- return this.size;
472
+ return this;
529
473
  }
530
474
  /**
531
475
  * Time Complexity: O(n log n)
@@ -539,7 +483,7 @@ class Deque extends base_1.IterableElementBase {
539
483
  * @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
540
484
  * `y` of type `E` and returns a number. The comparator function is used to determine the order of
541
485
  * the elements in the sorted array.
542
- * @returns The method is returning the sorted instance of the object on which the method is called.
486
+ * @returns Deque<E>
543
487
  */
544
488
  sort(comparator) {
545
489
  const arr = [];
@@ -610,7 +554,7 @@ class Deque extends base_1.IterableElementBase {
610
554
  return element;
611
555
  }
612
556
  }
613
- return undefined;
557
+ return;
614
558
  }
615
559
  /**
616
560
  * Time Complexity: O(n)
@@ -647,11 +591,7 @@ class Deque extends base_1.IterableElementBase {
647
591
  * @returns The `toArray()` method is returning an array of elements of type `E`.
648
592
  */
649
593
  toArray() {
650
- const arr = [];
651
- for (let i = 0; i < this.size; ++i) {
652
- arr.push(this.getAt(i));
653
- }
654
- return arr;
594
+ return [...this];
655
595
  }
656
596
  /**
657
597
  * Time Complexity: O(n)
@@ -711,11 +651,55 @@ class Deque extends base_1.IterableElementBase {
711
651
  return newDeque;
712
652
  }
713
653
  /**
714
- * Time Complexity: O(n)
715
- * Space Complexity: O(n)
654
+ * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
655
+ * Space Complexity: O(n) - Due to potential resizing.
656
+ */
657
+ /**
658
+ * Time Complexity: O(1)
659
+ * Space Complexity: O(n) - In worst case, resizing doubles the array size.
660
+ *
661
+ * The addLast function adds an element to the end of an array.
662
+ * @param {E} element - The element parameter represents the element that you want to add to the end of the
663
+ * data structure.
664
+ */
665
+ addLast(element) {
666
+ return this.push(element);
667
+ }
668
+ /**
669
+ * Time Complexity: O(1) - Removes the first element.
670
+ * Space Complexity: O(1) - In-place operation.
671
+ */
672
+ /**
673
+ * Time Complexity: O(1) - Removes the last element.
674
+ * Space Complexity: O(1) - Operates in-place.
675
+ *
676
+ * The function "pollLast" removes and returns the last element of an array.
677
+ * @returns The last element of the array is being returned.
716
678
  */
717
- print() {
718
- console.log([...this]);
679
+ pollLast() {
680
+ return this.pop();
681
+ }
682
+ /**
683
+ * Time Complexity: O(1).
684
+ * Space Complexity: O(n) - Due to potential resizing.
685
+ *
686
+ * The "addFirst" function adds an element to the beginning of an array.
687
+ * @param {E} element - The parameter "element" represents the element that you want to add to the
688
+ * beginning of the data structure.
689
+ */
690
+ addFirst(element) {
691
+ return this.unshift(element);
692
+ }
693
+ /**
694
+ * Time Complexity: O(1) - Removes the first element.
695
+ * Space Complexity: O(1) - In-place operation.
696
+ *
697
+ * The function "pollFirst" removes and returns the first element of an array.
698
+ * @returns The method `pollFirst()` is returning the first element of the array after removing it
699
+ * from the beginning. If the array is empty, it will return `undefined`.
700
+ */
701
+ pollFirst() {
702
+ return this.shift();
719
703
  }
720
704
  /**
721
705
  * Time Complexity: O(n)
@@ -3,9 +3,18 @@
3
3
  * @copyright Tyler Zeng <zrwusa@gmail.com>
4
4
  * @class
5
5
  */
6
- import { SinglyLinkedList } from '../linked-list';
6
+ import type { ElementCallback } from '../../types';
7
7
  import { IterableElementBase } from "../base";
8
- import { ElementCallback } from "../../types";
8
+ import { SinglyLinkedList } from '../linked-list';
9
+ /**
10
+ * 1. First In, First Out (FIFO): The core feature of a queue is its first in, first out nature. The element added to the queue first will be the one to be removed first.
11
+ * 2. Operations: The main operations include enqueue (adding an element to the end of the queue) and dequeue (removing and returning the element at the front of the queue). Typically, there is also a peek operation (looking at the front element without removing it).
12
+ * 3. Uses: Queues are commonly used to manage a series of tasks or elements that need to be processed in order. For example, managing task queues in a multi-threaded environment, or in algorithms for data structures like trees and graphs for breadth-first search.
13
+ * 4. Task Scheduling: Managing the order of task execution in operating systems or applications.
14
+ * 5. Data Buffering: Acting as a buffer for data packets in network communication.
15
+ * 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store nodes that are to be visited.
16
+ * 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
17
+ */
9
18
  export declare class Queue<E = any> extends IterableElementBase<E> {
10
19
  /**
11
20
  * The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
@@ -44,7 +53,7 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
44
53
  * @param {E} element - The `element` parameter represents the element that you want to add to the queue.
45
54
  * @returns The `add` method is returning a `Queue<E>` object.
46
55
  */
47
- push(element: E): Queue<E>;
56
+ push(element: E): boolean;
48
57
  /**
49
58
  * Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
50
59
  * Space Complexity: O(1) - no additional space is used.
@@ -66,11 +75,11 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
66
75
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
67
76
  * Space Complexity: O(1) - no additional space is used.
68
77
  *
69
- * The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
70
- * @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
78
+ * The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
79
+ * @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
71
80
  * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
72
81
  */
73
- getFirst(): E | undefined;
82
+ get first(): E | undefined;
74
83
  /**
75
84
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
76
85
  * Space Complexity: O(1) - no additional space is used.
@@ -92,11 +101,11 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
92
101
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
93
102
  * Space Complexity: O(1) - no additional space is used.
94
103
  *
95
- * The `getLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
96
- * @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
104
+ * The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
105
+ * @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
97
106
  * array is empty, it returns `undefined`.
98
107
  */
99
- getLast(): E | undefined;
108
+ get last(): E | undefined;
100
109
  /**
101
110
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
102
111
  * Space Complexity: O(1) - no additional space is used.
@@ -121,7 +130,7 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
121
130
  * The enqueue function adds a value to the end of a queue.
122
131
  * @param {E} value - The value parameter represents the value that you want to add to the queue.
123
132
  */
124
- enqueue(value: E): void;
133
+ enqueue(value: E): boolean;
125
134
  /**
126
135
  * Time Complexity: O(n) - same as shift().
127
136
  * Space Complexity: O(1) - same as shift().
@@ -185,7 +194,6 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
185
194
  * @returns The `clone()` method is returning a new instance of the `Queue` class.
186
195
  */
187
196
  clone(): Queue<E>;
188
- print(): void;
189
197
  /**
190
198
  * Time Complexity: O(n)
191
199
  * Space Complexity: O(n)
@@ -230,24 +238,30 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
230
238
  * Time Complexity: O(n)
231
239
  * Space Complexity: O(n)
232
240
  */
233
- protected _getIterator(): Generator<E, void, unknown>;
241
+ protected _getIterator(): IterableIterator<E>;
234
242
  }
243
+ /**
244
+ * 1. First In, First Out (FIFO) Strategy: Like other queue implementations, LinkedListQueue follows the first in, first out principle, meaning the element that is added to the queue first will be the first to be removed.
245
+ * 2. Based on Linked List: LinkedListQueue uses a linked list to store elements. Each node in the linked list contains data and a pointer to the next node.
246
+ * 3. Memory Usage: Since each element requires additional space to store a pointer to the next element, linked lists may use more memory compared to arrays.
247
+ * 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
248
+ */
235
249
  export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
236
250
  /**
237
251
  * The enqueue function adds a value to the end of an array.
238
252
  * @param {E} value - The value parameter represents the value that you want to add to the queue.
239
253
  */
240
- enqueue(value: E): void;
254
+ enqueue(value: E): boolean;
241
255
  /**
242
256
  * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
243
257
  * @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
244
258
  */
245
259
  dequeue(): E | undefined;
246
260
  /**
247
- * The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
248
- * @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
261
+ * The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
262
+ * @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
249
263
  */
250
- getFirst(): E | undefined;
264
+ get first(): E | undefined;
251
265
  /**
252
266
  * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
253
267
  * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
@@ -1,13 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LinkedListQueue = exports.Queue = void 0;
4
+ const base_1 = require("../base");
5
+ const linked_list_1 = require("../linked-list");
4
6
  /**
5
- * @license MIT
6
- * @copyright Tyler Zeng <zrwusa@gmail.com>
7
- * @class
7
+ * 1. First In, First Out (FIFO): The core feature of a queue is its first in, first out nature. The element added to the queue first will be the one to be removed first.
8
+ * 2. Operations: The main operations include enqueue (adding an element to the end of the queue) and dequeue (removing and returning the element at the front of the queue). Typically, there is also a peek operation (looking at the front element without removing it).
9
+ * 3. Uses: Queues are commonly used to manage a series of tasks or elements that need to be processed in order. For example, managing task queues in a multi-threaded environment, or in algorithms for data structures like trees and graphs for breadth-first search.
10
+ * 4. Task Scheduling: Managing the order of task execution in operating systems or applications.
11
+ * 5. Data Buffering: Acting as a buffer for data packets in network communication.
12
+ * 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store nodes that are to be visited.
13
+ * 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
8
14
  */
9
- const linked_list_1 = require("../linked-list");
10
- const base_1 = require("../base");
11
15
  class Queue extends base_1.IterableElementBase {
12
16
  /**
13
17
  * The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
@@ -58,7 +62,7 @@ class Queue extends base_1.IterableElementBase {
58
62
  */
59
63
  push(element) {
60
64
  this.nodes.push(element);
61
- return this;
65
+ return true;
62
66
  }
63
67
  /**
64
68
  * Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
@@ -75,7 +79,7 @@ class Queue extends base_1.IterableElementBase {
75
79
  shift() {
76
80
  if (this.size === 0)
77
81
  return undefined;
78
- const first = this.getFirst();
82
+ const first = this.first;
79
83
  this._offset += 1;
80
84
  if (this.offset * 2 < this.nodes.length)
81
85
  return first;
@@ -93,11 +97,11 @@ class Queue extends base_1.IterableElementBase {
93
97
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
94
98
  * Space Complexity: O(1) - no additional space is used.
95
99
  *
96
- * The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
97
- * @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
100
+ * The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
101
+ * @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
98
102
  * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
99
103
  */
100
- getFirst() {
104
+ get first() {
101
105
  return this.size > 0 ? this.nodes[this.offset] : undefined;
102
106
  }
103
107
  /**
@@ -113,7 +117,7 @@ class Queue extends base_1.IterableElementBase {
113
117
  * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
114
118
  */
115
119
  peek() {
116
- return this.getFirst();
120
+ return this.first;
117
121
  }
118
122
  /**
119
123
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
@@ -123,11 +127,11 @@ class Queue extends base_1.IterableElementBase {
123
127
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
124
128
  * Space Complexity: O(1) - no additional space is used.
125
129
  *
126
- * The `getLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
127
- * @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
130
+ * The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
131
+ * @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
128
132
  * array is empty, it returns `undefined`.
129
133
  */
130
- getLast() {
134
+ get last() {
131
135
  return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
132
136
  }
133
137
  /**
@@ -143,7 +147,7 @@ class Queue extends base_1.IterableElementBase {
143
147
  * array is empty, it returns `undefined`.
144
148
  */
145
149
  peekLast() {
146
- return this.getLast();
150
+ return this.last;
147
151
  }
148
152
  /**
149
153
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
@@ -157,7 +161,7 @@ class Queue extends base_1.IterableElementBase {
157
161
  * @param {E} value - The value parameter represents the value that you want to add to the queue.
158
162
  */
159
163
  enqueue(value) {
160
- this.push(value);
164
+ return this.push(value);
161
165
  }
162
166
  /**
163
167
  * Time Complexity: O(n) - same as shift().
@@ -235,9 +239,6 @@ class Queue extends base_1.IterableElementBase {
235
239
  clone() {
236
240
  return new Queue(this.nodes.slice(this.offset));
237
241
  }
238
- print() {
239
- console.log([...this]);
240
- }
241
242
  /**
242
243
  * Time Complexity: O(n)
243
244
  * Space Complexity: O(n)
@@ -307,13 +308,19 @@ class Queue extends base_1.IterableElementBase {
307
308
  }
308
309
  }
309
310
  exports.Queue = Queue;
311
+ /**
312
+ * 1. First In, First Out (FIFO) Strategy: Like other queue implementations, LinkedListQueue follows the first in, first out principle, meaning the element that is added to the queue first will be the first to be removed.
313
+ * 2. Based on Linked List: LinkedListQueue uses a linked list to store elements. Each node in the linked list contains data and a pointer to the next node.
314
+ * 3. Memory Usage: Since each element requires additional space to store a pointer to the next element, linked lists may use more memory compared to arrays.
315
+ * 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
316
+ */
310
317
  class LinkedListQueue extends linked_list_1.SinglyLinkedList {
311
318
  /**
312
319
  * The enqueue function adds a value to the end of an array.
313
320
  * @param {E} value - The value parameter represents the value that you want to add to the queue.
314
321
  */
315
322
  enqueue(value) {
316
- this.push(value);
323
+ return this.push(value);
317
324
  }
318
325
  /**
319
326
  * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
@@ -323,10 +330,10 @@ class LinkedListQueue extends linked_list_1.SinglyLinkedList {
323
330
  return this.shift();
324
331
  }
325
332
  /**
326
- * The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
327
- * @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
333
+ * The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
334
+ * @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
328
335
  */
329
- getFirst() {
336
+ get first() {
330
337
  var _a;
331
338
  return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
332
339
  }
@@ -335,7 +342,7 @@ class LinkedListQueue extends linked_list_1.SinglyLinkedList {
335
342
  * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
336
343
  */
337
344
  peek() {
338
- return this.getFirst();
345
+ return this.first;
339
346
  }
340
347
  }
341
348
  exports.LinkedListQueue = LinkedListQueue;
@@ -1,9 +1,19 @@
1
- import { IterableElementBase } from "../base";
2
- import { ElementCallback } from "../../types";
3
1
  /**
4
- * @license MIT
5
- * @copyright Tyler Zeng <zrwusa@gmail.com>
6
- * @class
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ import type { ElementCallback } from '../../types';
9
+ import { IterableElementBase } from '../base';
10
+ /**
11
+ * 1. Last In, First Out (LIFO): The core characteristic of a stack is its last in, first out nature, meaning the last element added to the stack will be the first to be removed.
12
+ * 2. Uses: Stacks are commonly used for managing a series of tasks or elements that need to be processed in a last in, first out manner. They are widely used in various scenarios, such as in function calls in programming languages, evaluation of arithmetic expressions, and backtracking algorithms.
13
+ * 3. Performance: Stack operations are typically O(1) in time complexity, meaning that regardless of the stack's size, adding, removing, and viewing the top element are very fast operations.
14
+ * 4. Function Calls: In most modern programming languages, the records of function calls are managed through a stack. When a function is called, its record (including parameters, local variables, and return address) is 'pushed' into the stack. When the function returns, its record is 'popped' from the stack.
15
+ * 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
16
+ * 6. Backtracking Algorithms: In problems where multiple branches need to be explored but only one branch can be explored at a time, stacks can be used to save the state at each branching point.
7
17
  */
8
18
  export declare class Stack<E = any> extends IterableElementBase<E> {
9
19
  /**
@@ -63,7 +73,7 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
63
73
  * @param {E} element - The parameter "element" is of type E, which means it can be any data type.
64
74
  * @returns The `push` method is returning the updated `Stack<E>` object.
65
75
  */
66
- push(element: E): Stack<E>;
76
+ push(element: E): boolean;
67
77
  /**
68
78
  * Time Complexity: O(1), as it only involves accessing the last element of the array.
69
79
  * Space Complexity: O(1), as it does not use any additional space.
@@ -144,10 +154,9 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
144
154
  * @returns The `map` method is returning a new `Stack` object.
145
155
  */
146
156
  map<T>(callback: ElementCallback<E, T>, thisArg?: any): Stack<T>;
147
- print(): void;
148
157
  /**
149
158
  * Custom iterator for the Stack class.
150
159
  * @returns An iterator object.
151
160
  */
152
- protected _getIterator(): Generator<E, void, unknown>;
161
+ protected _getIterator(): IterableIterator<E>;
153
162
  }