heap-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
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
6
|
import type { ElementCallback, QueueOptions } from '../../types';
|
|
7
|
-
import { IterableElementBase } from '../base';
|
|
8
7
|
import { SinglyLinkedList } from '../linked-list';
|
|
8
|
+
import { LinearBase } from '../base/linear-base';
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* 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.
|
|
@@ -16,7 +16,7 @@ import { SinglyLinkedList } from '../linked-list';
|
|
|
16
16
|
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited.
|
|
17
17
|
* 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
|
|
18
18
|
*/
|
|
19
|
-
export class Queue<E = any, R = any> extends
|
|
19
|
+
export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
20
20
|
constructor(elements: Iterable<E> | Iterable<R> = [], options?: QueueOptions<E, R>) {
|
|
21
21
|
super(options);
|
|
22
22
|
|
|
@@ -30,42 +30,40 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
30
30
|
|
|
31
31
|
protected _elements: E[] = [];
|
|
32
32
|
|
|
33
|
-
/**
|
|
34
|
-
* The elements function returns the elements of this set.
|
|
35
|
-
* @return An array of the elements in the stack
|
|
36
|
-
*/
|
|
37
33
|
get elements(): E[] {
|
|
38
34
|
return this._elements;
|
|
39
35
|
}
|
|
40
36
|
|
|
41
37
|
protected _offset: number = 0;
|
|
42
38
|
|
|
43
|
-
/**
|
|
44
|
-
* The offset function returns the offset of the current page.
|
|
45
|
-
* @return The value of the protected variable _offset
|
|
46
|
-
*/
|
|
47
39
|
get offset(): number {
|
|
48
40
|
return this._offset;
|
|
49
41
|
}
|
|
50
42
|
|
|
51
|
-
|
|
52
|
-
* The size function returns the number of elements in an array.
|
|
53
|
-
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
54
|
-
*/
|
|
55
|
-
get size(): number {
|
|
43
|
+
get length(): number {
|
|
56
44
|
return this.elements.length - this.offset;
|
|
57
45
|
}
|
|
58
46
|
|
|
47
|
+
protected _autoCompactRatio: number = 0.5;
|
|
48
|
+
|
|
49
|
+
get autoCompactRatio(): number {
|
|
50
|
+
return this._autoCompactRatio;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
set autoCompactRatio(v: number) {
|
|
54
|
+
this._autoCompactRatio = v;
|
|
55
|
+
}
|
|
56
|
+
|
|
59
57
|
/**
|
|
60
58
|
* Time Complexity: O(1)
|
|
61
59
|
* Space Complexity: O(1)
|
|
62
60
|
*
|
|
63
61
|
* The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
|
|
64
62
|
* @returns The `get first()` method returns the first element of the data structure, represented by the `_elements` array at
|
|
65
|
-
* the `_offset` index. If the data structure is empty (
|
|
63
|
+
* the `_offset` index. If the data structure is empty (length is 0), it returns `undefined`.
|
|
66
64
|
*/
|
|
67
65
|
get first(): E | undefined {
|
|
68
|
-
return this.
|
|
66
|
+
return this.length > 0 ? this.elements[this.offset] : undefined;
|
|
69
67
|
}
|
|
70
68
|
|
|
71
69
|
/**
|
|
@@ -77,26 +75,7 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
77
75
|
* array is empty, it returns `undefined`.
|
|
78
76
|
*/
|
|
79
77
|
get last(): E | undefined {
|
|
80
|
-
return this.
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
protected _autoCompactRatio: number = 0.5;
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* This function returns the value of the autoCompactRatio property.
|
|
87
|
-
* @returns The `autoCompactRatio` property of the object, which is a number.
|
|
88
|
-
*/
|
|
89
|
-
get autoCompactRatio(): number {
|
|
90
|
-
return this._autoCompactRatio;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* The above function sets the autoCompactRatio property to a specified number in TypeScript.
|
|
95
|
-
* @param {number} v - The parameter `v` represents the value that will be assigned to the
|
|
96
|
-
* `_autoCompactRatio` property.
|
|
97
|
-
*/
|
|
98
|
-
set autoCompactRatio(v: number) {
|
|
99
|
-
this._autoCompactRatio = v;
|
|
78
|
+
return this.length > 0 ? this.elements[this.elements.length - 1] : undefined;
|
|
100
79
|
}
|
|
101
80
|
|
|
102
81
|
/**
|
|
@@ -123,6 +102,7 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
123
102
|
*/
|
|
124
103
|
push(element: E): boolean {
|
|
125
104
|
this.elements.push(element);
|
|
105
|
+
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
126
106
|
return true;
|
|
127
107
|
}
|
|
128
108
|
|
|
@@ -155,7 +135,7 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
155
135
|
* @returns The function `shift()` returns either the first element in the queue or `undefined` if the queue is empty.
|
|
156
136
|
*/
|
|
157
137
|
shift(): E | undefined {
|
|
158
|
-
if (this.
|
|
138
|
+
if (this.length === 0) return undefined;
|
|
159
139
|
|
|
160
140
|
const first = this.first;
|
|
161
141
|
this._offset += 1;
|
|
@@ -174,7 +154,7 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
174
154
|
*/
|
|
175
155
|
delete(element: E): boolean {
|
|
176
156
|
const index = this.elements.indexOf(element);
|
|
177
|
-
return this.deleteAt(index);
|
|
157
|
+
return !!this.deleteAt(index);
|
|
178
158
|
}
|
|
179
159
|
|
|
180
160
|
/**
|
|
@@ -185,9 +165,10 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
185
165
|
* @param {number} index - Determine the index of the element to be deleted
|
|
186
166
|
* @return A boolean value
|
|
187
167
|
*/
|
|
188
|
-
deleteAt(index: number):
|
|
189
|
-
const
|
|
190
|
-
|
|
168
|
+
deleteAt(index: number): E | undefined {
|
|
169
|
+
const deleted = this.elements[index];
|
|
170
|
+
this.elements.splice(index, 1);
|
|
171
|
+
return deleted;
|
|
191
172
|
}
|
|
192
173
|
|
|
193
174
|
/**
|
|
@@ -205,26 +186,69 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
205
186
|
return this.elements[index + this._offset];
|
|
206
187
|
}
|
|
207
188
|
|
|
189
|
+
/**
|
|
190
|
+
* Time Complexity: O(n)
|
|
191
|
+
* Space Complexity: O(1)
|
|
192
|
+
*
|
|
193
|
+
* The `reverse` function in TypeScript reverses the elements of an array starting from a specified
|
|
194
|
+
* offset.
|
|
195
|
+
* @returns The `reverse()` method is returning the modified object itself (`this`) after reversing
|
|
196
|
+
* the elements in the array and resetting the offset to 0.
|
|
197
|
+
*/
|
|
198
|
+
reverse(): this {
|
|
199
|
+
this._elements = this.elements.slice(this.offset).reverse();
|
|
200
|
+
this._offset = 0;
|
|
201
|
+
return this;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Time Complexity: O(n)
|
|
206
|
+
* Space Complexity: O(1)
|
|
207
|
+
*
|
|
208
|
+
* The function `addAt` inserts a new element at a specified index in an array, returning true if
|
|
209
|
+
* successful and false if the index is out of bounds.
|
|
210
|
+
* @param {number} index - The `index` parameter represents the position at which the `newElement`
|
|
211
|
+
* should be added in the array.
|
|
212
|
+
* @param {E} newElement - The `newElement` parameter represents the element that you want to insert
|
|
213
|
+
* into the array at the specified index.
|
|
214
|
+
* @returns The `addAt` method returns a boolean value - `true` if the new element was successfully
|
|
215
|
+
* added at the specified index, and `false` if the index is out of bounds (less than 0 or greater
|
|
216
|
+
* than the length of the array).
|
|
217
|
+
*/
|
|
218
|
+
addAt(index: number, newElement: E): boolean {
|
|
219
|
+
if (index < 0 || index > this.length) return false;
|
|
220
|
+
this._elements.splice(this.offset + index, 0, newElement);
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
|
|
208
224
|
/**
|
|
209
225
|
* Time Complexity: O(1)
|
|
210
226
|
* Space Complexity: O(1)
|
|
211
227
|
*
|
|
212
|
-
* The function
|
|
213
|
-
* @
|
|
228
|
+
* The function `setAt` updates an element at a specified index in an array-like data structure.
|
|
229
|
+
* @param {number} index - The `index` parameter is a number that represents the position in the
|
|
230
|
+
* array where the new element will be set.
|
|
231
|
+
* @param {E} newElement - The `newElement` parameter represents the new value that you want to set
|
|
232
|
+
* at the specified index in the array.
|
|
233
|
+
* @returns The `setAt` method returns a boolean value - `true` if the element was successfully set
|
|
234
|
+
* at the specified index, and `false` if the index is out of bounds (less than 0 or greater than the
|
|
235
|
+
* length of the array).
|
|
214
236
|
*/
|
|
215
|
-
|
|
216
|
-
|
|
237
|
+
setAt(index: number, newElement: E): boolean {
|
|
238
|
+
if (index < 0 || index > this.length) return false;
|
|
239
|
+
this._elements[this.offset + index] = newElement;
|
|
240
|
+
return true;
|
|
217
241
|
}
|
|
218
242
|
|
|
219
243
|
/**
|
|
220
244
|
* Time Complexity: O(1)
|
|
221
|
-
* Space Complexity: O(
|
|
245
|
+
* Space Complexity: O(1)
|
|
222
246
|
*
|
|
223
|
-
* The
|
|
224
|
-
* @returns
|
|
247
|
+
* The function checks if a data structure is empty by comparing its length to zero.
|
|
248
|
+
* @returns {boolean} A boolean value indicating whether the length of the object is 0 or not.
|
|
225
249
|
*/
|
|
226
|
-
|
|
227
|
-
return this.
|
|
250
|
+
isEmpty(): boolean {
|
|
251
|
+
return this.length === 0;
|
|
228
252
|
}
|
|
229
253
|
|
|
230
254
|
/**
|
|
@@ -252,6 +276,40 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
252
276
|
return true;
|
|
253
277
|
}
|
|
254
278
|
|
|
279
|
+
/**
|
|
280
|
+
* Time Complexity: O(n)
|
|
281
|
+
* Space Complexity: O(n)
|
|
282
|
+
*
|
|
283
|
+
* The function overrides the splice method to remove and insert elements in a queue-like data
|
|
284
|
+
* structure.
|
|
285
|
+
* @param {number} start - The `start` parameter in the `splice` method specifies the index at which
|
|
286
|
+
* to start changing the array. Items will be added or removed starting from this index.
|
|
287
|
+
* @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
|
|
288
|
+
* number of elements to remove from the array starting at the specified `start` index. If
|
|
289
|
+
* `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
|
|
290
|
+
* elements can still be inserted at
|
|
291
|
+
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
|
|
292
|
+
* will be added to the array at the specified `start` index. These elements will replace the
|
|
293
|
+
* existing elements starting from the `start` index for the `deleteCount` number of elements.
|
|
294
|
+
* @returns The `splice` method is returning the `removedQueue`, which is an instance of the same
|
|
295
|
+
* class as the original object.
|
|
296
|
+
*/
|
|
297
|
+
override splice(start: number, deleteCount: number = 0, ...items: E[]): this {
|
|
298
|
+
const removedQueue = this._createInstance();
|
|
299
|
+
|
|
300
|
+
start = Math.max(0, Math.min(start, this.length));
|
|
301
|
+
deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
|
|
302
|
+
|
|
303
|
+
const globalStartIndex = this.offset + start;
|
|
304
|
+
|
|
305
|
+
const removedElements = this._elements.splice(globalStartIndex, deleteCount, ...items);
|
|
306
|
+
removedQueue.pushMany(removedElements);
|
|
307
|
+
|
|
308
|
+
this.compact();
|
|
309
|
+
|
|
310
|
+
return removedQueue;
|
|
311
|
+
}
|
|
312
|
+
|
|
255
313
|
/**
|
|
256
314
|
* Time Complexity: O(n)
|
|
257
315
|
* Space Complexity: O(n)
|
|
@@ -259,8 +317,8 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
259
317
|
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
|
|
260
318
|
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
261
319
|
*/
|
|
262
|
-
clone():
|
|
263
|
-
return new Queue(this.elements.slice(this.offset), { toElementFn: this.toElementFn });
|
|
320
|
+
clone(): this {
|
|
321
|
+
return new Queue(this.elements.slice(this.offset), { toElementFn: this.toElementFn, maxLen: this._maxLen }) as this;
|
|
264
322
|
}
|
|
265
323
|
|
|
266
324
|
/**
|
|
@@ -279,8 +337,12 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
279
337
|
* @returns The `filter` method is returning a new `Queue` object that contains the elements that
|
|
280
338
|
* satisfy the given predicate function.
|
|
281
339
|
*/
|
|
282
|
-
filter(predicate: ElementCallback<E, R, boolean
|
|
283
|
-
const newDeque =
|
|
340
|
+
filter(predicate: ElementCallback<E, R, boolean>, thisArg?: any): Queue<E, R> {
|
|
341
|
+
const newDeque = this._createInstance({
|
|
342
|
+
toElementFn: this._toElementFn,
|
|
343
|
+
autoCompactRatio: this._autoCompactRatio,
|
|
344
|
+
maxLen: this._maxLen
|
|
345
|
+
});
|
|
284
346
|
let index = 0;
|
|
285
347
|
for (const el of this) {
|
|
286
348
|
if (predicate.call(thisArg, el, index, this)) {
|
|
@@ -309,12 +371,12 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
309
371
|
* @returns A new Queue object containing elements of type EM, which are the result of applying the
|
|
310
372
|
* callback function to each element in the original Queue object.
|
|
311
373
|
*/
|
|
312
|
-
map<EM, RM>(
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
374
|
+
map<EM, RM>(callback: ElementCallback<E, R, EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Queue<EM, RM> {
|
|
375
|
+
const newDeque = new Queue<EM, RM>([], {
|
|
376
|
+
toElementFn,
|
|
377
|
+
autoCompactRatio: this._autoCompactRatio,
|
|
378
|
+
maxLen: this._maxLen
|
|
379
|
+
});
|
|
318
380
|
let index = 0;
|
|
319
381
|
for (const el of this) {
|
|
320
382
|
newDeque.push(callback.call(thisArg, el, index, this));
|
|
@@ -334,6 +396,30 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
334
396
|
yield item;
|
|
335
397
|
}
|
|
336
398
|
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* The function `_createInstance` returns a new instance of the `Queue` class with the specified
|
|
402
|
+
* options.
|
|
403
|
+
* @param [options] - The `options` parameter in the `_createInstance` method is of type
|
|
404
|
+
* `QueueOptions<E, R>`, which is used to configure the behavior of the queue being created. It
|
|
405
|
+
* allows you to specify settings or properties that can influence how the queue operates.
|
|
406
|
+
* @returns An instance of the `Queue` class with an empty array and the provided options is being
|
|
407
|
+
* returned.
|
|
408
|
+
*/
|
|
409
|
+
protected override _createInstance(options?: QueueOptions<E, R>): this {
|
|
410
|
+
return new Queue<E, R>([], options) as this;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* The function `_getReverseIterator` returns an iterator that iterates over elements in reverse
|
|
415
|
+
* order.
|
|
416
|
+
*/
|
|
417
|
+
protected *_getReverseIterator(): IterableIterator<E> {
|
|
418
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
419
|
+
const cur = this.at(i); // `at()` handles the offset.
|
|
420
|
+
if (cur !== undefined) yield cur;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
337
423
|
}
|
|
338
424
|
|
|
339
425
|
/**
|
|
@@ -351,7 +437,7 @@ export class LinkedListQueue<E = any, R = any> extends SinglyLinkedList<E, R> {
|
|
|
351
437
|
* @returns The `clone()` method is returning a new instance of `LinkedListQueue` with the same
|
|
352
438
|
* values as the original `LinkedListQueue`.
|
|
353
439
|
*/
|
|
354
|
-
override clone():
|
|
355
|
-
return new LinkedListQueue<E, R>(this, { toElementFn: this.toElementFn });
|
|
440
|
+
override clone(): this {
|
|
441
|
+
return new LinkedListQueue<E, R>(this, { toElementFn: this.toElementFn, maxLen: this._maxLen }) as this;
|
|
356
442
|
}
|
|
357
443
|
}
|
|
@@ -16,7 +16,7 @@ import { IterableElementBase } from '../base';
|
|
|
16
16
|
* 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
|
|
17
17
|
* 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.
|
|
18
18
|
*/
|
|
19
|
-
export class Stack<E = any, R = any> extends IterableElementBase<E, R
|
|
19
|
+
export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
20
20
|
constructor(elements: Iterable<E> | Iterable<R> = [], options?: StackOptions<E, R>) {
|
|
21
21
|
super(options);
|
|
22
22
|
this.pushMany(elements);
|
|
@@ -153,17 +153,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
|
|
|
153
153
|
return spliced.length === 1;
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
-
/**
|
|
157
|
-
* Time Complexity: O(n)
|
|
158
|
-
* Space Complexity: O(n)
|
|
159
|
-
*
|
|
160
|
-
* The toArray function returns a copy of the elements in an array.
|
|
161
|
-
* @returns An array of type E.
|
|
162
|
-
*/
|
|
163
|
-
toArray(): E[] {
|
|
164
|
-
return this.elements.slice();
|
|
165
|
-
}
|
|
166
|
-
|
|
167
156
|
/**
|
|
168
157
|
* Time Complexity: O(1)
|
|
169
158
|
* Space Complexity: O(1)
|
|
@@ -201,7 +190,7 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
|
|
|
201
190
|
* @returns The `filter` method is returning a new `Stack` object that contains the elements that
|
|
202
191
|
* satisfy the given predicate function.
|
|
203
192
|
*/
|
|
204
|
-
filter(predicate: ElementCallback<E, R, boolean
|
|
193
|
+
filter(predicate: ElementCallback<E, R, boolean>, thisArg?: any): Stack<E, R> {
|
|
205
194
|
const newStack = new Stack<E, R>([], { toElementFn: this.toElementFn });
|
|
206
195
|
let index = 0;
|
|
207
196
|
for (const el of this) {
|
|
@@ -230,11 +219,7 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
|
|
|
230
219
|
* value of
|
|
231
220
|
* @returns a new Stack object with elements of type EM and raw elements of type RM.
|
|
232
221
|
*/
|
|
233
|
-
map<EM, RM>(
|
|
234
|
-
callback: ElementCallback<E, R, EM, Stack<E, R>>,
|
|
235
|
-
toElementFn?: (rawElement: RM) => EM,
|
|
236
|
-
thisArg?: any
|
|
237
|
-
): Stack<EM, RM> {
|
|
222
|
+
map<EM, RM>(callback: ElementCallback<E, R, EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Stack<EM, RM> {
|
|
238
223
|
const newStack = new Stack<EM, RM>([], { toElementFn });
|
|
239
224
|
let index = 0;
|
|
240
225
|
for (const el of this) {
|
|
@@ -170,7 +170,7 @@ export class TrieNode {
|
|
|
170
170
|
* const subnet = ip.split('.').slice(0, 3).join('.');
|
|
171
171
|
* console.log(ipRoutingTable.hasPrefix(subnet)); // true
|
|
172
172
|
*/
|
|
173
|
-
export class Trie<R = any> extends IterableElementBase<string, R
|
|
173
|
+
export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
174
174
|
/**
|
|
175
175
|
* The constructor initializes a Trie data structure with optional options and words provided as
|
|
176
176
|
* input.
|
|
@@ -545,7 +545,7 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
|
|
|
545
545
|
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
|
|
546
546
|
* @returns The `filter` method is returning an array of strings (`string[]`).
|
|
547
547
|
*/
|
|
548
|
-
filter(predicate: ElementCallback<string, R, boolean
|
|
548
|
+
filter(predicate: ElementCallback<string, R, boolean>, thisArg?: any): Trie<R> {
|
|
549
549
|
const results = new Trie<R>([], { toElementFn: this.toElementFn, caseSensitive: this.caseSensitive });
|
|
550
550
|
let index = 0;
|
|
551
551
|
for (const word of this) {
|
|
@@ -576,7 +576,7 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
|
|
|
576
576
|
* @returns a new Trie object.
|
|
577
577
|
*/
|
|
578
578
|
map<RM>(
|
|
579
|
-
callback: ElementCallback<string, R, string
|
|
579
|
+
callback: ElementCallback<string, R, string>,
|
|
580
580
|
toElementFn?: (rawElement: RM) => string,
|
|
581
581
|
thisArg?: any
|
|
582
582
|
): Trie<RM> {
|
|
@@ -609,6 +609,10 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
|
|
|
609
609
|
yield* _dfs(this.root, '');
|
|
610
610
|
}
|
|
611
611
|
|
|
612
|
+
protected get _total() {
|
|
613
|
+
return this._size;
|
|
614
|
+
}
|
|
615
|
+
|
|
612
616
|
/**
|
|
613
617
|
* Time Complexity: O(l), where l is the length of the input string.
|
|
614
618
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -1,25 +1,34 @@
|
|
|
1
1
|
import { IterableElementBase, IterableEntryBase } from '../../../data-structures';
|
|
2
|
+
import { LinearBase } from '../../../data-structures/base/linear-base';
|
|
2
3
|
|
|
3
|
-
export type EntryCallback<K, V, R> = (key: K, value: V, index: number,
|
|
4
|
-
export type ElementCallback<E, R, RT
|
|
4
|
+
export type EntryCallback<K, V, R> = (key: K, value: V, index: number, original: IterableEntryBase<K, V>) => R;
|
|
5
|
+
export type ElementCallback<E, R, RT> = (element: E, index: number, original: IterableElementBase<E, R>) => RT;
|
|
5
6
|
export type ReduceEntryCallback<K, V, R> = (
|
|
6
7
|
accumulator: R,
|
|
7
8
|
value: V,
|
|
8
9
|
key: K,
|
|
9
10
|
index: number,
|
|
10
|
-
|
|
11
|
+
original: IterableEntryBase<K, V>
|
|
11
12
|
) => R;
|
|
12
|
-
|
|
13
|
+
|
|
14
|
+
export type ReduceElementCallback<E, R, RT = E> = (
|
|
13
15
|
accumulator: RT,
|
|
14
16
|
element: E,
|
|
15
17
|
index: number,
|
|
16
|
-
|
|
18
|
+
original: IterableElementBase<E, R>
|
|
17
19
|
) => RT;
|
|
18
20
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
export type ReduceLinearCallback<E, RT = E> = (
|
|
22
|
+
accumulator: RT,
|
|
23
|
+
element: E,
|
|
24
|
+
index: number,
|
|
25
|
+
original: LinearBase<E>
|
|
26
|
+
) => RT;
|
|
22
27
|
|
|
23
28
|
export type IterableElementBaseOptions<E, R> = {
|
|
24
29
|
toElementFn?: (rawElement: R) => E;
|
|
25
30
|
};
|
|
31
|
+
|
|
32
|
+
export type LinearBaseOptions<E, R> = IterableElementBaseOptions<E, R> & {
|
|
33
|
+
maxLen?: number;
|
|
34
|
+
};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LinearBaseOptions } from '../base';
|
|
2
2
|
|
|
3
|
-
export type DoublyLinkedListOptions<E, R> =
|
|
3
|
+
export type DoublyLinkedListOptions<E, R> = LinearBaseOptions<E, R> & {};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LinearBaseOptions } from '../base';
|
|
2
2
|
|
|
3
|
-
export type SinglyLinkedListOptions<E, R> =
|
|
3
|
+
export type SinglyLinkedListOptions<E, R> = LinearBaseOptions<E, R> & {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LinearBaseOptions } from '../base';
|
|
2
2
|
|
|
3
|
-
export type QueueOptions<E, R> =
|
|
3
|
+
export type QueueOptions<E, R> = LinearBaseOptions<E, R> & {
|
|
4
4
|
autoCompactRatio?: number;
|
|
5
5
|
};
|