min-heap-typed 1.42.8 → 1.42.9

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 (49) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +88 -23
  2. package/dist/data-structures/binary-tree/avl-tree.js +88 -23
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +180 -74
  4. package/dist/data-structures/binary-tree/binary-tree.js +388 -201
  5. package/dist/data-structures/binary-tree/bst.d.ts +121 -66
  6. package/dist/data-structures/binary-tree/bst.js +121 -67
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +72 -5
  8. package/dist/data-structures/binary-tree/rb-tree.js +95 -18
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +82 -43
  10. package/dist/data-structures/binary-tree/tree-multimap.js +82 -43
  11. package/dist/data-structures/graph/abstract-graph.d.ts +139 -36
  12. package/dist/data-structures/graph/abstract-graph.js +147 -36
  13. package/dist/data-structures/graph/directed-graph.d.ts +126 -0
  14. package/dist/data-structures/graph/directed-graph.js +126 -0
  15. package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
  16. package/dist/data-structures/graph/undirected-graph.js +63 -0
  17. package/dist/data-structures/heap/heap.d.ts +175 -12
  18. package/dist/data-structures/heap/heap.js +175 -12
  19. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
  20. package/dist/data-structures/linked-list/doubly-linked-list.js +203 -0
  21. package/dist/data-structures/linked-list/singly-linked-list.d.ts +182 -0
  22. package/dist/data-structures/linked-list/singly-linked-list.js +182 -0
  23. package/dist/data-structures/linked-list/skip-linked-list.d.ts +64 -0
  24. package/dist/data-structures/linked-list/skip-linked-list.js +64 -0
  25. package/dist/data-structures/queue/deque.d.ts +113 -3
  26. package/dist/data-structures/queue/deque.js +113 -3
  27. package/dist/data-structures/queue/queue.d.ts +87 -0
  28. package/dist/data-structures/queue/queue.js +87 -0
  29. package/dist/data-structures/stack/stack.d.ts +42 -0
  30. package/dist/data-structures/stack/stack.js +42 -0
  31. package/dist/data-structures/trie/trie.d.ts +76 -0
  32. package/dist/data-structures/trie/trie.js +76 -1
  33. package/package.json +2 -2
  34. package/src/data-structures/binary-tree/avl-tree.ts +97 -23
  35. package/src/data-structures/binary-tree/binary-tree.ts +419 -204
  36. package/src/data-structures/binary-tree/bst.ts +130 -68
  37. package/src/data-structures/binary-tree/rb-tree.ts +106 -19
  38. package/src/data-structures/binary-tree/tree-multimap.ts +88 -44
  39. package/src/data-structures/graph/abstract-graph.ts +133 -7
  40. package/src/data-structures/graph/directed-graph.ts +145 -1
  41. package/src/data-structures/graph/undirected-graph.ts +72 -0
  42. package/src/data-structures/heap/heap.ts +201 -12
  43. package/src/data-structures/linked-list/doubly-linked-list.ts +232 -0
  44. package/src/data-structures/linked-list/singly-linked-list.ts +208 -0
  45. package/src/data-structures/linked-list/skip-linked-list.ts +74 -0
  46. package/src/data-structures/queue/deque.ts +127 -3
  47. package/src/data-structures/queue/queue.ts +99 -0
  48. package/src/data-structures/stack/stack.ts +48 -0
  49. package/src/data-structures/trie/trie.ts +87 -4
@@ -50,6 +50,14 @@ export class ObjectDeque<E = number> {
50
50
  }
51
51
 
52
52
  /**
53
+ * Time Complexity: O(1)
54
+ * Space Complexity: O(1)
55
+ */
56
+
57
+ /**
58
+ * Time Complexity: O(1)
59
+ * Space Complexity: O(1)
60
+ *
53
61
  * The "addFirst" function adds a value to the beginning of an array-like data structure.
54
62
  * @param {E} value - The `value` parameter represents the value that you want to add to the beginning of the data
55
63
  * structure.
@@ -67,6 +75,14 @@ export class ObjectDeque<E = number> {
67
75
  }
68
76
 
69
77
  /**
78
+ * Time Complexity: O(1)
79
+ * Space Complexity: O(1)
80
+ */
81
+
82
+ /**
83
+ * Time Complexity: O(1)
84
+ * Space Complexity: O(1)
85
+ *
70
86
  * The addLast function adds a value to the end of an array-like data structure.
71
87
  * @param {E} value - The `value` parameter represents the value that you want to add to the end of the data structure.
72
88
  */
