directed-graph-typed 1.53.9 → 1.54.1

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 (84) hide show
  1. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
  2. package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -189
  4. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -357
  5. package/dist/data-structures/binary-tree/avl-tree.d.ts +108 -78
  6. package/dist/data-structures/binary-tree/avl-tree.js +126 -79
  7. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
  8. package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
  9. package/dist/data-structures/binary-tree/binary-tree.d.ts +243 -190
  10. package/dist/data-structures/binary-tree/binary-tree.js +273 -229
  11. package/dist/data-structures/binary-tree/bst.d.ts +141 -122
  12. package/dist/data-structures/binary-tree/bst.js +170 -134
  13. package/dist/data-structures/binary-tree/index.d.ts +2 -0
  14. package/dist/data-structures/binary-tree/index.js +2 -0
  15. package/dist/data-structures/binary-tree/red-black-tree.d.ts +84 -80
  16. package/dist/data-structures/binary-tree/red-black-tree.js +101 -79
  17. package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
  18. package/dist/data-structures/binary-tree/tree-counter.js +444 -0
  19. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -186
  20. package/dist/data-structures/binary-tree/tree-multi-map.js +140 -388
  21. package/dist/data-structures/graph/directed-graph.d.ts +3 -0
  22. package/dist/data-structures/graph/directed-graph.js +3 -0
  23. package/dist/data-structures/graph/map-graph.d.ts +3 -0
  24. package/dist/data-structures/graph/map-graph.js +3 -0
  25. package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
  26. package/dist/data-structures/graph/undirected-graph.js +3 -0
  27. package/dist/data-structures/linked-list/singly-linked-list.d.ts +3 -0
  28. package/dist/data-structures/linked-list/singly-linked-list.js +3 -0
  29. package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
  30. package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
  31. package/dist/data-structures/matrix/matrix.d.ts +3 -0
  32. package/dist/data-structures/matrix/matrix.js +3 -0
  33. package/dist/data-structures/matrix/navigator.d.ts +3 -0
  34. package/dist/data-structures/matrix/navigator.js +3 -0
  35. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
  36. package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
  37. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
  38. package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
  39. package/dist/data-structures/trie/trie.d.ts +0 -4
  40. package/dist/data-structures/trie/trie.js +0 -4
  41. package/dist/interfaces/binary-tree.d.ts +7 -6
  42. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  43. package/dist/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
  44. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -3
  45. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -2
  46. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
  47. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
  48. package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
  49. package/dist/types/data-structures/binary-tree/index.js +2 -0
  50. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -3
  51. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  52. package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
  53. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -3
  54. package/package.json +2 -2
  55. package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
  56. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +148 -394
  57. package/src/data-structures/binary-tree/avl-tree.ts +152 -112
  58. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
  59. package/src/data-structures/binary-tree/binary-tree.ts +446 -379
  60. package/src/data-structures/binary-tree/bst.ts +224 -201
  61. package/src/data-structures/binary-tree/index.ts +2 -0
  62. package/src/data-structures/binary-tree/red-black-tree.ts +138 -114
  63. package/src/data-structures/binary-tree/tree-counter.ts +504 -0
  64. package/src/data-structures/binary-tree/tree-multi-map.ts +156 -428
  65. package/src/data-structures/graph/directed-graph.ts +3 -0
  66. package/src/data-structures/graph/map-graph.ts +3 -0
  67. package/src/data-structures/graph/undirected-graph.ts +3 -0
  68. package/src/data-structures/linked-list/singly-linked-list.ts +3 -0
  69. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  70. package/src/data-structures/matrix/matrix.ts +3 -0
  71. package/src/data-structures/matrix/navigator.ts +3 -0
  72. package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
  73. package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
  74. package/src/data-structures/trie/trie.ts +0 -4
  75. package/src/interfaces/binary-tree.ts +10 -11
  76. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
  77. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -4
  78. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -3
  79. package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
  80. package/src/types/data-structures/binary-tree/bst.ts +5 -3
  81. package/src/types/data-structures/binary-tree/index.ts +2 -0
  82. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -4
  83. package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
  84. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -4
@@ -4,439 +4,191 @@ exports.TreeMultiMap = exports.TreeMultiMapNode = void 0;
4
4
  const red_black_tree_1 = require("./red-black-tree");
