undirected-graph-typed 1.53.6 → 1.53.8

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 (61) hide show
  1. package/dist/common/index.d.ts +12 -0
  2. package/dist/common/index.js +28 -0
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  4. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +9 -12
  5. package/dist/data-structures/binary-tree/avl-tree.js +2 -2
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +55 -20
  7. package/dist/data-structures/binary-tree/binary-tree.js +102 -68
  8. package/dist/data-structures/binary-tree/bst.d.ts +131 -37
  9. package/dist/data-structures/binary-tree/bst.js +222 -69
  10. package/dist/data-structures/binary-tree/index.d.ts +1 -1
  11. package/dist/data-structures/binary-tree/index.js +1 -1
  12. package/dist/data-structures/binary-tree/{rb-tree.d.ts → red-black-tree.d.ts} +53 -0
  13. package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +56 -3
  14. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  15. package/dist/data-structures/binary-tree/tree-multi-map.js +7 -7
  16. package/dist/data-structures/hash/hash-map.d.ts +30 -0
  17. package/dist/data-structures/hash/hash-map.js +30 -0
  18. package/dist/data-structures/heap/heap.d.ts +26 -9
  19. package/dist/data-structures/heap/heap.js +37 -17
  20. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +54 -9
  21. package/dist/data-structures/linked-list/doubly-linked-list.js +80 -19
  22. package/dist/data-structures/linked-list/singly-linked-list.d.ts +35 -2
  23. package/dist/data-structures/linked-list/singly-linked-list.js +55 -11
  24. package/dist/data-structures/queue/deque.d.ts +37 -8
  25. package/dist/data-structures/queue/deque.js +73 -29
  26. package/dist/data-structures/queue/queue.d.ts +41 -1
  27. package/dist/data-structures/queue/queue.js +51 -9
  28. package/dist/data-structures/stack/stack.d.ts +27 -10
  29. package/dist/data-structures/stack/stack.js +39 -20
  30. package/dist/data-structures/trie/trie.d.ts +111 -6
  31. package/dist/data-structures/trie/trie.js +123 -14
  32. package/dist/index.d.ts +2 -1
  33. package/dist/index.js +2 -1
  34. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  35. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
  36. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
  37. package/dist/types/utils/utils.d.ts +10 -6
  38. package/dist/utils/utils.js +4 -2
  39. package/package.json +2 -2
  40. package/src/common/index.ts +25 -0
  41. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +9 -11
  42. package/src/data-structures/binary-tree/avl-tree.ts +3 -2
  43. package/src/data-structures/binary-tree/binary-tree.ts +110 -66
  44. package/src/data-structures/binary-tree/bst.ts +232 -72
  45. package/src/data-structures/binary-tree/index.ts +1 -1
  46. package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +56 -3
  47. package/src/data-structures/binary-tree/tree-multi-map.ts +6 -6
  48. package/src/data-structures/hash/hash-map.ts +30 -0
  49. package/src/data-structures/heap/heap.ts +72 -49
  50. package/src/data-structures/linked-list/doubly-linked-list.ts +173 -105
  51. package/src/data-structures/linked-list/singly-linked-list.ts +61 -11
  52. package/src/data-structures/queue/deque.ts +72 -28
  53. package/src/data-structures/queue/queue.ts +50 -7
  54. package/src/data-structures/stack/stack.ts +39 -20
  55. package/src/data-structures/trie/trie.ts +123 -13
  56. package/src/index.ts +2 -1
  57. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  58. package/src/types/data-structures/binary-tree/bst.ts +3 -2
  59. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
  60. package/src/types/utils/utils.ts +16 -10
  61. package/src/utils/utils.ts +4 -2
@@ -22,14 +22,7 @@ class Queue extends base_1.IterableElementBase {
22
22
  const { autoCompactRatio = 0.5 } = options;
23
23
  this._autoCompactRatio = autoCompactRatio;
24
24
  }
25
- if (elements) {
26
- for (const el of elements) {
27
- if (this.toElementFn)
28
- this.push(this.toElementFn(el));
29
- else
30
- this.push(el);
31
- }
32
- }
25
+ this.pushMany(elements);
33
26
  }