@@ -83,6 +99,14 @@ export class ObjectDeque<E = number> {
83
99
  }
84
100
 
85
101
  /**
102
+ * Time Complexity: O(1)
103
+ * Space Complexity: O(1)
104
+ */
105
+
106
+ /**
107
+ * Time Complexity: O(1)
108
+ * Space Complexity: O(1)
109
+ *
86
110
  * The function `popFirst()` removes and returns the first element in a data structure.
87
111
  * @returns The value of the first element in the data structure.
88
112
  */
@@ -96,6 +120,14 @@ export class ObjectDeque<E = number> {
96
120
  }
97
121
 
98
122
  /**
123
+ * Time Complexity: O(1)
124
+ * Space Complexity: O(1)
125
+ */
126
+
127
+ /**
128
+ * Time Complexity: O(1)
129
+ * Space Complexity: O(1)
130
+ *
99
131
  * The `getFirst` function returns the first element in an array-like data structure if it exists.
100
132
  * @returns The element at the first position of the `_nodes` array.
101
133
  */
@@ -104,6 +136,14 @@ export class ObjectDeque<E = number> {
104
136
  }
105
137
 
106
138
  /**
139
+ * Time Complexity: O(1)
140
+ * Space Complexity: O(1)
141
+ */
142
+
143
+ /**
144
+ * Time Complexity: O(1)
145
+ * Space Complexity: O(1)
146
+ *
107
147
  * The `popLast()` function removes and returns the last element in a data structure.
108
148
  * @returns The value that was removed from the data structure.
109
149
  */
@@ -118,6 +158,14 @@ export class ObjectDeque<E = number> {
118
158
  }
119
159
 
120
160
  /**
161
+ * Time Complexity: O(1)
162
+ * Space Complexity: O(1)
163
+ */
164
+
165
+ /**
166
+ * Time Complexity: O(1)
167
+ * Space Complexity: O(1)
168
+ *
121
169
  * The `getLast()` function returns the last element in an array-like data structure.
122
170
  * @returns The last element in the array "_nodes" is being returned.
123
171
  */
@@ -126,6 +174,14 @@ export class ObjectDeque<E = number> {
126
174
  }
127
175
 
128
176
  /**
177
+ * Time Complexity: O(1)
178
+ * Space Complexity: O(1)
179
+ */
180
+
181
+ /**
182
+ * Time Complexity: O(1)
183
+ * Space Complexity: O(1)
184
+ *
129
185
  * The get function returns the element at the specified index in an array-like data structure.
130
186
  * @param {number} index - The index parameter is a number that represents the position of the element you want to
131
187
  * retrieve from the array.
@@ -159,10 +215,14 @@ export class ArrayDeque<E> {
159
215
  }
160
216
 
161
217
  /**
162
- * O(n) time complexity of adding at the beginning and the end
218
+ * Time Complexity: O(1)
219
+ * Space Complexity: O(1)
163
220
  */
164
221
 
165
222
  /**
223
+ * Time Complexity: O(1)
224
+ * Space Complexity: O(1)
225
+ *
166
226
  * The function "addLast" adds a value to the end of an array.
167
227
  * @param {E} value - The value parameter represents the value that you want to add to the end of the array.
168
228
  * @returns The return value is the new length of the array after the value has been added.
@@ -172,6 +232,14 @@ export class ArrayDeque<E> {
172
232
  }
173
233
 
174
234
  /**
235
+ * Time Complexity: O(1)
236
+ * Space Complexity: O(1)
237
+ */
238
+
239
+ /**
240
+ * Time Complexity: O(1)
241
+ * Space Complexity: O(1)
242
+ *
175
243
  * The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
176
244
  * @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
177
245
  */
@@ -180,6 +248,14 @@ export class ArrayDeque<E> {
180
248
  }
