graph-typed 1.53.6 → 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 (44) hide show
  1. package/dist/common/index.d.ts +12 -0
  2. package/dist/common/index.js +23 -0
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +7 -10
  4. package/dist/data-structures/binary-tree/avl-tree.js +2 -2
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +54 -19
  6. package/dist/data-structures/binary-tree/binary-tree.js +100 -66
  7. package/dist/data-structures/binary-tree/bst.d.ts +100 -36
  8. package/dist/data-structures/binary-tree/bst.js +185 -66
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +4 -0
  10. package/dist/data-structures/binary-tree/rb-tree.js +6 -2
  11. package/dist/data-structures/binary-tree/tree-multi-map.js +2 -2
  12. package/dist/data-structures/heap/heap.d.ts +6 -6
  13. package/dist/data-structures/heap/heap.js +6 -6
  14. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +18 -8
  15. package/dist/data-structures/linked-list/doubly-linked-list.js +24 -10
  16. package/dist/data-structures/linked-list/singly-linked-list.d.ts +1 -1
  17. package/dist/data-structures/linked-list/singly-linked-list.js +1 -1
  18. package/dist/data-structures/trie/trie.d.ts +104 -4
  19. package/dist/data-structures/trie/trie.js +116 -12
  20. package/dist/index.d.ts +2 -1
  21. package/dist/index.js +2 -1
  22. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  23. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
  24. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
  25. package/dist/types/utils/utils.d.ts +10 -6
  26. package/dist/utils/utils.js +4 -2
  27. package/package.json +2 -2
  28. package/src/common/index.ts +19 -0
  29. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -9
  30. package/src/data-structures/binary-tree/avl-tree.ts +3 -2
  31. package/src/data-structures/binary-tree/binary-tree.ts +108 -64
  32. package/src/data-structures/binary-tree/bst.ts +190 -69
  33. package/src/data-structures/binary-tree/rb-tree.ts +6 -2
  34. package/src/data-structures/binary-tree/tree-multi-map.ts +3 -3
  35. package/src/data-structures/heap/heap.ts +39 -39
  36. package/src/data-structures/linked-list/doubly-linked-list.ts +111 -97
  37. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  38. package/src/data-structures/trie/trie.ts +116 -11
  39. package/src/index.ts +2 -1
  40. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  41. package/src/types/data-structures/binary-tree/bst.ts +3 -2
  42. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
  43. package/src/types/utils/utils.ts +16 -10
  44. package/src/utils/utils.ts +4 -2
@@ -99,7 +99,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
99
99
  * existing `iterationType` property. The returned value is casted as `TREE`.
100
100
  */
101
101
  createTree(options) {
102
- return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, comparator: this._comparator, toEntryFn: this._toEntryFn }, options));
102
+ return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, extractComparable: this._extractComparable, toEntryFn: this._toEntryFn }, options));
103
103
  }