5
5
  class TreeMultiMapNode extends red_black_tree_1.RedBlackTreeNode {
6
6
  /**
7
- * The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
8
- * @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
9
- * used to identify and locate the node within the tree.
10
- * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
11
- * associated with the key in the Red-Black Tree node. It is not required and can be omitted when
12
- * creating a new node.
13
- * @param [count=1] - The `count` parameter represents the number of occurrences of a particular key
14
- * in the Red-Black Tree. It is an optional parameter with a default value of 1.
15
- * @param {RBTNColor} [color=BLACK] - The `color` parameter is used to specify the color of the node
16
- * in a Red-Black Tree. It is optional and has a default value of `'BLACK'`.
17
- */
18
- constructor(key, value, count = 1, color = 'BLACK') {
19
- super(key, value, color);
20
- this._count = 1;
21
- this.count = count;
7
+ * This TypeScript constructor initializes an object with a key of type K and an array of values of
8
+ * type V.
9
+ * @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the
10
+ * data being stored in the data structure. It helps in quickly accessing or retrieving the
11
+ * associated value in the data structure.
12
+ * @param {V[]} value - The `value` parameter in the constructor represents an array of values of
13
+ * type `V`.
14
+ */
15
+ constructor(key, value) {
16
+ super(key, value);
17
+ this.parent = undefined;
18
+ this._left = undefined;
19
+ this._right = undefined;
20
+ }
21
+ get left() {
22
+ return this._left;
23
+ }
24
+ set left(v) {
25
+ if (v) {
26
+ v.parent = this;
27
+ }
28
+ this._left = v;
22
29
  }
23
- /**
24
- * The function returns the value of the private variable _count.
25
- * @returns The count property of the object, which is of type number.
26
- */
27
- get count() {
28
- return this._count;
30
+ get right() {
31
+ return this._right;
29
32
  }
30
- /**
31
- * The above function sets the value of the count property.
32
- * @param {number} value - The value parameter is of type number, which means it can accept any
33
- * numeric value.
34
- */
35
- set count(value) {
36
- this._count = value;
33
+ set right(v) {
34
+ if (v) {
35
+ v.parent = this;
36
+ }
37
+ this._right = v;
37
38
  }
38
39
  }
39
40
  exports.TreeMultiMapNode = TreeMultiMapNode;
41
+ /**
42
+ *
43
+ * @example
44
+ * // Find elements in a range
45
+ * const tmm = new TreeMultiMap<number>([10, 5, 15, 3, 7, 12, 18]);
46
+ * console.log(tmm.search(new Range(5, 10))); // [5, 10, 7]
47
+ * console.log(tmm.search(new Range(4, 12))); // [5, 10, 12, 7]
48
+ * console.log(tmm.search(new Range(15, 20))); // [15, 18]
49
+ */
40
50
  class TreeMultiMap extends red_black_tree_1.RedBlackTree {
41
51
  /**
42
- * The constructor function initializes a TreeMultiMap object with optional initial data.
43
- * @param keysNodesEntriesOrRaws - The parameter `keysNodesEntriesOrRaws` is an
44
- * iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
45
- * TreeMultiMap with initial data.
46
- * @param [options] - The `options` parameter is an optional object that can be used to customize the
47
- * behavior of the `TreeMultiMap` constructor. It can include properties such as `compareKeys` and
48
- * `compareValues`, which are functions used to compare keys and values respectively.
52
+ * The constructor initializes an TreeMultiMap with the provided keys, nodes, entries, or raw data
53
+ * and options.
54
+ * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
55
+ * iterable that can contain either key-value pairs represented as `BTNRep<K, V[],
56
+ * TreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize
57
+ * the RedBlackTreeMulti
58
+ * @param [options] - The `options` parameter in the constructor is of type
59
+ * `TreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
60
+ * additional options for configuring the TreeMultiMap instance.
49
61
  */
50
62
  constructor(keysNodesEntriesOrRaws = [], options) {
51
- super([], options);
52
- this._count = 0;
53
- if (keysNodesEntriesOrRaws)
63
+ super([], Object.assign(Object.assign({}, options), { isMapMode: true }));
64
+ if (keysNodesEntriesOrRaws) {
54
65
  this.addMany(keysNodesEntriesOrRaws);
55
- }
56
- // TODO the _count is not accurate after nodes count modified
57
- /**
58
- * The function calculates the sum of the count property of all nodes in a tree structure.
59
- * @returns the sum of the count property of all nodes in the tree.
60
- */
61
- get count() {
62
- return this._count;
66
+ }
63
67
  }
64
68
  /**
65
- * Time Complexity: O(n)
69
+ * Time Complexity: O(1)
66
70
  * Space Complexity: O(1)
67
71
  *
68
- * The function calculates the sum of the count property of all nodes in a tree using depth-first
69
- * search.
70
- * @returns the sum of the count property of all nodes in the tree.
71
- */
72
- getComputedCount() {
73
- let sum = 0;
74
- this.dfs(node => (sum += node.count));
75
- return sum;
76
- }
77
- /**
78
- * The function creates a new TreeMultiMapNode with the specified key, value, color, and count.
79
- * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
80
- * which is a generic type representing the type of keys in the tree.
81
- * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
82
- * associated with the key in the node. It is of type `V`, which can be any data type.
83
- * @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
84
- * a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
85
- * @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
86
- * the tree. It is an optional parameter and is used to keep track of the number of values associated
87
- * with a key in the tree.
88
- * @returns A new instance of the TreeMultiMapNode class, casted as NODE.
72
+ * The `createTree` function in TypeScript overrides the default implementation to create a new
73
+ * TreeMultiMap with specified options.
74
+ * @param [options] - The `options` parameter in the `createTree` method is of type
75
+ * `TreeMultiMapOptions<K, V[], R>`. This parameter allows you to pass additional configuration
76
+ * options when creating a new `TreeMultiMap` instance. It includes properties such as
77
+ * `iterationType`, `specifyComparable
78
+ * @returns A new instance of `TreeMultiMap` is being returned, with an empty array as the initial
79
+ * data and the provided options merged with the existing properties of the current object.
89
80
  */
90
- createNode(key, value, color = 'BLACK', count) {
91
- return new TreeMultiMapNode(key, this._isMapMode ? undefined : value, count, color);
92
- }
93
- /**
94
- * The function creates a new instance of a TreeMultiMap with the specified options and returns it.
95
- * @param [options] - The `options` parameter is an optional object that contains additional
96
- * configuration options for creating the `TreeMultiMap`. It is of type `TreeMultiMapOptions<K, V,
97
- * R>`.
98
- * @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
99
- * existing `iterationType` property. The returned value is casted as `TREE`.
100
- */
101
- // @ts-ignore
102
81
  createTree(options) {
103
- return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn }, options));
104
- }
105
- /**
106
- * The function checks if the input is an instance of the TreeMultiMapNode class.
107
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
108
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
109
- * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
110
- * an instance of the `TreeMultiMapNode` class.
111
- */
112
- isNode(keyNodeEntryOrRaw) {
113
- return keyNodeEntryOrRaw instanceof TreeMultiMapNode;
82
+ return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
114
83
  }
115
84
  /**
116
- * Time Complexity: O(log n)
85
+ * Time Complexity: O(1)
117
86
  * Space Complexity: O(1)
118
87
  *
119
- * The function overrides the add method of a class and adds a new node to a data structure, updating
120
- * the count and returning a boolean indicating success.
121
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
122
- * `keyNodeEntryOrRaw` parameter can accept one of the following types:
123
- * @param {V} [value] - The `value` parameter represents the value associated with the key in the
124
- * data structure. It is an optional parameter, so it can be omitted if not needed.
125
- * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
126
- * be added to the data structure. By default, it is set to 1, meaning that if no value is provided
127
- * for `count`, the key-value pair will be added once.
128
- * @returns The method is returning a boolean value. It returns true if the addition of the new node
129
- * was successful, and false otherwise.
88
+ * The function `createNode` overrides the method to create a new `TreeMultiMapNode` with a specified
89
+ * key and an empty array of values.
90
+ * @param {K} key - The `key` parameter in the `createNode` method represents the key of the node
91
+ * that will be created in the TreeMultiMap data structure.
92
+ * @returns A new instance of `TreeMultiMapNode<K, V>` is being returned, with the specified key and
93
+ * an empty array as its value.
130
94
  */
131
- add(keyNodeEntryOrRaw, value, count = 1) {
132
- const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
133
- const orgCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
134
- const isSuccessAdded = super.add(newNode, newValue);
135
- if (isSuccessAdded) {
136
- this._count += orgCount;
137
- return true;
138
- }
139
- else {
140
- return false;
141
- }
95
+ createNode(key) {
96
+ return new TreeMultiMapNode(key, []);
142
97
  }
143
98
  /**
144
99
  * Time Complexity: O(log n)
145
- * Space Complexity: O(1)
100
+ * Space Complexity: O(log n)
146
101
  *
147
- * The function `delete` in TypeScript overrides the deletion operation in a binary tree data
148
- * structure, handling cases where nodes have children and maintaining balance in the tree.
149
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `predicate`
150
- * parameter in the `delete` method is used to specify the condition or key based on which a node
151
- * should be deleted from the binary tree. It can be a key, a node, or an entry.
152
- * @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
153
- * boolean flag that determines whether to ignore the count of nodes when performing deletion. If
154
- * `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
155
- * `ignoreCount` is `false
156
- * @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<NODE>` objects.
157
- */
158
- delete(keyNodeEntryOrRaw, ignoreCount = false) {
159
- if (keyNodeEntryOrRaw === null)
160
- return [];
161
- const results = [];
162
- let nodeToDelete;
163
- if (this._isPredicate(keyNodeEntryOrRaw))
164
- nodeToDelete = this.getNode(keyNodeEntryOrRaw);
165
- else
166
- nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw);
167
- if (!nodeToDelete) {
168
- return results;
169
- }
170
- let originalColor = nodeToDelete.color;
171
- let replacementNode;
172
- if (!this.isRealNode(nodeToDelete.left)) {
173
- replacementNode = nodeToDelete.right;
174
- if (ignoreCount || nodeToDelete.count <= 1) {
175
- this._transplant(nodeToDelete, nodeToDelete.right);
176
- this._count -= nodeToDelete.count;
177
- }
178
- else {
179
- nodeToDelete.count--;
180
- this._count--;
181
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
182
- return results;
183
- }
184
- }
185
- else if (!this.isRealNode(nodeToDelete.right)) {
186
- replacementNode = nodeToDelete.left;
187
- if (ignoreCount || nodeToDelete.count <= 1) {
188
- this._transplant(nodeToDelete, nodeToDelete.left);
189
- this._count -= nodeToDelete.count;
190
- }
191
- else {
192
- nodeToDelete.count--;
193
- this._count--;
194
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
195
- return results;
102
+ * The function `add` in TypeScript overrides the superclass method to add key-value pairs to a
103
+ * TreeMultiMapNode, handling different input types and scenarios.
104
+ * @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
105
+ * parameter in the `override add` method can be either a `BTNRep` object containing a key, an array
106
+ * of values, and a `TreeMultiMapNode`, or just a key.
107
+ * @param {V} [value] - The `value` parameter in the `override add` method represents the value that
108
+ * you want to add to the TreeMultiMap. If the key is already present in the map, the new value will
109
+ * be added to the existing list of values associated with that key. If the key is not present,
110
+ * @returns The `add` method is returning a boolean value, which indicates whether the operation was
111
+ * successful or not.
112
+ */
113
+ add(keyNodeOrEntry, value) {
114
+ if (this.isRealNode(keyNodeOrEntry))
115
+ return super.add(keyNodeOrEntry);
116
+ const _commonAdd = (key, values) => {
117
+ if (key === undefined || key === null)
118
+ return false;
119
+ const existingValues = this.get(key);
120
+ if (existingValues !== undefined && values !== undefined) {
121
+ for (const value of values)
122
+ existingValues.push(value);
123
+ return true;
196
124
  }
197
- }
198
- else {
199
- const successor = this.getLeftMost(node => node, nodeToDelete.right);
200
- if (successor) {
201
- originalColor = successor.color;
202
- replacementNode = successor.right;
203
- if (successor.parent === nodeToDelete) {
204
- if (this.isRealNode(replacementNode)) {
205
- replacementNode.parent = successor;
206
- }
125
+ const existingNode = this.getNode(key);
126
+ if (this.isRealNode(existingNode)) {
127
+ if (existingValues === undefined) {
128
+ super.add(key, values);
129
+ return true;
207
130
  }
208
- else {
209
- if (ignoreCount || nodeToDelete.count <= 1) {
210
- this._transplant(successor, successor.right);
211
- this._count -= nodeToDelete.count;
212
- }
213
- else {
214
- nodeToDelete.count--;
215
- this._count--;
216
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
217
- return results;
218
- }
219
- successor.right = nodeToDelete.right;
220
- if (this.isRealNode(successor.right)) {
221
- successor.right.parent = successor;
222
- }
223
- }
224
- if (ignoreCount || nodeToDelete.count <= 1) {
225
- this._transplant(nodeToDelete, successor);
226
- this._count -= nodeToDelete.count;
131
+ if (values !== undefined) {
132
+ for (const value of values)
133
+ existingValues.push(value);
134
+ return true;
227
135
  }
228
136
  else {
229
- nodeToDelete.count--;
230
- this._count--;
231
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
232
- return results;
233
- }
234
- successor.left = nodeToDelete.left;
235
- if (this.isRealNode(successor.left)) {
236
- successor.left.parent = successor;
137
+ return false;
237
138
  }
238
- successor.color = nodeToDelete.color;
239
139
  }
140
+ else {
141
+ return super.add(key, values);
142
+ }
143
+ };
144
+ if (this.isEntry(keyNodeOrEntry)) {
145
+ const [key, values] = keyNodeOrEntry;
146
+ return _commonAdd(key, value !== undefined ? [value] : values);
240
147
  }
241
- this._size--;
242
- // If the original color was black, fix the tree
243
- if (originalColor === 'BLACK') {
244
- this._deleteFixup(replacementNode);
245
- }
246
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
247
- return results;
148
+ return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined);
248
149
  }
249
150
  /**
250
- * Time Complexity: O(1)
251
- * Space Complexity: O(1)
252
- *
253
- * The "clear" function overrides the parent class's "clear" function and also resets the count to
254
- * zero.
255
- */
256
- clear() {
257
- super.clear();
258
- this._count = 0;
259
- }
260
- /**
261
- * Time Complexity: O(n log n)
151
+ * Time Complexity: O(log n)
262
152
  * Space Complexity: O(log n)
263
153
  *
264
- * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
265
- * tree using either a recursive or iterative approach.
266
- * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
267
- * specifies the type of iteration to use when building the balanced binary search tree. It has a
268
- * default value of `this.iterationType`, which means it will use the iteration type specified by the
269
- * `iterationType` property of the current object.
270
- * @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
271
- * balancing operation is successful, and `false` if there are no nodes to balance.
272
- */
273
- perfectlyBalance(iterationType = this.iterationType) {
274
- const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
275
- if (sorted.length < 1)
276
- return false;
277
- this.clear();
278
- if (iterationType === 'RECURSIVE') {
279
- const buildBalanceBST = (l, r) => {
280
- if (l > r)
281
- return;
282
- const m = l + Math.floor((r - l) / 2);
283
- const midNode = sorted[m];
284
- if (this._isMapMode)
285
- this.add(midNode.key, undefined, midNode.count);
286
- else
287
- this.add(midNode.key, midNode.value, midNode.count);
288
- buildBalanceBST(l, m - 1);
289
- buildBalanceBST(m + 1, r);
290
- };
291
- buildBalanceBST(0, n - 1);
292
- return true;
293
- }
294
- else {
295
- const stack = [[0, n - 1]];
296
- while (stack.length > 0) {
297
- const popped = stack.pop();
298
- if (popped) {
299
- const [l, r] = popped;
300
- if (l <= r) {
301
- const m = l + Math.floor((r - l) / 2);
302
- const midNode = sorted[m];
303
- if (this._isMapMode)
304
- this.add(midNode.key, undefined, midNode.count);
305
- else
306
- this.add(midNode.key, midNode.value, midNode.count);
307
- stack.push([m + 1, r]);
308
- stack.push([l, m - 1]);
309
- }
310
- }
311
- }
154
+ * The function `deleteValue` removes a specific value from a key in a TreeMultiMap data structure
155
+ * and deletes the entire node if no values are left for that key.
156
+ * @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
157
+ * parameter in the `deleteValue` function can be either a `BTNRep` object containing a key and an
158
+ * array of values, or just a key itself.
159
+ * @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
160
+ * value that you want to remove from the multi-map data structure associated with a particular key.
161
+ * The function checks if the value exists in the array of values associated with the key, and if
162
+ * found, removes it from the array.
163
+ * @returns The `deleteValue` function returns a boolean value - `true` if the specified `value` was
164
+ * successfully deleted from the values associated with the `keyNodeOrEntry`, and `false` otherwise.
165
+ */
166
+ deleteValue(keyNodeOrEntry, value) {
167
+ const values = this.get(keyNodeOrEntry);
168
+ if (Array.isArray(values)) {
169
+ const index = values.indexOf(value);
170
+ if (index === -1)
171
+ return false;
172
+ values.splice(index, 1);
173
+ // If no values left, remove the entire node
174
+ if (values.length === 0)
175
+ this.delete(keyNodeOrEntry);
312
176
  return true;
313
177
  }
178
+ return false;
314
179
  }
315
180
  /**
316
- * Time complexity: O(n)
317
- * Space complexity: O(n)
181
+ * Time Complexity: O(n)
182
+ * Space Complexity: O(n)
318
183
  *
319
- * The function overrides the clone method to create a deep copy of a tree object.
320
- * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
184
+ * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
185
+ * structure.
186
+ * @returns The `cloned` object is being returned.
321
187
  */
322
- // @ts-ignore
323
188
  clone() {
324
189
  const cloned = this.createTree();
325
- this.bfs(node => cloned.add(node.key, undefined, node.count));
326
- if (this._isMapMode)
327
- cloned._store = this._store;
190
+ this._clone(cloned);
328
191
  return cloned;
329
192
  }
330
- /**
331
- * The `map` function in TypeScript overrides the default behavior to create a new TreeMultiMap with
332
- * modified entries based on a provided callback.
333
- * @param callback - The `callback` parameter is a function that will be called for each entry in the
334
- * map. It takes four arguments:
335
- * @param [options] - The `options` parameter in the `override map` function is of type
336
- * `TreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional configuration
337
- * options when creating a new `TreeMultiMap` instance within the `map` function. These options could
338
- * include things like
339
- * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
340
- * the value of `this` when executing the `callback` function. It allows you to set the context
341
- * (value of `this`) for the callback function when it is called within the `map` function. This
342
- * @returns A new TreeMultiMap instance is being returned, which is populated with entries generated
343
- * by the provided callback function.
344
- */
345
- // @ts-ignore
346
- map(callback, options, thisArg) {
347
- const newTree = new TreeMultiMap([], options);
348
- let index = 0;
349
- for (const [key, value] of this) {
350
- newTree.add(callback.call(thisArg, key, value, index++, this));
351
- }
352
- return newTree;
353
- }
354
- /**
355
- * The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
356
- * node based on the input.
357
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
358
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
359
- * @param {V} [value] - The `value` parameter is an optional value that represents the value
360
- * associated with the key in the node. It is used when creating a new node or updating the value of
361
- * an existing node.
362
- * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
363
- * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
364
- * @returns either a NODE object or undefined.
365
- */
366
- _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
367
- if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null)
368
- return [undefined, undefined];
369
- if (this.isNode(keyNodeEntryOrRaw))
370
- return [keyNodeEntryOrRaw, value];
371
- if (this.isEntry(keyNodeEntryOrRaw)) {
372
- const [key, entryValue] = keyNodeEntryOrRaw;
373
- if (key === undefined || key === null)
374
- return [undefined, undefined];
375
- const finalValue = value !== null && value !== void 0 ? value : entryValue;
376
- if (this.isKey(key))
377
- return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
378
- }
379
- if (this.isRaw(keyNodeEntryOrRaw)) {
380
- const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
381
- const finalValue = value !== null && value !== void 0 ? value : entryValue;
382
- if (this.isKey(key))
383
- return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
384
- }
385
- if (this.isKey(keyNodeEntryOrRaw))
386
- return [this.createNode(keyNodeEntryOrRaw, value, 'BLACK', count), value];
387
- return [undefined, undefined];
388
- }
389
- /**
390
- * Time Complexity: O(1)
391
- * Space Complexity: O(1)
392
- *
393
- * The `_swapProperties` function swaps the properties (key, value, count, color) between two nodes
394
- * in a binary search tree.
395
- * @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
396
- * that will be swapped with the `destNode`. It can be either an instance of the `R` class or an
397
- * instance of the `BSTNOptKeyOrNode<K, NODE>` class.
398
- * @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
399
- * node where the properties will be swapped with the source node.
400
- * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
401
- * If either `srcNode` or `destNode` is undefined, it returns undefined.
402
- */
403
- _swapProperties(srcNode, destNode) {
404
- srcNode = this.ensureNode(srcNode);
405
- destNode = this.ensureNode(destNode);
406
- if (srcNode && destNode) {
407
- const { key, value, count, color } = destNode;
408
- const tempNode = this.createNode(key, value, color, count);
409
- if (tempNode) {
410
- tempNode.color = color;
411
- destNode.key = srcNode.key;
412
- if (!this._isMapMode)
413
- destNode.value = srcNode.value;
414
- destNode.count = srcNode.count;
415
- destNode.color = srcNode.color;
416
- srcNode.key = tempNode.key;
417
- if (!this._isMapMode)
418
- srcNode.value = tempNode.value;
419
- srcNode.count = tempNode.count;
420
- srcNode.color = tempNode.color;
421
- }
422
- return destNode;
423
- }
424
- return undefined;
425
- }
426
- /**
427
- * Time Complexity: O(1)
428
- * Space Complexity: O(1)
429
- *
430
- * The function replaces an old node with a new node and updates the count property of the new node.
431
- * @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace in the data
432
- * structure.
433
- * @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
434
- * @returns The method is returning the result of calling the `_replaceNode` method from the
435
- * superclass, which is of type `NODE`.
436
- */
437
- _replaceNode(oldNode, newNode) {
438
- newNode.count = oldNode.count + newNode.count;
439
- return super._replaceNode(oldNode, newNode);
440
- }
441
193
  }
442
194
  exports.TreeMultiMap = TreeMultiMap;
@@ -34,6 +34,9 @@ export declare class DirectedEdge<E = any> extends AbstractEdge<E> {
34
34
  */
35
35
  constructor(src: VertexKey, dest: VertexKey, weight?: number, value?: E);
36
36
  }
37
+ /**
38
+ *
39
+ */
37
40
  export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V> = DirectedVertex<V>, EO extends DirectedEdge<E> = DirectedEdge<E>> extends AbstractGraph<V, E, VO, EO> implements IGraph<V, E, VO, EO> {
38
41
  /**
39
42
  * The constructor function initializes an instance of a class.
@@ -35,6 +35,9 @@ class DirectedEdge extends abstract_graph_1.AbstractEdge {
35
35
  }
36
36
  }
37
37
  exports.DirectedEdge = DirectedEdge;
38
+ /**
39
+ *
40
+ */
38
41
  class DirectedGraph extends abstract_graph_1.AbstractGraph {
39
42
  /**
40
43
  * The constructor function initializes an instance of a class.
@@ -30,6 +30,9 @@ export declare class MapEdge<E = any> extends DirectedEdge<E> {
30
30
  */
31
31
  constructor(src: VertexKey, dest: VertexKey, weight?: number, value?: E);
32
32
  }
33
+ /**
34
+ *
35
+ */
33
36
  export declare class MapGraph<V = any, E = any, VO extends MapVertex<V> = MapVertex<V>, EO extends MapEdge<E> = MapEdge<E>> extends DirectedGraph<V, E, VO, EO> {
34
37
  /**
35
38
  * The constructor function initializes the originCoord and bottomRight properties of a MapGraphCoordinate object.
@@ -38,6 +38,9 @@ class MapEdge extends directed_graph_1.DirectedEdge {
38
38
  }
39
39
  }
40
40
  exports.MapEdge = MapEdge;
41
+ /**
42
+ *
43
+ */
41
44
  class MapGraph extends directed_graph_1.DirectedGraph {
42
45
  /**
43
46
  * The constructor function initializes the originCoord and bottomRight properties of a MapGraphCoordinate object.
@@ -32,6 +32,9 @@ export declare class UndirectedEdge<E = number> extends AbstractEdge<E> {
32
32
  */
33
33
  constructor(v1: VertexKey, v2: VertexKey, weight?: number, value?: E);
34
34
  }
35
+ /**
36
+ *
37
+ */
35
38
  export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVertex<V> = UndirectedVertex<V>, EO extends UndirectedEdge<E> = UndirectedEdge<E>> extends AbstractGraph<V, E, VO, EO> implements IGraph<V, E, VO, EO> {
36
39
  /**
37
40
  * The constructor initializes a new Map object to store edgeMap.