queue-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
@@ -60,17 +60,12 @@ export class SinglyLinkedListNode<E = any> {
60
60
  }
61
61
 
62
62
  export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R, SinglyLinkedList<E, R>> {
63
- constructor(elements: Iterable<E> | Iterable<R> = [], options?: SinglyLinkedListOptions<E, R>) {
63
+ constructor(
64
+ elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>> = [],
65
+ options?: SinglyLinkedListOptions<E, R>
66
+ ) {
64
67
  super(options);
65
- if (elements) {
66
- for (const el of elements) {
67
- if (this.toElementFn) {
68
- this.push(this.toElementFn(el as R));
69
- } else {
70
- this.push(el as E);
71
- }
72
- }
73
- }
68
+ this.pushMany(elements);
74
69
  }
75
70
 
76
71
  protected _head: SinglyLinkedListNode<E> | undefined;
@@ -211,6 +206,55 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
211
206
  return true;
212
207
  }
213
208
 
209
+ /**
210
+ * Time Complexity: O(k)
211
+ * Space Complexity: O(k)
212
+ *
213
+ * The function `pushMany` iterates over elements and pushes them into a data structure, applying a
214
+ * transformation function if provided.
215
+ * @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
216
+ * parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`,
217
+ * or `SinglyLinkedListNode<E>`.
218
+ * @returns The `pushMany` function returns an array of boolean values indicating whether each
219
+ * element was successfully pushed into the data structure.
220
+ */
221
+ pushMany(elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>) {
222
+ const ans: boolean[] = [];
223
+ for (const el of elements) {
224
+ if (this.toElementFn) {
225
+ ans.push(this.push(this.toElementFn(el as R)));
226
+ continue;
227
+ }
228
+ ans.push(this.push(el as E | SinglyLinkedListNode<E>));
229
+ }
230
+ return ans;
231
+ }
232
+
233
+ /**
234
+ * Time Complexity: O(k)
235
+ * Space Complexity: O(k)
236
+ *
237
+ * The function `unshiftMany` iterates over elements and adds them to a data structure, optionally
238
+ * converting them using a provided function.
239
+ * @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
240
+ * parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`,
241
+ * `R`, or `SinglyLinkedListNode<E>`. The function iterates over each element in the iterable and
242
+ * performs an `unshift` operation on the linked list for each
243
+ * @returns The `unshiftMany` function is returning an array of boolean values, where each value
244
+ * represents the result of calling the `unshift` method on the current instance of the class.
245
+ */
246
+ unshiftMany(elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>) {
247
+ const ans: boolean[] = [];
248
+ for (const el of elements) {
249
+ if (this.toElementFn) {
250
+ ans.push(this.unshift(this.toElementFn(el as R)));
251
+ continue;
252
+ }
253
+ ans.push(this.unshift(el as E | SinglyLinkedListNode<E>));
254
+ }
255
+ return ans;
256
+ }
257
+
214
258
  /**
215
259
  * Time Complexity: O(n)
216
260
  * Space Complexity: O(1)
@@ -223,7 +267,7 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
223
267
  * @returns The `get` method returns the value of the first node in the singly linked list that
224
268
  * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
225
269
  */
226
- get(
270
+ search(
227
271
  elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)
228
272
  ): E | undefined {
229
273
  const predicate = this._ensurePredicate(elementNodeOrPredicate);
@@ -399,6 +443,9 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
399
443
  }
400
444
 
401
445
  /**
446
+ * Time Complexity: O(1)
447
+ * Space Complexity: O(1)
448
+ *
402
449
  * The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
403
450
  * whether it is empty or not.
404
451
  * @returns A boolean value indicating whether the length of the object is equal to 0.
@@ -408,6 +455,9 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
408
455
  }
409
456
 
410
457
  /**
458
+ * Time Complexity: O(1)
459
+ * Space Complexity: O(1)
460
+ *
411
461
  * The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
412
462
  */
413
463
  clear(): void {
@@ -53,14 +53,7 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
53
53
  const needBucketNum = calcMinUnitsRequired(_size, this._bucketSize);
54
54
  this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
55
55
  this._firstInBucket = this._lastInBucket = (this._bucketSize - (_size % this._bucketSize)) >> 1;
56
-
57
- for (const el of elements) {
58
- if (this.toElementFn) {
59
- this.push(this.toElementFn(el as R));
60
- } else {
61
- this.push(el as E);
62
- }
63
- }
56
+ this.pushMany(elements);
64
57
  }
65
58
 
66
59
  protected _bucketSize: number = 1 << 12;
@@ -230,6 +223,33 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
230
223
  return element;
231
224
  }
