min-heap-typed 1.50.2 → 1.50.3

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 (75) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +6 -0
  2. package/dist/data-structures/binary-tree/avl-tree.d.ts +29 -1
  3. package/dist/data-structures/binary-tree/avl-tree.js +33 -1
  4. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  5. package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +1 -1
  7. package/dist/data-structures/binary-tree/binary-tree.js +1 -1
  8. package/dist/data-structures/binary-tree/bst.d.ts +46 -13
  9. package/dist/data-structures/binary-tree/bst.js +46 -13
  10. package/dist/data-structures/binary-tree/rb-tree.d.ts +54 -2
  11. package/dist/data-structures/binary-tree/rb-tree.js +73 -15
  12. package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
  13. package/dist/data-structures/binary-tree/segment-tree.js +127 -10
  14. package/dist/data-structures/binary-tree/tree-multimap.d.ts +35 -2
  15. package/dist/data-structures/binary-tree/tree-multimap.js +38 -0
  16. package/dist/data-structures/graph/abstract-graph.d.ts +0 -78
  17. package/dist/data-structures/graph/abstract-graph.js +0 -189
  18. package/dist/data-structures/graph/directed-graph.d.ts +59 -0
  19. package/dist/data-structures/graph/directed-graph.js +105 -0
  20. package/dist/data-structures/graph/undirected-graph.d.ts +60 -7
  21. package/dist/data-structures/graph/undirected-graph.js +126 -18
  22. package/dist/data-structures/hash/hash-map.d.ts +143 -23
  23. package/dist/data-structures/hash/hash-map.js +196 -62
  24. package/dist/data-structures/heap/heap.d.ts +29 -19
  25. package/dist/data-structures/heap/heap.js +29 -20
  26. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
  27. package/dist/data-structures/linked-list/doubly-linked-list.js +83 -25
  28. package/dist/data-structures/linked-list/singly-linked-list.d.ts +26 -3
  29. package/dist/data-structures/linked-list/singly-linked-list.js +34 -3
  30. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  31. package/dist/data-structures/linked-list/skip-linked-list.js +2 -2
  32. package/dist/data-structures/matrix/matrix.d.ts +1 -1
  33. package/dist/data-structures/matrix/matrix.js +1 -1
  34. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  35. package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
  36. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  37. package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
  38. package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
  39. package/dist/data-structures/priority-queue/priority-queue.js +8 -0
  40. package/dist/data-structures/queue/deque.d.ts +95 -21
  41. package/dist/data-structures/queue/deque.js +100 -16
  42. package/dist/data-structures/queue/queue.d.ts +65 -45
  43. package/dist/data-structures/queue/queue.js +65 -45
  44. package/dist/data-structures/stack/stack.d.ts +36 -22
  45. package/dist/data-structures/stack/stack.js +36 -22
  46. package/dist/data-structures/tree/tree.d.ts +57 -3
  47. package/dist/data-structures/tree/tree.js +77 -11
  48. package/dist/data-structures/trie/trie.d.ts +100 -36
  49. package/dist/data-structures/trie/trie.js +115 -36
  50. package/package.json +2 -2
  51. package/src/data-structures/base/iterable-base.ts +12 -0
  52. package/src/data-structures/binary-tree/avl-tree.ts +37 -3
  53. package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
  54. package/src/data-structures/binary-tree/binary-tree.ts +1 -1
  55. package/src/data-structures/binary-tree/bst.ts +46 -13
  56. package/src/data-structures/binary-tree/rb-tree.ts +79 -18
  57. package/src/data-structures/binary-tree/segment-tree.ts +145 -11
  58. package/src/data-structures/binary-tree/tree-multimap.ts +42 -3
  59. package/src/data-structures/graph/abstract-graph.ts +0 -211
  60. package/src/data-structures/graph/directed-graph.ts +122 -0
  61. package/src/data-structures/graph/undirected-graph.ts +143 -19
  62. package/src/data-structures/hash/hash-map.ts +228 -76
  63. package/src/data-structures/heap/heap.ts +31 -20
  64. package/src/data-structures/linked-list/doubly-linked-list.ts +96 -29
  65. package/src/data-structures/linked-list/singly-linked-list.ts +42 -6
  66. package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
  67. package/src/data-structures/matrix/matrix.ts +1 -1
  68. package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
  69. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
  70. package/src/data-structures/priority-queue/priority-queue.ts +8 -0
  71. package/src/data-structures/queue/deque.ts +118 -22
  72. package/src/data-structures/queue/queue.ts +68 -45
  73. package/src/data-structures/stack/stack.ts +39 -23
  74. package/src/data-structures/tree/tree.ts +89 -15
  75. package/src/data-structures/trie/trie.ts +131 -40
