priority-queue-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.
Files changed (45) hide show
  1. package/README.md +5 -9
  2. package/dist/common/index.d.ts +12 -0
  3. package/dist/common/index.js +23 -0
  4. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +7 -10
  5. package/dist/data-structures/binary-tree/avl-tree.js +2 -2
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +54 -19
  7. package/dist/data-structures/binary-tree/binary-tree.js +100 -66
  8. package/dist/data-structures/binary-tree/bst.d.ts +100 -36
  9. package/dist/data-structures/binary-tree/bst.js +185 -66
  10. package/dist/data-structures/binary-tree/rb-tree.d.ts +4 -0
  11. package/dist/data-structures/binary-tree/rb-tree.js +6 -2
  12. package/dist/data-structures/binary-tree/tree-multi-map.js +2 -2
  13. package/dist/data-structures/heap/heap.d.ts +6 -6
  14. package/dist/data-structures/heap/heap.js +6 -6
  15. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +31 -19
  16. package/dist/data-structures/linked-list/doubly-linked-list.js +49 -34
  17. package/dist/data-structures/linked-list/singly-linked-list.d.ts +144 -62
  18. package/dist/data-structures/linked-list/singly-linked-list.js +201 -97
  19. package/dist/data-structures/trie/trie.d.ts +104 -4
  20. package/dist/data-structures/trie/trie.js +116 -12
  21. package/dist/index.d.ts +2 -1
  22. package/dist/index.js +2 -1
  23. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  24. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
  25. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
  26. package/dist/types/utils/utils.d.ts +10 -6
  27. package/dist/utils/utils.js +4 -2
  28. package/package.json +2 -2
  29. package/src/common/index.ts +19 -0
  30. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -9
  31. package/src/data-structures/binary-tree/avl-tree.ts +3 -2
  32. package/src/data-structures/binary-tree/binary-tree.ts +108 -64
  33. package/src/data-structures/binary-tree/bst.ts +190 -69
  34. package/src/data-structures/binary-tree/rb-tree.ts +6 -2
  35. package/src/data-structures/binary-tree/tree-multi-map.ts +3 -3
  36. package/src/data-structures/heap/heap.ts +39 -39
  37. package/src/data-structures/linked-list/doubly-linked-list.ts +139 -121
  38. package/src/data-structures/linked-list/singly-linked-list.ts +219 -98
  39. package/src/data-structures/trie/trie.ts +116 -11
  40. package/src/index.ts +2 -1
  41. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  42. package/src/types/data-structures/binary-tree/bst.ts +3 -2
  43. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
  44. package/src/types/utils/utils.ts +16 -10
  45. package/src/utils/utils.ts +4 -2
