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