34
27
  /**
35
28
  * The elements function returns the elements of this set.
@@ -114,6 +107,27 @@ class Queue extends base_1.IterableElementBase {
114
107
  this.elements.push(element);
115
108
  return true;
116
109
  }
110
+ /**
111
+ * Time Complexity: O(k)
112
+ * Space Complexity: O(k)
113
+ *
114
+ * The `pushMany` function iterates over elements and pushes them into an array after applying a
115
+ * transformation function if provided.
116
+ * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
117
+ * is an iterable containing elements of type `E` or `R`.
118
+ * @returns The `pushMany` function is returning an array of boolean values indicating whether each
119
+ * element was successfully pushed into the data structure.
120
+ */
121
+ pushMany(elements) {
122
+ const ans = [];
123
+ for (const el of elements) {
124
+ if (this.toElementFn)
125
+ ans.push(this.push(this.toElementFn(el)));
126
+ else
127
+ ans.push(this.push(el));
128
+ }
129
+ return ans;
130
+ }
117
131
  /**
118
132
  * Time Complexity: O(1)
119
133
  * Space Complexity: O(1)
@@ -132,6 +146,9 @@ class Queue extends base_1.IterableElementBase {
132
146
  return first;
133
147
  }
134
148
  /**
149
+ * Time Complexity: O(n)
150
+ * Space Complexity: O(1)
151
+ *
135
152
  * The delete function removes an element from the list.
136
153
  * @param {E} element - Specify the element to be deleted
137
154
  * @return A boolean value indicating whether the element was successfully deleted or not
@@ -141,6 +158,9 @@ class Queue extends base_1.IterableElementBase {
141
158
  return this.deleteAt(index);
142
159
  }
143
160
  /**
161
+ * Time Complexity: O(n)
162
+ * Space Complexity: O(1)
163
+ *
144
164
  * The deleteAt function deletes the element at a given index.
145
165
  * @param {number} index - Determine the index of the element to be deleted
146
166
  * @return A boolean value
@@ -153,7 +173,12 @@ class Queue extends base_1.IterableElementBase {
153
173
  * Time Complexity: O(1)
154
174
  * Space Complexity: O(1)
155
175
  *
156
- * @param index
176
+ * The `at` function returns the element at a specified index adjusted by an offset, or `undefined`
177
+ * if the index is out of bounds.
178
+ * @param {number} index - The `index` parameter represents the position of the element you want to
179
+ * retrieve from the data structure.
180
+ * @returns The `at` method is returning the element at the specified index adjusted by the offset
181
+ * `_offset`.
157
182
  */