@@ -99,33 +99,17 @@ class SinglyLinkedList extends base_1.IterableElementBase {
99
99
  get size() {
100
100
  return this._size;
101
101
  }
102
- /**
103
- * Time Complexity: O(n)
104
- * Space Complexity: O(n)
105
- *
106
- * The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
107
- * array.
108
- * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
109
- * @returns The `fromArray` function returns a `SinglyLinkedList` object.
110
- */
111
- static fromArray(data) {
112
- const singlyLinkedList = new SinglyLinkedList();
113
- for (const item of data) {
114
- singlyLinkedList.push(item);
115
- }
116
- return singlyLinkedList;
117
- }
118
102
  /**
119
103
  * Time Complexity: O(1)
120
104
  * Space Complexity: O(1)
121
105
  *
122
- * The push function adds a new element to the end of a singly linked list.
123
- * @param {E} element - The "element" parameter represents the value of the element that you want to
124
- * add to the linked list.
125
- * @returns The `push` method is returning a boolean value, `true`.
106
+ * The `push` function adds a new element or node to the end of a singly linked list.
107
+ * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the `push`
108
+ * method can accept either an element of type `E` or a `SinglyLinkedListNode<E>` object.
109
+ * @returns The `push` method is returning a boolean value, specifically `true`.
126
110
  */
127
- push(element) {
128
- const newNode = new SinglyLinkedListNode(element);
111
+ push(elementOrNode) {
112
+ const newNode = this._ensureNode(elementOrNode);
129
113
  if (!this.head) {
130
114
  this._head = newNode;
131
115
  this._tail = newNode;
@@ -184,13 +168,15 @@ class SinglyLinkedList extends base_1.IterableElementBase {
184
168
  * Time Complexity: O(1)
185
169
  * Space Complexity: O(1)
186
170
  *
187
- * The unshift function adds a new element to the beginning of a singly linked list.
188
- * @param {E} element - The "element" parameter represents the value of the element that you want to
189
- * add to the beginning of the singly linked list.
190
- * @returns The `unshift` method is returning a boolean value, `true`.
191
- */
192
- unshift(element) {
193
- const newNode = new SinglyLinkedListNode(element);
171
+ * The unshift function adds a new element or node to the beginning of a singly linked list in
172
+ * TypeScript.
173
+ * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
174
+ * `unshift` method can be either an element of type `E` or a `SinglyLinkedListNode` containing an
175
+ * element of type `E`.
176
+ * @returns The `unshift` method is returning a boolean value, specifically `true`.
177
+ */
178
+ unshift(elementOrNode) {
179
+ const newNode = this._ensureNode(elementOrNode);
194
180
  if (!this.head) {
195
181
  this._head = newNode;
196
182
  this._tail = newNode;
@@ -202,6 +188,28 @@ class SinglyLinkedList extends base_1.IterableElementBase {
202
188
  this._size++;
203
189
  return true;
204
190
  }
191
+ /**
192
+ * Time Complexity: O(n)
193
+ * Space Complexity: O(1)
194
+ *
195
+ * This function searches for a specific element in a singly linked list based on a given node or
196
+ * predicate.
197
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
198
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `get` method can be one of
199
+ * the following types:
200
+ * @returns The `get` method returns the value of the first node in the singly linked list that
201
+ * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
202
+ */
203
+ search(elementNodeOrPredicate) {
204
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
205
+ let current = this.head;
206
+ while (current) {
207
+ if (predicate(current))
208
+ return current.value;
209
+ current = current.next;
210
+ }
211
+ return undefined;
212
+ }
205
213
  /**
206
214
  * Time Complexity: O(n)
207
215
  * Space Complexity: O(1)
@@ -221,6 +229,22 @@ class SinglyLinkedList extends base_1.IterableElementBase {
221
229
  }
222
230
  return current.value;
223
231
  }
232
+ /**
233
+ * Time Complexity: O(1)
234
+ * Space Complexity: O(1)
235
+ *
236
+ * The function `isNode` in TypeScript checks if the input is an instance of `SinglyLinkedListNode`.
237
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
238
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `isNode` function can be
239
+ * one of the following types:
240
+ * @returns The `isNode` function is checking if the `elementNodeOrPredicate` parameter is an
241
+ * instance of `SinglyLinkedListNode<E>`. If it is, the function returns `true`, indicating that the
242
+ * parameter is a `SinglyLinkedListNode<E>`. If it is not an instance of `SinglyLinkedListNode<E>`,
243
+ * the function returns `false`.
244
+ */
245
+ isNode(elementNodeOrPredicate) {
246
+ return elementNodeOrPredicate instanceof SinglyLinkedListNode;
247
+ }
224
248
  /**
225
249
  * Time Complexity: O(n)
226
250
  * Space Complexity: O(1)
@@ -270,20 +294,20 @@ class SinglyLinkedList extends base_1.IterableElementBase {
270
294
  * Space Complexity: O(1)
271
295
  *
272
296
  * The delete function removes a node with a specific value from a singly linked list.
273
- * @param {E | SinglyLinkedListNode<E>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `E`
297
+ * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can accept either a value of type `E`
274
298
  * or a `SinglyLinkedListNode<E>` object.
275
299
  * @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
276
300
  * successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
277
301
  */
278
- delete(valueOrNode) {
279
- if (valueOrNode === undefined)
302
+ delete(elementOrNode) {
303
+ if (elementOrNode === undefined)
280
304
  return false;
281
305
  let value;
282
- if (valueOrNode instanceof SinglyLinkedListNode) {
283
- value = valueOrNode.value;
306
+ if (elementOrNode instanceof SinglyLinkedListNode) {
307
+ value = elementOrNode.value;
284
308
  }
285
309
  else {
286
- value = valueOrNode;
310
+ value = elementOrNode;
287
311
  }
288
312
  let current = this.head, prev = undefined;
289
313
  while (current) {
@@ -312,26 +336,29 @@ class SinglyLinkedList extends base_1.IterableElementBase {
312
336
  * Time Complexity: O(n)
313
337
  * Space Complexity: O(1)
314
338
  *
315
- * The `addAt` function inserts a value at a specified index in a singly linked list.
316
- * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
317
- * linked list. It is of type number.
318
- * @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
319
- * specified index.
320
- * @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
321
- * if the index is out of bounds.
322
- */
323
- addAt(index, value) {
339
+ * The `addAt` function inserts a new element or node at a specified index in a singly linked list.
340
+ * @param {number} index - The `index` parameter represents the position at which you want to add a
341
+ * new element or node in the linked list. It is a number that indicates the index where the new
342
+ * element or node should be inserted.
343
+ * @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
344
+ * `addAt` method can be either a value of type `E` or a `SinglyLinkedListNode<E>` object. This
345
+ * parameter represents the element or node that you want to add to the linked list at the specified
346
+ * index.
347
+ * @returns The `addAt` method returns a boolean value - `true` if the element or node was
348
+ * successfully added at the specified index, and `false` if the index is out of bounds.
349
+ */
350
+ addAt(index, newElementOrNode) {
324
351
  if (index < 0 || index > this._size)
325
352
  return false;
326
353
  if (index === 0) {
327
- this.unshift(value);
354
+ this.unshift(newElementOrNode);
328
355
  return true;
329
356
  }
330
357
  if (index === this._size) {
331
- this.push(value);
358
+ this.push(newElementOrNode);
332
359
  return true;
333
360
  }
334
- const newNode = new SinglyLinkedListNode(value);
361
+ const newNode = this._ensureNode(newElementOrNode);
335
362
  const prevNode = this.getNodeAt(index - 1);
336
363
  newNode.next = prevNode.next;
337
364
  prevNode.next = newNode;
@@ -396,16 +423,21 @@ class SinglyLinkedList extends base_1.IterableElementBase {
396
423
  * Time Complexity: O(n)
397
424
  * Space Complexity: O(1)
398
425
  *
399
- * The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
400
- * @param {E} value - The value parameter is the value that you want to find the index of in the linked list.
401
- * @returns The method is returning the index of the first occurrence of the specified value in the linked list. If the
402
- * value is not found, it returns -1.
403
- */
404
- indexOf(value) {
426
+ * The `indexOf` function in TypeScript searches for a specific element or node in a singly linked
427
+ * list and returns its index if found.
428
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
429
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `indexOf` method can be one
430
+ * of the following types:
431
+ * @returns The `indexOf` method returns the index of the first occurrence of the element that
432
+ * matches the provided predicate in the singly linked list. If no matching element is found, it
433
+ * returns -1.
434
+ */
435
+ indexOf(elementNodeOrPredicate) {
436
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
405
437
  let index = 0;
406
438
  let current = this.head;
407
439
  while (current) {
408
- if (current.value === value) {
440
+ if (predicate(current)) {
409
441
  return index;
410
442
  }
411
443
  index++;
@@ -417,16 +449,22 @@ class SinglyLinkedList extends base_1.IterableElementBase {
417
449
  * Time Complexity: O(n)
418
450
  * Space Complexity: O(1)
419
451
  *
420
- * The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
421
- * undefined.
422
- * @param {E} value - The value parameter is the value that we want to search for in the linked list.
423
- * @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
424
- * the specified value is found, the function returns `undefined`.
425
- */
426
- getNode(value) {
452
+ * The function `getNode` in TypeScript searches for a node in a singly linked list based on a given
453
+ * element, node, or predicate.
454
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean) | undefined} elementNodeOrPredicate
455
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `getNode` method can be one
456
+ * of the following types:
457
+ * @returns The `getNode` method returns either a `SinglyLinkedListNode<E>` if a matching node is
458
+ * found based on the provided predicate, or it returns `undefined` if no matching node is found or
459
+ * if the input parameter is `undefined`.
460
+ */
461
+ getNode(elementNodeOrPredicate) {
462
+ if (elementNodeOrPredicate === undefined)
463
+ return;
464
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
427
465
  let current = this.head;
428
466
  while (current) {
429
- if (current.value === value) {
467
+ if (predicate(current)) {
430
468
  return current;
431
469
  }
432
470
  current = current.next;
@@ -437,31 +475,36 @@ class SinglyLinkedList extends base_1.IterableElementBase {
437
475
  * Time Complexity: O(n)
438
476
  * Space Complexity: O(1)
439
477
  *
440
- * The `addBefore` function inserts a new value before an existing value in a singly linked list.
441
- * @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
442
- * new value before. It can be either the value itself or a node containing the value in the linked list.
443
- * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
444
- * @returns The method `addBefore` returns a boolean value. It returns `true` if the new value was successfully
445
- * inserted before the existing value, and `false` otherwise.
446
- */
447
- addBefore(existingValueOrNode, newValue) {
478
+ * The function `addBefore` in TypeScript adds a new element or node before an existing element or
479
+ * node in a singly linked list.
480
+ * @param {E | SinglyLinkedListNode<E>} existingElementOrNode - existingElementOrNode represents the
481
+ * element or node in the linked list before which you want to add a new element or node.
482
+ * @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
483
+ * `addBefore` method represents the element or node that you want to insert before the existing
484
+ * element or node in the linked list. This new element can be of type `E` or a
485
+ * `SinglyLinkedListNode<E>`.
486
+ * @returns The `addBefore` method returns a boolean value - `true` if the new element or node was
487
+ * successfully added before the existing element or node, and `false` if the operation was
488
+ * unsuccessful.
489
+ */
490
+ addBefore(existingElementOrNode, newElementOrNode) {
448
491
  if (!this.head)
449
492
  return false;
450
493
  let existingValue;
451
- if (existingValueOrNode instanceof SinglyLinkedListNode) {
452
- existingValue = existingValueOrNode.value;
494
+ if (this.isNode(existingElementOrNode)) {
495
+ existingValue = existingElementOrNode.value;
453
496
  }
454
497
  else {
455
- existingValue = existingValueOrNode;
498
+ existingValue = existingElementOrNode;
456
499
  }
457
500
  if (this.head.value === existingValue) {
458
- this.unshift(newValue);
501
+ this.unshift(newElementOrNode);
459
502
  return true;
460
503
  }
461
504
  let current = this.head;
462
505
  while (current.next) {
463
506
  if (current.next.value === existingValue) {
464
- const newNode = new SinglyLinkedListNode(newValue);
507
+ const newNode = this._ensureNode(newElementOrNode);
465
508
  newNode.next = current.next;
466
509
  current.next = newNode;
467
510
  this._size++;
@@ -475,23 +518,22 @@ class SinglyLinkedList extends base_1.IterableElementBase {
475
518
  * Time Complexity: O(n)
476
519
  * Space Complexity: O(1)
477
520
  *
478
- * The `addAfter` function inserts a new node with a given value after an existing node in a singly linked list.
479
- * @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
480
- * the new value will be inserted. It can be either the value of the existing node or the existing node itself.
481
- * @param {E} newValue - The value that you want to insert into the linked list after the existing value or node.
482
- * @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
483
- * existing value or node, and false if the existing value or node was not found in the linked list.
484
- */
485
- addAfter(existingValueOrNode, newValue) {
486
- let existingNode;
487
- if (existingValueOrNode instanceof SinglyLinkedListNode) {
488
- existingNode = existingValueOrNode;
489
- }
490
- else {
491
- existingNode = this.getNode(existingValueOrNode);
492
- }
521
+ * The `addAfter` function in TypeScript adds a new element or node after an existing element or node
522
+ * in a singly linked list.
523
+ * @param {E | SinglyLinkedListNode<E>} existingElementOrNode - existingElementOrNode can be either
524
+ * an element of type E or a SinglyLinkedListNode of type E.
525
+ * @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
526
+ * `addAfter` method represents the element or node that you want to add after the existing element
527
+ * or node in a singly linked list. This parameter can be either the value of the new element or a
528
+ * reference to a `SinglyLinkedListNode` containing
529
+ * @returns The `addAfter` method returns a boolean value - `true` if the new element or node was
530
+ * successfully added after the existing element or node, and `false` if the existing element or node
531
+ * was not found.
532
+ */
533
+ addAfter(existingElementOrNode, newElementOrNode) {
534
+ const existingNode = this.getNode(existingElementOrNode);
493
535
  if (existingNode) {
494
- const newNode = new SinglyLinkedListNode(newValue);
536
+ const newNode = this._ensureNode(newElementOrNode);
495
537
  newNode.next = existingNode.next;
496
538
  existingNode.next = newNode;
497
539
  if (existingNode === this.tail) {
@@ -506,15 +548,19 @@ class SinglyLinkedList extends base_1.IterableElementBase {
506
548
  * Time Complexity: O(n)
507
549
  * Space Complexity: O(1)
508
550
  *
509
- * The function counts the number of occurrences of a given value in a linked list.
510
- * @param {E} value - The value parameter is the value that you want to count the occurrences of in the linked list.
511
- * @returns The count of occurrences of the given value in the linked list.
512
- */
513
- countOccurrences(value) {
551
+ * The function `countOccurrences` iterates through a singly linked list and counts the occurrences
552
+ * of a specified element or nodes that satisfy a given predicate.
553
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementOrNode
554
+ * - The `elementOrNode` parameter in the `countOccurrences` method can accept three types of values:
555
+ * @returns The `countOccurrences` method returns the number of occurrences of the specified element,
556
+ * node, or predicate function in the singly linked list.
557
+ */
558
+ countOccurrences(elementOrNode) {
559
+ const predicate = this._ensurePredicate(elementOrNode);
514
560
  let count = 0;
515
561
  let current = this.head;
516
562
  while (current) {
517
- if (current.value === value) {
563
+ if (predicate(current)) {
518
564
  count++;
519
565
  }
520
566
  current = current.next;
@@ -600,5 +646,63 @@ class SinglyLinkedList extends base_1.IterableElementBase {
600
646
  current = current.next;
601
647
  }
602
648
  }
649
+ /**
650
+ * Time Complexity: O(n)
651
+ * Space Complexity: O(n)
652
+ *
653
+ * The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
654
+ * array.
655
+ * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
656
+ * @returns The `fromArray` function returns a `SinglyLinkedList` object.
657
+ */
658
+ static fromArray(data) {
659
+ const singlyLinkedList = new SinglyLinkedList();
660
+ for (const item of data) {
661
+ singlyLinkedList.push(item);
662
+ }
663
+ return singlyLinkedList;
664
+ }
665
+ /**
666
+ * The _isPredicate function in TypeScript checks if the input is a function that takes a
667
+ * SinglyLinkedListNode as an argument and returns a boolean.
668
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
669
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following types:
670
+ * @returns The _isPredicate method is returning a boolean value based on whether the
671
+ * elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a
672
+ * function, the method will return true, indicating that it is a predicate function. If it is not a
673
+ * function, the method will return false.
674
+ */
675
+ _isPredicate(elementNodeOrPredicate) {
676
+ return typeof elementNodeOrPredicate === 'function';
677
+ }
678
+ /**
679
+ * The function `_ensureNode` ensures that the input is a valid node and returns it, creating a new
680
+ * node if necessary.
681
+ * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can be either
682
+ * an element of type `E` or a `SinglyLinkedListNode` containing an element of type `E`.
683
+ * @returns A SinglyLinkedListNode<E> object is being returned.
684
+ */
685
+ _ensureNode(elementOrNode) {
686
+ if (this.isNode(elementOrNode))
687
+ return elementOrNode;
688
+ return new SinglyLinkedListNode(elementOrNode);
689
+ }
690
+ /**
691
+ * The function `_ensurePredicate` in TypeScript ensures that the input is either a node, a predicate
692
+ * function, or a value to compare with the node's value.
693
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
694
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following types:
695
+ * @returns A function is being returned. If the input `elementNodeOrPredicate` is already a node, a
696
+ * function is returned that checks if a given node is equal to the input node. If the input is a
697
+ * predicate function, it is returned as is. If the input is neither a node nor a predicate function,
698
+ * a function is returned that checks if a given node's value is equal to the input
699
+ */
700
+ _ensurePredicate(elementNodeOrPredicate) {
701
+ if (this.isNode(elementNodeOrPredicate))
702
+ return (node) => node === elementNodeOrPredicate;
703
+ if (this._isPredicate(elementNodeOrPredicate))
704
+ return elementNodeOrPredicate;
705
+ return (node) => node.value === elementNodeOrPredicate;
706
+ }
603
707
  }
604
708
  exports.SinglyLinkedList = SinglyLinkedList;
@@ -65,13 +65,100 @@ export declare class TrieNode {
65
65
  * 9. Spell Check: Checking the spelling of words.
66
66
  * 10. IP Routing: Used in certain types of IP routing algorithms.
67
67
  * 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
68
+ * @example
69
+ * // Autocomplete: Prefix validation and checking
70
+ * const autocomplete = new Trie<string>(['gmail.com', 'gmail.co.nz', 'gmail.co.jp', 'yahoo.com', 'outlook.com']);
71
+ *
72
+ * // Get all completions for a prefix
73
+ * const gmailCompletions = autocomplete.getWords('gmail');
74
+ * console.log(gmailCompletions); // ['gmail.com', 'gmail.co.nz', 'gmail.co.jp']
75
+ * @example
76
+ * // File System Path Operations
77
+ * const fileSystem = new Trie<string>([
78
+ * '/home/user/documents/file1.txt',
79
+ * '/home/user/documents/file2.txt',
80
+ * '/home/user/pictures/photo.jpg',
81
+ * '/home/user/pictures/vacation/',
82
+ * '/home/user/downloads'
83
+ * ]);
84
+ *
85
+ * // Find common directory prefix
86
+ * console.log(fileSystem.getLongestCommonPrefix()); // '/home/user/'
87
+ *
88
+ * // List all files in a directory
89
+ * const documentsFiles = fileSystem.getWords('/home/user/documents/');
90
+ * console.log(documentsFiles); // ['/home/user/documents/file1.txt', '/home/user/documents/file2.txt']
91
+ * @example
92
+ * // Autocomplete: Basic word suggestions
93
+ * // Create a trie for autocomplete
94
+ * const autocomplete = new Trie<string>([
95
+ * 'function',
96
+ * 'functional',
97
+ * 'functions',
98
+ * 'class',
99
+ * 'classes',
100
+ * 'classical',
101
+ * 'closure',
102
+ * 'const',
103
+ * 'constructor'
104
+ * ]);
105
+ *
106
+ * // Test autocomplete with different prefixes
107
+ * console.log(autocomplete.getWords('fun')); // ['functional', 'functions', 'function']
108
+ * console.log(autocomplete.getWords('cla')); // ['classes', 'classical', 'class']
109
+ * console.log(autocomplete.getWords('con')); // ['constructor', 'const']
110
+ *
111
+ * // Test with non-matching prefix
112
+ * console.log(autocomplete.getWords('xyz')); // []
113
+ * @example
114
+ * // Dictionary: Case-insensitive word lookup
115
+ * // Create a case-insensitive dictionary
116
+ * const dictionary = new Trie<string>([], { caseSensitive: false });
117
+ *
118
+ * // Add words with mixed casing
119
+ * dictionary.add('Hello');
120
+ * dictionary.add('WORLD');
121
+ * dictionary.add('JavaScript');
122
+ *
123
+ * // Test lookups with different casings
124
+ * console.log(dictionary.has('hello')); // true
125
+ * console.log(dictionary.has('HELLO')); // true
126
+ * console.log(dictionary.has('Hello')); // true
127
+ * console.log(dictionary.has('javascript')); // true
128
+ * console.log(dictionary.has('JAVASCRIPT')); // true
129
+ * @example
130
+ * // IP Address Routing Table
131
+ * // Add IP address prefixes and their corresponding routes
132
+ * const routes = {
133
+ * '192.168.1': 'LAN_SUBNET_1',
134
+ * '192.168.2': 'LAN_SUBNET_2',
135
+ * '10.0.0': 'PRIVATE_NETWORK_1',
136
+ * '10.0.1': 'PRIVATE_NETWORK_2'
137
+ * };
138
+ *
139
+ * const ipRoutingTable = new Trie<string>(Object.keys(routes));
140
+ *
141
+ * // Check IP address prefix matching
142
+ * console.log(ipRoutingTable.hasPrefix('192.168.1')); // true
143
+ * console.log(ipRoutingTable.hasPrefix('192.168.2')); // true
144
+ *
145
+ * // Validate IP address belongs to subnet
146
+ * const ip = '192.168.1.100';
147
+ * const subnet = ip.split('.').slice(0, 3).join('.');
148
+ * console.log(ipRoutingTable.hasPrefix(subnet)); // true
68
149
  */
69
150
  export declare class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
70
151
  /**
71
- * The constructor function for the Trie class.
72
- * @param words: Iterable string Initialize the trie with a set of words
73
- * @param options?: TrieOptions Allow the user to pass in options for the trie
74
- * @return This
152
+ * The constructor initializes a Trie data structure with optional options and words provided as
153
+ * input.
154
+ * @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the constructor is an
155
+ * iterable containing either strings or elements of type `R`. It is used to initialize the Trie with
156
+ * a list of words or elements. If no `words` are provided, an empty iterable is used as the default
157
+ * value.
158
+ * @param [options] - The `options` parameter in the constructor is an optional object that can
159
+ * contain configuration options for the Trie data structure. One of the options it can have is
160
+ * `caseSensitive`, which is a boolean value indicating whether the Trie should be case-sensitive or
161
+ * not. If `caseSensitive` is set to `
75
162
  */
76
163
  constructor(words?: Iterable<string> | Iterable<R>, options?: TrieOptions<R>);
77
164
  protected _size: number;
@@ -101,6 +188,19 @@ export declare class Trie<R = any> extends IterableElementBase<string, R, Trie<R
101
188
  * @returns {boolean} True if the word was successfully added.
102
189
  */
103
190
  add(word: string): boolean;
191
+ /**
192
+ * Time Complexity: O(n * l)
193
+ * Space Complexity: O(1)
194
+ *
195
+ * The `addMany` function in TypeScript takes an iterable of strings or elements of type R, converts
196
+ * them using a provided function if available, and adds them to a data structure while returning an
197
+ * array of boolean values indicating success.
198
+ * @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the `addMany` function is
199
+ * an iterable that contains either strings or elements of type `R`.
200
+ * @returns The `addMany` method returns an array of boolean values indicating whether each word in
201
+ * the input iterable was successfully added to the data structure.
202
+ */
203
+ addMany(words?: Iterable<string> | Iterable<R>): boolean[];
104
204
  /**
105
205
  * Time Complexity: O(l), where l is the length of the input word.
106
206
  * Space Complexity: O(1) - Constant space.