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