min-heap-typed 1.50.0 → 1.50.2

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 (67) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +114 -9
  2. package/dist/data-structures/base/iterable-base.js +143 -7
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +43 -46
  4. package/dist/data-structures/binary-tree/avl-tree.js +68 -71
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
  6. package/dist/data-structures/binary-tree/binary-tree.js +484 -376
  7. package/dist/data-structures/binary-tree/bst.d.ts +54 -74
  8. package/dist/data-structures/binary-tree/bst.js +30 -71
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +78 -60
  10. package/dist/data-structures/binary-tree/rb-tree.js +84 -89
  11. package/dist/data-structures/binary-tree/tree-multimap.d.ts +37 -56
  12. package/dist/data-structures/binary-tree/tree-multimap.js +64 -85
  13. package/dist/data-structures/graph/abstract-graph.d.ts +1 -0
  14. package/dist/data-structures/graph/abstract-graph.js +3 -0
  15. package/dist/data-structures/graph/directed-graph.d.ts +14 -0
  16. package/dist/data-structures/graph/directed-graph.js +26 -0
  17. package/dist/data-structures/graph/map-graph.d.ts +8 -0
  18. package/dist/data-structures/graph/map-graph.js +14 -0
  19. package/dist/data-structures/graph/undirected-graph.d.ts +16 -0
  20. package/dist/data-structures/graph/undirected-graph.js +25 -0
  21. package/dist/data-structures/hash/hash-map.d.ts +121 -15
  22. package/dist/data-structures/hash/hash-map.js +160 -25
  23. package/dist/data-structures/heap/heap.d.ts +66 -6
  24. package/dist/data-structures/heap/heap.js +66 -6
  25. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
  26. package/dist/data-structures/linked-list/doubly-linked-list.js +70 -64
  27. package/dist/data-structures/linked-list/singly-linked-list.d.ts +128 -103
  28. package/dist/data-structures/linked-list/singly-linked-list.js +130 -112
  29. package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  30. package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
  31. package/dist/data-structures/matrix/matrix.d.ts +35 -4
  32. package/dist/data-structures/matrix/matrix.js +50 -11
  33. package/dist/data-structures/queue/deque.d.ts +49 -19
  34. package/dist/data-structures/queue/deque.js +101 -47
  35. package/dist/data-structures/queue/queue.d.ts +39 -5
  36. package/dist/data-structures/queue/queue.js +47 -5
  37. package/dist/data-structures/stack/stack.d.ts +16 -0
  38. package/dist/data-structures/stack/stack.js +22 -0
  39. package/dist/data-structures/trie/trie.d.ts +38 -1
  40. package/dist/data-structures/trie/trie.js +41 -0
  41. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  42. package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
  43. package/dist/types/utils/utils.d.ts +1 -0
  44. package/package.json +2 -2
  45. package/src/data-structures/base/iterable-base.ts +172 -19
  46. package/src/data-structures/binary-tree/avl-tree.ts +97 -97
  47. package/src/data-structures/binary-tree/binary-tree.ts +674 -671
  48. package/src/data-structures/binary-tree/bst.ts +89 -131
  49. package/src/data-structures/binary-tree/rb-tree.ts +127 -155
  50. package/src/data-structures/binary-tree/tree-multimap.ts +96 -112
  51. package/src/data-structures/graph/abstract-graph.ts +4 -0
  52. package/src/data-structures/graph/directed-graph.ts +30 -0
  53. package/src/data-structures/graph/map-graph.ts +15 -0
  54. package/src/data-structures/graph/undirected-graph.ts +28 -0
  55. package/src/data-structures/hash/hash-map.ts +175 -34
  56. package/src/data-structures/heap/heap.ts +66 -6
  57. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -66
  58. package/src/data-structures/linked-list/singly-linked-list.ts +132 -114
  59. package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
  60. package/src/data-structures/matrix/matrix.ts +52 -12
  61. package/src/data-structures/queue/deque.ts +108 -49
  62. package/src/data-structures/queue/queue.ts +51 -5
  63. package/src/data-structures/stack/stack.ts +24 -0
  64. package/src/data-structures/trie/trie.ts +45 -1
  65. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  66. package/src/types/data-structures/hash/hash-map.ts +4 -3
  67. package/src/types/utils/utils.ts +2 -0
