min-heap-typed 1.53.5 → 1.53.7
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/README.md +5 -5
- package/dist/common/index.d.ts +12 -0
- package/dist/common/index.js +23 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +7 -10
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +54 -19
- package/dist/data-structures/binary-tree/binary-tree.js +100 -66
- package/dist/data-structures/binary-tree/bst.d.ts +100 -36
- package/dist/data-structures/binary-tree/bst.js +185 -66
- package/dist/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/data-structures/binary-tree/rb-tree.js +6 -2
- package/dist/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/data-structures/heap/heap.d.ts +6 -6
- package/dist/data-structures/heap/heap.js +6 -6
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +31 -19
- package/dist/data-structures/linked-list/doubly-linked-list.js +49 -34
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +144 -62
- package/dist/data-structures/linked-list/singly-linked-list.js +201 -97
- package/dist/data-structures/trie/trie.d.ts +104 -4
- package/dist/data-structures/trie/trie.js +116 -12
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +10 -6
- package/dist/utils/utils.js +4 -2
- package/package.json +2 -2
- package/src/common/index.ts +19 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -9
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-tree.ts +108 -64
- package/src/data-structures/binary-tree/bst.ts +190 -69
- package/src/data-structures/binary-tree/rb-tree.ts +6 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +39 -39
- package/src/data-structures/linked-list/doubly-linked-list.ts +139 -121
- package/src/data-structures/linked-list/singly-linked-list.ts +219 -98
- package/src/data-structures/trie/trie.ts +116 -11
- package/src/index.ts +2 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +3 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/utils/utils.ts +16 -10
- package/src/utils/utils.ts +4 -2
|
@@ -121,34 +121,17 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
121
121
|
return this._size;
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
/**
|
|
125
|
-
* Time Complexity: O(n)
|
|
126
|
-
* Space Complexity: O(n)
|
|
127
|
-
*
|
|
128
|
-
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
129
|
-
* array.
|
|
130
|
-
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
131
|
-
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
132
|
-
*/
|
|
133
|
-
static fromArray<E>(data: E[]) {
|
|
134
|
-
const singlyLinkedList = new SinglyLinkedList<E>();
|
|
135
|
-
for (const item of data) {
|
|
136
|
-
singlyLinkedList.push(item);
|
|
137
|
-
}
|
|
138
|
-
return singlyLinkedList;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
124
|
/**
|
|
142
125
|
* Time Complexity: O(1)
|
|
143
126
|
* Space Complexity: O(1)
|
|
144
127
|
*
|
|
145
|
-
* The push function adds a new element to the end of a singly linked list.
|
|
146
|
-
* @param {E}
|
|
147
|
-
*
|
|
148
|
-
* @returns The `push` method is returning a boolean value, `true`.
|
|
128
|
+
* The `push` function adds a new element or node to the end of a singly linked list.
|
|
129
|
+
* @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the `push`
|
|
130
|
+
* method can accept either an element of type `E` or a `SinglyLinkedListNode<E>` object.
|
|
131
|
+
* @returns The `push` method is returning a boolean value, specifically `true`.
|
|
149
132
|
*/
|
|
150
|
-
push(
|
|
151
|
-
const newNode =
|
|
133
|
+
push(elementOrNode: E | SinglyLinkedListNode<E>): boolean {
|
|
134
|
+
const newNode = this._ensureNode(elementOrNode);
|
|
152
135
|
if (!this.head) {
|
|
153
136
|
this._head = newNode;
|
|
154
137
|
this._tail = newNode;
|
|
@@ -208,13 +191,15 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
208
191
|
* Time Complexity: O(1)
|
|
209
192
|
* Space Complexity: O(1)
|
|
210
193
|
*
|
|
211
|
-
* The unshift function adds a new element to the beginning of a singly linked list
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
194
|
+
* The unshift function adds a new element or node to the beginning of a singly linked list in
|
|
195
|
+
* TypeScript.
|
|
196
|
+
* @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
|
|
197
|
+
* `unshift` method can be either an element of type `E` or a `SinglyLinkedListNode` containing an
|
|
198
|
+
* element of type `E`.
|
|
199
|
+
* @returns The `unshift` method is returning a boolean value, specifically `true`.
|
|
200
|
+
*/
|
|
201
|
+
unshift(elementOrNode: E | SinglyLinkedListNode<E>): boolean {
|
|
202
|
+
const newNode = this._ensureNode(elementOrNode);
|
|
218
203
|
if (!this.head) {
|
|
219
204
|
this._head = newNode;
|
|
220
205
|
this._tail = newNode;
|
|
@@ -226,6 +211,30 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
226
211
|
return true;
|
|
227
212
|
}
|
|
228
213
|
|
|
214
|
+
/**
|
|
215
|
+
* Time Complexity: O(n)
|
|
216
|
+
* Space Complexity: O(1)
|
|
217
|
+
*
|
|
218
|
+
* This function searches for a specific element in a singly linked list based on a given node or
|
|
219
|
+
* predicate.
|
|
220
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
221
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `get` method can be one of
|
|
222
|
+
* the following types:
|
|
223
|
+
* @returns The `get` method returns the value of the first node in the singly linked list that
|
|
224
|
+
* satisfies the provided predicate function. If no such node is found, it returns `undefined`.
|
|
225
|
+
*/
|
|
226
|
+
search(
|
|
227
|
+
elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)
|
|
228
|
+
): E | undefined {
|
|
229
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
230
|
+
let current = this.head;
|
|
231
|
+
while (current) {
|
|
232
|
+
if (predicate(current)) return current.value;
|
|
233
|
+
current = current.next;
|
|
234
|
+
}
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
|
|
229
238
|
/**
|
|
230
239
|
* Time Complexity: O(n)
|
|
231
240
|
* Space Complexity: O(1)
|
|
@@ -245,6 +254,25 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
245
254
|
return current!.value;
|
|
246
255
|
}
|
|
247
256
|
|
|
257
|
+
/**
|
|
258
|
+
* Time Complexity: O(1)
|
|
259
|
+
* Space Complexity: O(1)
|
|
260
|
+
*
|
|
261
|
+
* The function `isNode` in TypeScript checks if the input is an instance of `SinglyLinkedListNode`.
|
|
262
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
263
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `isNode` function can be
|
|
264
|
+
* one of the following types:
|
|
265
|
+
* @returns The `isNode` function is checking if the `elementNodeOrPredicate` parameter is an
|
|
266
|
+
* instance of `SinglyLinkedListNode<E>`. If it is, the function returns `true`, indicating that the
|
|
267
|
+
* parameter is a `SinglyLinkedListNode<E>`. If it is not an instance of `SinglyLinkedListNode<E>`,
|
|
268
|
+
* the function returns `false`.
|
|
269
|
+
*/
|
|
270
|
+
isNode(
|
|
271
|
+
elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)
|
|
272
|
+
): elementNodeOrPredicate is SinglyLinkedListNode<E> {
|
|
273
|
+
return elementNodeOrPredicate instanceof SinglyLinkedListNode;
|
|
274
|
+
}
|
|
275
|
+
|
|
248
276
|
/**
|
|
249
277
|
* Time Complexity: O(n)
|
|
250
278
|
* Space Complexity: O(1)
|
|
@@ -296,18 +324,18 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
296
324
|
* Space Complexity: O(1)
|
|
297
325
|
*
|
|
298
326
|
* The delete function removes a node with a specific value from a singly linked list.
|
|
299
|
-
* @param {E | SinglyLinkedListNode<E>}
|
|
327
|
+
* @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can accept either a value of type `E`
|
|
300
328
|
* or a `SinglyLinkedListNode<E>` object.
|
|
301
329
|
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
|
|
302
330
|
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
|
|
303
331
|
*/
|
|
304
|
-
delete(
|
|
305
|
-
if (
|
|
332
|
+
delete(elementOrNode: E | SinglyLinkedListNode<E> | undefined): boolean {
|
|
333
|
+
if (elementOrNode === undefined) return false;
|
|
306
334
|
let value: E;
|
|
307
|
-
if (
|
|
308
|
-
value =
|
|
335
|
+
if (elementOrNode instanceof SinglyLinkedListNode) {
|
|
336
|
+
value = elementOrNode.value;
|
|
309
337
|
} else {
|
|
310
|
-
value =
|
|
338
|
+
value = elementOrNode;
|
|
311
339
|
}
|
|
312
340
|
let current = this.head,
|
|
313
341
|
prev = undefined;
|
|
@@ -339,26 +367,30 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
339
367
|
* Time Complexity: O(n)
|
|
340
368
|
* Space Complexity: O(1)
|
|
341
369
|
*
|
|
342
|
-
* The `addAt` function inserts a
|
|
343
|
-
* @param {number} index - The index parameter represents the position at which
|
|
344
|
-
* linked list. It is
|
|
345
|
-
*
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
|
|
350
|
-
|
|
370
|
+
* The `addAt` function inserts a new element or node at a specified index in a singly linked list.
|
|
371
|
+
* @param {number} index - The `index` parameter represents the position at which you want to add a
|
|
372
|
+
* new element or node in the linked list. It is a number that indicates the index where the new
|
|
373
|
+
* element or node should be inserted.
|
|
374
|
+
* @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
|
|
375
|
+
* `addAt` method can be either a value of type `E` or a `SinglyLinkedListNode<E>` object. This
|
|
376
|
+
* parameter represents the element or node that you want to add to the linked list at the specified
|
|
377
|
+
* index.
|
|
378
|
+
* @returns The `addAt` method returns a boolean value - `true` if the element or node was
|
|
379
|
+
* successfully added at the specified index, and `false` if the index is out of bounds.
|
|
380
|
+
*/
|
|
381
|
+
addAt(index: number, newElementOrNode: E | SinglyLinkedListNode<E>): boolean {
|
|
351
382
|
if (index < 0 || index > this._size) return false;
|
|
383
|
+
|
|
352
384
|
if (index === 0) {
|
|
353
|
-
this.unshift(
|
|
385
|
+
this.unshift(newElementOrNode);
|
|
354
386
|
return true;
|
|
355
387
|
}
|
|
356
388
|
if (index === this._size) {
|
|
357
|
-
this.push(
|
|
389
|
+
this.push(newElementOrNode);
|
|
358
390
|
return true;
|
|
359
391
|
}
|
|
360
392
|
|
|
361
|
-
const newNode =
|
|
393
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
362
394
|
const prevNode = this.getNodeAt(index - 1);
|
|
363
395
|
newNode.next = prevNode!.next;
|
|
364
396
|
prevNode!.next = newNode;
|
|
@@ -430,17 +462,22 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
430
462
|
* Time Complexity: O(n)
|
|
431
463
|
* Space Complexity: O(1)
|
|
432
464
|
*
|
|
433
|
-
* The `indexOf` function
|
|
434
|
-
*
|
|
435
|
-
* @
|
|
436
|
-
*
|
|
437
|
-
|
|
438
|
-
|
|
465
|
+
* The `indexOf` function in TypeScript searches for a specific element or node in a singly linked
|
|
466
|
+
* list and returns its index if found.
|
|
467
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
468
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `indexOf` method can be one
|
|
469
|
+
* of the following types:
|
|
470
|
+
* @returns The `indexOf` method returns the index of the first occurrence of the element that
|
|
471
|
+
* matches the provided predicate in the singly linked list. If no matching element is found, it
|
|
472
|
+
* returns -1.
|
|
473
|
+
*/
|
|
474
|
+
indexOf(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): number {
|
|
475
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
439
476
|
let index = 0;
|
|
440
477
|
let current = this.head;
|
|
441
478
|
|
|
442
479
|
while (current) {
|
|
443
|
-
if (current
|
|
480
|
+
if (predicate(current)) {
|
|
444
481
|
return index;
|
|
445
482
|
}
|
|
446
483
|
index++;
|
|
@@ -454,17 +491,24 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
454
491
|
* Time Complexity: O(n)
|
|
455
492
|
* Space Complexity: O(1)
|
|
456
493
|
*
|
|
457
|
-
* The function
|
|
458
|
-
*
|
|
459
|
-
* @param {E
|
|
460
|
-
*
|
|
461
|
-
*
|
|
462
|
-
|
|
463
|
-
|
|
494
|
+
* The function `getNode` in TypeScript searches for a node in a singly linked list based on a given
|
|
495
|
+
* element, node, or predicate.
|
|
496
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean) | undefined} elementNodeOrPredicate
|
|
497
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `getNode` method can be one
|
|
498
|
+
* of the following types:
|
|
499
|
+
* @returns The `getNode` method returns either a `SinglyLinkedListNode<E>` if a matching node is
|
|
500
|
+
* found based on the provided predicate, or it returns `undefined` if no matching node is found or
|
|
501
|
+
* if the input parameter is `undefined`.
|
|
502
|
+
*/
|
|
503
|
+
getNode(
|
|
504
|
+
elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean) | undefined
|
|
505
|
+
): SinglyLinkedListNode<E> | undefined {
|
|
506
|
+
if (elementNodeOrPredicate === undefined) return;
|
|
507
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
464
508
|
let current = this.head;
|
|
465
509
|
|
|
466
510
|
while (current) {
|
|
467
|
-
if (current
|
|
511
|
+
if (predicate(current)) {
|
|
468
512
|
return current;
|
|
469
513
|
}
|
|
470
514
|
current = current.next;
|
|
@@ -477,31 +521,39 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
477
521
|
* Time Complexity: O(n)
|
|
478
522
|
* Space Complexity: O(1)
|
|
479
523
|
*
|
|
480
|
-
* The `addBefore`
|
|
481
|
-
*
|
|
482
|
-
*
|
|
483
|
-
*
|
|
484
|
-
* @
|
|
485
|
-
*
|
|
486
|
-
|
|
487
|
-
|
|
524
|
+
* The function `addBefore` in TypeScript adds a new element or node before an existing element or
|
|
525
|
+
* node in a singly linked list.
|
|
526
|
+
* @param {E | SinglyLinkedListNode<E>} existingElementOrNode - existingElementOrNode represents the
|
|
527
|
+
* element or node in the linked list before which you want to add a new element or node.
|
|
528
|
+
* @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
|
|
529
|
+
* `addBefore` method represents the element or node that you want to insert before the existing
|
|
530
|
+
* element or node in the linked list. This new element can be of type `E` or a
|
|
531
|
+
* `SinglyLinkedListNode<E>`.
|
|
532
|
+
* @returns The `addBefore` method returns a boolean value - `true` if the new element or node was
|
|
533
|
+
* successfully added before the existing element or node, and `false` if the operation was
|
|
534
|
+
* unsuccessful.
|
|
535
|
+
*/
|
|
536
|
+
addBefore(
|
|
537
|
+
existingElementOrNode: E | SinglyLinkedListNode<E>,
|
|
538
|
+
newElementOrNode: E | SinglyLinkedListNode<E>
|
|
539
|
+
): boolean {
|
|
488
540
|
if (!this.head) return false;
|
|
489
541
|
|
|
490
542
|
let existingValue: E;
|
|
491
|
-
if (
|
|
492
|
-
existingValue =
|
|
543
|
+
if (this.isNode(existingElementOrNode)) {
|
|
544
|
+
existingValue = existingElementOrNode.value;
|
|
493
545
|
} else {
|
|
494
|
-
existingValue =
|
|
546
|
+
existingValue = existingElementOrNode;
|
|
495
547
|
}
|
|
496
548
|
if (this.head.value === existingValue) {
|
|
497
|
-
this.unshift(
|
|
549
|
+
this.unshift(newElementOrNode);
|
|
498
550
|
return true;
|
|
499
551
|
}
|
|
500
552
|
|
|
501
553
|
let current = this.head;
|
|
502
554
|
while (current.next) {
|
|
503
555
|
if (current.next.value === existingValue) {
|
|
504
|
-
const newNode =
|
|
556
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
505
557
|
newNode.next = current.next;
|
|
506
558
|
current.next = newNode;
|
|
507
559
|
this._size++;
|
|
@@ -517,24 +569,23 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
517
569
|
* Time Complexity: O(n)
|
|
518
570
|
* Space Complexity: O(1)
|
|
519
571
|
*
|
|
520
|
-
* The `addAfter` function
|
|
521
|
-
*
|
|
522
|
-
*
|
|
523
|
-
*
|
|
524
|
-
* @
|
|
525
|
-
*
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
}
|
|
572
|
+
* The `addAfter` function in TypeScript adds a new element or node after an existing element or node
|
|
573
|
+
* in a singly linked list.
|
|
574
|
+
* @param {E | SinglyLinkedListNode<E>} existingElementOrNode - existingElementOrNode can be either
|
|
575
|
+
* an element of type E or a SinglyLinkedListNode of type E.
|
|
576
|
+
* @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
|
|
577
|
+
* `addAfter` method represents the element or node that you want to add after the existing element
|
|
578
|
+
* or node in a singly linked list. This parameter can be either the value of the new element or a
|
|
579
|
+
* reference to a `SinglyLinkedListNode` containing
|
|
580
|
+
* @returns The `addAfter` method returns a boolean value - `true` if the new element or node was
|
|
581
|
+
* successfully added after the existing element or node, and `false` if the existing element or node
|
|
582
|
+
* was not found.
|
|
583
|
+
*/
|
|
584
|
+
addAfter(existingElementOrNode: E | SinglyLinkedListNode<E>, newElementOrNode: E | SinglyLinkedListNode<E>): boolean {
|
|
585
|
+
const existingNode: SinglyLinkedListNode<E> | undefined = this.getNode(existingElementOrNode);
|
|
535
586
|
|
|
536
587
|
if (existingNode) {
|
|
537
|
-
const newNode =
|
|
588
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
538
589
|
newNode.next = existingNode.next;
|
|
539
590
|
existingNode.next = newNode;
|
|
540
591
|
if (existingNode === this.tail) {
|
|
@@ -551,16 +602,20 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
551
602
|
* Time Complexity: O(n)
|
|
552
603
|
* Space Complexity: O(1)
|
|
553
604
|
*
|
|
554
|
-
* The function
|
|
555
|
-
*
|
|
556
|
-
* @
|
|
557
|
-
|
|
558
|
-
|
|
605
|
+
* The function `countOccurrences` iterates through a singly linked list and counts the occurrences
|
|
606
|
+
* of a specified element or nodes that satisfy a given predicate.
|
|
607
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementOrNode
|
|
608
|
+
* - The `elementOrNode` parameter in the `countOccurrences` method can accept three types of values:
|
|
609
|
+
* @returns The `countOccurrences` method returns the number of occurrences of the specified element,
|
|
610
|
+
* node, or predicate function in the singly linked list.
|
|
611
|
+
*/
|
|
612
|
+
countOccurrences(elementOrNode: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): number {
|
|
613
|
+
const predicate = this._ensurePredicate(elementOrNode);
|
|
559
614
|
let count = 0;
|
|
560
615
|
let current = this.head;
|
|
561
616
|
|
|
562
617
|
while (current) {
|
|
563
|
-
if (current
|
|
618
|
+
if (predicate(current)) {
|
|
564
619
|
count++;
|
|
565
620
|
}
|
|
566
621
|
current = current.next;
|
|
@@ -657,4 +712,70 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
657
712
|
current = current.next;
|
|
658
713
|
}
|
|
659
714
|
}
|
|
715
|
+
|
|
716
|
+
/**
|
|
717
|
+
* Time Complexity: O(n)
|
|
718
|
+
* Space Complexity: O(n)
|
|
719
|
+
*
|
|
720
|
+
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
721
|
+
* array.
|
|
722
|
+
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
723
|
+
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
724
|
+
*/
|
|
725
|
+
static fromArray<E>(data: E[]) {
|
|
726
|
+
const singlyLinkedList = new SinglyLinkedList<E>();
|
|
727
|
+
for (const item of data) {
|
|
728
|
+
singlyLinkedList.push(item);
|
|
729
|
+
}
|
|
730
|
+
return singlyLinkedList;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* The _isPredicate function in TypeScript checks if the input is a function that takes a
|
|
735
|
+
* SinglyLinkedListNode as an argument and returns a boolean.
|
|
736
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
737
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following types:
|
|
738
|
+
* @returns The _isPredicate method is returning a boolean value based on whether the
|
|
739
|
+
* elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a
|
|
740
|
+
* function, the method will return true, indicating that it is a predicate function. If it is not a
|
|
741
|
+
* function, the method will return false.
|
|
742
|
+
*/
|
|
743
|
+
protected _isPredicate(
|
|
744
|
+
elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)
|
|
745
|
+
): elementNodeOrPredicate is (node: SinglyLinkedListNode<E>) => boolean {
|
|
746
|
+
return typeof elementNodeOrPredicate === 'function';
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* The function `_ensureNode` ensures that the input is a valid node and returns it, creating a new
|
|
751
|
+
* node if necessary.
|
|
752
|
+
* @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can be either
|
|
753
|
+
* an element of type `E` or a `SinglyLinkedListNode` containing an element of type `E`.
|
|
754
|
+
* @returns A SinglyLinkedListNode<E> object is being returned.
|
|
755
|
+
*/
|
|
756
|
+
protected _ensureNode(elementOrNode: E | SinglyLinkedListNode<E>) {
|
|
757
|
+
if (this.isNode(elementOrNode)) return elementOrNode;
|
|
758
|
+
|
|
759
|
+
return new SinglyLinkedListNode<E>(elementOrNode);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
/**
|
|
763
|
+
* The function `_ensurePredicate` in TypeScript ensures that the input is either a node, a predicate
|
|
764
|
+
* function, or a value to compare with the node's value.
|
|
765
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
766
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following types:
|
|
767
|
+
* @returns A function is being returned. If the input `elementNodeOrPredicate` is already a node, a
|
|
768
|
+
* function is returned that checks if a given node is equal to the input node. If the input is a
|
|
769
|
+
* predicate function, it is returned as is. If the input is neither a node nor a predicate function,
|
|
770
|
+
* a function is returned that checks if a given node's value is equal to the input
|
|
771
|
+
*/
|
|
772
|
+
protected _ensurePredicate(
|
|
773
|
+
elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)
|
|
774
|
+
) {
|
|
775
|
+
if (this.isNode(elementNodeOrPredicate)) return (node: SinglyLinkedListNode<E>) => node === elementNodeOrPredicate;
|
|
776
|
+
|
|
777
|
+
if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
|
|
778
|
+
|
|
779
|
+
return (node: SinglyLinkedListNode<E>) => node.value === elementNodeOrPredicate;
|
|
780
|
+
}
|
|
660
781
|
}
|
|
@@ -92,13 +92,100 @@ export class TrieNode {
|
|
|
92
92
|
* 9. Spell Check: Checking the spelling of words.
|
|
93
93
|
* 10. IP Routing: Used in certain types of IP routing algorithms.
|
|
94
94
|
* 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
|
|
95
|
+
* @example
|
|
96
|
+
* // Autocomplete: Prefix validation and checking
|
|
97
|
+
* const autocomplete = new Trie<string>(['gmail.com', 'gmail.co.nz', 'gmail.co.jp', 'yahoo.com', 'outlook.com']);
|
|
98
|
+
*
|
|
99
|
+
* // Get all completions for a prefix
|
|
100
|
+
* const gmailCompletions = autocomplete.getWords('gmail');
|
|
101
|
+
* console.log(gmailCompletions); // ['gmail.com', 'gmail.co.nz', 'gmail.co.jp']
|
|
102
|
+
* @example
|
|
103
|
+
* // File System Path Operations
|
|
104
|
+
* const fileSystem = new Trie<string>([
|
|
105
|
+
* '/home/user/documents/file1.txt',
|
|
106
|
+
* '/home/user/documents/file2.txt',
|
|
107
|
+
* '/home/user/pictures/photo.jpg',
|
|
108
|
+
* '/home/user/pictures/vacation/',
|
|
109
|
+
* '/home/user/downloads'
|
|
110
|
+
* ]);
|
|
111
|
+
*
|
|
112
|
+
* // Find common directory prefix
|
|
113
|
+
* console.log(fileSystem.getLongestCommonPrefix()); // '/home/user/'
|
|
114
|
+
*
|
|
115
|
+
* // List all files in a directory
|
|
116
|
+
* const documentsFiles = fileSystem.getWords('/home/user/documents/');
|
|
117
|
+
* console.log(documentsFiles); // ['/home/user/documents/file1.txt', '/home/user/documents/file2.txt']
|
|
118
|
+
* @example
|
|
119
|
+
* // Autocomplete: Basic word suggestions
|
|
120
|
+
* // Create a trie for autocomplete
|
|
121
|
+
* const autocomplete = new Trie<string>([
|
|
122
|
+
* 'function',
|
|
123
|
+
* 'functional',
|
|
124
|
+
* 'functions',
|
|
125
|
+
* 'class',
|
|
126
|
+
* 'classes',
|
|
127
|
+
* 'classical',
|
|
128
|
+
* 'closure',
|
|
129
|
+
* 'const',
|
|
130
|
+
* 'constructor'
|
|
131
|
+
* ]);
|
|
132
|
+
*
|
|
133
|
+
* // Test autocomplete with different prefixes
|
|
134
|
+
* console.log(autocomplete.getWords('fun')); // ['functional', 'functions', 'function']
|
|
135
|
+
* console.log(autocomplete.getWords('cla')); // ['classes', 'classical', 'class']
|
|
136
|
+
* console.log(autocomplete.getWords('con')); // ['constructor', 'const']
|
|
137
|
+
*
|
|
138
|
+
* // Test with non-matching prefix
|
|
139
|
+
* console.log(autocomplete.getWords('xyz')); // []
|
|
140
|
+
* @example
|
|
141
|
+
* // Dictionary: Case-insensitive word lookup
|
|
142
|
+
* // Create a case-insensitive dictionary
|
|
143
|
+
* const dictionary = new Trie<string>([], { caseSensitive: false });
|
|
144
|
+
*
|
|
145
|
+
* // Add words with mixed casing
|
|
146
|
+
* dictionary.add('Hello');
|
|
147
|
+
* dictionary.add('WORLD');
|
|
148
|
+
* dictionary.add('JavaScript');
|
|
149
|
+
*
|
|
150
|
+
* // Test lookups with different casings
|
|
151
|
+
* console.log(dictionary.has('hello')); // true
|
|
152
|
+
* console.log(dictionary.has('HELLO')); // true
|
|
153
|
+
* console.log(dictionary.has('Hello')); // true
|
|
154
|
+
* console.log(dictionary.has('javascript')); // true
|
|
155
|
+
* console.log(dictionary.has('JAVASCRIPT')); // true
|
|
156
|
+
* @example
|
|
157
|
+
* // IP Address Routing Table
|
|
158
|
+
* // Add IP address prefixes and their corresponding routes
|
|
159
|
+
* const routes = {
|
|
160
|
+
* '192.168.1': 'LAN_SUBNET_1',
|
|
161
|
+
* '192.168.2': 'LAN_SUBNET_2',
|
|
162
|
+
* '10.0.0': 'PRIVATE_NETWORK_1',
|
|
163
|
+
* '10.0.1': 'PRIVATE_NETWORK_2'
|
|
164
|
+
* };
|
|
165
|
+
*
|
|
166
|
+
* const ipRoutingTable = new Trie<string>(Object.keys(routes));
|
|
167
|
+
*
|
|
168
|
+
* // Check IP address prefix matching
|
|
169
|
+
* console.log(ipRoutingTable.hasPrefix('192.168.1')); // true
|
|
170
|
+
* console.log(ipRoutingTable.hasPrefix('192.168.2')); // true
|
|
171
|
+
*
|
|
172
|
+
* // Validate IP address belongs to subnet
|
|
173
|
+
* const ip = '192.168.1.100';
|
|
174
|
+
* const subnet = ip.split('.').slice(0, 3).join('.');
|
|
175
|
+
* console.log(ipRoutingTable.hasPrefix(subnet)); // true
|
|
95
176
|
*/
|
|
96
177
|
export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
|
|
97
178
|
/**
|
|
98
|
-
* The constructor
|
|
99
|
-
*
|
|
100
|
-
* @param
|
|
101
|
-
*
|
|
179
|
+
* The constructor initializes a Trie data structure with optional options and words provided as
|
|
180
|
+
* input.
|
|
181
|
+
* @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the constructor is an
|
|
182
|
+
* iterable containing either strings or elements of type `R`. It is used to initialize the Trie with
|
|
183
|
+
* a list of words or elements. If no `words` are provided, an empty iterable is used as the default
|
|
184
|
+
* value.
|
|
185
|
+
* @param [options] - The `options` parameter in the constructor is an optional object that can
|
|
186
|
+
* contain configuration options for the Trie data structure. One of the options it can have is
|
|
187
|
+
* `caseSensitive`, which is a boolean value indicating whether the Trie should be case-sensitive or
|
|
188
|
+
* not. If `caseSensitive` is set to `
|
|
102
189
|
*/
|
|
103
190
|
constructor(words: Iterable<string> | Iterable<R> = [], options?: TrieOptions<R>) {
|
|
104
191
|
super(options);
|
|
@@ -107,13 +194,7 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
|
|
|
107
194
|
if (caseSensitive !== undefined) this._caseSensitive = caseSensitive;
|
|
108
195
|
}
|
|
109
196
|
if (words) {
|
|
110
|
-
|
|
111
|
-
if (this.toElementFn) {
|
|
112
|
-
this.add(this.toElementFn(word as R));
|
|
113
|
-
} else {
|
|
114
|
-
this.add(word as string);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
197
|
+
this.addMany(words);
|
|
117
198
|
}
|
|
118
199
|
}
|
|
119
200
|
|
|
@@ -175,6 +256,30 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
|
|
|
175
256
|
return isNewWord;
|
|
176
257
|
}
|
|
177
258
|
|
|
259
|
+
/**
|
|
260
|
+
* Time Complexity: O(n * l)
|
|
261
|
+
* Space Complexity: O(1)
|
|
262
|
+
*
|
|
263
|
+
* The `addMany` function in TypeScript takes an iterable of strings or elements of type R, converts
|
|
264
|
+
* them using a provided function if available, and adds them to a data structure while returning an
|
|
265
|
+
* array of boolean values indicating success.
|
|
266
|
+
* @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the `addMany` function is
|
|
267
|
+
* an iterable that contains either strings or elements of type `R`.
|
|
268
|
+
* @returns The `addMany` method returns an array of boolean values indicating whether each word in
|
|
269
|
+
* the input iterable was successfully added to the data structure.
|
|
270
|
+
*/
|
|
271
|
+
addMany(words: Iterable<string> | Iterable<R> = []): boolean[] {
|
|
272
|
+
const ans: boolean[] = [];
|
|
273
|
+
for (const word of words) {
|
|
274
|
+
if (this.toElementFn) {
|
|
275
|
+
ans.push(this.add(this.toElementFn(word as R)));
|
|
276
|
+
} else {
|
|
277
|
+
ans.push(this.add(word as string));
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return ans;
|
|
281
|
+
}
|
|
282
|
+
|
|
178
283
|
/**
|
|
179
284
|
* Time Complexity: O(l), where l is the length of the input word.
|
|
180
285
|
* Space Complexity: O(1) - Constant space.
|
package/src/index.ts
CHANGED
|
@@ -10,4 +10,5 @@ export * from './data-structures/heap/heap';
|
|
|
10
10
|
export * from './types/data-structures/heap/min-heap';
|
|
11
11
|
export * from './types/data-structures/heap/heap';
|
|
12
12
|
export * from './types/common';
|
|
13
|
-
export * from './
|
|
13
|
+
export * from './types/utils';
|
|
14
|
+
export * from './common';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../../../data-structures';
|
|
2
2
|
import { IterationType, OptValue } from '../../common';
|
|
3
|
-
import { DFSOperation } from '../../../
|
|
3
|
+
import { DFSOperation } from '../../../common';
|
|
4
4
|
|
|
5
5
|
export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
6
6
|
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { BST, BSTNode } from '../../../data-structures';
|
|
2
2
|
import type { BinaryTreeOptions } from './binary-tree';
|
|
3
|
-
import {
|
|
3
|
+
import { Comparable } from '../../utils';
|
|
4
4
|
|
|
5
5
|
export type BSTNodeNested<K, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
6
6
|
|
|
7
7
|
export type BSTNested<K, V, R, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
8
8
|
|
|
9
9
|
export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & {
|
|
10
|
-
|
|
10
|
+
extractComparable?: (key: K) => Comparable
|
|
11
|
+
isReverse?: boolean;
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
export type BSTNOptKey<K> = K | undefined;
|
|
@@ -7,4 +7,4 @@ export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNo
|
|
|
7
7
|
|
|
8
8
|
export type RedBlackTreeNested<K, V, R, NODE extends RedBlackTreeNode<K, V, NODE>> = RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
9
9
|
|
|
10
|
-
export type RBTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};
|
|
10
|
+
export type RBTreeOptions<K, V, R> = Omit<BSTOptions<K, V, R>, 'isReverse'> & {};
|