181
249
 
182
250
  /**
251
+ * Time Complexity: O(n)
252
+ * Space Complexity: O(1)
253
+ */
254
+
255
+ /**
256
+ * Time Complexity: O(n)
257
+ * Space Complexity: O(1)
258
+ *
183
259
  * The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
184
260
  * @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
185
261
  * empty.
@@ -189,10 +265,14 @@ export class ArrayDeque<E> {
189
265
  }
190
266
 
191
267
  /**
192
- * O(n) time complexity of adding at the beginning and the end
268
+ * Time Complexity: O(n)
269
+ * Space Complexity: O(1)
193
270
  */
194
271
 
195
272
  /**
273
+ * Time Complexity: O(n)
274
+ * Space Complexity: O(1)
275
+ *
196
276
  * The function "addFirst" adds a value to the beginning of an array.
197
277
  * @param {E} value - The value parameter represents the value that you want to add to the beginning of the array.
198
278
  * @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
@@ -203,6 +283,14 @@ export class ArrayDeque<E> {
203
283
  }
204
284
 
205
285
  /**
286
+ * Time Complexity: O(1)
287
+ * Space Complexity: O(1)
288
+ */
289
+
290
+ /**
291
+ * Time Complexity: O(1)
292
+ * Space Complexity: O(1)
293
+ *
206
294
  * The `getFirst` function returns the first element of an array or null if the array is empty.
207
295
  * @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
208
296
  * empty, it will return `null`.
@@ -212,6 +300,14 @@ export class ArrayDeque<E> {
212
300
  }
213
301
 
214
302
  /**
303
+ * Time Complexity: O(1)
304
+ * Space Complexity: O(1)
305
+ */
306
+
307
+ /**
308
+ * Time Complexity: O(1)
309
+ * Space Complexity: O(1)
310
+ *
215
311
  * The `getLast` function returns the last element of an array or null if the array is empty.
216
312
  * @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
217
313
  */
@@ -220,10 +316,14 @@ export class ArrayDeque<E> {
220
316
  }
221
317
 
222
318
  /**
223
- * O(1) time complexity of obtaining the value
319
+ * Time Complexity: O(1)
320
+ * Space Complexity: O(1)
224
321
  */
225
322
 