@@ -9,7 +9,7 @@ import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEnt
9
9
  import { IterationType } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
- export declare class TreeMultimapNode<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNodeNested<K, V>> extends AVLTreeNode<K, V, N> {
12
+ export declare class TreeMultimapNode<K = any, V = any, NODE extends TreeMultimapNode<K, V, NODE> = TreeMultimapNodeNested<K, V>> extends AVLTreeNode<K, V, NODE> {
13
13
  count: number;
14
14
  /**
15
15
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
@@ -26,44 +26,43 @@ export declare class TreeMultimapNode<K = any, V = any, N extends TreeMultimapNo
26
26
  /**
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
- export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>, TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>> extends AVLTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
30
- constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: TreeMultimapOptions<K>);
29
+ export declare class TreeMultimap<K = any, V = any, NODE extends TreeMultimapNode<K, V, NODE> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>, TREE extends TreeMultimap<K, V, NODE, TREE> = TreeMultimap<K, V, NODE, TreeMultimapNested<K, V, NODE>>> extends AVLTree<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
30
+ constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: TreeMultimapOptions<K>);
31
31
  private _count;
32
32
  get count(): number;
33
33
  /**
34
34
  * The function creates a new BSTNode with the given key, value, and count.
35
35
  * @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
36
36
  * distinguish one node from another in the tree.
37
- * @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
37
+ * @param {NODE} value - The `value` parameter represents the value that will be stored in the binary search tree node.
38
38
  * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
39
39
  * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
40
40
  * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
41
41
  */
42
- createNode(key: K, value?: V, count?: number): N;
42
+ createNode(key: K, value?: V, count?: number): NODE;
43
43
  createTree(options?: TreeMultimapOptions<K>): TREE;
44
44
  /**
45
- * The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
46
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
45
+ * The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
46
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, which means it
47
47
  * can be one of the following:
48
48
  * @param {V} [value] - The `value` parameter is an optional argument that represents the value
49
49
  * associated with the node. It is of type `V`, which can be any data type. If no value is provided,
50
50
  * it defaults to `undefined`.
51
51
  * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
52
52
  * times the value should be added to the node. If not provided, it defaults to 1.
53
- * @returns a node of type `N` or `undefined`.
53
+ * @returns a node of type `NODE` or `undefined`.
54
54
  */
55
- exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): N | undefined;
55
+ keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): NODE | undefined;
56
56
  /**
57
57
  * The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
58
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
58
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
59
59
  * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
60
60
  * class.
61
61
  */
62
- isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N;
62
+ isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE;
63
63
  /**
64
64
  * Time Complexity: O(log n)
65
65
  * Space Complexity: O(1)
66
- * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
67
66
  */
68
67
  /**
69
68
  * Time Complexity: O(log n)
@@ -81,47 +80,13 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
81
80
  * @returns The method is returning either the newly inserted node or `undefined` if the insertion
82
81
  * was not successful.
83
82
  */
84
- add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): boolean;
83
+ add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): boolean;
85
84
  /**
86
- * Time Complexity: O(k log n)
87
- * Space Complexity: O(1)
88
- * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
89
- */
90
- /**
91
- * Time Complexity: O(k log n)
92
- * Space Complexity: O(1)
93
- *
94
- * The function overrides the addMany method to add multiple keys, nodes, or entries to a data
95
- * structure.
96
- * @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
97
- * either keys, nodes, or entries.
98
- * @returns The method is returning an array of type `N | undefined`.
99
- */
100
- addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>): boolean[];
101
- /**
102
- * Time Complexity: O(n log n)
103
- * Space Complexity: O(n)
104
- * 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. linear space, as it creates an array to store the sorted nodes.
105
- */
106
- /**
107
- * Time Complexity: O(n log n)
108
- * Space Complexity: O(n)
109
- *
110
- * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
111
- * tree using either a recursive or iterative approach.
112
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
113
- * type of iteration to use when building the balanced binary search tree. It can have two possible
114
- * values:
115
- * @returns a boolean value.
116
- */
117
- perfectlyBalance(iterationType?: IterationType): boolean;
118
- /**
119
- * Time Complexity: O(k log n)
85
+ * Time Complexity: O(log n)
120
86
  * Space Complexity: O(1)
121
- * 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. constant space, as it doesn't use additional data structures that scale with input size.
122
87
  */
