priority-queue-typed 1.54.3 → 2.0.0
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.
- package/dist/data-structures/base/iterable-element-base.d.ts +14 -40
- package/dist/data-structures/base/iterable-element-base.js +14 -11
- package/dist/data-structures/base/linear-base.d.ts +277 -0
- package/dist/data-structures/base/linear-base.js +552 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +50 -37
- package/dist/data-structures/binary-tree/avl-tree.d.ts +64 -0
- package/dist/data-structures/binary-tree/avl-tree.js +64 -0
- package/dist/data-structures/binary-tree/binary-tree.js +5 -5
- package/dist/data-structures/binary-tree/bst.js +11 -11
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
- package/dist/data-structures/binary-tree/tree-multi-map.js +210 -40
- package/dist/data-structures/graph/abstract-graph.js +2 -2
- package/dist/data-structures/heap/heap.d.ts +3 -11
- package/dist/data-structures/heap/heap.js +0 -10
- package/dist/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/data-structures/linked-list/doubly-linked-list.js +131 -146
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +79 -75
- package/dist/data-structures/linked-list/singly-linked-list.js +217 -169
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -2
- package/dist/data-structures/queue/deque.d.ts +130 -91
- package/dist/data-structures/queue/deque.js +269 -169
- package/dist/data-structures/queue/queue.d.ts +84 -40
- package/dist/data-structures/queue/queue.js +134 -50
- package/dist/data-structures/stack/stack.d.ts +3 -11
- package/dist/data-structures/stack/stack.js +0 -10
- package/dist/data-structures/trie/trie.d.ts +4 -3
- package/dist/data-structures/trie/trie.js +3 -0
- package/dist/types/data-structures/base/base.d.ts +9 -4
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/types/data-structures/queue/queue.d.ts +2 -2
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +29 -20
- package/src/data-structures/base/linear-base.ts +649 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +51 -36
- package/src/data-structures/binary-tree/avl-tree.ts +64 -0
- package/src/data-structures/binary-tree/binary-tree.ts +5 -5
- package/src/data-structures/binary-tree/bst.ts +9 -9
- package/src/data-structures/binary-tree/tree-multi-map.ts +214 -40
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/heap/heap.ts +3 -14
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/heap/min-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
- package/src/data-structures/linked-list/singly-linked-list.ts +241 -185
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +286 -183
- package/src/data-structures/queue/queue.ts +149 -63
- package/src/data-structures/stack/stack.ts +3 -18
- package/src/data-structures/trie/trie.ts +7 -3
- package/src/types/data-structures/base/base.ts +17 -8
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
- package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/types/data-structures/queue/deque.ts +2 -3
- package/src/types/data-structures/queue/queue.ts +2 -2
|
@@ -6,15 +6,103 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { DequeOptions, ElementCallback, IterableWithSizeOrLength } from '../../types';
|
|
9
|
-
import {
|
|
9
|
+
import { LinearBase } from '../base/linear-base';
|
|
10
10
|
/**
|
|
11
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
12
|
* 2. Efficient Random Access: Being based on an array, it offers fast random access capability, allowing constant time access to any element.
|
|
13
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
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
15
|
* 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
|
|
16
|
+
* @example
|
|
17
|
+
* // prize roulette
|
|
18
|
+
* class PrizeRoulette {
|
|
19
|
+
* private deque: Deque<string>;
|
|
20
|
+
*
|
|
21
|
+
* constructor(prizes: string[]) {
|
|
22
|
+
* // Initialize the deque with prizes
|
|
23
|
+
* this.deque = new Deque<string>(prizes);
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* // Rotate clockwise to the right (forward)
|
|
27
|
+
* rotateClockwise(steps: number): void {
|
|
28
|
+
* const n = this.deque.length;
|
|
29
|
+
* if (n === 0) return;
|
|
30
|
+
*
|
|
31
|
+
* for (let i = 0; i < steps; i++) {
|
|
32
|
+
* const last = this.deque.pop(); // Remove the last element
|
|
33
|
+
* this.deque.unshift(last!); // Add it to the front
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* // Rotate counterclockwise to the left (backward)
|
|
38
|
+
* rotateCounterClockwise(steps: number): void {
|
|
39
|
+
* const n = this.deque.length;
|
|
40
|
+
* if (n === 0) return;
|
|
41
|
+
*
|
|
42
|
+
* for (let i = 0; i < steps; i++) {
|
|
43
|
+
* const first = this.deque.shift(); // Remove the first element
|
|
44
|
+
* this.deque.push(first!); // Add it to the back
|
|
45
|
+
* }
|
|
46
|
+
* }
|
|
47
|
+
*
|
|
48
|
+
* // Display the current prize at the head
|
|
49
|
+
* display() {
|
|
50
|
+
* return this.deque.first;
|
|
51
|
+
* }
|
|
52
|
+
* }
|
|
53
|
+
*
|
|
54
|
+
* // Example usage
|
|
55
|
+
* const prizes = ['Car', 'Bike', 'Laptop', 'Phone', 'Watch', 'Headphones']; // Initialize the prize list
|
|
56
|
+
* const roulette = new PrizeRoulette(prizes);
|
|
57
|
+
*
|
|
58
|
+
* // Display the initial state
|
|
59
|
+
* console.log(roulette.display()); // 'Car' // Car
|
|
60
|
+
*
|
|
61
|
+
* // Rotate clockwise by 3 steps
|
|
62
|
+
* roulette.rotateClockwise(3);
|
|
63
|
+
* console.log(roulette.display()); // 'Phone' // Phone
|
|
64
|
+
*
|
|
65
|
+
* // Rotate counterclockwise by 2 steps
|
|
66
|
+
* roulette.rotateCounterClockwise(2);
|
|
67
|
+
* console.log(roulette.display()); // 'Headphones'
|
|
68
|
+
* @example
|
|
69
|
+
* // sliding window
|
|
70
|
+
* // Maximum function of sliding window
|
|
71
|
+
* function maxSlidingWindow(nums: number[], k: number): number[] {
|
|
72
|
+
* const n = nums.length;
|
|
73
|
+
* if (n * k === 0) return [];
|
|
74
|
+
*
|
|
75
|
+
* const deq = new Deque<number>();
|
|
76
|
+
* const result: number[] = [];
|
|
77
|
+
*
|
|
78
|
+
* for (let i = 0; i < n; i++) {
|
|
79
|
+
* // Delete indexes in the queue that are not within the window range
|
|
80
|
+
* if (deq.length > 0 && deq.first! === i - k) {
|
|
81
|
+
* deq.shift();
|
|
82
|
+
* }
|
|
83
|
+
*
|
|
84
|
+
* // Remove all indices less than the current value from the tail of the queue
|
|
85
|
+
* while (deq.length > 0 && nums[deq.last!] < nums[i]) {
|
|
86
|
+
* deq.pop();
|
|
87
|
+
* }
|
|
88
|
+
*
|
|
89
|
+
* // Add the current index to the end of the queue
|
|
90
|
+
* deq.push(i);
|
|
91
|
+
*
|
|
92
|
+
* // Add the maximum value of the window to the results
|
|
93
|
+
* if (i >= k - 1) {
|
|
94
|
+
* result.push(nums[deq.first!]);
|
|
95
|
+
* }
|
|
96
|
+
* }
|
|
97
|
+
*
|
|
98
|
+
* return result;
|
|
99
|
+
* }
|
|
100
|
+
*
|
|
101
|
+
* const nums = [1, 3, -1, -3, 5, 3, 6, 7];
|
|
102
|
+
* const k = 3;
|
|
103
|
+
* console.log(maxSlidingWindow(nums, k)); // [3, 3, 5, 5, 6, 7]
|
|
16
104
|
*/
|
|
17
|
-
export declare class Deque<E = any, R = any> extends
|
|
105
|
+
export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
18
106
|
/**
|
|
19
107
|
* The constructor initializes a Deque object with optional iterable of elements and options.
|
|
20
108
|
* @param elements - An iterable object (such as an array or a Set) that contains the initial
|
|
@@ -28,63 +116,21 @@ export declare class Deque<E = any, R = any> extends IterableElementBase<E, R, D
|
|
|
28
116
|
*/
|
|
29
117
|
constructor(elements?: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>, options?: DequeOptions<E, R>);
|
|
30
118
|
protected _bucketSize: number;
|
|
31
|
-
/**
|
|
32
|
-
* The bucketSize function returns the size of the bucket.
|
|
33
|
-
*
|
|
34
|
-
* @return The size of the bucket
|
|
35
|
-
*/
|
|
36
119
|
get bucketSize(): number;
|
|
37
|
-
protected _maxLen: number;
|
|
38
|
-
/**
|
|
39
|
-
* The maxLen function returns the max length of the deque.
|
|
40
|
-
*
|
|
41
|
-
* @return The max length of the deque
|
|
42
|
-
*/
|
|
43
|
-
get maxLen(): number;
|
|
44
120
|
protected _bucketFirst: number;
|
|
45
|
-
/**
|
|
46
|
-
* The function returns the value of the protected variable `_bucketFirst`.
|
|
47
|
-
* @returns The value of the `_bucketFirst` property.
|
|
48
|
-
*/
|
|
49
121
|
get bucketFirst(): number;
|
|
50
122
|
protected _firstInBucket: number;
|
|
51
|
-
/**
|
|
52
|
-
* The function returns the value of the protected variable _firstInBucket.
|
|
53
|
-
* @returns The method is returning the value of the variable `_firstInBucket`, which is of type
|
|
54
|
-
* `number`.
|
|
55
|
-
*/
|
|
56
123
|
get firstInBucket(): number;
|
|
57
124
|
protected _bucketLast: number;
|
|
58
|
-
/**
|
|
59
|
-
* The function returns the value of the protected variable `_bucketLast`.
|
|
60
|
-
* @returns The value of the `_bucketLast` property, which is a number.
|
|
61
|
-
*/
|
|
62
125
|
get bucketLast(): number;
|
|
63
126
|
protected _lastInBucket: number;
|
|
64
|
-
/**
|
|
65
|
-
* The function returns the value of the protected variable _lastInBucket.
|
|
66
|
-
* @returns The method is returning the value of the variable `_lastInBucket`, which is of type
|
|
67
|
-
* `number`.
|
|
68
|
-
*/
|
|
69
127
|
get lastInBucket(): number;
|
|
70
128
|
protected _bucketCount: number;
|
|
71
|
-
/**
|
|
72
|
-
* The function returns the number of buckets.
|
|
73
|
-
* @returns The number of buckets.
|
|
74
|
-
*/
|
|
75
129
|
get bucketCount(): number;
|
|
76
130
|
protected _buckets: E[][];
|
|
77
|
-
/**
|
|
78
|
-
* The buckets function returns the buckets property of the object.
|
|
79
|
-
* @return The buckets property
|
|
80
|
-
*/
|
|
81
131
|
get buckets(): E[][];
|
|
82
|
-
protected
|
|
83
|
-
|
|
84
|
-
* The size function returns the number of items in the stack.
|
|
85
|
-
* @return The number of values in the set
|
|
86
|
-
*/
|
|
87
|
-
get size(): number;
|
|
132
|
+
protected _length: number;
|
|
133
|
+
get length(): number;
|
|
88
134
|
/**
|
|
89
135
|
* The function returns the first element in a collection if it exists, otherwise it returns
|
|
90
136
|
* undefined.
|
|
@@ -181,15 +227,6 @@ export declare class Deque<E = any, R = any> extends IterableElementBase<E, R, D
|
|
|
181
227
|
* values.
|
|
182
228
|
*/
|
|
183
229
|
clear(): void;
|
|
184
|
-
/**
|
|
185
|
-
* The below function is a generator that yields elements from a collection one by one.
|
|
186
|
-
*/
|
|
187
|
-
begin(): Generator<E>;
|
|
188
|
-
/**
|
|
189
|
-
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
190
|
-
* the last element.
|
|
191
|
-
*/
|
|
192
|
-
reverseBegin(): Generator<E>;
|
|
193
230
|
/**
|
|
194
231
|
* Time Complexity: O(1)
|
|
195
232
|
* Space Complexity: O(1)
|
|
@@ -240,6 +277,25 @@ export declare class Deque<E = any, R = any> extends IterableElementBase<E, R, D
|
|
|
240
277
|
* @returns The method is returning the updated size of the data structure.
|
|
241
278
|
*/
|
|
242
279
|
cut(pos: number, isCutSelf?: boolean): Deque<E>;
|
|
280
|
+
/**
|
|
281
|
+
* Time Complexity: O(n)
|
|
282
|
+
* Space Complexity: O(1)
|
|
283
|
+
*
|
|
284
|
+
* The `splice` function in TypeScript overrides the default behavior to remove and insert elements
|
|
285
|
+
* in a Deque data structure while ensuring the starting position and delete count are within bounds.
|
|
286
|
+
* @param {number} start - The `start` parameter in the `splice` method represents the index at which
|
|
287
|
+
* to start changing the array. Items will be removed or added starting from this index.
|
|
288
|
+
* @param {number} deleteCount - The `deleteCount` parameter in the `splice` method represents the
|
|
289
|
+
* number of elements to remove from the array starting at the specified `start` index. If
|
|
290
|
+
* `deleteCount` is not provided, it defaults to the number of elements from the `start` index to the
|
|
291
|
+
* end of the array (`
|
|
292
|
+
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
|
|
293
|
+
* will be inserted into the deque at the specified `start` index. These elements will be inserted in
|
|
294
|
+
* place of the elements that are removed based on the `start` and `deleteCount` parameters.
|
|
295
|
+
* @returns The `splice` method is returning the array `deletedElements` which contains the elements
|
|
296
|
+
* that were removed from the Deque during the splice operation.
|
|
297
|
+
*/
|
|
298
|
+
splice(start: number, deleteCount?: number, ...items: E[]): this;
|
|
243
299
|
/**
|
|
244
300
|
* Time Complexity: O(1)
|
|
245
301
|
* Space Complexity: O(1) or O(n)
|
|
@@ -267,7 +323,7 @@ export declare class Deque<E = any, R = any> extends IterableElementBase<E, R, D
|
|
|
267
323
|
* the index of the element to be deleted.
|
|
268
324
|
* @returns The size of the data structure after the deletion operation is performed.
|
|
269
325
|
*/
|
|
270
|
-
deleteAt(pos: number):
|
|
326
|
+
deleteAt(pos: number): E | undefined;
|
|
271
327
|
/**
|
|
272
328
|
* Time Complexity: O(n)
|
|
273
329
|
* Space Complexity: O(1)
|
|
@@ -298,17 +354,6 @@ export declare class Deque<E = any, R = any> extends IterableElementBase<E, R, D
|
|
|
298
354
|
* @returns The size of the modified array is being returned.
|
|
299
355
|
*/
|
|
300
356
|
unique(): this;
|
|
301
|
-
/**
|
|
302
|
-
* Time Complexity: O(n log n)
|
|
303
|
-
* Space Complexity: O(n)
|
|
304
|
-
*
|
|
305
|
-
* The `sort` function sorts the elements in a data structure using a provided comparator function.
|
|
306
|
-
* @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
|
|
307
|
-
* `y` of type `E` and returns a number. The comparator function is used to determine the order of
|
|
308
|
-
* the elements in the sorted array.
|
|
309
|
-
* @returns Deque<E>
|
|
310
|
-
*/
|
|
311
|
-
sort(comparator?: (x: E, y: E) => number): this;
|
|
312
357
|
/**
|
|
313
358
|
* Time Complexity: O(n)
|
|
314
359
|
* Space Complexity: O(n)
|
|
@@ -316,29 +361,9 @@ export declare class Deque<E = any, R = any> extends IterableElementBase<E, R, D
|
|
|
316
361
|
* The `shrinkToFit` function reorganizes the elements in an array-like data structure to minimize
|
|
317
362
|
* memory usage.
|
|
318
363
|
* @returns Nothing is being returned. The function is using the `return` statement to exit early if
|
|
319
|
-
* `this.
|
|
364
|
+
* `this._length` is 0, but it does not return any value.
|
|
320
365
|
*/
|
|
321
366
|
shrinkToFit(): void;
|
|
322
|
-
/**
|
|
323
|
-
* Time Complexity: O(n)
|
|
324
|
-
* Space Complexity: O(1)
|
|
325
|
-
*
|
|
326
|
-
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
327
|
-
* or -1 if the element is not found.
|
|
328
|
-
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
329
|
-
* index of in the data structure.
|
|
330
|
-
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
331
|
-
* in the data structure. If the element is not found, it returns -1.
|
|
332
|
-
*/
|
|
333
|
-
indexOf(element: E): number;
|
|
334
|
-
/**
|
|
335
|
-
* Time Complexity: O(n)
|
|
336
|
-
* Space Complexity: O(n)
|
|
337
|
-
*
|
|
338
|
-
* The `toArray` function converts the elements of a data structure into an array.
|
|
339
|
-
* @returns The `toArray()` method is returning an array of elements of type `E`.
|
|
340
|
-
*/
|
|
341
|
-
toArray(): E[];
|
|
342
367
|
/**
|
|
343
368
|
* Time Complexity: O(n)
|
|
344
369
|
* Space Complexity: O(n)
|
|
@@ -348,7 +373,7 @@ export declare class Deque<E = any, R = any> extends IterableElementBase<E, R, D
|
|
|
348
373
|
* @returns The `clone()` method is returning a new instance of the `Deque` class with the same
|
|
349
374
|
* elements as the original deque (`this`) and the same bucket size.
|
|
350
375
|
*/
|
|
351
|
-
clone():
|
|
376
|
+
clone(): this;
|
|
352
377
|
/**
|
|
353
378
|
* Time Complexity: O(n)
|
|
354
379
|
* Space Complexity: O(n)
|
|
@@ -365,7 +390,7 @@ export declare class Deque<E = any, R = any> extends IterableElementBase<E, R, D
|
|
|
365
390
|
* @returns The `filter` method is returning a new `Deque` object that contains the elements that
|
|
366
391
|
* satisfy the given predicate function.
|
|
367
392
|
*/
|
|
368
|
-
filter(predicate: ElementCallback<E, R, boolean
|
|
393
|
+
filter(predicate: ElementCallback<E, R, boolean>, thisArg?: any): Deque<E, R>;
|
|
369
394
|
/**
|
|
370
395
|
* Time Complexity: O(n)
|
|
371
396
|
* Space Complexity: O(n)
|
|
@@ -384,7 +409,7 @@ export declare class Deque<E = any, R = any> extends IterableElementBase<E, R, D
|
|
|
384
409
|
* value of
|
|
385
410
|
* @returns a new Deque object with elements of type EM and raw elements of type RM.
|
|
386
411
|
*/
|
|
387
|
-
map<EM, RM>(callback: ElementCallback<E, R, EM
|
|
412
|
+
map<EM, RM>(callback: ElementCallback<E, R, EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Deque<EM, RM>;
|
|
388
413
|
/**
|
|
389
414
|
* Time Complexity: O(n)
|
|
390
415
|
* Space Complexity: O(1)
|
|
@@ -416,4 +441,18 @@ export declare class Deque<E = any, R = any> extends IterableElementBase<E, R, D
|
|
|
416
441
|
bucketIndex: number;
|
|
417
442
|
indexInBucket: number;
|
|
418
443
|
};
|
|
444
|
+
/**
|
|
445
|
+
* The function `_createInstance` returns a new instance of the `Deque` class with the specified
|
|
446
|
+
* options.
|
|
447
|
+
* @param [options] - The `options` parameter in the `_createInstance` method is of type
|
|
448
|
+
* `DequeOptions<E, R>`, which is an optional parameter that allows you to pass additional
|
|
449
|
+
* configuration options when creating a new instance of the `Deque` class.
|
|
450
|
+
* @returns An instance of the `Deque` class with an empty array and the provided options, casted as
|
|
451
|
+
* `this`.
|
|
452
|
+
*/
|
|
453
|
+
protected _createInstance(options?: DequeOptions<E, R>): this;
|
|
454
|
+
/**
|
|
455
|
+
* This function returns an iterator that iterates over elements in reverse order.
|
|
456
|
+
*/
|
|
457
|
+
protected _getReverseIterator(): IterableIterator<E>;
|
|
419
458
|
}
|