226
323
  /**
324
+ * Time Complexity: O(1)
325
+ * Space Complexity: O(1)
326
+ *
227
327
  * The get function returns the element at the specified index in an array, or null if the index is out of bounds.
228
328
  * @param {number} index - The index parameter is a number that represents the position of the element you want to
229
329
  * retrieve from the array.
@@ -235,6 +335,14 @@ export class ArrayDeque<E> {
235
335
  }
236
336
 
237
337
  /**
338
+ * Time Complexity: O(1)
339
+ * Space Complexity: O(1)
340
+ */
341
+
342
+ /**
343
+ * Time Complexity: O(1)
344
+ * Space Complexity: O(1)
345
+ *
238
346
  * The set function assigns a value to a specific index in an array.
239
347
  * @param {number} index - The index parameter is a number that represents the position of the element in the array
240
348
  * that you want to set a new value for.
@@ -247,6 +355,14 @@ export class ArrayDeque<E> {
247
355
  }
248
356
 
249
357
  /**
358
+ * Time Complexity: O(n)
359
+ * Space Complexity: O(1)
360
+ */
361
+
362
+ /**
363
+ * Time Complexity: O(n)
364
+ * Space Complexity: O(1)
365
+ *
250
366
  * The insert function adds a value at a specified index in an array.
251
367
  * @param {number} index - The index parameter specifies the position at which the value should be inserted in the
252
368
  * array. It is a number that represents the index of the array where the value should be inserted. The index starts
@@ -261,6 +377,14 @@ export class ArrayDeque<E> {
261
377
  }
262
378
 
263
379
  /**
380
+ * Time Complexity: O(n)
381
+ * Space Complexity: O(1)
382
+ */
383
+
384
+ /**
385
+ * Time Complexity: O(n)
386
+ * Space Complexity: O(1)
387
+ *
264
388
  * The delete function removes an element from an array at a specified index.
265
389
  * @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
266
390
  * is a number that represents the index of the element to be removed.
@@ -84,6 +84,14 @@ export class Queue<E = any> {
84
84
  }
85
85
 
86
86
  /**
87
+ * Time Complexity: O(1) - constant time as it adds an element to the end of the array.
88
+ * Space Complexity: O(1) - no additional space is used.
89
+ */
90
+
91
+ /**
92
+ * Time Complexity: O(1) - constant time as it adds an element to the end of the array.
93
+ * Space Complexity: O(1) - no additional space is used.
94
+ *
87
95
  * The push function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
88
96
  * @param {E} element - The `element` parameter represents the element that you want to add to the queue.
89
97
  * @returns The `add` method is returning a `Queue<E>` object.
@@ -94,6 +102,14 @@ export class Queue<E = any> {
94
102
  }
95
103
 
96
104
  /**
105
+ * Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
106
+ * Space Complexity: O(1) - no additional space is used.
107
+ */
108
+
109
+ /**
110
+ * Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
111
+ * Space Complexity: O(1) - no additional space is used.
112
+ *
97
113
  * The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
98
114
  * necessary to optimize performance.
99
115
  * @returns The function `shift()` returns either the first element in the queue or `null` if the queue is empty.
@@ -114,6 +130,14 @@ export class Queue<E = any> {
114
130
  }
115
131
 
116
132
  /**
133
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
134
+ * Space Complexity: O(1) - no additional space is used.
135
+ */
136
+
137
+ /**
138
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
139
+ * Space Complexity: O(1) - no additional space is used.
140
+ *
117
141
  * The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
118
142
  * @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
119
143
  * the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
@@ -123,6 +147,14 @@ export class Queue<E = any> {
123
147
  }
124
148
 
125
149
  /**
150
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
151
+ * Space Complexity: O(1) - no additional space is used.
152
+ */
153
+
154
+ /**
155
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
156
+ * Space Complexity: O(1) - no additional space is used.
157
+ *
126
158
  * The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
127
159
  * @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
128
160
  * the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
@@ -132,6 +164,14 @@ export class Queue<E = any> {
132
164
  }
133
165
 
134
166
  /**
167
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
168
+ * Space Complexity: O(1) - no additional space is used.
169
+ */
170
+
171
+ /**
172
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
173
+ * Space Complexity: O(1) - no additional space is used.
174
+ *
135
175
  * The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
136
176
  * @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
137
177
  * array is empty, it returns `null`.
@@ -141,6 +181,14 @@ export class Queue<E = any> {
141
181
  }
142
182
 
143
183
  /**
184
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
185
+ * Space Complexity: O(1) - no additional space is used.
186
+ */
187
+
188
+ /**
189
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
190
+ * Space Complexity: O(1) - no additional space is used.
191
+ *
144
192
  * The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
145
193
  * @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
146
194
  * array is empty, it returns `null`.
@@ -150,6 +198,14 @@ export class Queue<E = any> {
150
198
  }
151
199
 
152
200
  /**
201
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
202
+ * Space Complexity: O(1) - no additional space is used.
203
+ */
204
+
205
+ /**
206
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
207
+ * Space Complexity: O(1) - no additional space is used.
208
+ *
153
209
  * The enqueue function adds a value to the end of a queue.
154
210
  * @param {E} value - The value parameter represents the value that you want to add to the queue.
155
211
  */
@@ -158,6 +214,14 @@ export class Queue<E = any> {
158
214
  }
159
215
 
160
216
  /**
217
+ * Time Complexity: O(n) - same as shift().
218
+ * Space Complexity: O(1) - same as shift().
219
+ */
220
+
221
+ /**
222
+ * Time Complexity: O(n) - same as shift().
223
+ * Space Complexity: O(1) - same as shift().
224
+ *
161
225
  * The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
162
226
  * @returns The method is returning a value of type E or null.
163
227
  */
@@ -165,11 +229,30 @@ export class Queue<E = any> {
165
229
  return this.shift();
166
230
  }
167
231
 