@@ -1,25 +1,99 @@
1
1
  export class TreeNode<V = any> {
2
- key: string;
3
- value?: V | undefined;
4
- children?: TreeNode<V>[] | undefined;
5
-
2
+ /**
3
+ * The constructor function initializes a TreeNode object with a key, optional value, and optional
4
+ * children.
5
+ * @param {string} key - A string representing the key of the tree node.
6
+ * @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
7
+ * value associated with the node. If no value is provided, it defaults to `undefined`.
8
+ * @param {TreeNode<V>[]} [children] - The `children` parameter is an optional array of `TreeNode<V>`
9
+ * objects. It represents the child nodes of the current node. If no children are provided, the
10
+ * default value is an empty array.
11
+ */
6
12
  constructor(key: string, value?: V, children?: TreeNode<V>[]) {
7
- this.key = key;
8
- this.value = value || undefined;
9
- this.children = children || [];
13
+ this._key = key;
14
+ this._value = value || undefined;
15
+ this._children = children || [];
16
+ }
17
+
18
+ protected _key: string;
19
+
20
+ /**
21
+ * The function returns the value of the protected variable _key.
22
+ * @returns The value of the `_key` property, which is a string.
23
+ */
24
+ get key(): string {
25
+ return this._key;
26
+ }
27
+
28
+ /**
29
+ * The above function sets the value of a protected variable called "key".
30
+ * @param {string} value - The value parameter is a string that represents the value to be assigned
31
+ * to the key.
32
+ */
33
+ set key(value: string) {
34
+ this._key = value;
35
+ }
36
+
37
+ protected _value?: V | undefined;
38
+
39
+ /**
40
+ * The function returns the value stored in a variable, or undefined if the variable is empty.
41
+ * @returns The value of the variable `_value` is being returned.
42
+ */
43
+ get value(): V | undefined {
44
+ return this._value;
45
+ }
46
+
47
+ /**
48
+ * The function sets the value of a variable.
49
+ * @param {V | undefined} value - The parameter "value" is of type "V | undefined", which means it
50
+ * can accept a value of type "V" or it can be undefined.
51
+ */
52
+ set value(value: V | undefined) {
53
+ this._value = value;
54
+ }
55
+
56
+ protected _children?: TreeNode<V>[] | undefined;
57
+
58
+ /**
59
+ * The function returns an array of TreeNode objects or undefined.
60
+ * @returns The `children` property is being returned. It is of type `TreeNode<V>[] | undefined`,
61
+ * which means it can either be an array of `TreeNode<V>` objects or `undefined`.
62
+ */
63
+ get children(): TreeNode<V>[] | undefined {
64
+ return this._children;
65
+ }
66
+
67
+ /**
68
+ * The function sets the value of the children property of a TreeNode object.
69
+ * @param {TreeNode<V>[] | undefined} value - The value parameter is of type TreeNode<V>[] |
70
+ * undefined. This means that it can accept an array of TreeNode objects or undefined.
71
+ */
72
+ set children(value: TreeNode<V>[] | undefined) {
73
+ this._children = value;
10
74
  }
11
75
 
76
+ /**
77
+ * The function `addChildren` adds one or more child nodes to the current node.
78
+ * @param {TreeNode<V> | TreeNode<V>[]} children - The `children` parameter can be either a single
79
+ * `TreeNode<V>` object or an array of `TreeNode<V>` objects.
80
+ */
12
81
  addChildren(children: TreeNode<V> | TreeNode<V>[]) {
13
- if (!this.children) {
14
- this.children = [];
82
+ if (!this._children) {
83
+ this._children = [];
15
84
  }
16
85
  if (children instanceof TreeNode) {
17
- this.children.push(children);
86
+ this._children.push(children);
18
87
  } else {
19
- this.children = this.children.concat(children);
88
+ this._children = this._children.concat(children);
20
89
  }
21
90
  }
22
91
 
92
+ /**
93
+ * The function `getHeight()` calculates the maximum depth of a tree structure by performing a
94
+ * breadth-first search.
95
+ * @returns the maximum depth or height of the tree.
96
+ */
23
97
  getHeight() {
24
98
  let maxDepth = 0;
25
99
  if (this) {
@@ -27,10 +101,10 @@ export class TreeNode<V = any> {
27
101
  if (level > maxDepth) {
28
102
  maxDepth = level;
29
103
  }
30
- const { children } = node;
31
- if (children) {
32
- for (let i = 0, len = children.length; i < len; i++) {
33
- bfs(children[i], level + 1);
104
+ const { _children } = node;
105
+ if (_children) {
106
+ for (let i = 0, len = _children.length; i < len; i++) {
107
+ bfs(_children[i], level + 1);
34
108
  }
35
109
  }
36
110
  };
@@ -13,14 +13,70 @@ import { IterableElementBase } from '../base';
13
13
  * and a flag indicating whether it's the end of a word.
14
14
  */
15
15
  export class TrieNode {
16
- key: string;
17
- children: Map<string, TrieNode>;
18
- isEnd: boolean;
19
-
20
16
  constructor(key: string) {
21
- this.key = key;
22
- this.isEnd = false;
23
- this.children = new Map<string, TrieNode>();
17
+ this._key = key;
18
+ this._isEnd = false;
19
+ this._children = new Map<string, TrieNode>();
20
+ }
21
+
22
+ protected _key: string;
23
+
24
+ /**
25
+ * The function returns the value of the protected variable _key.
26
+ * @returns The value of the `_key` property, which is a string.
27
+ */
28
+ get key(): string {
29
+ return this._key;
30
+ }
31
+
32
+ /**
33
+ * The above function sets the value of a protected variable called "key".
34
+ * @param {string} value - The value parameter is a string that represents the value to be assigned
35
+ * to the key.
36
+ */
37
+ set key(value: string) {
38
+ this._key = value;
39
+ }
40
+
41
+ protected _children: Map<string, TrieNode>;
42
+
43
+ /**
44
+ * The function returns the children of a TrieNode as a Map.
45
+ * @returns The `children` property of the TrieNode object, which is a Map containing string keys and
46
+ * TrieNode values.
47
+ */
48
+ get children(): Map<string, TrieNode> {
49
+ return this._children;
50
+ }
51
+
52
+ /**
53
+ * The function sets the value of the `_children` property of a TrieNode object.
54
+ * @param value - The value parameter is a Map object that represents the children of a TrieNode. The
55
+ * keys of the map are strings, which represent the characters that are associated with each child
56
+ * TrieNode. The values of the map are TrieNode objects, which represent the child nodes of the
57
+ * current TrieNode.
58
+ */
59
+ set children(value: Map<string, TrieNode>) {
60
+ this._children = value;
61
+ }
62
+
63
+ protected _isEnd: boolean;
64
+
65
+ /**
66
+ * The function returns a boolean value indicating whether a certain condition is met.
67
+ * @returns The method is returning a boolean value, specifically the value of the variable `_isEnd`.
68
+ */
69
+ get isEnd(): boolean {
70
+ return this._isEnd;
71
+ }
72
+
73
+ /**
74
+ * The function sets the value of the "_isEnd" property.
75
+ * @param {boolean} value - The value parameter is a boolean value that indicates whether the current
76
+ * state is the end state or not.
77
+ */
78
+ set isEnd(value: boolean) {
79
+ this._isEnd = value;
24
80
  }
25
81
  }
26
82
 
@@ -68,9 +124,8 @@ export class Trie extends IterableElementBase<string, Trie> {
68
124
  protected _caseSensitive: boolean = true;
69
125
 
70
126
  /**
71
- * The caseSensitive function is a getter that returns the value of the private _caseSensitive property.
72
- *
73
- * @return The value of the _casesensitive private variable
127
+ * The caseSensitive function is a getter that returns the value of the protected _caseSensitive property.
128
+ * @return The value of the _caseSensitive protected variable
74
129
  */
75
130
  get caseSensitive(): boolean {
76
131
  return this._caseSensitive;
@@ -87,13 +142,13 @@ export class Trie extends IterableElementBase<string, Trie> {
87
142
  }
88
143
 
89
144
  /**
90
- * Time Complexity: O(M), where M is the length of the word being added.
91
- * Space Complexity: O(M) - Each character in the word adds a TrieNode.
145
+ * Time Complexity: O(l), where l is the length of the word being added.
146
+ * Space Complexity: O(l) - Each character in the word adds a TrieNode.
92
147
  */
93
148
 
94
149
  /**
95
- * Time Complexity: O(M), where M is the length of the word being added.
96
- * Space Complexity: O(M) - Each character in the word adds a TrieNode.
150
+ * Time Complexity: O(l), where l is the length of the word being added.
151
+ * Space Complexity: O(l) - Each character in the word adds a TrieNode.
97
152
  *
98
153
  * Add a word to the Trie structure.
99
154
  * @param {string} word - The word to add.
@@ -120,12 +175,12 @@ export class Trie extends IterableElementBase<string, Trie> {
120
175
  }
121
176
 
122
177
  /**
123
- * Time Complexity: O(M), where M is the length of the input word.
178
+ * Time Complexity: O(l), where l is the length of the input word.
124
179
  * Space Complexity: O(1) - Constant space.
125
180
  */
126
181
 
127
182
  /**
128
- * Time Complexity: O(M), where M is the length of the input word.
183
+ * Time Complexity: O(l), where l is the length of the input word.
129
184
  * Space Complexity: O(1) - Constant space.
130
185
  *
131
186
  * Check if the Trie contains a given word.
@@ -144,6 +199,14 @@ export class Trie extends IterableElementBase<string, Trie> {
144
199
  }
145
200
 
146
201
  /**
202
+ * Time Complexity: O(1)
203
+ * Space Complexity: O(1)
204
+ */
205
+
206
+ /**
207
+ * Time Complexity: O(1)
208
+ * Space Complexity: O(1)
209
+ *
147
210
  * The isEmpty function checks if the size of the queue is 0.
148
211
  * @return True if the size of the queue is 0
149
212
  */
@@ -152,13 +215,29 @@ export class Trie extends IterableElementBase<string, Trie> {
152
215
  }
153
216
 
154
217
  /**
155
- * Time Complexity: O(M), where M is the length of the word being deleted.
156
- * Space Complexity: O(M) - Due to the recursive DFS approach.
218
+ * Time Complexity: O(1)
219
+ * Space Complexity: O(1)
157
220
  */
158
221
 
159
222
  /**
160
- * Time Complexity: O(M), where M is the length of the word being deleted.
161
- * Space Complexity: O(M) - Due to the recursive DFS approach.
223
+ * Time Complexity: O(1)
224
+ * Space Complexity: O(1)
225
+ *
226
+ * The clear function resets the size of the Trie to 0 and creates a new root TrieNode.
227
+ */
228
+ clear(): void {
229
+ this._size = 0;
230
+ this._root = new TrieNode('');
231
+ }
232
+
233
+ /**
234
+ * Time Complexity: O(l), where l is the length of the word being deleted.
235
+ * Space Complexity: O(n) - Due to the recursive DFS approach.
236
+ */
237
+
238
+ /**
239
+ * Time Complexity: O(l), where l is the length of the word being deleted.
240
+ * Space Complexity: O(n) - Due to the recursive DFS approach.
162
241
  *
163
242
  * Remove a word from the Trie structure.
164
243
  * @param{string} word - The word to delete.
@@ -201,12 +280,12 @@ export class Trie extends IterableElementBase<string, Trie> {
201
280
  }
202
281
 
203
282
  /**
204
- * Time Complexity: O(N), where N is the total number of nodes in the trie.
283
+ * Time Complexity: O(n), where n is the total number of nodes in the trie.
205
284
  * Space Complexity: O(1) - Constant space.
206
285
  */
207
286
 
208
287
  /**
209
- * Time Complexity: O(N), where N is the total number of nodes in the trie.
288
+ * Time Complexity: O(n), where n is the total number of nodes in the trie.
210
289
  * Space Complexity: O(1) - Constant space.
211
290
  *
212
291
  */
@@ -231,12 +310,12 @@ export class Trie extends IterableElementBase<string, Trie> {
231
310
  }
232
311
 
233
312
  /**
234
- * Time Complexity: O(M), where M is the length of the input prefix.
313
+ * Time Complexity: O(l), where l is the length of the input prefix.
235
314
  * Space Complexity: O(1) - Constant space.
236
315
  */
237
316
 
238
317
  /**
239
- * Time Complexity: O(M), where M is the length of the input prefix.
318
+ * Time Complexity: O(l), where l is the length of the input prefix.
240
319
  * Space Complexity: O(1) - Constant space.
241
320
  *
242
321
  * Check if a given input string has an absolute prefix in the Trie, meaning it's not a complete word.
@@ -255,12 +334,12 @@ export class Trie extends IterableElementBase<string, Trie> {
255
334
  }
256
335
 
257
336
  /**
258
- * Time Complexity: O(M), where M is the length of the input prefix.
337
+ * Time Complexity: O(l), where l is the length of the input prefix.
259
338
  * Space Complexity: O(1) - Constant space.
260
339
  */
261
340
 
262
341
  /**
263
- * Time Complexity: O(M), where M is the length of the input prefix.
342
+ * Time Complexity: O(l), where l is the length of the input prefix.
264
343
  * Space Complexity: O(1) - Constant space.
265
344
  *
266
345
  * 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.
@@ -279,13 +358,13 @@ export class Trie extends IterableElementBase<string, Trie> {
279
358
  }
280
359
 
281
360
  /**
282
- * Time Complexity: O(N), where N is the total number of nodes in the trie.
283
- * Space Complexity: O(M), where M is the length of the input prefix.
361
+ * Time Complexity: O(n), where n is the total number of nodes in the trie.
362
+ * Space Complexity: O(l), where l is the length of the input prefix.
284
363
  */
285
364
 
286
365
  /**
287
- * Time Complexity: O(N), where N is the total number of nodes in the trie.
288
- * Space Complexity: O(M), where M is the length of the input prefix.
366
+ * Time Complexity: O(n), where n is the total number of nodes in the trie.
367
+ * Space Complexity: O(l), where l is the length of the input prefix.
289
368
  *
290
369
  * Check if the input string is a common prefix in the Trie, meaning it's a prefix shared by all words in the Trie.
291
370
  * @param {string} input - The input string representing the common prefix to check for.
@@ -306,13 +385,13 @@ export class Trie extends IterableElementBase<string, Trie> {
306
385
  }
307
386
 
308
387
  /**
309
- * Time Complexity: O(N), where N is the total number of nodes in the trie.
310
- * Space Complexity: O(M), where M is the length of the longest common prefix.
388
+ * Time Complexity: O(n), where n is the total number of nodes in the trie.
389
+ * Space Complexity: O(l), where l is the length of the longest common prefix.
311
390
  */
312
391
 
313
392
  /**
314
- * Time Complexity: O(N), where N is the total number of nodes in the trie.
315
- * Space Complexity: O(M), where M is the length of the longest common prefix.
393
+ * Time Complexity: O(n), where n is the total number of nodes in the trie.
394
+ * Space Complexity: O(l), where l is the length of the longest common prefix.
316
395
  *
317
396
  * Get the longest common prefix among all the words stored in the Trie.
318
397
  * @returns {string} The longest common prefix found in the Trie.
@@ -330,13 +409,13 @@ export class Trie extends IterableElementBase<string, Trie> {
330
409
  }
331
410
 
332
411
  /**
333
- * Time Complexity: O(K * L), where K is the number of words retrieved, and L is the average length of the words.
334
- * Space Complexity: O(K * L) - The space required for the output array.
412
+ * Time Complexity: O(w * l), where w is the number of words retrieved, and l is the average length of the words.
413
+ * Space Complexity: O(w * l) - The space required for the output array.
335
414
  */
336
415
 
337
416
  /**
338
- * Time Complexity: O(K * L), where K is the number of words retrieved, and L is the average length of the words.
339
- * Space Complexity: O(K * L) - The space required for the output array.
417
+ * Time Complexity: O(w * l), where w is the number of words retrieved, and l is the average length of the words.
418
+ * Space Complexity: O(w * l) - The space required for the output array.
340
419
  *
341
420
  * The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
342
421
  * @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
@@ -454,6 +533,18 @@ export class Trie extends IterableElementBase<string, Trie> {
454
533
  return newTrie;
455
534
  }
456
535
 
536
+ /**
537
+ * Time Complexity: O(n)
538
+ * Space Complexity: O(n)
539
+ */
540
+
541
+ /**
542
+ * Time Complexity: O(n)
543
+ * Space Complexity: O(n)
544
+ *
545
+ * The function `_getIterator` returns an iterable iterator that performs a depth-first search on a
546
+ * trie data structure and yields all the paths to the end nodes.
547
+ */
457
548
  protected* _getIterator(): IterableIterator<string> {
458
549
  function* _dfs(node: TrieNode, path: string): IterableIterator<string> {
459
550
  if (node.isEnd) {
@@ -468,12 +559,12 @@ export class Trie extends IterableElementBase<string, Trie> {
468
559
  }
469
560
 
470
561
  /**
471
- * Time Complexity: O(M), where M is the length of the input string.
562
+ * Time Complexity: O(l), where l is the length of the input string.
472
563
  * Space Complexity: O(1) - Constant space.
473
564
  */
474
565
 
475
566
  /**
476
- * Time Complexity: O(M), where M is the length of the input string.
567
+ * Time Complexity: O(l), where l is the length of the input string.
477
568
  * Space Complexity: O(1) - Constant space.
478
569
  *
479
570
  * @param str