data-structure-typed 1.47.5 → 1.47.6

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 (139) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
  3. package/dist/cjs/data-structures/binary-tree/avl-tree.js +23 -15
  4. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  5. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.js +66 -82
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/bst.d.ts +38 -37
  9. package/dist/cjs/data-structures/binary-tree/bst.js +56 -40
  10. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
  12. package/dist/cjs/data-structures/binary-tree/rb-tree.js +26 -17
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
  15. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +31 -22
  16. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  17. package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
  18. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  19. package/dist/cjs/data-structures/heap/heap.d.ts +19 -21
  20. package/dist/cjs/data-structures/heap/heap.js +52 -34
  21. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  22. package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
  23. package/dist/cjs/data-structures/heap/max-heap.js +2 -2
  24. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  25. package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
  26. package/dist/cjs/data-structures/heap/min-heap.js +2 -2
  27. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  28. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  29. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +9 -1
  30. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  31. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  32. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +8 -1
  33. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  34. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  35. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
  36. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  37. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  38. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
  39. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  40. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
  41. package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
  42. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  43. package/dist/cjs/data-structures/queue/deque.d.ts +1 -0
  44. package/dist/cjs/data-structures/queue/deque.js +3 -0
  45. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  46. package/dist/cjs/data-structures/queue/queue.d.ts +1 -0
  47. package/dist/cjs/data-structures/queue/queue.js +3 -0
  48. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  49. package/dist/cjs/data-structures/stack/stack.d.ts +2 -1
  50. package/dist/cjs/data-structures/stack/stack.js +10 -2
  51. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  52. package/dist/cjs/interfaces/binary-tree.d.ts +3 -1
  53. package/dist/cjs/types/common.d.ts +2 -0
  54. package/dist/cjs/types/common.js.map +1 -1
  55. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  56. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  57. package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
  58. package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  59. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
  60. package/dist/mjs/data-structures/binary-tree/avl-tree.js +26 -16
  61. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
  62. package/dist/mjs/data-structures/binary-tree/binary-tree.js +66 -83
  63. package/dist/mjs/data-structures/binary-tree/bst.d.ts +38 -37
  64. package/dist/mjs/data-structures/binary-tree/bst.js +59 -41
  65. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
  66. package/dist/mjs/data-structures/binary-tree/rb-tree.js +30 -19
  67. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
  68. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +34 -23
  69. package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
  70. package/dist/mjs/data-structures/heap/heap.d.ts +19 -21
  71. package/dist/mjs/data-structures/heap/heap.js +53 -35
  72. package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
  73. package/dist/mjs/data-structures/heap/max-heap.js +2 -2
  74. package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
  75. package/dist/mjs/data-structures/heap/min-heap.js +2 -2
  76. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  77. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +9 -1
  78. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  79. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +8 -1
  80. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  81. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
  82. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  83. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
  84. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
  85. package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
  86. package/dist/mjs/data-structures/queue/deque.d.ts +1 -0
  87. package/dist/mjs/data-structures/queue/deque.js +3 -0
  88. package/dist/mjs/data-structures/queue/queue.d.ts +1 -0
  89. package/dist/mjs/data-structures/queue/queue.js +3 -0
  90. package/dist/mjs/data-structures/stack/stack.d.ts +2 -1
  91. package/dist/mjs/data-structures/stack/stack.js +10 -2
  92. package/dist/mjs/interfaces/binary-tree.d.ts +3 -1
  93. package/dist/mjs/types/common.d.ts +2 -0
  94. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  95. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  96. package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
  97. package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  98. package/dist/umd/data-structure-typed.js +307 -229
  99. package/dist/umd/data-structure-typed.min.js +2 -2
  100. package/dist/umd/data-structure-typed.min.js.map +1 -1
  101. package/package.json +1 -1
  102. package/src/data-structures/binary-tree/avl-tree.ts +27 -17
  103. package/src/data-structures/binary-tree/binary-tree.ts +114 -97
  104. package/src/data-structures/binary-tree/bst.ts +67 -47
  105. package/src/data-structures/binary-tree/rb-tree.ts +34 -20
  106. package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
  107. package/src/data-structures/graph/abstract-graph.ts +1 -1
  108. package/src/data-structures/heap/heap.ts +57 -39
  109. package/src/data-structures/heap/max-heap.ts +5 -5
  110. package/src/data-structures/heap/min-heap.ts +5 -5
  111. package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
  112. package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
  113. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  114. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  115. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  116. package/src/data-structures/queue/deque.ts +4 -0
  117. package/src/data-structures/queue/queue.ts +4 -0
  118. package/src/data-structures/stack/stack.ts +12 -3
  119. package/src/interfaces/binary-tree.ts +13 -1
  120. package/src/types/common.ts +5 -1
  121. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  122. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  123. package/src/types/data-structures/heap/heap.ts +3 -1
  124. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
  125. package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
  126. package/test/performance/data-structures/heap/heap.test.ts +2 -2
  127. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
  128. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
  129. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -6
  130. package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
  131. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +1 -1
  132. package/test/unit/data-structures/heap/heap.test.ts +2 -2
  133. package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
  134. package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
  135. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
  136. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
  137. package/test/unit/data-structures/stack/stack.test.ts +2 -2
  138. package/test/unit/unrestricted-interconversion.test.ts +100 -0
  139. package/test/integration/conversion.test.ts +0 -0