232
+ /**
233
+ * Time Complexity: O(1) - constant time as it retrieves the value at the specified index.
234
+ * Space Complexity: O(1) - no additional space is used.
235
+ */
236
+
237
+ /**
238
+ * Time Complexity: O(1) - constant time as it retrieves the value at the specified index.
239
+ * Space Complexity: O(1) - no additional space is used.
240
+ *
241
+ * @param index
242
+ */
168
243
  getAt(index: number): E | undefined {
169
244
  return this.nodes[index];
170
245
  }
171
246
 
172
247
  /**
248
+ * Time Complexity: O(1) - constant time as it retrieves the value at the specified index.
249
+ * Space Complexity: O(1) - no additional space is used.
250
+ */
251
+
252
+ /**
253
+ * Time Complexity: O(1) - constant time as it retrieves the value at the specified index.
254
+ * Space Complexity: O(1) - no additional space is used.
255
+ *
173
256
  * The function checks if a data structure is empty by comparing its size to zero.
174
257
  * @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
175
258
  */
@@ -178,6 +261,14 @@ export class Queue<E = any> {
178
261
  }
179
262
 
180
263
  /**
264
+ * Time Complexity: O(1) - constant time as it returns a shallow copy of the internal array.
265
+ * Space Complexity: O(n) - where n is the number of elements in the queue.
266
+ */
267
+
268
+ /**
269
+ * Time Complexity: O(1) - constant time as it returns a shallow copy of the internal array.
270
+ * Space Complexity: O(n) - where n is the number of elements in the queue.
271
+ *
181
272
  * The toArray() function returns an array of elements from the current offset to the end of the _nodes array.
182
273
  * @returns An array of type E is being returned.
183
274
  */
@@ -194,6 +285,14 @@ export class Queue<E = any> {
194
285
  }
195
286
 
196
287
  /**
288
+ * Time Complexity: O(n) - where n is the number of elements in the queue. It creates a shallow copy of the internal array.
289
+ * Space Complexity: O(n) - the space required is proportional to the number of elements in the queue.
290
+ */
291
+
292
+ /**
293
+ * Time Complexity: O(n) - where n is the number of elements in the queue. It creates a shallow copy of the internal array.
294
+ * Space Complexity: O(n) - the space required is proportional to the number of elements in the queue.
295
+ *
197
296
  * The `clone()` function returns a new Queue object with the same elements as the original Queue.
198
297
  * @returns The `clone()` method is returning a new instance of the `Queue` class.
199
298
  */
@@ -21,6 +21,14 @@ export class Stack<E = any> {
21
21
  }
22
22
 
23
23
  /**
24
+ * Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
25
+ * Space Complexity: O(n), as it creates a new stack with the elements from the input array.
26
+ */
27
+
28
+ /**
29
+ * Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
30
+ * Space Complexity: O(n), as it creates a new stack with the elements from the input array.
31
+ *
24
32
  * The function "fromArray" creates a new Stack object from an array of elements.
25
33
  * @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
26
34
  * @returns {Stack} The method is returning a new instance of the Stack class, initialized with the elements from the input
@@ -47,6 +55,14 @@ export class Stack<E = any> {
47
55
  }
48
56
 
49
57
  /**
58
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
59
+ * Space Complexity: O(1), as it does not use any additional space.
60
+ */
61
+
62
+ /**
63
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
64
+ * Space Complexity: O(1), as it does not use any additional space.
65
+ *
50
66
  * The `peek` function returns the last element of an array, or null if the array is empty.
51
67
  * @returns The `peek()` function returns the last element of the `_elements` array, or `null` if the array is empty.
52
68
  */
@@ -57,6 +73,14 @@ export class Stack<E = any> {
57
73
  }
58
74
 