232
225
 
226
+ /**
227
+ * Time Complexity: O(1)
228
+ * Space Complexity: O(1)
229
+ *
230
+ * The `shift()` function removes and returns the first element from a data structure, updating the
231
+ * internal state variables accordingly.
232
+ * @returns The element that is being removed from the beginning of the data structure is being
233
+ * returned.
234
+ */
235
+ shift(): E | undefined {
236
+ if (this._size === 0) return;
237
+ const element = this._buckets[this._bucketFirst][this._firstInBucket];
238
+ if (this._size !== 1) {
239
+ if (this._firstInBucket < this._bucketSize - 1) {
240
+ this._firstInBucket += 1;
241
+ } else if (this._bucketFirst < this._bucketCount - 1) {
242
+ this._bucketFirst += 1;
243
+ this._firstInBucket = 0;
244
+ } else {
245
+ this._bucketFirst = 0;
246
+ this._firstInBucket = 0;
247
+ }
248
+ }
249
+ this._size -= 1;
250
+ return element;
251
+ }
252
+
233
253
  /**
234
254
  * Time Complexity: Amortized O(1)
235
255
  * Space Complexity: O(n)
@@ -260,30 +280,54 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
260
280
  }
261
281
 
262
282
  /**
263
- * Time Complexity: O(1)
264
- * Space Complexity: O(1)
283
+ * Time Complexity: O(k)
284
+ * Space Complexity: O(k)
265
285
  *
266
- * The `shift()` function removes and returns the first element from a data structure, updating the
267
- * internal state variables accordingly.
268
- * @returns The element that is being removed from the beginning of the data structure is being
269
- * returned.
270
- */
271
- shift(): E | undefined {
272
- if (this._size === 0) return;
273
- const element = this._buckets[this._bucketFirst][this._firstInBucket];
274
- if (this._size !== 1) {
275
- if (this._firstInBucket < this._bucketSize - 1) {
276
- this._firstInBucket += 1;
277
- } else if (this._bucketFirst < this._bucketCount - 1) {
278
- this._bucketFirst += 1;
279
- this._firstInBucket = 0;
286
+ * The function `pushMany` iterates over elements and pushes them into an array after applying a
287
+ * transformation function if provided.
288
+ * @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
289
+ * parameter in the `pushMany` function is expected to be an iterable containing elements of type `E`
290
+ * or `R`. It can be either an `IterableWithSizeOrLength<E>` or an `IterableWithSizeOrLength<R>`. The
291
+ * function iterates over each element
292
+ * @returns The `pushMany` function is returning an array of boolean values, where each value
293
+ * represents the result of calling the `push` method on the current object instance with the
294
+ * corresponding element from the input `elements` iterable.
295
+ */
296
+ pushMany(elements: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>) {
297
+ const ans: boolean[] = [];
298
+ for (const el of elements) {
299
+ if (this.toElementFn) {
300
+ ans.push(this.push(this.toElementFn(el as R)));
280
301
  } else {
281
- this._bucketFirst = 0;
282
- this._firstInBucket = 0;
302
+ ans.push(this.push(el as E));
283
303
  }
284
304
  }
285
- this._size -= 1;
286
- return element;
305
+ return ans;
306
+ }
307
+
308
+ /**
309
+ * Time Complexity: O(k)
310
+ * Space Complexity: O(k)
311
+ *
312
+ * The `unshiftMany` function in TypeScript iterates over elements and adds them to the beginning of
313
+ * an array, optionally converting them using a provided function.
314
+ * @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
315
+ * parameter in the `unshiftMany` function is an iterable containing elements of type `E` or `R`. It
316
+ * can be an array or any other iterable data structure that has a known size or length. The function
317
+ * iterates over each element in the `elements` iterable and
318
+ * @returns The `unshiftMany` function returns an array of boolean values indicating whether each
319
+ * element was successfully added to the beginning of the array.
320
+ */
321
+ unshiftMany(elements: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R> = []) {
322
+ const ans: boolean[] = [];
323
+ for (const el of elements) {
324
+ if (this.toElementFn) {
325
+ ans.push(this.unshift(this.toElementFn(el as R)));
326
+ } else {
327
+ ans.push(this.unshift(el as E));
328
+ }
329
+ }
330
+ return ans;
287
331
  }
288
332
 
289
333
  /**
@@ -25,12 +25,7 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
25
25
  this._autoCompactRatio = autoCompactRatio;
26
26
  }
27
27
 
28
- if (elements) {
29
- for (const el of elements) {
30
- if (this.toElementFn) this.push(this.toElementFn(el as R));
31
- else this.push(el as E);
32
- }
33
- }
28
+ this.pushMany(elements);
34
29
  }
35
30
 
36
31
  protected _elements: E[] = [];
@@ -131,6 +126,26 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
131
126
  return true;
132
127
  }
133
128
 
129
+ /**
130
+ * Time Complexity: O(k)
131
+ * Space Complexity: O(k)
132
+ *
133
+ * The `pushMany` function iterates over elements and pushes them into an array after applying a
134
+ * transformation function if provided.
135
+ * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
136
+ * is an iterable containing elements of type `E` or `R`.
137
+ * @returns The `pushMany` function is returning an array of boolean values indicating whether each
138
+ * element was successfully pushed into the data structure.
139
+ */
140
+ pushMany(elements: Iterable<E> | Iterable<R>) {
141
+ const ans: boolean[] = [];
142
+ for (const el of elements) {
143
+ if (this.toElementFn) ans.push(this.push(this.toElementFn(el as R)));
144
+ else ans.push(this.push(el as E));
145
+ }
146
+ return ans;
147
+ }
148
+
134
149
  /**
135
150
  * Time Complexity: O(1)
136
151
  * Space Complexity: O(1)
@@ -150,6 +165,9 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
150
165
  }
151
166
 
152
167
  /**
168
+ * Time Complexity: O(n)
169
+ * Space Complexity: O(1)
170
+ *
153
171
  * The delete function removes an element from the list.
154
172
  * @param {E} element - Specify the element to be deleted
155
173
  * @return A boolean value indicating whether the element was successfully deleted or not
@@ -160,6 +178,9 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
160
178
  }
161
179
 
162
180
  /**
181
+ * Time Complexity: O(n)
182
+ * Space Complexity: O(1)
183
+ *
163
184
  * The deleteAt function deletes the element at a given index.
164
185
  * @param {number} index - Determine the index of the element to be deleted
165
186
  * @return A boolean value
@@ -173,7 +194,12 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
173
194
  * Time Complexity: O(1)
174
195
  * Space Complexity: O(1)
175
196
  *
176
- * @param index
197
+ * The `at` function returns the element at a specified index adjusted by an offset, or `undefined`
198
+ * if the index is out of bounds.
199
+ * @param {number} index - The `index` parameter represents the position of the element you want to
200
+ * retrieve from the data structure.
201
+ * @returns The `at` method is returning the element at the specified index adjusted by the offset
202
+ * `_offset`.
177
203
  */
178
204
  at(index: number): E | undefined {
179
205
  return this.elements[index + this._offset];
@@ -213,6 +239,9 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
213
239
  }
214
240
 
215
241
  /**
242
+ * Time Complexity: O(n)
243
+ * Space Complexity: O(1)
244
+ *
216
245
  * The `compact` function in TypeScript slices the elements array based on the offset and resets the
217
246
  * offset to zero.
218
247
  * @returns The `compact()` method is returning a boolean value of `true`.
@@ -265,6 +294,20 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
265
294
  /**
266
295
  * Time Complexity: O(n)
267
296
  * Space Complexity: O(n)
297
+ *
298
+ * The `map` function in TypeScript creates a new Queue by applying a callback function to each
299
+ * element in the original Queue.
300
+ * @param callback - The `callback` parameter is a function that will be applied to each element in
301
+ * the queue. It takes the current element, its index, and the queue itself as arguments, and returns
302
+ * a new element.
303
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be provided to
304
+ * convert a raw element of type `RM` to a new element of type `EM`. This function is used within the
305
+ * `map` method to transform each raw element before passing it to the `callback` function. If
306
+ * @param {any} [thisArg] - The `thisArg` parameter in the `map` function is used to specify the
307
+ * value of `this` when executing the `callback` function. It allows you to set the context (the
308
+ * value of `this`) within the callback function. If `thisArg` is provided, it will be
309
+ * @returns A new Queue object containing elements of type EM, which are the result of applying the
310
+ * callback function to each element in the original Queue object.
268
311
  */
269
312
  map<EM, RM>(
270
313
  callback: ElementCallback<E, R, EM, Queue<E, R>>,
@@ -19,15 +19,7 @@ import { IterableElementBase } from '../base';
19
19
  export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E, R>> {
20
20
  constructor(elements: Iterable<E> | Iterable<R> = [], options?: StackOptions<E, R>) {
21
21
  super(options);
22
- if (elements) {
23
- for (const el of elements) {
24
- if (this.toElementFn) {
25
- this.push(this.toElementFn(el as R));
26
- } else {
27
- this.push(el as E);
28
- }
29
- }
30
- }
22
+ this.pushMany(elements);
31
23
  }
32
24
 
33
25
  protected _elements: E[] = [];
@@ -48,11 +40,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
48
40
  return this.elements.length;
49
41
  }
50
42
 
51
- /**
52
- * Time Complexity: O(n)
53
- * Space Complexity: O(n)
54
- */
55
-
56
43
  /**
57
44
  * Time Complexity: O(n)
58
45
  * Space Complexity: O(n)
@@ -67,6 +54,9 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
67
54
  }
68
55
 
69
56
  /**
57
+ * Time Complexity: O(1)
58
+ * Space Complexity: O(1)
59
+ *
70
60
  * The function checks if an array is empty and returns a boolean value.
71
61
  * @returns A boolean value indicating whether the `_elements` array is empty or not.
72
62
  */
@@ -115,9 +105,36 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
115
105
  }
116
106
 
117
107
  /**
118
- * The delete function removes an element from the stack.
119
- * @param element: E Specify the element to be deleted
120
- * @return A boolean value indicating whether the element was successfully deleted or not
108
+ * Time Complexity: O(k)
109
+ * Space Complexity: O(1)
110
+ *
111
+ * The function `pushMany` iterates over elements and pushes them into an array after applying a
112
+ * transformation function if provided.
113
+ * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
114
+ * is an iterable containing elements of type `E` or `R`. The function iterates over each element in
115
+ * the iterable and pushes it into the data structure. If a transformation function `toElementFn` is
116
+ * provided, it is used to
117
+ * @returns The `pushMany` function is returning an array of boolean values indicating whether each
118
+ * element was successfully pushed into the data structure.
119
+ */
120
+ pushMany(elements: Iterable<E> | Iterable<R>) {
121
+ const ans: boolean[] = [];
122
+ for (const el of elements) {
123
+ if (this.toElementFn) {
124
+ ans.push(this.push(this.toElementFn(el as R)));
125
+ } else {
126
+ ans.push(this.push(el as E));
127
+ }
128
+ }
129
+ return ans;
130
+ }
131
+
132
+ /**
133
+ * Time Complexity: O(n)
134
+ * Space Complexity: O(1)
135
+ *
136
+ * The toArray function returns a copy of the elements in an array.
137
+ * @returns An array of type E.
121
138
  */
122
139
  delete(element: E): boolean {
123
140
  const index = this.elements.indexOf(element);
@@ -125,9 +142,11 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
125
142
  }
126
143
 
127
144
  /**
128
- * The deleteAt function deletes the element at a given index.
129
- * @param index: number Determine the index of the element to be deleted
130
- * @return A boolean value
145
+ * Time Complexity: O(n)
146
+ * Space Complexity: O(1)
147
+ *
148
+ * The toArray function returns a copy of the elements in an array.
149
+ * @returns An array of type E.
131
150
  */
132
151
  deleteAt(index: number): boolean {
133
152
  const spliced = this.elements.splice(index, 1);
@@ -92,13 +92,100 @@ export class TrieNode {
92
92
  * 9. Spell Check: Checking the spelling of words.
93
93
  * 10. IP Routing: Used in certain types of IP routing algorithms.
94
94
  * 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
95
+ * @example
96
+ * // Autocomplete: Prefix validation and checking
97
+ * const autocomplete = new Trie<string>(['gmail.com', 'gmail.co.nz', 'gmail.co.jp', 'yahoo.com', 'outlook.com']);
98
+ *
99
+ * // Get all completions for a prefix
100
+ * const gmailCompletions = autocomplete.getWords('gmail');
101
+ * console.log(gmailCompletions); // ['gmail.com', 'gmail.co.nz', 'gmail.co.jp']
102
+ * @example
103
+ * // File System Path Operations
104
+ * const fileSystem = new Trie<string>([
105
+ * '/home/user/documents/file1.txt',
106
+ * '/home/user/documents/file2.txt',
107
+ * '/home/user/pictures/photo.jpg',
108
+ * '/home/user/pictures/vacation/',
109
+ * '/home/user/downloads'
110
+ * ]);
111
+ *
112
+ * // Find common directory prefix
113
+ * console.log(fileSystem.getLongestCommonPrefix()); // '/home/user/'
114
+ *
115
+ * // List all files in a directory
116
+ * const documentsFiles = fileSystem.getWords('/home/user/documents/');
117
+ * console.log(documentsFiles); // ['/home/user/documents/file1.txt', '/home/user/documents/file2.txt']
118
+ * @example
119
+ * // Autocomplete: Basic word suggestions
120
+ * // Create a trie for autocomplete
121
+ * const autocomplete = new Trie<string>([
122
+ * 'function',
123
+ * 'functional',
124
+ * 'functions',
125
+ * 'class',
126
+ * 'classes',
127
+ * 'classical',
128
+ * 'closure',
129
+ * 'const',
130
+ * 'constructor'
131
+ * ]);
132
+ *
133
+ * // Test autocomplete with different prefixes
134
+ * console.log(autocomplete.getWords('fun')); // ['functional', 'functions', 'function']
135
+ * console.log(autocomplete.getWords('cla')); // ['classes', 'classical', 'class']
136
+ * console.log(autocomplete.getWords('con')); // ['constructor', 'const']
137
+ *
138
+ * // Test with non-matching prefix
139
+ * console.log(autocomplete.getWords('xyz')); // []
140
+ * @example
141
+ * // Dictionary: Case-insensitive word lookup
142
+ * // Create a case-insensitive dictionary
143
+ * const dictionary = new Trie<string>([], { caseSensitive: false });
144
+ *
145
+ * // Add words with mixed casing
146
+ * dictionary.add('Hello');
147
+ * dictionary.add('WORLD');
148
+ * dictionary.add('JavaScript');
149
+ *
150
+ * // Test lookups with different casings
151
+ * console.log(dictionary.has('hello')); // true
152
+ * console.log(dictionary.has('HELLO')); // true
153
+ * console.log(dictionary.has('Hello')); // true
154
+ * console.log(dictionary.has('javascript')); // true
155
+ * console.log(dictionary.has('JAVASCRIPT')); // true
156
+ * @example
157
+ * // IP Address Routing Table
158
+ * // Add IP address prefixes and their corresponding routes
159
+ * const routes = {
160
+ * '192.168.1': 'LAN_SUBNET_1',
161
+ * '192.168.2': 'LAN_SUBNET_2',
162
+ * '10.0.0': 'PRIVATE_NETWORK_1',
163
+ * '10.0.1': 'PRIVATE_NETWORK_2'
164
+ * };
165
+ *
166
+ * const ipRoutingTable = new Trie<string>(Object.keys(routes));
167
+ *
168
+ * // Check IP address prefix matching
169
+ * console.log(ipRoutingTable.hasPrefix('192.168.1')); // true
170
+ * console.log(ipRoutingTable.hasPrefix('192.168.2')); // true
171
+ *
172
+ * // Validate IP address belongs to subnet
173
+ * const ip = '192.168.1.100';
174
+ * const subnet = ip.split('.').slice(0, 3).join('.');
175
+ * console.log(ipRoutingTable.hasPrefix(subnet)); // true
95
176
  */
96
177
  export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
97
178
  /**
98
- * The constructor function for the Trie class.
99
- * @param words: Iterable string Initialize the trie with a set of words
100
- * @param options?: TrieOptions Allow the user to pass in options for the trie
101
- * @return This
179
+ * The constructor initializes a Trie data structure with optional options and words provided as
180
+ * input.
181
+ * @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the constructor is an
182
+ * iterable containing either strings or elements of type `R`. It is used to initialize the Trie with
183
+ * a list of words or elements. If no `words` are provided, an empty iterable is used as the default
184
+ * value.
185
+ * @param [options] - The `options` parameter in the constructor is an optional object that can
186
+ * contain configuration options for the Trie data structure. One of the options it can have is
187
+ * `caseSensitive`, which is a boolean value indicating whether the Trie should be case-sensitive or
188
+ * not. If `caseSensitive` is set to `
102
189
  */
103
190
  constructor(words: Iterable<string> | Iterable<R> = [], options?: TrieOptions<R>) {
104
191
  super(options);
@@ -107,13 +194,7 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
107
194
  if (caseSensitive !== undefined) this._caseSensitive = caseSensitive;
108
195
  }
109
196
  if (words) {
110
- for (const word of words) {
111
- if (this.toElementFn) {
112
- this.add(this.toElementFn(word as R));
113
- } else {
114
- this.add(word as string);
115
- }
116
- }
197
+ this.addMany(words);
117
198
  }
118
199
  }
119
200
 
@@ -175,6 +256,30 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
175
256
  return isNewWord;
176
257
  }
177
258
 
259
+ /**
260
+ * Time Complexity: O(n * l)
261
+ * Space Complexity: O(1)
262
+ *
263
+ * The `addMany` function in TypeScript takes an iterable of strings or elements of type R, converts
264
+ * them using a provided function if available, and adds them to a data structure while returning an
265
+ * array of boolean values indicating success.
266
+ * @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the `addMany` function is
267
+ * an iterable that contains either strings or elements of type `R`.
268
+ * @returns The `addMany` method returns an array of boolean values indicating whether each word in
269
+ * the input iterable was successfully added to the data structure.
270
+ */
271
+ addMany(words: Iterable<string> | Iterable<R>): boolean[] {
272
+ const ans: boolean[] = [];
273
+ for (const word of words) {
274
+ if (this.toElementFn) {
275
+ ans.push(this.add(this.toElementFn(word as R)));
276
+ } else {
277
+ ans.push(this.add(word as string));
278
+ }
279
+ }
280
+ return ans;
281
+ }
282
+
178
283
  /**
179
284
  * Time Complexity: O(l), where l is the length of the input word.
180
285
  * Space Complexity: O(1) - Constant space.
@@ -261,9 +366,14 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
261
366
  }
262
367
 
263
368
  /**
264
- * Time Complexity: O(n), where n is the total number of nodes in the trie.
265
- * Space Complexity: O(1) - Constant space.
369
+ * Time Complexity: O(n)
370
+ * Space Complexity: O(1)
266
371
  *
372
+ * The function `getHeight` calculates the height of a trie data structure starting from the root
373
+ * node.
374
+ * @returns The `getHeight` method returns the maximum depth or height of the trie tree starting from
375
+ * the root node. It calculates the depth using a breadth-first search (BFS) traversal of the trie
376
+ * tree and returns the maximum depth found.
267
377
  */
268
378
  getHeight(): number {
269
379
  const startNode = this.root;
package/src/index.ts CHANGED
@@ -9,4 +9,5 @@
9
9
  export * from './data-structures/queue';
10
10
  export * from './types/data-structures/queue';
11
11
  export * from './types/common';
12
- export * from './constants';
12
+ export * from './types/utils';
13
+ export * from './common';
@@ -1,6 +1,6 @@
1
1
  import { BinaryTree, BinaryTreeNode } from '../../../data-structures';
2
2
  import { IterationType, OptValue } from '../../common';
3
- import { DFSOperation } from '../../../constants';
3
+ import { DFSOperation } from '../../../common';
4
4
 
5
5
  export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
6
6
 
@@ -1,13 +1,14 @@
1
1
  import { BST, BSTNode } from '../../../data-structures';
2
2
  import type { BinaryTreeOptions } from './binary-tree';
3
- import { Comparator } from '../../common';
3
+ import { Comparable } from '../../utils';
4
4
 
5
5
  export type BSTNodeNested<K, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
6
6
 
7
7
  export type BSTNested<K, V, R, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
8
8
 
9
9
  export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & {
10
- comparator?: Comparator<K>
10
+ extractComparable?: (key: K) => Comparable
11
+ isReverse?: boolean;
11
12
  }
12
13
 
13
14
  export type BSTNOptKey<K> = K | undefined;
@@ -7,4 +7,4 @@ export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNo
7
7
 
8
8
  export type RedBlackTreeNested<K, V, R, NODE extends RedBlackTreeNode<K, V, NODE>> = RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
9
9
 
10
- export type RBTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};
10
+ export type RBTreeOptions<K, V, R> = Omit<BSTOptions<K, V, R>, 'isReverse'> & {};