104
104
  /**
105
105
  * The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
@@ -126,7 +126,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
126
126
  if (this.isKey(key))
127
127
  return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
128
128
  }
129
- if (this._toEntryFn) {
129
+ if (this.isRaw(keyNodeEntryOrRaw)) {
130
130
  const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
131
131
  const finalValue = value !== null && value !== void 0 ? value : entryValue;
132
132
  if (this.isKey(key))
@@ -19,7 +19,7 @@ import { IterableElementBase } from '../base';
19
19
  * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
20
20
  * @example
21
21
  * // Use Heap to sort an array
22
- * function heapSort(arr: number[]): number[] {
22
+ * function heapSort(arr: number[]): number[] {
23
23
  * const heap = new Heap<number>(arr, { comparator: (a, b) => a - b });
24
24
  * const sorted: number[] = [];
25
25
  * while (!heap.isEmpty()) {
@@ -32,7 +32,7 @@ import { IterableElementBase } from '../base';
32
32
  * console.log(heapSort(array)); // [1, 2, 3, 4, 5, 8]
33
33
  * @example
34
34
  * // Use Heap to solve top k problems
35
- * function topKElements(arr: number[], k: number): number[] {
35
+ * function topKElements(arr: number[], k: number): number[] {
36
36
  * const heap = new Heap<number>([], { comparator: (a, b) => b - a }); // Max heap
37
37
  * arr.forEach(num => {
38
38
  * heap.add(num);
@@ -45,7 +45,7 @@ import { IterableElementBase } from '../base';
45
45
  * console.log(topKElements(numbers, 3)); // [15, 10, 5]
46
46
  * @example
47
47
  * // Use Heap to merge sorted sequences
48
- * function mergeSortedSequences(sequences: number[][]): number[] {
48
+ * function mergeSortedSequences(sequences: number[][]): number[] {
49
49
  * const heap = new Heap<{ value: number; seqIndex: number; itemIndex: number }>([], {
50
50
  * comparator: (a, b) => a.value - b.value // Min heap
51
51
  * });
@@ -82,7 +82,7 @@ import { IterableElementBase } from '../base';
82
82
  * console.log(mergeSortedSequences(sequences)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
83
83
  * @example
84
84
  * // Use Heap to dynamically maintain the median
85
- * class MedianFinder {
85
+ * class MedianFinder {
86
86
  * private low: MaxHeap<number>; // Max heap, stores the smaller half
87
87
  * private high: MinHeap<number>; // Min heap, stores the larger half
88
88
  *
@@ -119,7 +119,7 @@ import { IterableElementBase } from '../base';
119
119
  * console.log(medianFinder.findMedian()); // 30
120
120
  * @example
121
121
  * // Use Heap for load balancing
122
- * function loadBalance(requests: number[], servers: number): number[] {
122
+ * function loadBalance(requests: number[], servers: number): number[] {
123
123
  * const serverHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // min heap
124
124
  * const serverLoads = new Array(servers).fill(0);
125
125
  *
@@ -141,7 +141,7 @@ import { IterableElementBase } from '../base';
141
141
  * console.log(loadBalance(requests, 3)); // [12, 8, 5]
142
142
  * @example
143
143
  * // Use Heap to schedule tasks
144
- * type Task = [string, number];
144
+ * type Task = [string, number];
145
145
  *
146
146
  * function scheduleTasks(tasks: Task[], machines: number): Map<number, Task[]> {
147
147
  * const machineHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // Min heap
@@ -21,7 +21,7 @@ const base_1 = require("../base");
21
21
  * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
22
22
  * @example
23
23
  * // Use Heap to sort an array
24
- * function heapSort(arr: number[]): number[] {
24
+ * function heapSort(arr: number[]): number[] {
25
25
  * const heap = new Heap<number>(arr, { comparator: (a, b) => a - b });
26
26
  * const sorted: number[] = [];
27
27
  * while (!heap.isEmpty()) {
@@ -34,7 +34,7 @@ const base_1 = require("../base");
34
34
  * console.log(heapSort(array)); // [1, 2, 3, 4, 5, 8]
35
35
  * @example
36
36
  * // Use Heap to solve top k problems
37
- * function topKElements(arr: number[], k: number): number[] {
37
+ * function topKElements(arr: number[], k: number): number[] {
38
38
  * const heap = new Heap<number>([], { comparator: (a, b) => b - a }); // Max heap
39
39
  * arr.forEach(num => {
40
40
  * heap.add(num);
@@ -47,7 +47,7 @@ const base_1 = require("../base");
47
47
  * console.log(topKElements(numbers, 3)); // [15, 10, 5]
48
48
  * @example
49
49
  * // Use Heap to merge sorted sequences
50
- * function mergeSortedSequences(sequences: number[][]): number[] {
50
+ * function mergeSortedSequences(sequences: number[][]): number[] {
51
51
  * const heap = new Heap<{ value: number; seqIndex: number; itemIndex: number }>([], {
52
52
  * comparator: (a, b) => a.value - b.value // Min heap
53
53
  * });
@@ -84,7 +84,7 @@ const base_1 = require("../base");
84
84
  * console.log(mergeSortedSequences(sequences)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
85
85
  * @example
86
86
  * // Use Heap to dynamically maintain the median
87
- * class MedianFinder {
87
+ * class MedianFinder {
88
88
  * private low: MaxHeap<number>; // Max heap, stores the smaller half
89
89
  * private high: MinHeap<number>; // Min heap, stores the larger half
90
90
  *
@@ -121,7 +121,7 @@ const base_1 = require("../base");
121
121
  * console.log(medianFinder.findMedian()); // 30
122
122
  * @example
123
123
  * // Use Heap for load balancing
124
- * function loadBalance(requests: number[], servers: number): number[] {
124
+ * function loadBalance(requests: number[], servers: number): number[] {
125
125
  * const serverHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // min heap
126
126
  * const serverLoads = new Array(servers).fill(0);
127
127
  *
@@ -143,7 +143,7 @@ const base_1 = require("../base");
143
143
  * console.log(loadBalance(requests, 3)); // [12, 8, 5]
144
144
  * @example
145
145
  * // Use Heap to schedule tasks
146
- * type Task = [string, number];
146
+ * type Task = [string, number];
147
147
  *
148
148
  * function scheduleTasks(tasks: Task[], machines: number): Map<number, Task[]> {
149
149
  * const machineHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // Min heap
@@ -55,13 +55,13 @@ export declare class DoublyLinkedListNode<E = any> {
55
55
  set prev(value: DoublyLinkedListNode<E> | undefined);
56
56
  }
57
57
  /**
58
- * 1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
58
+ *1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
59
59
  * 2. Bidirectional Traversal: Unlike singly linked lists, doubly linked lists can be easily traversed forwards or backwards. This makes insertions and deletions in the list more flexible and efficient.
60
60
  * 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
61
61
  * 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
62
62
  * @example
63
63
  * // text editor operation history
64
- * const actions = [
64
+ * const actions = [
65
65
  * { type: 'insert', content: 'first line of text' },
66
66
  * { type: 'insert', content: 'second line of text' },
67
67
  * { type: 'delete', content: 'delete the first line' }
@@ -73,7 +73,7 @@ export declare class DoublyLinkedListNode<E = any> {
73
73
  * console.log(editorHistory.last?.type); // 'insert'
74
74
  * @example
75
75
  * // Browser history
76
- * const browserHistory = new DoublyLinkedList<string>();
76
+ * const browserHistory = new DoublyLinkedList<string>();
77
77
  *
78
78
  * browserHistory.push('home page');
79
79
  * browserHistory.push('search page');
@@ -84,7 +84,7 @@ export declare class DoublyLinkedListNode<E = any> {
84
84
  * console.log(browserHistory.last); // 'search page'
85
85
  * @example
86
86
  * // Use DoublyLinkedList to implement music player
87
- * // Define the Song interface
87
+ * // Define the Song interface
88
88
  * interface Song {
89
89
  * title: string;
90
90
  * artist: string;
@@ -209,7 +209,7 @@ export declare class DoublyLinkedListNode<E = any> {
209
209
  * // ]
210
210
  * @example
211
211
  * // Use DoublyLinkedList to implement LRU cache
212
- * interface CacheEntry<K, V> {
212
+ * interface CacheEntry<K, V> {
213
213
  * key: K;
214
214
  * value: V;
215
215
  * }
@@ -371,7 +371,7 @@ export declare class DoublyLinkedListNode<E = any> {
371
371
  * console.log(cache.isEmpty); // true
372
372
  * @example
373
373
  * // finding lyrics by timestamp in Coldplay's "Fix You"
374
- * // Create a DoublyLinkedList to store song lyrics with timestamps
374
+ * // Create a DoublyLinkedList to store song lyrics with timestamps
375
375
  * const lyricsList = new DoublyLinkedList<{ time: number; text: string }>();
376
376
  *
377
377
  * // Detailed lyrics with precise timestamps (in milliseconds)
@@ -411,7 +411,7 @@ export declare class DoublyLinkedListNode<E = any> {
411
411
  * console.log(lateTimeLyric?.text); // 'And I will try to fix you'
412
412
  * @example
413
413
  * // cpu process schedules
414
- * class Process {
414
+ * class Process {
415
415
  * constructor(
416
416
  * public id: number,
417
417
  * public priority: number
@@ -487,6 +487,16 @@ export declare class DoublyLinkedListNode<E = any> {
487
487
  * console.log(scheduler.listProcesses()); // []
488
488
  */