59
75
  /**
76
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
77
+ * Space Complexity: O(1), as it does not use any additional space.
78
+ */
79
+
80
+ /**
81
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
82
+ * Space Complexity: O(1), as it does not use any additional space.
83
+ *
60
84
  * The push function adds an element to the stack and returns the updated stack.
61
85
  * @param {E} element - The parameter "element" is of type E, which means it can be any data type.
62
86
  * @returns The `push` method is returning the updated `Stack<E>` object.
@@ -67,6 +91,14 @@ export class Stack<E = any> {
67
91
  }
68
92
 
69
93
  /**
94
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
95
+ * Space Complexity: O(1), as it does not use any additional space.
96
+ */
97
+
98
+ /**
99
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
100
+ * Space Complexity: O(1), as it does not use any additional space.
101
+ *
70
102
  * The `pop` function removes and returns the last element from an array, or returns null if the array is empty.
71
103
  * @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
72
104
  * array is empty, it returns `null`.
@@ -78,6 +110,14 @@ export class Stack<E = any> {
78
110
  }
79
111
 
80
112
  /**
113
+ * Time Complexity: O(n)
114
+ * Space Complexity: O(n)
115
+ */
116
+
117
+ /**
118
+ * Time Complexity: O(n)
119
+ * Space Complexity: O(n)
120
+ *
81
121
  * The toArray function returns a copy of the elements in an array.
82
122
  * @returns An array of type E.
83
123
  */
@@ -93,6 +133,14 @@ export class Stack<E = any> {
93
133
  }
94
134
 
95
135
  /**
136
+ * Time Complexity: O(n), where n is the number of elements in the stack, as it creates a new stack and copies all elements into it.
137
+ * Space Complexity: O(n), as it creates a new stack.
138
+ */
139
+
140
+ /**
141
+ * Time Complexity: O(n), where n is the number of elements in the stack, as it creates a new stack and copies all elements into it.
142
+ * Space Complexity: O(n), as it creates a new stack.
143
+ *
96
144
  * The `clone()` function returns a new `Stack` object with the same elements as the original stack.
97
145
  * @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
98
146
  */
@@ -49,6 +49,14 @@ export class Trie {
49
49
  }
50
50
 