123
88
  /**
124
- * Time Complexity: O(k log n)
89
+ * Time Complexity: O(log n)
125
90
  * Space Complexity: O(1)
126
91
  *
127
92
  * The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
@@ -137,9 +102,9 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
137
102
  * being deleted. If set to true, the count of the node will not be considered and the node will be
138
103
  * deleted regardless of its count. If set to false (default), the count of the node will be
139
104
  * decremented by 1 and
140
- * @returns an array of `BinaryTreeDeleteResult<N>`.
105
+ * @returns an array of `BinaryTreeDeleteResult<NODE>`.
141
106
  */
142
- delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeleteResult<N>[];
107
+ delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeleteResult<NODE>[];
143
108
  /**
144
109
  * Time Complexity: O(1)
145
110
  * Space Complexity: O(1)
@@ -151,6 +116,22 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
151
116
  * The clear() function clears the contents of a data structure and sets the count to zero.
152
117
  */
153
118
  clear(): void;
119
+ /**
120
+ * Time Complexity: O(n log n)
121
+ * Space Complexity: O(log n)
122
+ */
123
+ /**
124
+ * Time Complexity: O(n log n)
125
+ * Space Complexity: O(log n)
126
+ *
127
+ * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
128
+ * tree using either a recursive or iterative approach.
129
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
130
+ * type of iteration to use when building the balanced binary search tree. It can have two possible
131
+ * values:
132
+ * @returns a boolean value.
133
+ */
134
+ perfectlyBalance(iterationType?: IterationType): boolean;
154
135
  /**
155
136
  * Time complexity: O(n)
156
137
  * Space complexity: O(n)
@@ -165,13 +146,13 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
165
146
  clone(): TREE;
166
147
  /**
167
148
  * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
168
- * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
169
- * which the values will be swapped. It can be of type `K`, `N`, or `undefined`.
170
- * @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
149
+ * @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node from
150
+ * which the values will be swapped. It can be of type `K`, `NODE`, or `undefined`.
151
+ * @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
171
152
  * node where the values from the source node will be swapped to.
172
153
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
173
154
  * if either `srcNode` or `destNode` is undefined.
174
155
  */
175
- protected _swapProperties(srcNode: BSTNKeyOrNode<K, N>, destNode: BSTNKeyOrNode<K, N>): N | undefined;
176
- protected _replaceNode(oldNode: N, newNode: N): N;
156
+ protected _swapProperties(srcNode: BSTNKeyOrNode<K, NODE>, destNode: BSTNKeyOrNode<K, NODE>): NODE | undefined;
157
+ protected _replaceNode(oldNode: NODE, newNode: NODE): NODE;
177
158
  }
@@ -40,7 +40,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
40
40
  * The function creates a new BSTNode with the given key, value, and count.
41
41
  * @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
42
42
  * distinguish one node from another in the tree.
43
- * @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
43
+ * @param {NODE} value - The `value` parameter represents the value that will be stored in the binary search tree node.
44
44
  * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
45
45
  * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
46
46
  * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