@@ -6,7 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type { BTNKey, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
9
- import { BiTreeDeleteResult, BTNCallback, IterationType, TreeMultimapNested } from '../../types';
9
+ import { BiTreeDeleteResult, BTNCallback, IterableEntriesOrKeys, IterationType, TreeMultimapNested } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
12
  export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>> extends AVLTreeNode<V, N> {
@@ -27,14 +27,13 @@ export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N>
27
27
  * The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
28
28
  */
29
29
  export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNode<V, TreeMultimapNodeNested<V>>, TREE extends TreeMultimap<V, N, TREE> = TreeMultimap<V, N, TreeMultimapNested<V, N>>> extends AVLTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
30
- options: TreeMultimapOptions;
31
30
  /**
32
31
  * The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
33
32
  * merge duplicated values.
34
33
  * @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
35
34
  * TreeMultimap.
36
35
  */
37
- constructor(options?: TreeMultimapOptions);
36
+ constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<TreeMultimapOptions>);
38
37
  private _count;
39
38
  get count(): number;
40
39
  /**
@@ -48,10 +47,6 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
48
47
  */
49
48
  createNode(key: BTNKey, value?: V, count?: number): N;
50
49
  createTree(options?: TreeMultimapOptions): TREE;
51
- /**
52
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
53
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
54
- */
55
50
  /**
56
51
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
57
52
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -68,8 +63,8 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
68
63
  */
69
64
  add(keyOrNode: BTNKey | N | null | undefined, value?: V, count?: number): N | undefined;
70
65
  /**
71
- * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
72
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
66
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
67
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
73
68
  */
74
69
  /**
75
70
  * Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
@@ -86,8 +81,8 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
86
81
  */
87
82
  addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: V[]): (N | undefined)[];
88
83
  /**
89
- * Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
90
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
84
+ * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
85
+ * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
91
86
  */
92
87
  /**
93
88
  * Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
@@ -100,10 +95,10 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
100
95
  * values:
101
96
  * @returns a boolean value.
102
97
  */
103
- perfectlyBalance(iterationType?: IterationType | undefined): boolean;
98
+ perfectlyBalance(iterationType?: IterationType): boolean;
104
99
  /**
105
- * Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
106
- * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
100
+ * Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
101
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
107
102
  */
108
103
  /**
109
104
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
@@ -126,13 +121,18 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
126
121
  */
127
122
  delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BiTreeDeleteResult<N>[];
128
123
  /**
129
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
130
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
124
+ * Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
125
+ * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
131
126
  */