51
51
  /**
52
+ * Time Complexity: O(M), where M is the length of the word being added.
53
+ * Space Complexity: O(M) - Each character in the word adds a TrieNode.
54
+ */
55
+
56
+ /**
57
+ * Time Complexity: O(M), where M is the length of the word being added.
58
+ * Space Complexity: O(M) - Each character in the word adds a TrieNode.
59
+ *
52
60
  * Add a word to the Trie structure.
53
61
  * @param {string} word - The word to add.
54
62
  * @returns {boolean} True if the word was successfully added.
@@ -69,6 +77,14 @@ export class Trie {
69
77
  }
70
78
 
71
79
  /**
80
+ * Time Complexity: O(M), where M is the length of the input word.
81
+ * Space Complexity: O(1) - Constant space.
82
+ */
83
+
84
+ /**
85
+ * Time Complexity: O(M), where M is the length of the input word.
86
+ * Space Complexity: O(1) - Constant space.
87
+ *
72
88
  * Check if the Trie contains a given word.
73
89
  * @param {string} word - The word to check for.
74
90
  * @returns {boolean} True if the word is present in the Trie.
@@ -85,6 +101,14 @@ export class Trie {
85
101
  }
86
102
 
87
103
  /**
104
+ * Time Complexity: O(M), where M is the length of the word being deleted.
105
+ * Space Complexity: O(M) - Due to the recursive DFS approach.
106
+ */
107
+
108
+ /**
109
+ * Time Complexity: O(M), where M is the length of the word being deleted.
110
+ * Space Complexity: O(M) - Due to the recursive DFS approach.
111
+ *
88
112
  * Remove a word from the Trie structure.
89
113
  * @param{string} word - The word to delete.
90
114
  * @returns {boolean} True if the word was successfully removed.
@@ -122,6 +146,16 @@ export class Trie {
122
146
  return isDeleted;
123
147
  }
124
148
 
149
+ /**
150
+ * Time Complexity: O(N), where N is the total number of nodes in the trie.
151
+ * Space Complexity: O(1) - Constant space.
152
+ */
153
+
154
+ /**
155
+ * Time Complexity: O(N), where N is the total number of nodes in the trie.
156
+ * Space Complexity: O(1) - Constant space.
157
+ *
158
+ */
125
159
  getHeight() {
126
160
  const beginRoot = this.root;
127
161
  let maxDepth = 0;
@@ -142,8 +176,15 @@ export class Trie {
142
176
  return maxDepth;
143
177
  }
144
178
 
145
- // --- start additional methods ---
146
179
  /**
180
+ * Time Complexity: O(M), where M is the length of the input prefix.
181
+ * Space Complexity: O(1) - Constant space.
182
+ */
183
+
184
+ /**
185
+ * Time Complexity: O(M), where M is the length of the input prefix.
186
+ * Space Complexity: O(1) - Constant space.
187
+ *
147
188
  * Check if a given input string has an absolute prefix in the Trie, meaning it's not a complete word.
148
189
  * @param {string} input - The input string to check.
149
190
  * @returns {boolean} True if it's an absolute prefix in the Trie.
@@ -160,6 +201,14 @@ export class Trie {
160
201
  }
161
202
 
162
203
  /**
204
+ * Time Complexity: O(M), where M is the length of the input prefix.
205
+ * Space Complexity: O(1) - Constant space.
206
+ */
207
+
208
+ /**
209
+ * Time Complexity: O(M), where M is the length of the input prefix.
210
+ * Space Complexity: O(1) - Constant space.
211
+ *
163
212
  * Check if a given input string is a prefix of any existing word in the Trie, whether as an absolute prefix or a complete word.
164
213
  * @param {string} input - The input string representing the prefix to check.
165
214
  * @returns {boolean} True if it's a prefix in the Trie.
@@ -176,6 +225,14 @@ export class Trie {
176
225
  }
177
226
 
178
227
  /**
228
+ * Time Complexity: O(N), where N is the total number of nodes in the trie.
229
+ * Space Complexity: O(M), where M is the length of the input prefix.
230
+ */
231
+
232
+ /**
233
+ * Time Complexity: O(N), where N is the total number of nodes in the trie.
234
+ * Space Complexity: O(M), where M is the length of the input prefix.
235
+ *
179
236
  * Check if the input string is a common prefix in the Trie, meaning it's a prefix shared by all words in the Trie.
180
237
  * @param {string} input - The input string representing the common prefix to check for.
181
238
  * @returns {boolean} True if it's a common prefix in the Trie.
@@ -195,6 +252,14 @@ export class Trie {
195
252
  }
196
253
 
197
254
  /**
255
+ * Time Complexity: O(N), where N is the total number of nodes in the trie.
256
+ * Space Complexity: O(M), where M is the length of the longest common prefix.
257
+ */
258
+
259
+ /**
260
+ * Time Complexity: O(N), where N is the total number of nodes in the trie.
261
+ * Space Complexity: O(M), where M is the length of the longest common prefix.
262
+ *
198
263
  * Get the longest common prefix among all the words stored in the Trie.
199
264
  * @returns {string} The longest common prefix found in the Trie.
200
265
  */
@@ -211,6 +276,14 @@ export class Trie {
211
276
  }
212
277
 
213
278
  /**
279
+ * Time Complexity: O(K * L), where K is the number of words retrieved, and L is the average length of the words.
280
+ * Space Complexity: O(K * L) - The space required for the output array.
281
+ */
282
+
283
+ /**
284
+ * Time Complexity: O(K * L), where K is the number of words retrieved, and L is the average length of the words.
285
+ * Space Complexity: O(K * L) - The space required for the output array.
286
+ *
214
287
  * The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
215
288
  * @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
216
289
  * trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
@@ -251,12 +324,22 @@ export class Trie {
251
324
  return words;
252
325
  }
253
326
 
327
+ /**
328
+ * Time Complexity: O(M), where M is the length of the input string.
329
+ * Space Complexity: O(1) - Constant space.
330
+ */
331
+
332
+ /**
333
+ * Time Complexity: O(M), where M is the length of the input string.
334
+ * Space Complexity: O(1) - Constant space.
335
+ *
336
+ * @param str
337
+ * @protected
338
+ */
254
339
  protected _caseProcess(str: string) {
255
340
  if (!this._caseSensitive) {
256
341
  str = str.toLowerCase(); // Convert str to lowercase if case-insensitive
257
342
  }
258
343
  return str;
259
344
  }
260
-
261
- // --- end additional methods ---
262
- }
345
+ }