deque-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,410 +4,186 @@ exports.AVLTreeMultiMap = exports.AVLTreeMultiMapNode = void 0;
4
4
  const avl_tree_1 = require("./avl-tree");
5
5
  class AVLTreeMultiMapNode extends avl_tree_1.AVLTreeNode {
6
6
  /**
7
- * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
8
- * @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
9
- * of the binary tree node.
10
- * @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary
11
- * tree node. If no value is provided, it will be `undefined`.
12
- * @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
13
- * occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
14
- * parameter when creating a new instance of the `BinaryTreeNode` class.
15
- */
16
- constructor(key, value, count = 1) {
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) {
17
16
  super(key, value);
18
- this._count = 1;
19
- this.count = count;
17
+ this.parent = undefined;
18
+ this._left = undefined;
19
+ this._right = undefined;
20
20
  }
21
- /**
22
- * The function returns the value of the protected variable _count.
23
- * @returns The count property of the object, which is of type number.
24
- */
25
- get count() {
26
- return this._count;
21
+ get left() {
22
+ return this._left;
27
23
  }
28
- /**
29
- * The above function sets the value of the count property.
30
- * @param {number} value - The value parameter is of type number, which means it can accept any
31
- * numeric value.
32
- */
33
- set count(value) {
34
- this._count = value;
24
+ set left(v) {
25
+ if (v) {
26
+ v.parent = this;
27
+ }
28
+ this._left = v;
29
+ }
30
+ get right() {
31
+ return this._right;
32
+ }
33
+ set right(v) {
34
+ if (v) {
35
+ v.parent = this;
36
+ }
37
+ this._right = v;
35
38
  }
36
39
  }
37
40
  exports.AVLTreeMultiMapNode = AVLTreeMultiMapNode;
38
41
  /**
39
- * The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
42
+ *
40
43
  */
41
44
  class AVLTreeMultiMap extends avl_tree_1.AVLTree {
42
45
  /**
43
- * The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
44
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
45
- * iterable object that can contain either keys, nodes, entries, or raw elements.
46
- * @param [options] - The `options` parameter is an optional object that can be used to customize the
47
- * behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and
48
- * `compareValues` functions to define custom comparison logic for keys and values, respectively.
46
+ * The constructor initializes an AVLTreeMultiMap with the provided keys, nodes, entries, or raw data
47
+ * and options.
48
+ * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
49
+ * iterable that can contain either key-value pairs represented as `BTNRep<K, V[],
50
+ * AVLTreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize
51
+ * the AVLTreeMulti
52
+ * @param [options] - The `options` parameter in the constructor is of type
53
+ * `AVLTreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
54
+ * additional options for configuring the AVLTreeMultiMap instance.
49
55
  */
50
56
  constructor(keysNodesEntriesOrRaws = [], options) {
51
- super([], options);
52
- this._count = 0;
53
- if (keysNodesEntriesOrRaws)
57
+ super([], Object.assign(Object.assign({}, options), { isMapMode: true }));
58
+ if (keysNodesEntriesOrRaws) {
54
59
  this.addMany(keysNodesEntriesOrRaws);
60
+ }
55
61
  }
56
62
  /**
57
- * The function calculates the sum of the count property of all nodes in a tree using depth-first
58
- * search.
59
- * @returns the sum of the count property of all nodes in the tree.
60
- */
61
- get count() {
62
- return this._count;
63
- }
64
- /**
65
- * Time Complexity: O(n)
63
+ * Time Complexity: O(1)
66
64
  * Space Complexity: O(1)
67
65
  *
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.
66
+ * The function `createTree` in TypeScript overrides the creation of an AVLTreeMultiMap with
67
+ * specified options.
68
+ * @param [options] - The `options` parameter in the `createTree` function is of type
69
+ * `AVLTreeMultiMapOptions<K, V[], R>`. This means it is an object that can have properties of type
70
+ * `K`, `V[]`, and `R`. The function creates a new `AVL
71
+ * @returns The `createTree` method is returning a new instance of `AVLTreeMultiMap` with the
72
+ * provided options.
71
73
  */
72
- getComputedCount() {
73
- let sum = 0;
74
- this.dfs(node => (sum += node.count));
75
- return sum;
76
- }
77
- /**
78
- * The function creates a new AVLTreeMultiMapNode with the specified key, value, 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 that can be replaced with any specific type when using the function.
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 {number} [count] - The `count` parameter represents the number of occurrences of a
84
- * key-value pair in the AVLTreeMultiMapNode. It is an optional parameter, so it can be omitted when
85
- * calling the `createNode` method. If provided, it specifies the initial count for the node.
86
- * @returns a new instance of the AVLTreeMultiMapNode class, casted as NODE.
87
- */
88
- createNode(key, value, count) {
89
- return new AVLTreeMultiMapNode(key, this._isMapMode ? undefined : value, count);
90
- }
91
- /**
92
- * The function creates a new AVLTreeMultiMap object with the specified options and returns it.
93
- * @param [options] - The `options` parameter is an optional object that contains additional
94
- * configuration options for creating the AVLTreeMultiMap. It can have the following properties:
95
- * @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
96
- * object.
97
- */
98
- // @ts-ignore
99
74
  createTree(options) {
100
- return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
101
- }
102
- /**
103
- * The function checks if the input is an instance of AVLTreeMultiMapNode.
104
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
105
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
106
- * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
107
- * an instance of the `AVLTreeMultiMapNode` class.
108
- */
109
- isNode(keyNodeEntryOrRaw) {
110
- return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode;
75
+ return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
111
76
  }
112
77
  /**
113
- * Time Complexity: O(log n)
78
+ * Time Complexity: O(1)
114
79
  * Space Complexity: O(1)
115
80
  *
116
- * The function overrides the add method of a TypeScript class to add a new node to a data structure
117
- * and update the count.
118
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
119
- * `keyNodeEntryOrRaw` parameter can accept a value of type `R`, which can be any type. It
120
- * can also accept a value of type `BTNRep<K, V, NODE>`, which represents a key, node,
121
- * entry, or raw element
122
- * @param {V} [value] - The `value` parameter represents the value associated with the key in the
123
- * data structure. It is an optional parameter, so it can be omitted if not needed.
124
- * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
125
- * be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
126
- * be added once. However, you can specify a different value for `count` if you want to add
127
- * @returns a boolean value.
81
+ * The function `createNode` overrides the method to create a new AVLTreeMultiMapNode with a
82
+ * specified key and an empty array of values.
83
+ * @param {K} key - The `key` parameter in the `createNode` method represents the key of the node
84
+ * that will be created in the AVLTreeMultiMap.
85
+ * @returns An AVLTreeMultiMapNode object is being returned, initialized with the provided key and an
86
+ * empty array.
128
87
  */
129
- add(keyNodeEntryOrRaw, value, count = 1) {
130
- const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
131
- if (newNode === undefined)
132
- return false;
133
- const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
134
- const inserted = super.add(newNode, newValue);
135
- if (inserted) {
136
- this._count += orgNodeCount;
137
- }
138
- return true;
88
+ createNode(key) {
89
+ return new AVLTreeMultiMapNode(key, []);
139
90
  }
140
91
  /**
141
92
  * Time Complexity: O(log n)
142
- * Space Complexity: O(1)
93
+ * Space Complexity: O(log n)
143
94
  *
144
- * The function overrides the delete method in a binary tree data structure, handling deletion of
145
- * nodes and maintaining balance in the tree.
146
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `predicate`
147
- * parameter in the `delete` method is used to specify the condition for deleting a node from the
148
- * binary tree. It can be a key, node, or entry that determines which
149
- * node(s) should be deleted.
150
- * @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
151
- * boolean flag that determines whether to ignore the count of the node being deleted. If
152
- * `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
153
- * `ignoreCount` is set to
154
- * @returns The `delete` method overrides the default delete behavior in a binary tree data
155
- * structure. It takes a predicate or node to be deleted and an optional flag to ignore count. The
156
- * method returns an array of `BinaryTreeDeleteResult` objects, each containing information about the
157
- * deleted node and whether balancing is needed in the tree.
158
- */
159
- delete(keyNodeEntryOrRaw, ignoreCount = false) {
160
- var _a;
161
- const deletedResult = [];
162
- if (!this.root)
163
- return deletedResult;
164
- const curr = (_a = this.getNode(keyNodeEntryOrRaw)) !== null && _a !== void 0 ? _a : undefined;
165
- if (!curr)
166
- return deletedResult;
167
- const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : undefined;
168
- let needBalanced = undefined, orgCurrent = curr;
169
- if (curr.count > 1 && !ignoreCount) {
170
- curr.count--;
171
- this._count--;
172
- }
173
- else {
174
- if (!curr.left) {
175
- if (!parent) {
176
- if (curr.right !== undefined)
177
- this._setRoot(curr.right);
95
+ * The function `add` in TypeScript overrides the superclass method to add key-value pairs to an AVL
96
+ * tree multi-map.
97
+ * @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
98
+ * parameter in the `override add` method can be either a key-value pair entry or just a key. If it
99
+ * is a key-value pair entry, it will be in the format `[key, values]`, where `key` is the key and
100
+ * `values`
101
+ * @param {V} [value] - The `value` parameter in the `override add` method represents the value that
102
+ * you want to add to the AVLTreeMultiMap. It can be a single value or an array of values associated
103
+ * with a specific key.
104
+ * @returns The `override add` method is returning a boolean value, which indicates whether the
105
+ * addition operation was successful or not.
106
+ */
107
+ add(keyNodeOrEntry, value) {
108
+ if (this.isRealNode(keyNodeOrEntry))
109
+ return super.add(keyNodeOrEntry);
110
+ const _commonAdd = (key, values) => {
111
+ if (key === undefined || key === null)
112
+ return false;
113
+ const existingValues = this.get(key);
114
+ if (existingValues !== undefined && values !== undefined) {
115
+ for (const value of values)
116
+ existingValues.push(value);
117
+ return true;
118
+ }
119
+ const existingNode = this.getNode(key);
120
+ if (this.isRealNode(existingNode)) {
121
+ if (existingValues === undefined) {
122
+ super.add(key, values);
123
+ return true;
124
+ }
125
+ if (values !== undefined) {
126
+ for (const value of values)
127
+ existingValues.push(value);
128
+ return true;
178
129
  }
179
130
  else {
180
- const { familyPosition: fp } = curr;
181
- if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
182
- parent.left = curr.right;
183
- }
184
- else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
185
- parent.right = curr.right;
186
- }
187
- needBalanced = parent;
131
+ return false;
188
132
  }
189
133
  }
190
134
  else {
191
- const leftSubTreeRightMost = curr.left ? this.getRightMost(node => node, curr.left) : undefined;
192
- if (leftSubTreeRightMost) {
193
- const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
194
- orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
195
- if (parentOfLeftSubTreeMax) {
196
- if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
197
- parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
198
- }
199
- else {
200
- parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
201
- }
202
- needBalanced = parentOfLeftSubTreeMax;
203
- }
204
- }
135
+ return super.add(key, values);
205
136
  }
206
- this._size = this._size - 1;
207
- // TODO How to handle when the count of target node is lesser than current node's count
208
- if (orgCurrent)
209
- this._count -= orgCurrent.count;
137
+ };
138
+ if (this.isEntry(keyNodeOrEntry)) {
139
+ const [key, values] = keyNodeOrEntry;
140
+ return _commonAdd(key, value !== undefined ? [value] : values);
210
141
  }
211
- deletedResult.push({ deleted: orgCurrent, needBalanced });
212
- if (needBalanced) {
213
- this._balancePath(needBalanced);
214
- }
215
- return deletedResult;
216
- }
217
- /**
218
- * Time Complexity: O(1)
219
- * Space Complexity: O(1)
220
- *
221
- * The "clear" function overrides the parent class's "clear" function and also resets the count to
222
- * zero.
223
- */
224
- clear() {
225
- super.clear();
226
- this._count = 0;
142
+ return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined);
227
143
  }
228
144
  /**
229
- * Time Complexity: O(n log n)
145
+ * Time Complexity: O(log n)
230
146
  * Space Complexity: O(log n)
231
- * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
232
- * tree using either a recursive or iterative approach.
233
- * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
234
- * specifies the type of iteration to use when building the balanced binary search tree. It has a
235
- * default value of `this.iterationType`, which means it will use the iteration type currently set in
236
- * the object.
237
- * @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
238
- * balancing operation is successful, and `false` if there are no nodes to balance.
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 === '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
- if (this._isMapMode)
252
- this.add(midNode.key, undefined, midNode.count);
253
- else
254
- this.add(midNode.key, midNode.value, midNode.count);
255
- buildBalanceBST(l, m - 1);
256
- buildBalanceBST(m + 1, r);
257
- };
258
- buildBalanceBST(0, n - 1);
259
- return true;
260
- }
261
- else {
262
- const stack = [[0, n - 1]];
263
- while (stack.length > 0) {
264
- const popped = stack.pop();
265
- if (popped) {
266
- const [l, r] = popped;
267
- if (l <= r) {
268
- const m = l + Math.floor((r - l) / 2);
269
- const midNode = sorted[m];
270
- if (this._isMapMode)
271
- this.add(midNode.key, undefined, midNode.count);
272
- else
273
- this.add(midNode.key, midNode.value, midNode.count);
274
- stack.push([m + 1, r]);
275
- stack.push([l, m - 1]);
276
- }
277
- }
278
- }
147
+ *
148
+ * The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data
149
+ * structure and deletes the entire node if no values are left for that key.
150
+ * @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
151
+ * parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value
152
+ * pair in the AVLTreeMultiMapNode, or just the key itself.
153
+ * @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
154
+ * value that you want to delete from the multi-map data structure associated with a particular key.
155
+ * The function checks if the value exists in the array of values associated with the key, and if
156
+ * found, removes it from the array.
157
+ * @returns The `deleteValue` function returns a boolean value. It returns `true` if the specified
158
+ * `value` was successfully deleted from the array of values associated with the `keyNodeOrEntry`. If
159
+ * the value was not found in the array, it returns `false`.
160
+ */
161
+ deleteValue(keyNodeOrEntry, value) {
162
+ const values = this.get(keyNodeOrEntry);
163
+ if (Array.isArray(values)) {
164
+ const index = values.indexOf(value);
165
+ if (index === -1)
166
+ return false;
167
+ values.splice(index, 1);
168
+ // If no values left, remove the entire node
169
+ if (values.length === 0)
170
+ this.delete(keyNodeOrEntry);
279
171
  return true;
280
172
  }
173
+ return false;
281
174
  }
282
175
  /**
283
- * Time complexity: O(n)
284
- * Space complexity: O(n)
176
+ * Time Complexity: O(n)
177
+ * Space Complexity: O(n)
285
178
  *
286
- * The function overrides the clone method to create a deep copy of a tree object.
287
- * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
179
+ * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
180
+ * structure.
181
+ * @returns A cloned tree object is being returned.
288
182
  */
289
- // @ts-ignore
290
183
  clone() {
291
184
  const cloned = this.createTree();
292
- if (this._isMapMode)
293
- this.bfs(node => cloned.add(node.key, undefined, node.count));
294
- else
295
- this.bfs(node => cloned.add(node.key, node.value, node.count));
296
- if (this._isMapMode)
297
- cloned._store = this._store;
185
+ this._clone(cloned);
298
186
  return cloned;
299
187
  }
300
- /**
301
- * The `map` function in TypeScript overrides the default behavior to create a new AVLTreeMultiMap
302
- * with modified entries based on a provided callback.
303
- * @param callback - The `callback` parameter is a function that will be called for each entry in the
304
- * AVLTreeMultiMap. It takes four arguments:
305
- * @param [options] - The `options` parameter in the `override map` function is of type
306
- * `AVLTreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional
307
- * configuration options when creating a new `AVLTreeMultiMap` instance within the `map` function.
308
- * These options
309
- * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
310
- * the value of `this` when executing the `callback` function. It allows you to set the context
311
- * (value of `this`) for the callback function. This can be useful when you want to access properties
312
- * or
313
- * @returns The `map` method is returning a new `AVLTreeMultiMap` instance with the entries
314
- * transformed by the provided `callback` function. Each entry in the original tree is passed to the
315
- * `callback` function along with the index and the original tree itself. The transformed entries are
316
- * then added to the new `AVLTreeMultiMap` instance, which is returned at the end.
317
- */
318
- // @ts-ignore
319
- map(callback, options, thisArg) {
320
- const newTree = new AVLTreeMultiMap([], options);
321
- let index = 0;
322
- for (const [key, value] of this) {
323
- newTree.add(callback.call(thisArg, key, value, index++, this));
324
- }
325
- return newTree;
326
- }
327
- /**
328
- * The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
329
- * a node object.
330
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
331
- * `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
332
- * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
333
- * `override` function. It represents the value associated with the key in the data structure. If no
334
- * value is provided, it will default to `undefined`.
335
- * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
336
- * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
337
- * @returns either a NODE object or undefined.
338
- */
339
- _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
340
- if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null)
341
- return [undefined, undefined];
342
- if (this.isNode(keyNodeEntryOrRaw))
343
- return [keyNodeEntryOrRaw, value];
344
- if (this.isEntry(keyNodeEntryOrRaw)) {
345
- const [key, entryValue] = keyNodeEntryOrRaw;
346
- if (key === undefined || key === null)
347
- return [undefined, undefined];
348
- const finalValue = value !== null && value !== void 0 ? value : entryValue;
349
- return [this.createNode(key, finalValue, count), finalValue];
350
- }
351
- if (this.isRaw(keyNodeEntryOrRaw)) {
352
- const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
353
- const finalValue = value !== null && value !== void 0 ? value : entryValue;
354
- if (this.isKey(key))
355
- return [this.createNode(key, finalValue, count), finalValue];
356
- }
357
- if (this.isKey(keyNodeEntryOrRaw))
358
- return [this.createNode(keyNodeEntryOrRaw, value, count), value];
359
- return [undefined, undefined];
360
- }
361
- /**
362
- * Time Complexity: O(1)
363
- * Space Complexity: O(1)
364
- *
365
- * The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
366
- * in a binary search tree.
367
- * @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
368
- * that will be swapped with the `destNode`.
369
- * @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
370
- * node where the properties will be swapped with the source node.
371
- * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
372
- * If either `srcNode` or `destNode` is undefined, it returns `undefined`.
373
- */
374
- _swapProperties(srcNode, destNode) {
375
- srcNode = this.ensureNode(srcNode);
376
- destNode = this.ensureNode(destNode);
377
- if (srcNode && destNode) {
378
- const { key, value, count, height } = destNode;
379
- const tempNode = this.createNode(key, value, count);
380
- if (tempNode) {
381
- tempNode.height = height;
382
- destNode.key = srcNode.key;
383
- if (!this._isMapMode)
384
- destNode.value = srcNode.value;
385
- destNode.count = srcNode.count;
386
- destNode.height = srcNode.height;
387
- srcNode.key = tempNode.key;
388
- if (!this._isMapMode)
389
- srcNode.value = tempNode.value;
390
- srcNode.count = tempNode.count;
391
- srcNode.height = tempNode.height;
392
- }
393
- return destNode;
394
- }
395
- return undefined;
396
- }
397
- /**
398
- * Time Complexity: O(1)
399
- * Space Complexity: O(1)
400
- *
401
- * The function replaces an old node with a new node and updates the count property of the new node.
402
- * @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
403
- * data structure. It is of type NODE.
404
- * @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
405
- * @returns The method is returning the result of calling the `_replaceNode` method from the
406
- * superclass, which is of type `NODE`.
407
- */
408
- _replaceNode(oldNode, newNode) {
409
- newNode.count = oldNode.count + newNode.count;
410
- return super._replaceNode(oldNode, newNode);
411
- }
412
188
  }
413
189
  exports.AVLTreeMultiMap = AVLTreeMultiMap;