132
127
  /**
133
128
  * The clear() function clears the contents of a data structure and sets the count to zero.
134
129
  */
135
130
  clear(): void;
131
+ /**
132
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
133
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
134
+ */
135
+ init(elements: IterableEntriesOrKeys<V>): void;
136
136
  /**
137
137
  * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
138
138
  * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
@@ -21,21 +21,16 @@ export class TreeMultimapNode extends AVLTreeNode {
21
21
  * The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
22
22
  */
23
23
  export class TreeMultimap extends AVLTree {
24
- options;
25
24
  /**
26
25
  * The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
27
26
  * merge duplicated values.
28
27
  * @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
29
28
  * TreeMultimap.
30
29
  */
31
- constructor(options = { iterationType: IterationType.ITERATIVE }) {
32
- super(options);
33
- if (options) {
34
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b, ...options };
35
- }
36
- else {
37
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
38
- }
30
+ constructor(elements, options) {
31
+ super([], options);
32
+ if (elements)
33
+ this.init(elements);
39
34
  }
40
35
  _count = 0;
41
36
  get count() {
@@ -54,12 +49,11 @@ export class TreeMultimap extends AVLTree {
54
49
  return new TreeMultimapNode(key, value, count);
55
50
  }
56
51
  createTree(options) {
57
- return new TreeMultimap({ ...this.options, ...options });
52
+ return new TreeMultimap([], {
53
+ iterationType: this.iterationType,
54
+ comparator: this.comparator, ...options
55
+ });
58
56
  }
59
- /**
60
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
61
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
62
- */
63
57
  /**
64
58
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
65
59
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -154,8 +148,8 @@ export class TreeMultimap extends AVLTree {
154
148
  return inserted;
155
149
  }
156
150
  /**
157
- * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
158
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
151
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
152
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
159
153
  */
160
154
  /**
161
155
  * Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
@@ -187,8 +181,8 @@ export class TreeMultimap extends AVLTree {
187
181
  return inserted;
188
182
  }
189
183
  /**
190
- * Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
191
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
184
+ * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
185
+ * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
192
186
  */
193
187
  /**
194
188
  * Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
@@ -201,7 +195,7 @@ export class TreeMultimap extends AVLTree {
201
195
  * values:
202
196
  * @returns a boolean value.
203
197
  */
204
- perfectlyBalance(iterationType = this.options.iterationType) {
198
+ perfectlyBalance(iterationType = this.iterationType) {
205
199
  const sorted = this.dfs(node => node, 'in'), n = sorted.length;
206
200
  if (sorted.length < 1)
207
201
  return false;
@@ -238,8 +232,8 @@ export class TreeMultimap extends AVLTree {
238
232
  }
239
233
  }
240
234
  /**
241
- * Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
242
- * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
235
+ * Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
236
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
243
237
  */
244
238
  /**
245
239
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
@@ -318,8 +312,8 @@ export class TreeMultimap extends AVLTree {
318
312
  return deletedResult;
319
313
  }
320
314
  /**
321
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
322
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
315
+ * Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
316
+ * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
323
317
  */
324
318
  /**
325
319
  * The clear() function clears the contents of a data structure and sets the count to zero.
@@ -328,6 +322,23 @@ export class TreeMultimap extends AVLTree {
328
322
  super.clear();
329
323
  this._count = 0;
330
324
  }
325
+ /**
326
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
327
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
328
+ */
329
+ init(elements) {
330
+ if (elements) {
331
+ for (const entryOrKey of elements) {
332
+ if (Array.isArray(entryOrKey)) {
333
+ const [key, value] = entryOrKey;
334
+ this.add(key, value);
335
+ }
336
+ else {
337
+ this.add(entryOrKey);
338
+ }
339
+ }
340
+ }
341
+ }
331
342
  /**
332
343
  * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
333
344
  * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
@@ -589,7 +589,7 @@ export class AbstractGraph {
589
589
  if (vertexOrKey instanceof AbstractVertex)
590
590
  distMap.set(vertexOrKey, Infinity);
591
591
  }
592
- const heap = new PriorityQueue({ comparator: (a, b) => a.key - b.key });
592
+ const heap = new PriorityQueue([], { comparator: (a, b) => a.key - b.key });
593
593
  heap.add({ key: 0, value: srcVertex });
594
594
  distMap.set(srcVertex, 0);
595
595
  preMap.set(srcVertex, null);
@@ -5,15 +5,12 @@
5
5
  * @license MIT License
6
6
  */
7
7
  import type { Comparator, DFSOrderPattern } from '../../types';
8
+ import { HeapOptions } from "../../types";
8
9
  export declare class Heap<E = any> {
9
- constructor(options: {
10
- comparator: Comparator<E>;
11
- elements?: E[];
12
- });
10
+ options: HeapOptions<E>;
11
+ constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
13
12
  protected _elements: E[];
14
13
  get elements(): E[];
15
- protected _comparator: Comparator<E>;
16
- get comparator(): Comparator<E>;
17
14
  /**
18
15
  * Get the size (number of elements) of the heap.
19
16
  */
@@ -26,10 +23,10 @@ export declare class Heap<E = any> {
26
23
  /**
27
24
  * Static method that creates a binary heap from an array of elements and a comparison function.
28
25
  * @returns A new Heap instance.
26
+ * @param elements
29
27
  * @param options
30
28
  */
31
- static heapify<E>(options: {
32
- elements: E[];
29
+ static heapify<E>(elements: Iterable<E>, options: {
33
30
  comparator: Comparator<E>;
34
31
  }): Heap<E>;
35
32
  /**
@@ -204,6 +201,11 @@ export declare class Heap<E = any> {
204
201
  * Time Complexity: O(log n)
205
202
  * Space Complexity: O(1)
206
203
  */
204
+ print(): void;
205
+ /**
206
+ * Time Complexity: O(n)
207
+ * Space Complexity: O(1)
208
+ */
207
209
  /**
208
210
  * Time Complexity: O(log n)
209
211
  * Space Complexity: O(1)
@@ -212,10 +214,6 @@ export declare class Heap<E = any> {
212
214
  * @param index - The index of the newly added element.
213
215
  */
214
216
  protected _bubbleUp(index: number): void;
215
- /**
216
- * Time Complexity: O(n)
217
- * Space Complexity: O(1)
218
- */
219
217
  /**
220
218
  * Time Complexity: O(log n)
221
219
  * Space Complexity: O(1)
@@ -349,18 +347,18 @@ export declare class FibonacciHeap<E> {
349
347
  */
350
348
  merge(heapToMerge: FibonacciHeap<E>): void;
351
349
  /**
352
- * Default comparator function used by the heap.
353
- * @param {E} a
354
- * @param {E} b
350
+ * Create a new node.
351
+ * @param element
355
352
  * @protected
356
353
  */
357
- protected defaultComparator(a: E, b: E): number;
354
+ createNode(element: E): FibonacciHeapNode<E>;
358
355
  /**
359
- * Create a new node.
360
- * @param element
356
+ * Default comparator function used by the heap.
357
+ * @param {E} a
358
+ * @param {E} b
361
359
  * @protected
362
360
  */
363
- protected createNode(element: E): FibonacciHeapNode<E>;
361
+ protected _defaultComparator(a: E, b: E): number;
364
362
  /**
365
363
  * Time Complexity: O(1)
366
364
  * Space Complexity: O(1)
@@ -399,7 +397,7 @@ export declare class FibonacciHeap<E> {
399
397
  * @param x
400
398
  * @protected
401
399
  */
402
- protected link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
400
+ protected _link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
403
401
  /**
404
402
  * Time Complexity: O(n log n), where n is the number of elements in the heap.
405
403
  * Space Complexity: O(n)
@@ -411,5 +409,5 @@ export declare class FibonacciHeap<E> {
411
409
  * Remove and return the top element (smallest or largest element) from the heap.
412
410
  * @protected
413
411
  */
414
- protected consolidate(): void;
412
+ protected _consolidate(): void;
415
413
  }
@@ -5,21 +5,35 @@
5
5
  * @license MIT License
6
6
  */
7
7
  export class Heap {
8
- constructor(options) {
9
- this._comparator = options.comparator;
10
- if (options.elements && options.elements.length > 0) {
11
- this._elements = options.elements;
12
- this.fix();
8
+ options;
9
+ constructor(elements, options) {
10
+ const defaultComparator = (a, b) => {
11
+ if (!(typeof a === 'number' && typeof b === 'number')) {
12
+ throw new Error('The a, b params of compare function must be number');
13
+ }
14
+ else {
15
+ return a - b;
16
+ }
17
+ };
18
+ if (options) {
19
+ this.options = options;
20
+ }
21
+ else {
22
+ this.options = {
23
+ comparator: defaultComparator
24
+ };
25
+ }
26
+ if (elements) {
27
+ for (const el of elements) {
28
+ this.push(el);
29
+ }
30
+ // this.fix();
13
31
  }
14
32
  }
15
33
  _elements = [];
16
34
  get elements() {
17
35
  return this._elements;
18
36
  }
19
- _comparator;
20
- get comparator() {
21
- return this._comparator;
22
- }
23
37
  /**
24
38
  * Get the size (number of elements) of the heap.
25
39
  */
@@ -36,10 +50,11 @@ export class Heap {
36
50
  /**
37
51
  * Static method that creates a binary heap from an array of elements and a comparison function.
38
52
  * @returns A new Heap instance.
53
+ * @param elements
39
54
  * @param options
40
55
  */
41
- static heapify(options) {
42
- return new Heap(options);
56
+ static heapify(elements, options) {
57
+ return new Heap(elements, options);
43
58
  }
44
59
  /**
45
60
  * Time Complexity: O(log n), where n is the number of elements in the heap.
@@ -253,7 +268,7 @@ export class Heap {
253
268
  * @returns A new Heap instance containing the same elements.
254
269
  */
255
270
  clone() {
256
- const clonedHeap = new Heap({ comparator: this.comparator });
271
+ const clonedHeap = new Heap([], this.options);
257
272
  clonedHeap._elements = [...this.elements];
258
273
  return clonedHeap;
259
274
  }
@@ -305,7 +320,7 @@ export class Heap {
305
320
  }
306
321
  }
307
322
  filter(predicate) {
308
- const filteredHeap = new Heap({ comparator: this.comparator });
323
+ const filteredHeap = new Heap([], this.options);
309
324
  let index = 0;
310
325
  for (const el of this) {
311
326
  if (predicate(el, index, this)) {
@@ -316,7 +331,7 @@ export class Heap {
316
331
  return filteredHeap;
317
332
  }
318
333
  map(callback, comparator) {
319
- const mappedHeap = new Heap({ comparator: comparator });
334
+ const mappedHeap = new Heap([], { comparator: comparator });
320
335
  let index = 0;
321
336
  for (const el of this) {
322
337
  mappedHeap.add(callback(el, index, this));
@@ -337,6 +352,13 @@ export class Heap {
337
352
  * Time Complexity: O(log n)
338
353
  * Space Complexity: O(1)
339
354
  */
355
+ print() {
356
+ console.log([...this]);
357
+ }
358
+ /**
359
+ * Time Complexity: O(n)
360
+ * Space Complexity: O(1)
361
+ */
340
362
  /**
341
363
  * Time Complexity: O(log n)
342
364
  * Space Complexity: O(1)
@@ -349,17 +371,13 @@ export class Heap {
349
371
  while (index > 0) {
350
372
  const parent = (index - 1) >> 1;
351
373
  const parentItem = this.elements[parent];
352
- if (this._comparator(parentItem, element) <= 0)
374
+ if (this.options.comparator(parentItem, element) <= 0)
353
375
  break;
354
376
  this.elements[index] = parentItem;
355
377
  index = parent;
356
378
  }
357
379
  this.elements[index] = element;
358
380
  }
359
- /**
360
- * Time Complexity: O(n)
361
- * Space Complexity: O(1)
362
- */
363
381
  /**
364
382
  * Time Complexity: O(log n)
365
383
  * Space Complexity: O(1)
@@ -375,11 +393,11 @@ export class Heap {
375
393
  const right = left + 1;
376
394
  let minItem = this.elements[left];
377
395
  if (right < this.elements.length &&
378
- this._comparator(minItem, this.elements[right]) > 0) {
396
+ this.options.comparator(minItem, this.elements[right]) > 0) {
379
397
  left = right;
380
398
  minItem = this.elements[right];
381
399
  }
382
- if (this._comparator(minItem, element) >= 0)
400
+ if (this.options.comparator(minItem, element) >= 0)
383
401
  break;
384
402
  this.elements[index] = minItem;
385
403
  index = left;
@@ -404,7 +422,7 @@ export class FibonacciHeapNode {
404
422
  export class FibonacciHeap {
405
423
  constructor(comparator) {
406
424
  this.clear();
407
- this._comparator = comparator || this.defaultComparator;
425
+ this._comparator = comparator || this._defaultComparator;
408
426
  if (typeof this.comparator !== 'function') {
409
427
  throw new Error('FibonacciHeap constructor: given comparator should be a function.');
410
428
  }
@@ -579,7 +597,7 @@ export class FibonacciHeap {
579
597
  }
580
598
  else {
581
599
  this._min = z.right;
582
- this.consolidate();
600
+ this._consolidate();
583
601
  }
584
602
  this._size--;
585
603
  return z.element;
@@ -619,27 +637,27 @@ export class FibonacciHeap {
619
637
  // Clear the heap that was merged
620
638
  heapToMerge.clear();
621
639
  }
640
+ /**
641
+ * Create a new node.
642
+ * @param element
643
+ * @protected
644
+ */
645
+ createNode(element) {
646
+ return new FibonacciHeapNode(element);
647
+ }
622
648
  /**
623
649
  * Default comparator function used by the heap.
624
650
  * @param {E} a
625
651
  * @param {E} b
626
652
  * @protected
627
653
  */
628
- defaultComparator(a, b) {
654
+ _defaultComparator(a, b) {
629
655
  if (a < b)
630
656
  return -1;
631
657
  if (a > b)
632
658
  return 1;
633
659
  return 0;
634
660
  }
635
- /**
636
- * Create a new node.
637
- * @param element
638
- * @protected
639
- */
640
- createNode(element) {
641
- return new FibonacciHeapNode(element);
642
- }
643
661
  /**
644
662
  * Time Complexity: O(1)
645
663
  * Space Complexity: O(1)
@@ -695,7 +713,7 @@ export class FibonacciHeap {
695
713
  * @param x
696
714
  * @protected
697
715
  */
698
- link(y, x) {
716
+ _link(y, x) {
699
717
  this.removeFromRoot(y);
700
718
  y.left = y;
701
719
  y.right = y;
@@ -714,7 +732,7 @@ export class FibonacciHeap {
714
732
  * Remove and return the top element (smallest or largest element) from the heap.
715
733
  * @protected
716
734
  */
717
- consolidate() {
735
+ _consolidate() {
718
736
  const A = new Array(this.size);
719
737
  const elements = this.consumeLinkedList(this.root);
720
738
  let x, y, d, t;
@@ -728,7 +746,7 @@ export class FibonacciHeap {
728
746
  x = y;
729
747
  y = t;
730
748
  }
731
- this.link(y, x);
749
+ this._link(y, x);
732
750
  A[d] = undefined;
733
751
  d++;
734
752
  }
@@ -6,10 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { Heap } from './heap';
9
- import type { Comparator } from '../../types';
9
+ import type { HeapOptions } from '../../types';
10
10
  export declare class MaxHeap<E = any> extends Heap<E> {
11
- constructor(options?: {
12
- comparator: Comparator<E>;
13
- nodes?: E[];
14
- });
11
+ constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
15
12
  }
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import { Heap } from './heap';
9
9
  export class MaxHeap extends Heap {
10
- constructor(options = {
10
+ constructor(elements, options = {
11
11
  comparator: (a, b) => {
12
12
  if (!(typeof a === 'number' && typeof b === 'number')) {
13
13
  throw new Error('The a, b params of compare function must be number');
@@ -17,6 +17,6 @@ export class MaxHeap extends Heap {
17
17
  }
18
18
  }
19
19
  }) {
20
- super(options);
20
+ super(elements, options);
21
21
  }
22
22
  }
@@ -6,10 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { Heap } from './heap';
9
- import type { Comparator } from '../../types';
9
+ import type { HeapOptions } from '../../types';
10
10
  export declare class MinHeap<E = any> extends Heap<E> {
11
- constructor(options?: {
12
- comparator: Comparator<E>;
13
- nodes?: E[];
14
- });
11
+ constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
15
12
  }
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import { Heap } from './heap';
9
9
  export class MinHeap extends Heap {
10
- constructor(options = {
10
+ constructor(elements, options = {
11
11
  comparator: (a, b) => {
12
12
  if (!(typeof a === 'number' && typeof b === 'number')) {
13
13
  throw new Error('The a, b params of compare function must be number');
@@ -17,6 +17,6 @@ export class MinHeap extends Heap {
17
17
  }
18
18
  }
19
19
  }) {
20
- super(options);
20
+ super(elements, options);
21
21
  }
22
22
  }
@@ -20,7 +20,7 @@ export declare class DoublyLinkedList<E = any> {
20
20
  /**
21
21
  * The constructor initializes the linked list with an empty head, tail, and length.
22
22
  */
23
- constructor();
23
+ constructor(elements?: Iterable<E>);
24
24
  protected _head: DoublyLinkedListNode<E> | null;
25
25
  get head(): DoublyLinkedListNode<E> | null;
26
26
  protected _tail: DoublyLinkedListNode<E> | null;
@@ -453,4 +453,5 @@ export declare class DoublyLinkedList<E = any> {
453
453
  * elements in the linked list.
454
454
  */
455
455
  reduce<T>(callback: (accumulator: T, value: E, index: number, list: DoublyLinkedList<E>) => T, initialValue: T): T;
456
+ print(): void;
456
457
  }
@@ -24,10 +24,15 @@ export class DoublyLinkedList {
24
24
  /**
25
25
  * The constructor initializes the linked list with an empty head, tail, and length.
26
26
  */
27
- constructor() {
27
+ constructor(elements) {
28
28
  this._head = null;
29
29
  this._tail = null;
30
30
  this._length = 0;
31
+ if (elements) {
32
+ for (const el of elements) {
33
+ this.push(el);
34
+ }
35
+ }
31
36
  }
32
37
  _head;
33
38
  get head() {
@@ -766,4 +771,7 @@ export class DoublyLinkedList {
766
771
  }
767
772
  return accumulator;
768
773
  }
774
+ print() {
775
+ console.log([...this]);
776
+ }
769
777
  }
@@ -19,7 +19,7 @@ export declare class SinglyLinkedList<E = any> {
19
19
  /**
20
20
  * The constructor initializes the linked list with an empty head, tail, and length.
21
21
  */
22
- constructor();
22
+ constructor(elements?: Iterable<E>);
23
23
  protected _head: SinglyLinkedListNode<E> | null;
24
24
  get head(): SinglyLinkedListNode<E> | null;
25
25
  protected _tail: SinglyLinkedListNode<E> | null;
@@ -411,4 +411,5 @@ export declare class SinglyLinkedList<E = any> {
411
411
  * elements in the linked list.
412
412
  */
413
413
  reduce<T>(callback: (accumulator: T, value: E, index: number, list: SinglyLinkedList<E>) => T, initialValue: T): T;
414
+ print(): void;
414
415
  }