@@ -52,17 +52,17 @@ class TreeMultimap extends avl_tree_1.AVLTree {
52
52
  return new TreeMultimap([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
53
53
  }
54
54
  /**
55
- * The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
56
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
55
+ * The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
56
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, which means it
57
57
  * can be one of the following:
58
58
  * @param {V} [value] - The `value` parameter is an optional argument that represents the value
59
59
  * associated with the node. It is of type `V`, which can be any data type. If no value is provided,
60
60
  * it defaults to `undefined`.
61
61
  * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
62
62
  * times the value should be added to the node. If not provided, it defaults to 1.
63
- * @returns a node of type `N` or `undefined`.
63
+ * @returns a node of type `NODE` or `undefined`.
64
64
  */
65
- exemplarToNode(keyOrNodeOrEntry, value, count = 1) {
65
+ keyValueOrEntryToNode(keyOrNodeOrEntry, value, count = 1) {
66
66
  let node;
67
67
  if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
68
68
  return;
@@ -89,7 +89,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
89
89
  }
90
90
  /**
91
91
  * The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
92
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
92
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
93
93
  * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
94
94
  * class.
95
95
  */
@@ -99,7 +99,6 @@ class TreeMultimap extends avl_tree_1.AVLTree {
99
99
  /**
100
100
  * Time Complexity: O(log n)
101
101
  * Space Complexity: O(1)
102
- * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
103
102
  */
104
103
  /**
105
104
  * Time Complexity: O(log n)
@@ -118,7 +117,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
118
117
  * was not successful.
119
118
  */
120
119
  add(keyOrNodeOrEntry, value, count = 1) {
121
- const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
120
+ const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
122
121
  if (newNode === undefined)
123
122
  return false;
124
123
  const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
@@ -129,82 +128,11 @@ class TreeMultimap extends avl_tree_1.AVLTree {
129
128
  return true;
130
129
  }
131
130
  /**
132
- * Time Complexity: O(k log n)
133
- * Space Complexity: O(1)
134
- * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
135
- */
136
- /**
137
- * Time Complexity: O(k log n)
138
- * Space Complexity: O(1)
139
- *
140
- * The function overrides the addMany method to add multiple keys, nodes, or entries to a data
141
- * structure.
142
- * @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
143
- * either keys, nodes, or entries.
144
- * @returns The method is returning an array of type `N | undefined`.
145
- */
146
- addMany(keysOrNodesOrEntries) {
147
- return super.addMany(keysOrNodesOrEntries);
148
- }
149
- /**
150
- * Time Complexity: O(n log n)
151
- * Space Complexity: O(n)
152
- * 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. linear space, as it creates an array to store the sorted nodes.
153
- */
154
- /**
155
- * Time Complexity: O(n log n)
156
- * Space Complexity: O(n)
157
- *
158
- * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
159
- * tree using either a recursive or iterative approach.
160
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
161
- * type of iteration to use when building the balanced binary search tree. It can have two possible
162
- * values:
163
- * @returns a boolean value.
164
- */
165
- perfectlyBalance(iterationType = this.iterationType) {
166
- const sorted = this.dfs(node => node, 'in'), n = sorted.length;
167
- if (sorted.length < 1)
168
- return false;
169
- this.clear();
170
- if (iterationType === types_1.IterationType.RECURSIVE) {
171
- const buildBalanceBST = (l, r) => {
172
- if (l > r)
173
- return;
174
- const m = l + Math.floor((r - l) / 2);
175
- const midNode = sorted[m];
176
- this.add(midNode.key, midNode.value, midNode.count);
177
- buildBalanceBST(l, m - 1);
178
- buildBalanceBST(m + 1, r);
179
- };
180
- buildBalanceBST(0, n - 1);
181
- return true;
182
- }
183
- else {
184
- const stack = [[0, n - 1]];
185
- while (stack.length > 0) {
186
- const popped = stack.pop();
187
- if (popped) {
188
- const [l, r] = popped;
189
- if (l <= r) {
190
- const m = l + Math.floor((r - l) / 2);
191
- const midNode = sorted[m];
192
- this.add(midNode.key, midNode.value, midNode.count);
193
- stack.push([m + 1, r]);
194
- stack.push([l, m - 1]);
195
- }
196
- }
197
- }
198
- return true;
199
- }
200
- }
201
- /**
202
- * Time Complexity: O(k log n)
131
+ * Time Complexity: O(log n)
203
132
  * Space Complexity: O(1)
204
- * 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. constant space, as it doesn't use additional data structures that scale with input size.
205
133
  */
206
134
  /**
207
- * Time Complexity: O(k log n)
135
+ * Time Complexity: O(log n)
208
136
  * Space Complexity: O(1)
209
137
  *
210
138
  * The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
@@ -220,7 +148,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
220
148
  * being deleted. If set to true, the count of the node will not be considered and the node will be
221
149
  * deleted regardless of its count. If set to false (default), the count of the node will be
222
150
  * decremented by 1 and
223
- * @returns an array of `BinaryTreeDeleteResult<N>`.
151
+ * @returns an array of `BinaryTreeDeleteResult<NODE>`.
224
152
  */
225
153
  delete(identifier, callback = this._defaultOneParamCallback, ignoreCount = false) {
226
154
  var _a;
@@ -294,6 +222,57 @@ class TreeMultimap extends avl_tree_1.AVLTree {
294
222
  super.clear();
295
223
  this._count = 0;
296
224
  }
225
+ /**
226
+ * Time Complexity: O(n log n)
227
+ * Space Complexity: O(log n)
228
+ */
229
+ /**
230
+ * Time Complexity: O(n log n)
231
+ * Space Complexity: O(log n)
232
+ *
233
+ * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
234
+ * tree using either a recursive or iterative approach.
235
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
236
+ * type of iteration to use when building the balanced binary search tree. It can have two possible
237
+ * values:
238
+ * @returns a boolean value.
239
+ */
240
+ perfectlyBalance(iterationType = this.iterationType) {
241
+ const sorted = this.dfs(node => node, 'in'), n = sorted.length;
242
+ if (sorted.length < 1)
243
+ return false;
244
+ this.clear();
245
+ if (iterationType === types_1.IterationType.RECURSIVE) {
246
+ const buildBalanceBST = (l, r) => {
247
+ if (l > r)
248
+ return;
249
+ const m = l + Math.floor((r - l) / 2);
250
+ const midNode = sorted[m];
251
+ this.add(midNode.key, midNode.value, midNode.count);
252
+ buildBalanceBST(l, m - 1);
253
+ buildBalanceBST(m + 1, r);
254
+ };
255
+ buildBalanceBST(0, n - 1);
256
+ return true;
257
+ }
258
+ else {
259
+ const stack = [[0, n - 1]];
260
+ while (stack.length > 0) {
261
+ const popped = stack.pop();
262
+ if (popped) {
263
+ const [l, r] = popped;
264
+ if (l <= r) {
265
+ const m = l + Math.floor((r - l) / 2);
266
+ const midNode = sorted[m];
267
+ this.add(midNode.key, midNode.value, midNode.count);
268
+ stack.push([m + 1, r]);
269
+ stack.push([l, m - 1]);
270
+ }
271
+ }
272
+ }
273
+ return true;
274
+ }
275
+ }
297
276
  /**
298
277
  * Time complexity: O(n)
299
278
  * Space complexity: O(n)
@@ -312,9 +291,9 @@ class TreeMultimap extends avl_tree_1.AVLTree {
312
291
  }
313
292
  /**
314
293
  * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
315
- * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
316
- * which the values will be swapped. It can be of type `K`, `N`, or `undefined`.
317
- * @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
294
+ * @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node from
295
+ * which the values will be swapped. It can be of type `K`, `NODE`, or `undefined`.
296
+ * @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
318
297
  * node where the values from the source node will be swapped to.
319
298
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
320
299
  * if either `srcNode` or `destNode` is undefined.
@@ -40,6 +40,7 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
40
40
  constructor();
41
41
  protected _vertexMap: Map<VertexKey, VO>;
42
42
  get vertexMap(): Map<VertexKey, VO>;
43
+ set vertexMap(v: Map<VertexKey, VO>);
43
44
  /**
44
45
  * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
45
46
  * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
@@ -47,6 +47,9 @@ class AbstractGraph extends base_1.IterableEntryBase {
47
47
  get vertexMap() {
48
48
  return this._vertexMap;
49
49
  }
50
+ set vertexMap(v) {
51
+ this._vertexMap = v;
52
+ }
50
53
  /**
51
54
  * Time Complexity: O(1) - Constant time for Map lookup.
52
55
  * Space Complexity: O(1) - Constant space, as it creates only a few variables.
@@ -41,8 +41,10 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
41
41
  constructor();
42
42
  protected _outEdgeMap: Map<VO, EO[]>;
43
43
  get outEdgeMap(): Map<VO, EO[]>;
44
+ set outEdgeMap(v: Map<VO, EO[]>);
44
45
  protected _inEdgeMap: Map<VO, EO[]>;
45
46
  get inEdgeMap(): Map<VO, EO[]>;
47
+ set inEdgeMap(v: Map<VO, EO[]>);
46
48
  /**
47
49
  * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
48
50
  * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
@@ -327,6 +329,18 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
327
329
  * graph. If the edge does not exist, it returns `undefined`.
328
330
  */
329
331
  getEndsOfEdge(edge: EO): [VO, VO] | undefined;
332
+ /**
333
+ * The isEmpty function checks if the graph is empty.
334
+ *
335
+ * @return A boolean value
336
+ */
337
+ isEmpty(): boolean;
338
+ /**
339
+ * The clone function creates a new DirectedGraph object with the same vertices and edges as the original.
340
+ *
341
+ * @return A new instance of the directedgraph class
342
+ */
343
+ clone(): DirectedGraph<V, E, VO, EO>;
330
344
  /**
331
345
  * Time Complexity: O(1)
332
346
  * Space Complexity: O(1)
@@ -47,9 +47,15 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
47
47
  get outEdgeMap() {
48
48
  return this._outEdgeMap;
49
49
  }
50
+ set outEdgeMap(v) {
51
+ this._outEdgeMap = v;
52
+ }
50
53
  get inEdgeMap() {
51
54
  return this._inEdgeMap;
52
55
  }
56
+ set inEdgeMap(v) {
57
+ this._inEdgeMap = v;
58
+ }
53
59
  /**
54
60
  * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
55
61
  * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
@@ -527,6 +533,26 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
527
533
  return undefined;
528
534
  }
529
535
  }
536
+ /**
537
+ * The isEmpty function checks if the graph is empty.
538
+ *
539
+ * @return A boolean value
540
+ */
541
+ isEmpty() {
542
+ return this.vertexMap.size === 0 && this.inEdgeMap.size === 0 && this.outEdgeMap.size === 0;
543
+ }
544
+ /**
545
+ * The clone function creates a new DirectedGraph object with the same vertices and edges as the original.
546
+ *
547
+ * @return A new instance of the directedgraph class
548
+ */
549
+ clone() {
550
+ const cloned = new DirectedGraph();
551
+ cloned.vertexMap = new Map(this.vertexMap);
552
+ cloned.inEdgeMap = new Map(this.inEdgeMap);
553
+ cloned.outEdgeMap = new Map(this.outEdgeMap);
554
+ return cloned;
555
+ }
530
556
  /**
531
557
  * Time Complexity: O(1)
532
558
  * Space Complexity: O(1)
@@ -70,4 +70,12 @@ export declare class MapGraph<V = any, E = any, VO extends MapVertex<V> = MapVer
70
70
  * @returns a new instance of the `MapEdge` class, cast as type `EO`.
71
71
  */
72
72
  createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO;
73
+ /**
74
+ * The override function is used to override the default behavior of a function.
75
+ * In this case, we are overriding the clone() function from Graph&lt;V, E&gt;.
76
+ * The clone() function returns a new graph that is an exact copy of the original graph.
77
+ *
78
+ * @return A mapgraph&lt;v, e, vo, eo&gt;
79
+ */
80
+ clone(): MapGraph<V, E, VO, EO>;
73
81
  }
@@ -89,5 +89,19 @@ class MapGraph extends directed_graph_1.DirectedGraph {
89
89
  createEdge(src, dest, weight, value) {
90
90
  return new MapEdge(src, dest, weight, value);
91
91
  }
92
+ /**
93
+ * The override function is used to override the default behavior of a function.
94
+ * In this case, we are overriding the clone() function from Graph&lt;V, E&gt;.
95
+ * The clone() function returns a new graph that is an exact copy of the original graph.
96
+ *
97
+ * @return A mapgraph&lt;v, e, vo, eo&gt;
98
+ */
99
+ clone() {
100
+ const cloned = new MapGraph(this.originCoord, this.bottomRight);
101
+ cloned.vertexMap = new Map(this.vertexMap);
102
+ cloned.inEdgeMap = new Map(this.inEdgeMap);
103
+ cloned.outEdgeMap = new Map(this.outEdgeMap);
104
+ return cloned;
105
+ }
92
106
  }
93
107
  exports.MapGraph = MapGraph;
@@ -39,6 +39,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
39
39
  constructor();
40
40
  protected _edgeMap: Map<VO, EO[]>;
41
41
  get edgeMap(): Map<VO, EO[]>;
42
+ set edgeMap(v: Map<VO, EO[]>);
42
43
  /**
43
44
  * The function creates a new vertex with an optional value and returns it.
44
45
  * @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
@@ -193,6 +194,21 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
193
194
  * graph. If the edge does not exist, it returns `undefined`.
194
195
  */
195
196
  getEndsOfEdge(edge: EO): [VO, VO] | undefined;
197
+ /**
198
+ * The isEmpty function checks if the graph is empty.
199
+ * @return True if the graph is empty and false otherwise
200
+ */
201
+ isEmpty(): boolean;
202
+ /**
203
+ * The clone function creates a new UndirectedGraph object and copies the
204
+ * vertexMap and edgeMap from this graph to the new one. This is done by
205
+ * assigning each of these properties to their respective counterparts in the
206
+ * cloned graph. The clone function returns a reference to this newly created,
207
+ * cloned UndirectedGraph object.
208
+ *
209
+ * @return A new instance of the undirectedgraph class
210
+ */
211
+ clone(): UndirectedGraph<V, E, VO, EO>;
196
212
  /**
197
213
  * Time Complexity: O(1)
198
214
  * Space Complexity: O(1)
@@ -44,6 +44,9 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
44
44
  get edgeMap() {
45
45
  return this._edgeMap;
46
46
  }
47
+ set edgeMap(v) {
48
+ this._edgeMap = v;
49
+ }
47
50
  /**
48
51
  * The function creates a new vertex with an optional value and returns it.
49
52
  * @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
@@ -325,6 +328,28 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
325
328
  return undefined;
326
329
  }
327
330
  }
331
+ /**
332
+ * The isEmpty function checks if the graph is empty.
333
+ * @return True if the graph is empty and false otherwise
334
+ */
335
+ isEmpty() {
336
+ return this.vertexMap.size === 0 && this.edgeMap.size === 0;
337
+ }
338
+ /**
339
+ * The clone function creates a new UndirectedGraph object and copies the
340
+ * vertexMap and edgeMap from this graph to the new one. This is done by
341
+ * assigning each of these properties to their respective counterparts in the
342
+ * cloned graph. The clone function returns a reference to this newly created,
343
+ * cloned UndirectedGraph object.
344
+ *
345
+ * @return A new instance of the undirectedgraph class
346
+ */
347
+ clone() {
348
+ const cloned = new UndirectedGraph();
349
+ cloned.vertexMap = new Map(this.vertexMap);
350
+ cloned.edgeMap = new Map(this.edgeMap);
351
+ return cloned;
352
+ }
328
353
  /**
329
354
  * Time Complexity: O(1)
330
355
  * Space Complexity: O(1)