489
489
  export declare class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R, DoublyLinkedList<E, R>> {
490
+ /**
491
+ * This TypeScript constructor initializes a DoublyLinkedList with optional elements and options.
492
+ * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the constructor is an
493
+ * iterable collection of elements of type `E` or `R`. It is used to initialize the DoublyLinkedList
494
+ * with the elements provided in the iterable. If no elements are provided, the default value is an
495
+ * empty iterable.
496
+ * @param [options] - The `options` parameter in the constructor is of type
497
+ * `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
498
+ * configuration options to customize the behavior of the DoublyLinkedList.
499
+ */
490
500
  constructor(elements?: Iterable<E> | Iterable<R>, options?: DoublyLinkedListOptions<E, R>);
491
501
  protected _head: DoublyLinkedListNode<E> | undefined;
492
502
  /**
@@ -729,7 +739,7 @@ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementB
729
739
  * @returns The `get` method returns the value of the first node in the doubly linked list that
730
740
  * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
731
741
  */
732
- get(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): E | undefined;
742
+ search(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): E | undefined;
733
743
  /**
734
744
  * Time Complexity: O(n)
735
745
  * Space Complexity: O(1)
@@ -64,13 +64,13 @@ class DoublyLinkedListNode {
64
64
  }
65
65
  exports.DoublyLinkedListNode = DoublyLinkedListNode;
66
66
  /**
67
- * 1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
67
+ *1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
68
68
  * 2. Bidirectional Traversal: Unlike singly linked lists, doubly linked lists can be easily traversed forwards or backwards. This makes insertions and deletions in the list more flexible and efficient.
69
69
  * 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
70
70
  * 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
71
71
  * @example
72
72
  * // text editor operation history
73
- * const actions = [
73
+ * const actions = [
74
74
  * { type: 'insert', content: 'first line of text' },
75
75
  * { type: 'insert', content: 'second line of text' },
76
76
  * { type: 'delete', content: 'delete the first line' }
@@ -82,7 +82,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
82
82
  * console.log(editorHistory.last?.type); // 'insert'
83
83
  * @example
84
84
  * // Browser history
85
- * const browserHistory = new DoublyLinkedList<string>();
85
+ * const browserHistory = new DoublyLinkedList<string>();
86
86
  *
87
87
  * browserHistory.push('home page');
88
88
  * browserHistory.push('search page');
@@ -93,7 +93,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
93
93
  * console.log(browserHistory.last); // 'search page'
94
94
  * @example
95
95
  * // Use DoublyLinkedList to implement music player
96
- * // Define the Song interface
96
+ * // Define the Song interface
97
97
  * interface Song {
98
98
  * title: string;
99
99
  * artist: string;
@@ -218,7 +218,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
218
218
  * // ]
219
219
  * @example
220
220
  * // Use DoublyLinkedList to implement LRU cache
221
- * interface CacheEntry<K, V> {
221
+ * interface CacheEntry<K, V> {
222
222
  * key: K;
223
223
  * value: V;
224
224
  * }
@@ -380,7 +380,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
380
380
  * console.log(cache.isEmpty); // true
381
381
  * @example
382
382
  * // finding lyrics by timestamp in Coldplay's "Fix You"
383
- * // Create a DoublyLinkedList to store song lyrics with timestamps
383
+ * // Create a DoublyLinkedList to store song lyrics with timestamps
384
384
  * const lyricsList = new DoublyLinkedList<{ time: number; text: string }>();
385
385
  *
386
386
  * // Detailed lyrics with precise timestamps (in milliseconds)
@@ -420,7 +420,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
420
420
  * console.log(lateTimeLyric?.text); // 'And I will try to fix you'
421
421
  * @example
422
422
  * // cpu process schedules
423
- * class Process {
423
+ * class Process {
424
424
  * constructor(
425
425
  * public id: number,
426
426
  * public priority: number
@@ -496,6 +496,16 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
496
496
  * console.log(scheduler.listProcesses()); // []
497
497
  */
498
498
  class DoublyLinkedList extends base_1.IterableElementBase {
499
+ /**
500
+ * This TypeScript constructor initializes a DoublyLinkedList with optional elements and options.
501
+ * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the constructor is an
502
+ * iterable collection of elements of type `E` or `R`. It is used to initialize the DoublyLinkedList
503
+ * with the elements provided in the iterable. If no elements are provided, the default value is an
504
+ * empty iterable.
505
+ * @param [options] - The `options` parameter in the constructor is of type
506
+ * `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
507
+ * configuration options to customize the behavior of the DoublyLinkedList.
508
+ */
499
509
  constructor(elements = [], options) {
500
510
  super(options);
501
511
  this._head = undefined;
@@ -784,7 +794,9 @@ class DoublyLinkedList extends base_1.IterableElementBase {
784
794
  * node was not found.
785
795
  */
786
796
  addBefore(existingElementOrNode, newElementOrNode) {
787
- const existingNode = this.getNode(existingElementOrNode);
797
+ const existingNode = this.isNode(existingElementOrNode)
798
+ ? existingElementOrNode
799
+ : this.getNode(existingElementOrNode);
788
800
  if (existingNode) {
789
801
  const newNode = this._ensureNode(newElementOrNode);
790
802
  newNode.prev = existingNode.prev;
@@ -818,7 +830,9 @@ class DoublyLinkedList extends base_1.IterableElementBase {
818
830
  * was not found in the linked list.
819
831
  */
820
832
  addAfter(existingElementOrNode, newElementOrNode) {
821
- const existingNode = this.getNode(existingElementOrNode);
833
+ const existingNode = this.isNode(existingElementOrNode)
834
+ ? existingElementOrNode
835
+ : this.getNode(existingElementOrNode);
822
836
  if (existingNode) {
823
837
  const newNode = this._ensureNode(newElementOrNode);
824
838
  newNode.next = existingNode.next;
@@ -956,7 +970,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
956
970
  * @returns The `get` method returns the value of the first node in the doubly linked list that
957
971
  * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
958
972
  */
959
- get(elementNodeOrPredicate) {
973
+ search(elementNodeOrPredicate) {
960
974
  const predicate = this._ensurePredicate(elementNodeOrPredicate);
961
975
  let current = this.head;
962
976
  while (current) {
@@ -123,7 +123,7 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
123
123
  * @returns The `get` method returns the value of the first node in the singly linked list that
124
124
  * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
125
125
  */
126
- get(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): E | undefined;
126
+ search(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): E | undefined;
127
127
  /**
128
128
  * Time Complexity: O(n)
129
129
  * Space Complexity: O(1)
@@ -200,7 +200,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
200
200
  * @returns The `get` method returns the value of the first node in the singly linked list that
201
201
  * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
202
202
  */
203
- get(elementNodeOrPredicate) {
203
+ search(elementNodeOrPredicate) {
204
204
  const predicate = this._ensurePredicate(elementNodeOrPredicate);
205
205
  let current = this.head;
206
206
  while (current) {
@@ -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.
@@ -74,13 +74,100 @@ exports.TrieNode = TrieNode;
74
74
  * 9. Spell Check: Checking the spelling of words.
75
75
  * 10. IP Routing: Used in certain types of IP routing algorithms.
76
76
  * 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
77
+ * @example
78
+ * // Autocomplete: Prefix validation and checking
79
+ * const autocomplete = new Trie<string>(['gmail.com', 'gmail.co.nz', 'gmail.co.jp', 'yahoo.com', 'outlook.com']);
80
+ *
81
+ * // Get all completions for a prefix
82
+ * const gmailCompletions = autocomplete.getWords('gmail');
83
+ * console.log(gmailCompletions); // ['gmail.com', 'gmail.co.nz', 'gmail.co.jp']
84
+ * @example
85
+ * // File System Path Operations
86
+ * const fileSystem = new Trie<string>([
87
+ * '/home/user/documents/file1.txt',
88
+ * '/home/user/documents/file2.txt',
89
+ * '/home/user/pictures/photo.jpg',
90
+ * '/home/user/pictures/vacation/',
91
+ * '/home/user/downloads'
92
+ * ]);
93
+ *
94
+ * // Find common directory prefix
95
+ * console.log(fileSystem.getLongestCommonPrefix()); // '/home/user/'
96
+ *
97
+ * // List all files in a directory
98
+ * const documentsFiles = fileSystem.getWords('/home/user/documents/');
99
+ * console.log(documentsFiles); // ['/home/user/documents/file1.txt', '/home/user/documents/file2.txt']
100
+ * @example
101
+ * // Autocomplete: Basic word suggestions
102
+ * // Create a trie for autocomplete
103
+ * const autocomplete = new Trie<string>([
104
+ * 'function',
105
+ * 'functional',
106
+ * 'functions',
107
+ * 'class',
108
+ * 'classes',
109
+ * 'classical',
110
+ * 'closure',
111
+ * 'const',
112
+ * 'constructor'
113
+ * ]);
114
+ *
115
+ * // Test autocomplete with different prefixes
116
+ * console.log(autocomplete.getWords('fun')); // ['functional', 'functions', 'function']
117
+ * console.log(autocomplete.getWords('cla')); // ['classes', 'classical', 'class']
118
+ * console.log(autocomplete.getWords('con')); // ['constructor', 'const']
119
+ *
120
+ * // Test with non-matching prefix
121
+ * console.log(autocomplete.getWords('xyz')); // []
122
+ * @example
123
+ * // Dictionary: Case-insensitive word lookup
124
+ * // Create a case-insensitive dictionary
125
+ * const dictionary = new Trie<string>([], { caseSensitive: false });
126
+ *
127
+ * // Add words with mixed casing
128
+ * dictionary.add('Hello');
129
+ * dictionary.add('WORLD');
130
+ * dictionary.add('JavaScript');
131
+ *
132
+ * // Test lookups with different casings
133
+ * console.log(dictionary.has('hello')); // true
134
+ * console.log(dictionary.has('HELLO')); // true
135
+ * console.log(dictionary.has('Hello')); // true
136
+ * console.log(dictionary.has('javascript')); // true
137
+ * console.log(dictionary.has('JAVASCRIPT')); // true
138
+ * @example
139
+ * // IP Address Routing Table
140
+ * // Add IP address prefixes and their corresponding routes
141
+ * const routes = {
142
+ * '192.168.1': 'LAN_SUBNET_1',
143
+ * '192.168.2': 'LAN_SUBNET_2',
144
+ * '10.0.0': 'PRIVATE_NETWORK_1',
145
+ * '10.0.1': 'PRIVATE_NETWORK_2'
146
+ * };
147
+ *
148
+ * const ipRoutingTable = new Trie<string>(Object.keys(routes));
149
+ *
150
+ * // Check IP address prefix matching
151
+ * console.log(ipRoutingTable.hasPrefix('192.168.1')); // true
152
+ * console.log(ipRoutingTable.hasPrefix('192.168.2')); // true
153
+ *
154
+ * // Validate IP address belongs to subnet
155
+ * const ip = '192.168.1.100';
156
+ * const subnet = ip.split('.').slice(0, 3).join('.');
157
+ * console.log(ipRoutingTable.hasPrefix(subnet)); // true
77
158
  */
78
159
  class Trie extends base_1.IterableElementBase {
79
160
  /**
80
- * The constructor function for the Trie class.
81
- * @param words: Iterable string Initialize the trie with a set of words
82
- * @param options?: TrieOptions Allow the user to pass in options for the trie
83
- * @return This
161
+ * The constructor initializes a Trie data structure with optional options and words provided as
162
+ * input.
163
+ * @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the constructor is an
164
+ * iterable containing either strings or elements of type `R`. It is used to initialize the Trie with
165
+ * a list of words or elements. If no `words` are provided, an empty iterable is used as the default
166
+ * value.
167
+ * @param [options] - The `options` parameter in the constructor is an optional object that can
168
+ * contain configuration options for the Trie data structure. One of the options it can have is
169
+ * `caseSensitive`, which is a boolean value indicating whether the Trie should be case-sensitive or
170
+ * not. If `caseSensitive` is set to `
84
171
  */
85
172
  constructor(words = [], options) {
86
173
  super(options);
@@ -93,14 +180,7 @@ class Trie extends base_1.IterableElementBase {
93
180
  this._caseSensitive = caseSensitive;
94
181
  }
95
182
  if (words) {
96
- for (const word of words) {
97
- if (this.toElementFn) {
98
- this.add(this.toElementFn(word));
99
- }
100
- else {
101
- this.add(word);
102
- }
103
- }
183
+ this.addMany(words);
104
184
  }
105
185
  }
106
186
  /**
@@ -151,6 +231,30 @@ class Trie extends base_1.IterableElementBase {
151
231
  }
152
232
  return isNewWord;
153
233
  }
234
+ /**
235
+ * Time Complexity: O(n * l)
236
+ * Space Complexity: O(1)
237
+ *
238
+ * The `addMany` function in TypeScript takes an iterable of strings or elements of type R, converts
239
+ * them using a provided function if available, and adds them to a data structure while returning an
240
+ * array of boolean values indicating success.
241
+ * @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the `addMany` function is
242
+ * an iterable that contains either strings or elements of type `R`.
243
+ * @returns The `addMany` method returns an array of boolean values indicating whether each word in
244
+ * the input iterable was successfully added to the data structure.
245
+ */
246
+ addMany(words = []) {
247
+ const ans = [];
248
+ for (const word of words) {
249
+ if (this.toElementFn) {
250
+ ans.push(this.add(this.toElementFn(word)));
251
+ }
252
+ else {
253
+ ans.push(this.add(word));
254
+ }
255
+ }
256
+ return ans;
257
+ }
154
258
  /**
155
259
  * Time Complexity: O(l), where l is the length of the input word.
156
260
  * Space Complexity: O(1) - Constant space.
package/dist/index.d.ts CHANGED
@@ -8,4 +8,5 @@
8
8
  export * from './data-structures/graph';
9
9
  export * from './types/data-structures/graph';
10
10
  export * from './types/common';
11
- export * from './constants';
11
+ export * from './types/utils';
12
+ export * from './common';
package/dist/index.js CHANGED
@@ -38,4 +38,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
38
38
  __exportStar(require("./data-structures/graph"), exports);
39
39
  __exportStar(require("./types/data-structures/graph"), exports);
40
40
  __exportStar(require("./types/common"), exports);
41
- __exportStar(require("./constants"), exports);
41
+ __exportStar(require("./types/utils"), exports);
42
+ __exportStar(require("./common"), exports);
@@ -1,6 +1,6 @@
1
1
  import { BinaryTree, BinaryTreeNode } from '../../../data-structures';
2
2
  import { IterationType, OptValue } from '../../common';
3
- import { DFSOperation } from '../../../constants';
3
+ import { DFSOperation } from '../../../common';
4
4
  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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
5
5
  export type BinaryTreeNested<K, V, R, NODE extends BinaryTreeNode<K, V, NODE>> = BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
6
6
  export type ToEntryFn<K, V, R> = (rawElement: R) => BTNEntry<K, V>;