158
183
  at(index) {
159
184
  return this.elements[index + this._offset];
@@ -189,6 +214,9 @@ class Queue extends base_1.IterableElementBase {
189
214
  this._offset = 0;
190
215
  }
191
216
  /**
217
+ * Time Complexity: O(n)
218
+ * Space Complexity: O(1)
219
+ *
192
220
  * The `compact` function in TypeScript slices the elements array based on the offset and resets the
193
221
  * offset to zero.
194
222
  * @returns The `compact()` method is returning a boolean value of `true`.
@@ -238,6 +266,20 @@ class Queue extends base_1.IterableElementBase {
238
266
  /**
239
267
  * Time Complexity: O(n)
240
268
  * Space Complexity: O(n)
269
+ *
270
+ * The `map` function in TypeScript creates a new Queue by applying a callback function to each
271
+ * element in the original Queue.
272
+ * @param callback - The `callback` parameter is a function that will be applied to each element in
273
+ * the queue. It takes the current element, its index, and the queue itself as arguments, and returns
274
+ * a new element.
275
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be provided to
276
+ * convert a raw element of type `RM` to a new element of type `EM`. This function is used within the
277
+ * `map` method to transform each raw element before passing it to the `callback` function. If
278
+ * @param {any} [thisArg] - The `thisArg` parameter in the `map` function is used to specify the
279
+ * value of `this` when executing the `callback` function. It allows you to set the context (the
280
+ * value of `this`) within the callback function. If `thisArg` is provided, it will be
281
+ * @returns A new Queue object containing elements of type EM, which are the result of applying the
282
+ * callback function to each element in the original Queue object.
241
283
  */
242
284
  map(callback, toElementFn, thisArg) {
243
285
  const newDeque = new Queue([], { toElementFn });
@@ -28,10 +28,6 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R, S
28
28
  * @returns The size of the elements array.
29
29
  */
30
30
  get size(): number;
31
- /**
32
- * Time Complexity: O(n)
33
- * Space Complexity: O(n)
34
- */
35
31
  /**
36
32
  * Time Complexity: O(n)
37
33
  * Space Complexity: O(n)
@@ -43,6 +39,9 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R, S
43
39
  */
44
40
  static fromArray<E>(elements: E[]): Stack<E>;
45
41
  /**
42
+ * Time Complexity: O(1)
43
+ * Space Complexity: O(1)
44
+ *
46
45
  * The function checks if an array is empty and returns a boolean value.
47
46
  * @returns A boolean value indicating whether the `_elements` array is empty or not.
48
47
  */
@@ -74,15 +73,33 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R, S
74
73
  */
75
74
  pop(): E | undefined;
76
75
  /**
77
- * The delete function removes an element from the stack.
78
- * @param element: E Specify the element to be deleted
79
- * @return A boolean value indicating whether the element was successfully deleted or not
76
+ * Time Complexity: O(k)
77
+ * Space Complexity: O(1)
78
+ *
79
+ * The function `pushMany` iterates over elements and pushes them into an array after applying a
80
+ * transformation function if provided.
81
+ * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
82
+ * is an iterable containing elements of type `E` or `R`. The function iterates over each element in
83
+ * the iterable and pushes it into the data structure. If a transformation function `toElementFn` is
84
+ * provided, it is used to
85
+ * @returns The `pushMany` function is returning an array of boolean values indicating whether each
86
+ * element was successfully pushed into the data structure.
87
+ */
88
+ pushMany(elements: Iterable<E> | Iterable<R>): boolean[];
89
+ /**
90
+ * Time Complexity: O(n)
91
+ * Space Complexity: O(1)
92
+ *
93
+ * The toArray function returns a copy of the elements in an array.
94
+ * @returns An array of type E.
80
95
  */
81
96
  delete(element: E): boolean;
82
97
  /**
83
- * The deleteAt function deletes the element at a given index.
84
- * @param index: number Determine the index of the element to be deleted
85
- * @return A boolean value
98
+ * Time Complexity: O(n)
99
+ * Space Complexity: O(1)
100
+ *
101
+ * The toArray function returns a copy of the elements in an array.
102
+ * @returns An array of type E.
86
103
  */
87
104
  deleteAt(index: number): boolean;
88
105
  /**
@@ -14,16 +14,7 @@ class Stack extends base_1.IterableElementBase {
14
14
  constructor(elements = [], options) {
15
15
  super(options);
16
16
  this._elements = [];
17
- if (elements) {
18
- for (const el of elements) {
19
- if (this.toElementFn) {
20
- this.push(this.toElementFn(el));
21
- }
22
- else {
23
- this.push(el);
24
- }
25
- }
26
- }
17
+ this.pushMany(elements);
27
18
  }
28
19
  /**
29
20
  * The elements function returns the elements of this set.
@@ -39,10 +30,6 @@ class Stack extends base_1.IterableElementBase {
39
30
  get size() {
40
31
  return this.elements.length;
41
32
  }
42
- /**
43
- * Time Complexity: O(n)
44
- * Space Complexity: O(n)
45
- */
46
33
  /**
47
34
  * Time Complexity: O(n)
48
35
  * Space Complexity: O(n)
@@ -56,6 +43,9 @@ class Stack extends base_1.IterableElementBase {
56
43
  return new Stack(elements);
57
44
  }
58
45
  /**
46
+ * Time Complexity: O(1)
47
+ * Space Complexity: O(1)
48
+ *
59
49
  * The function checks if an array is empty and returns a boolean value.
60
50
  * @returns A boolean value indicating whether the `_elements` array is empty or not.
61
51
  */
@@ -100,18 +90,47 @@ class Stack extends base_1.IterableElementBase {
100
90
  return this.elements.pop();
101
91
  }
102
92
  /**
103
- * The delete function removes an element from the stack.
104
- * @param element: E Specify the element to be deleted
105
- * @return A boolean value indicating whether the element was successfully deleted or not
93
+ * Time Complexity: O(k)
94
+ * Space Complexity: O(1)
95
+ *
96
+ * The function `pushMany` iterates over elements and pushes them into an array after applying a
97
+ * transformation function if provided.
98
+ * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
99
+ * is an iterable containing elements of type `E` or `R`. The function iterates over each element in
100
+ * the iterable and pushes it into the data structure. If a transformation function `toElementFn` is
101
+ * provided, it is used to
102
+ * @returns The `pushMany` function is returning an array of boolean values indicating whether each
103
+ * element was successfully pushed into the data structure.
104
+ */
105
+ pushMany(elements) {
106
+ const ans = [];
107
+ for (const el of elements) {
108
+ if (this.toElementFn) {
109
+ ans.push(this.push(this.toElementFn(el)));
110
+ }
111
+ else {
112
+ ans.push(this.push(el));
113
+ }
114
+ }
115
+ return ans;
116
+ }
117
+ /**
118
+ * Time Complexity: O(n)
119
+ * Space Complexity: O(1)
120
+ *
121
+ * The toArray function returns a copy of the elements in an array.
122
+ * @returns An array of type E.
106
123
  */
107
124
  delete(element) {
108
125
  const index = this.elements.indexOf(element);
109
126
  return this.deleteAt(index);
110
127
  }
111
128
  /**
112
- * The deleteAt function deletes the element at a given index.
113
- * @param index: number Determine the index of the element to be deleted
114
- * @return A boolean value
129
+ * Time Complexity: O(n)
130
+ * Space Complexity: O(1)
131
+ *
132
+ * The toArray function returns a copy of the elements in an array.
133
+ * @returns An array of type E.
115
134
  */
116
135
  deleteAt(index) {
117
136
  const spliced = this.elements.splice(index, 1);
@@ -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.
@@ -135,9 +235,14 @@ export declare class Trie<R = any> extends IterableElementBase<string, R, Trie<R
135
235
  */
136
236
  delete(word: string): boolean;
137
237
  /**
138
- * Time Complexity: O(n), where n is the total number of nodes in the trie.
139
- * Space Complexity: O(1) - Constant space.
238
+ * Time Complexity: O(n)
239
+ * Space Complexity: O(1)
140
240
  *
241
+ * The function `getHeight` calculates the height of a trie data structure starting from the root
242
+ * node.
243
+ * @returns The `getHeight` method returns the maximum depth or height of the trie tree starting from
244
+ * the root node. It calculates the depth using a breadth-first search (BFS) traversal of the trie
245
+ * tree and returns the maximum depth found.
141
246
  */
142
247
  getHeight(): number;
143
248
  /**
@@ -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.
@@ -234,9 +338,14 @@ class Trie extends base_1.IterableElementBase {
234
338
  return isDeleted;
235
339
  }
236
340
  /**
237
- * Time Complexity: O(n), where n is the total number of nodes in the trie.
238
- * Space Complexity: O(1) - Constant space.
341
+ * Time Complexity: O(n)
342
+ * Space Complexity: O(1)
239
343
  *
344
+ * The function `getHeight` calculates the height of a trie data structure starting from the root
345
+ * node.
346
+ * @returns The `getHeight` method returns the maximum depth or height of the trie tree starting from
347
+ * the root node. It calculates the depth using a breadth-first search (BFS) traversal of the trie
348
+ * tree and returns the maximum depth found.
240
349
  */
241
350
  getHeight() {
242
351
  const startNode = this.root;
package/dist/index.d.ts CHANGED
@@ -8,4 +8,5 @@
8
8
  export * from './data-structures/graph/undirected-graph';
9
9
  export * from './types/data-structures/graph/undirected-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
@@ -25,4 +25,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
25
25
  __exportStar(require("./data-structures/graph/undirected-graph"), exports);
26
26
  __exportStar(require("./types/data-structures/graph/undirected-graph"), exports);
27
27
  __exportStar(require("./types/common"), exports);
28
- __exportStar(require("./constants"), exports);
28
+ __exportStar(require("./types/utils"), exports);
29
+ __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>;
@@ -1,10 +1,11 @@
1
1
  import { BST, BSTNode } from '../../../data-structures';
2
2
  import type { BinaryTreeOptions } from './binary-tree';
3
- import { Comparator } from '../../common';
3
+ import { Comparable } from '../../utils';
4
4
  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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
5
5
  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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
6
6
  export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & {
7
- comparator?: Comparator<K>;
7
+ extractComparable?: (key: K) => Comparable;
8
+ isReverse?: boolean;
8
9
  };
9
10
  export type BSTNOptKey<K> = K | undefined;
10
11
  export type OptNode<NODE> = NODE | undefined;
@@ -3,4 +3,4 @@ import type { BSTOptions } from "./bst";
3
3
  export type RBTNColor = 'RED' | 'BLACK';
4
4
  export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
5
5
  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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
6
- export type RBTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};
6
+ export type RBTreeOptions<K, V, R> = Omit<BSTOptions<K, V, R>, 'isReverse'> & {};
@@ -6,13 +6,17 @@ export type TrlFn<A extends any[] = any[], R = any> = (...args: A) => R;
6
6
  export type TrlAsyncFn = (...args: any[]) => any;
7
7
  export type SpecifyOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
8
8
  export type Any = string | number | bigint | boolean | symbol | undefined | object;
9
+ export type Arithmetic = number | bigint;
9
10
  export type ComparablePrimitive = number | bigint | string | boolean;
10
- export type ComparableObject = {
11
- [key in string]: any;
12
- } & ({
13
- valueOf: () => ComparablePrimitive | ComparableObject;
11
+ export interface BaseComparableObject {
12
+ [key: string]: unknown;
13
+ }
14
+ export interface ValueComparableObject extends BaseComparableObject {
15
+ valueOf: () => ComparablePrimitive | ValueComparableObject;
14
16
  toString?: () => string;
15
- } | {
17
+ }
18
+ export interface StringComparableObject extends BaseComparableObject {
16
19
  toString: () => string;
17
- });
20
+ }
21
+ export type ComparableObject = ValueComparableObject | StringComparableObject;
18
22
  export type Comparable = ComparablePrimitive | Date | ComparableObject;