heap-typed 2.0.4 → 2.1.0

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 (101) hide show
  1. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  2. package/dist/data-structures/base/iterable-element-base.js +149 -107
  3. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  4. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  5. package/dist/data-structures/base/linear-base.d.ts +250 -192
  6. package/dist/data-structures/base/linear-base.js +137 -274
  7. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  8. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  9. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  10. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  11. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  12. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  14. package/dist/data-structures/binary-tree/binary-tree.js +612 -879
  15. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  16. package/dist/data-structures/binary-tree/bst.js +505 -481
  17. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  18. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  19. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  20. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  21. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  22. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  23. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  24. package/dist/data-structures/graph/abstract-graph.js +267 -237
  25. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  26. package/dist/data-structures/graph/directed-graph.js +146 -233
  27. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  28. package/dist/data-structures/graph/map-graph.js +56 -59
  29. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  30. package/dist/data-structures/graph/undirected-graph.js +129 -149
  31. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  32. package/dist/data-structures/hash/hash-map.js +270 -457
  33. package/dist/data-structures/heap/heap.d.ts +214 -289
  34. package/dist/data-structures/heap/heap.js +340 -349
  35. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  36. package/dist/data-structures/heap/max-heap.js +11 -66
  37. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  38. package/dist/data-structures/heap/min-heap.js +11 -66
  39. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  40. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  41. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  42. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  43. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  44. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  45. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  46. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  47. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  48. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  49. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  50. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  51. package/dist/data-structures/queue/deque.d.ts +227 -254
  52. package/dist/data-structures/queue/deque.js +309 -348
  53. package/dist/data-structures/queue/queue.d.ts +180 -201
  54. package/dist/data-structures/queue/queue.js +265 -248
  55. package/dist/data-structures/stack/stack.d.ts +124 -102
  56. package/dist/data-structures/stack/stack.js +181 -125
  57. package/dist/data-structures/trie/trie.d.ts +164 -165
  58. package/dist/data-structures/trie/trie.js +189 -172
  59. package/dist/interfaces/binary-tree.d.ts +56 -6
  60. package/dist/interfaces/graph.d.ts +16 -0
  61. package/dist/types/data-structures/base/base.d.ts +1 -1
  62. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  63. package/dist/types/utils/utils.d.ts +6 -6
  64. package/dist/utils/utils.d.ts +110 -49
  65. package/dist/utils/utils.js +148 -73
  66. package/package.json +2 -2
  67. package/src/data-structures/base/iterable-element-base.ts +238 -115
  68. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  69. package/src/data-structures/base/linear-base.ts +271 -277
  70. package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
  71. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
  72. package/src/data-structures/binary-tree/avl-tree.ts +239 -206
  73. package/src/data-structures/binary-tree/binary-tree.ts +681 -905
  74. package/src/data-structures/binary-tree/bst.ts +568 -570
  75. package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
  76. package/src/data-structures/binary-tree/tree-counter.ts +199 -218
  77. package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
  78. package/src/data-structures/graph/abstract-graph.ts +339 -264
  79. package/src/data-structures/graph/directed-graph.ts +146 -236
  80. package/src/data-structures/graph/map-graph.ts +63 -60
  81. package/src/data-structures/graph/undirected-graph.ts +129 -152
  82. package/src/data-structures/hash/hash-map.ts +274 -496
  83. package/src/data-structures/heap/heap.ts +389 -402
  84. package/src/data-structures/heap/max-heap.ts +12 -76
  85. package/src/data-structures/heap/min-heap.ts +13 -76
  86. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  87. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  88. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  89. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  90. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  91. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  92. package/src/data-structures/queue/deque.ts +381 -357
  93. package/src/data-structures/queue/queue.ts +310 -264
  94. package/src/data-structures/stack/stack.ts +217 -131
  95. package/src/data-structures/trie/trie.ts +240 -175
  96. package/src/interfaces/binary-tree.ts +240 -6
  97. package/src/interfaces/graph.ts +37 -0
  98. package/src/types/data-structures/base/base.ts +5 -5
  99. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  100. package/src/types/utils/utils.ts +9 -5
  101. package/src/utils/utils.ts +152 -86
@@ -1,19 +1,29 @@
1
1
  "use strict";
2
+ /**
3
+ * data-structure-typed
4
+ *
5
+ * @author Pablo Zeng
6
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
7
+ * @license MIT License
8
+ */
2
9
  Object.defineProperty(exports, "__esModule", { value: true });
3
10
  exports.TreeCounter = exports.TreeCounterNode = void 0;
4
11
  const red_black_tree_1 = require("./red-black-tree");
12
+ /**
13
+ * RB-tree node with an extra 'count' field; keeps parent/child links.
14
+ * @remarks Time O(1), Space O(1)
15
+ * @template K
16
+ * @template V
17
+ */
5
18
  class TreeCounterNode extends red_black_tree_1.RedBlackTreeNode {
6
19
  /**
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'`.
20
+ * Create a tree counter node.
21
+ * @remarks Time O(1), Space O(1)
22
+ * @param key - Key of the node.
23
+ * @param [value] - Value associated with the key (ignored in map mode).
24
+ * @param [count] - Initial count for this node (default 1).
25
+ * @param color - Initial color ('RED' or 'BLACK').
26
+ * @returns New TreeCounterNode instance.
17
27
  */
18
28
  constructor(key, value, count = 1, color = 'BLACK') {
19
29
  super(key, value, color);
@@ -22,18 +32,40 @@ class TreeCounterNode extends red_black_tree_1.RedBlackTreeNode {
22
32
  this._right = undefined;
23
33
  this.count = count;
24
34
  }
35
+ /**
36
+ * Get the left child pointer.
37
+ * @remarks Time O(1), Space O(1)
38
+ * @returns Left child node, or null/undefined.
39
+ */
25
40
  get left() {
26
41
  return this._left;
27
42
  }
43
+ /**
44
+ * Set the left child and update its parent pointer.
45
+ * @remarks Time O(1), Space O(1)
46
+ * @param v - New left child node, or null/undefined.
47
+ * @returns void
48
+ */
28
49
  set left(v) {
29
50
  if (v) {
30
51
  v.parent = this;
31
52
  }
32
53
  this._left = v;
33
54
  }
55
+ /**
56
+ * Get the right child pointer.
57
+ * @remarks Time O(1), Space O(1)
58
+ * @returns Right child node, or null/undefined.
59
+ */
34
60
  get right() {
35
61
  return this._right;
36
62
  }
63
+ /**
64
+ * Set the right child and update its parent pointer.
65
+ * @remarks Time O(1), Space O(1)
66
+ * @param v - New right child node, or null/undefined.
67
+ * @returns void
68
+ */
37
69
  set right(v) {
38
70
  if (v) {
39
71
  v.parent = this;
@@ -43,17 +75,19 @@ class TreeCounterNode extends red_black_tree_1.RedBlackTreeNode {
43
75
  }
44
76
  exports.TreeCounterNode = TreeCounterNode;
45
77
  /**
46
- *
78
+ * Red-Black Tree–based counter map (key → value with per-node count). Supports O(log N) updates and map-like mode.
79
+ * @remarks Time O(1), Space O(1)
80
+ * @template K
81
+ * @template V
82
+ * @template R
47
83
  */
48
84
  class TreeCounter extends red_black_tree_1.RedBlackTree {
49
85
  /**
50
- * The constructor function initializes a TreeCounter object with optional initial data.
51
- * @param keysNodesEntriesOrRaws - The parameter `keysNodesEntriesOrRaws` is an
52
- * iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
53
- * TreeCounter with initial data.
54
- * @param [options] - The `options` parameter is an optional object that can be used to customize the
55
- * behavior of the `TreeCounter` constructor. It can include properties such as `compareKeys` and
56
- * `compareValues`, which are functions used to compare keys and values respectively.
86
+ * Create a TreeCounter and optionally bulk-insert items.
87
+ * @remarks Time O(N log N), Space O(N)
88
+ * @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
89
+ * @param [options] - Options for TreeCounter (comparator, reverse, map mode).
90
+ * @returns New TreeCounter instance.
57
91
  */
58
92
  constructor(keysNodesEntriesOrRaws = [], options) {
59
93
  super([], options);
@@ -61,79 +95,42 @@ class TreeCounter extends red_black_tree_1.RedBlackTree {
61
95
  if (keysNodesEntriesOrRaws)
62
96
  this.addMany(keysNodesEntriesOrRaws);
63
97
  }
64
- // TODO the _count is not accurate after nodes count modified
65
98
  /**
66
- * The function calculates the sum of the count property of all nodes in a tree structure.
67
- * @returns the sum of the count property of all nodes in the tree.
99
+ * Get the total aggregate count across all nodes.
100
+ * @remarks Time O(1), Space O(1)
101
+ * @returns Total count.
68
102
  */
69
103
  get count() {
70
104
  return this._count;
71
105
  }
72
106
  /**
73
- * Time Complexity: O(n)
74
- * Space Complexity: O(1)
75
- *
76
- * The function calculates the sum of the count property of all nodes in a tree using depth-first
77
- * search.
78
- * @returns the sum of the count property of all nodes in the tree.
107
+ * Compute the total count by traversing the tree (sums node.count).
108
+ * @remarks Time O(N), Space O(H)
109
+ * @returns Total count recomputed from nodes.
79
110
  */
80
111
  getComputedCount() {
81
112
  let sum = 0;
82
113
  this.dfs(node => (sum += node ? node.count : 0));
83
114
  return sum;
84
115
  }
85
- /**
86
- * The function creates a new TreeCounterNode with the specified key, value, color, and count.
87
- * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
88
- * which is a generic type representing the type of keys in the tree.
89
- * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
90
- * associated with the key in the node. It is of type `V`, which can be any data type.
91
- * @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
92
- * a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
93
- * @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
94
- * the tree. It is an optional parameter and is used to keep track of the number of values associated
95
- * with a key in the tree.
96
- * @returns A new instance of the TreeCounterNode class, casted as TreeCounterNode<K, V>.
97
- */
98
- createNode(key, value, color = 'BLACK', count) {
116
+ _createNode(key, value, color = 'BLACK', count) {
99
117
  return new TreeCounterNode(key, this._isMapMode ? undefined : value, count, color);
100
118
  }
101
119
  /**
102
- * The function creates a new instance of a TreeCounter with the specified options and returns it.
103
- * @param [options] - The `options` parameter is an optional object that contains additional
104
- * configuration options for creating the `TreeCounter`. It is of type `TreeCounterOptions<K, V,
105
- * R>`.
106
- * @returns a new instance of the `TreeCounter` class, with the provided options merged with the
107
- * existing `iterationType` property. The returned value is casted as `TREE`.
108
- */
109
- createTree(options) {
110
- return new TreeCounter([], Object.assign({ iterationType: this.iterationType, specifyComparable: this._specifyComparable, isMapMode: this._isMapMode, toEntryFn: this._toEntryFn }, options));
111
- }
112
- /**
113
- * The function checks if the input is an instance of the TreeCounterNode class.
114
- * @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
115
- * `keyNodeOrEntry` can be of type `R` or `K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
116
- * @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
117
- * an instance of the `TreeCounterNode` class.
120
+ * Type guard: check whether the input is a TreeCounterNode.
121
+ * @remarks Time O(1), Space O(1)
122
+ * @returns True if the value is a TreeCounterNode.
118
123
  */
119
124
  isNode(keyNodeOrEntry) {
120
125
  return keyNodeOrEntry instanceof TreeCounterNode;
121
126
  }
122
127
  /**
123
- * Time Complexity: O(log n)
124
- * Space Complexity: O(1)
125
- *
126
- * The function overrides the add method of a class and adds a new node to a data structure, updating
127
- * the count and returning a boolean indicating success.
128
- * @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The
129
- * `keyNodeOrEntry` parameter can accept one of the following types:
130
- * @param {V} [value] - The `value` parameter represents the value associated with the key in the
131
- * data structure. It is an optional parameter, so it can be omitted if not needed.
132
- * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
133
- * be added to the data structure. By default, it is set to 1, meaning that if no value is provided
134
- * for `count`, the key-value pair will be added once.
135
- * @returns The method is returning a boolean value. It returns true if the addition of the new node
136
- * was successful, and false otherwise.
128
+ * Insert or increment a node and update aggregate count.
129
+ * @remarks Time O(log N), Space O(1)
130
+ * @param keyNodeOrEntry - Key, node, or [key, value] entry to insert.
131
+ * @param [value] - Value when a bare key is provided (ignored in map mode).
132
+ * @param [count] - How much to increase the node's count (default 1).
133
+ * @returns True if inserted/updated; false if ignored.
137
134
  */
138
135
  add(keyNodeOrEntry, value, count = 1) {
139
136
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
@@ -148,19 +145,11 @@ class TreeCounter extends red_black_tree_1.RedBlackTree {
148
145
  }
149
146
  }
150
147
  /**
151
- * Time Complexity: O(log n)
152
- * Space Complexity: O(1)
153
- *
154
- * The function `delete` in TypeScript overrides the deletion operation in a binary tree data
155
- * structure, handling cases where nodes have children and maintaining balance in the tree.
156
- * @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `predicate`
157
- * parameter in the `delete` method is used to specify the condition or key based on which a node
158
- * should be deleted from the binary tree. It can be a key, a node, or an entry.
159
- * @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
160
- * boolean flag that determines whether to ignore the count of nodes when performing deletion. If
161
- * `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
162
- * `ignoreCount` is `false
163
- * @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<TreeCounterNode<K, V>>` objects.
148
+ * Delete a node (or decrement its count) and rebalance if needed.
149
+ * @remarks Time O(log N), Space O(1)
150
+ * @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node.
151
+ * @param [ignoreCount] - If true, remove the node regardless of its count.
152
+ * @returns Array of deletion results including deleted node and a rebalance hint when present.
164
153
  */
165
154
  delete(keyNodeOrEntry, ignoreCount = false) {
166
155
  if (keyNodeOrEntry === null)
@@ -252,7 +241,6 @@ class TreeCounter extends red_black_tree_1.RedBlackTree {
252
241
  }
253
242
  }
254
243
  this._size--;
255
- // If the original color was black, fix the tree
256
244
  if (originalColor === 'BLACK') {
257
245
  this._deleteFixup(replacementNode);
258
246
  }
@@ -260,119 +248,111 @@ class TreeCounter extends red_black_tree_1.RedBlackTree {
260
248
  return results;
261
249
  }
262
250
  /**
263
- * Time Complexity: O(1)
264
- * Space Complexity: O(1)
265
- *
266
- * The "clear" function overrides the parent class's "clear" function and also resets the count to
267
- * zero.
251
+ * Remove all nodes and reset aggregate counters.
252
+ * @remarks Time O(N), Space O(1)
253
+ * @returns void
268
254
  */
269
255
  clear() {
270
256
  super.clear();
271
257
  this._count = 0;
272
258
  }
273
259
  /**
274
- * Time Complexity: O(n log n)
275
- * Space Complexity: O(log n)
276
- *
277
- * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
278
- * tree using either a recursive or iterative approach.
279
- * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
280
- * specifies the type of iteration to use when building the balanced binary search tree. It has a
281
- * default value of `this.iterationType`, which means it will use the iteration type specified by the
282
- * `iterationType` property of the current object.
283
- * @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
284
- * balancing operation is successful, and `false` if there are no nodes to balance.
260
+ * Rebuild the tree into a perfectly balanced form using in-order nodes.
261
+ * @remarks Time O(N), Space O(N)
262
+ * @param [iterationType] - Traversal style to use when constructing the balanced tree.
263
+ * @returns True if rebalancing succeeded (tree not empty).
285
264
  */
286
265
  perfectlyBalance(iterationType = this.iterationType) {
287
- const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
288
- if (sorted.length < 1)
266
+ const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
267
+ const n = nodes.length;
268
+ if (n < 1)
289
269
  return false;
290
- this.clear();
291
- if (iterationType === 'RECURSIVE') {
292
- const buildBalanceBST = (l, r) => {
293
- if (l > r)
294
- return;
295
- const m = l + Math.floor((r - l) / 2);
296
- const midNode = sorted[m];
297
- if (this._isMapMode && midNode !== null)
298
- this.add(midNode.key, undefined, midNode.count);
299
- else if (midNode !== null)
300
- this.add(midNode.key, midNode.value, midNode.count);
301
- buildBalanceBST(l, m - 1);
302
- buildBalanceBST(m + 1, r);
303
- };
304
- buildBalanceBST(0, n - 1);
305
- return true;
306
- }
307
- else {
308
- const stack = [[0, n - 1]];
309
- while (stack.length > 0) {
310
- const popped = stack.pop();
311
- if (popped) {
312
- const [l, r] = popped;
313
- if (l <= r) {
314
- const m = l + Math.floor((r - l) / 2);
315
- const midNode = sorted[m];
316
- if (this._isMapMode && midNode !== null)
317
- this.add(midNode.key, undefined, midNode.count);
318
- else if (midNode !== null)
319
- this.add(midNode.key, midNode.value, midNode.count);
320
- stack.push([m + 1, r]);
321
- stack.push([l, m - 1]);
322
- }
323
- }
324
- }
325
- return true;
270
+ let total = 0;
271
+ for (const nd of nodes)
272
+ total += nd ? nd.count : 0;
273
+ this._clearNodes();
274
+ const build = (l, r, parent) => {
275
+ if (l > r)
276
+ return undefined;
277
+ const m = l + ((r - l) >> 1);
278
+ const root = nodes[m];
279
+ const leftChild = build(l, m - 1, root);
280
+ const rightChild = build(m + 1, r, root);
281
+ root.left = leftChild;
282
+ root.right = rightChild;
283
+ root.parent = parent;
284
+ return root;
285
+ };
286
+ const newRoot = build(0, n - 1, undefined);
287
+ this._setRoot(newRoot);
288
+ this._size = n;
289
+ this._count = total;
290
+ return true;
291
+ }
292
+ /**
293
+ * Create a new TreeCounter by mapping each [key, value] entry.
294
+ * @remarks Time O(N log N), Space O(N)
295
+ * @template MK
296
+ * @template MV
297
+ * @template MR
298
+ * @param callback - Function mapping (key, value, index, tree) → [newKey, newValue].
299
+ * @param [options] - Options for the output tree.
300
+ * @param [thisArg] - Value for `this` inside the callback.
301
+ * @returns A new TreeCounter with mapped entries.
302
+ */
303
+ map(callback, options, thisArg) {
304
+ const out = this._createLike([], options);
305
+ let index = 0;
306
+ for (const [key, value] of this) {
307
+ out.add(callback.call(thisArg, key, value, index++, this));
326
308
  }
309
+ return out;
327
310
  }
328
311
  /**
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.
312
+ * Deep copy this tree, preserving map mode and aggregate counts.
313
+ * @remarks Time O(N), Space O(N)
314
+ * @returns A deep copy of this tree.
334
315
  */
335
316
  clone() {
336
- const cloned = this.createTree();
337
- this.bfs(node => cloned.add(node === null ? null : node.key, undefined, node === null ? 0 : node.count));
338
- if (this._isMapMode)
339
- cloned._store = this._store;
340
- return cloned;
317
+ const out = this._createInstance();
318
+ this._clone(out);
319
+ out._count = this._count;
320
+ return out;
341
321
  }
342
322
  /**
343
- * The `map` function in TypeScript overrides the default behavior to create a new TreeCounter with
344
- * modified entries based on a provided callback.
345
- * @param callback - The `callback` parameter is a function that will be called for each entry in the
346
- * map. It takes four arguments:
347
- * @param [options] - The `options` parameter in the `override map` function is of type
348
- * `TreeCounterOptions<MK, MV, MR>`. This parameter allows you to provide additional configuration
349
- * options when creating a new `TreeCounter` instance within the `map` function. These options could
350
- * include things like
351
- * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
352
- * the value of `this` when executing the `callback` function. It allows you to set the context
353
- * (value of `this`) for the callback function when it is called within the `map` function. This
354
- * @returns A new TreeCounter instance is being returned, which is populated with entries generated
355
- * by the provided callback function.
323
+ * (Protected) Create an empty instance of the same concrete class.
324
+ * @remarks Time O(1), Space O(1)
325
+ * @template TK
326
+ * @template TV
327
+ * @template TR
328
+ * @param [options] - Optional constructor options for the like-kind instance.
329
+ * @returns An empty like-kind instance.
356
330
  */
357
- map(callback, options, thisArg) {
358
- const newTree = new TreeCounter([], options);
359
- let index = 0;
360
- for (const [key, value] of this) {
361
- newTree.add(callback.call(thisArg, key, value, index++, this));
362
- }
363
- return newTree;
331
+ _createInstance(options) {
332
+ const Ctor = this.constructor;
333
+ return new Ctor([], Object.assign(Object.assign({}, this._snapshotOptions()), (options !== null && options !== void 0 ? options : {})));
334
+ }
335
+ /**
336
+ * (Protected) Create a like-kind instance and seed it from an iterable.
337
+ * @remarks Time O(N log N), Space O(N)
338
+ * @template TK
339
+ * @template TV
340
+ * @template TR
341
+ * @param iter - Iterable used to seed the new tree.
342
+ * @param [options] - Options merged with the current snapshot.
343
+ * @returns A like-kind TreeCounter built from the iterable.
344
+ */
345
+ _createLike(iter = [], options) {
346
+ const Ctor = this.constructor;
347
+ return new Ctor(iter, Object.assign(Object.assign({}, this._snapshotOptions()), (options !== null && options !== void 0 ? options : {})));
364
348
  }
365
349
  /**
366
- * The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
367
- * node based on the input.
368
- * @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
369
- * `keyNodeOrEntry` can be of type `R` or `K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
370
- * @param {V} [value] - The `value` parameter is an optional value that represents the value
371
- * associated with the key in the node. It is used when creating a new node or updating the value of
372
- * an existing node.
373
- * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
374
- * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
375
- * @returns either a TreeCounterNode<K, V> object or undefined.
350
+ * (Protected) Normalize input into a node plus its effective value and count.
351
+ * @remarks Time O(1), Space O(1)
352
+ * @param keyNodeOrEntry - Key, node, or [key, value] entry.
353
+ * @param [value] - Value used when a bare key is provided.
354
+ * @param [count] - Count increment to apply (default 1).
355
+ * @returns Tuple [node, value] where node may be undefined.
376
356
  */
377
357
  _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count = 1) {
378
358
  if (keyNodeOrEntry === undefined || keyNodeOrEntry === null)
@@ -384,30 +364,23 @@ class TreeCounter extends red_black_tree_1.RedBlackTree {
384
364
  if (key === undefined || key === null)
385
365
  return [undefined, undefined];
386
366
  const finalValue = value !== null && value !== void 0 ? value : entryValue;
387
- return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
367
+ return [this._createNode(key, finalValue, 'BLACK', count), finalValue];
388
368
  }
389
- return [this.createNode(keyNodeOrEntry, value, 'BLACK', count), value];
369
+ return [this._createNode(keyNodeOrEntry, value, 'BLACK', count), value];
390
370
  }
391
371
  /**
392
- * Time Complexity: O(1)
393
- * Space Complexity: O(1)
394
- *
395
- * The `_swapProperties` function swaps the properties (key, value, count, color) between two nodes
396
- * in a binary search tree.
397
- * @param {R | BSTNOptKeyOrNode<K, TreeCounterNode<K, V>>} srcNode - The `srcNode` parameter represents the source node
398
- * that will be swapped with the `destNode`. It can be either an instance of the `R` class or an
399
- * instance of the `BSTNOptKeyOrNode<K, TreeCounterNode<K, V>>` class.
400
- * @param {R | BSTNOptKeyOrNode<K, TreeCounterNode<K, V>>} destNode - The `destNode` parameter represents the destination
401
- * node where the properties will be swapped with the source node.
402
- * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
403
- * If either `srcNode` or `destNode` is undefined, it returns undefined.
372
+ * (Protected) Swap keys/values/counters between the source and destination nodes.
373
+ * @remarks Time O(1), Space O(1)
374
+ * @param srcNode - Source node (or key) whose properties will be moved.
375
+ * @param destNode - Destination node (or key) to receive properties.
376
+ * @returns Destination node after swap, or undefined.
404
377
  */
405
378
  _swapProperties(srcNode, destNode) {
406
379
  srcNode = this.ensureNode(srcNode);
407
380
  destNode = this.ensureNode(destNode);
408
381
  if (srcNode && destNode) {
409
382
  const { key, value, count, color } = destNode;
410
- const tempNode = this.createNode(key, value, color, count);
383
+ const tempNode = this._createNode(key, value, color, count);
411
384
  if (tempNode) {
412
385
  tempNode.color = color;
413
386
  destNode.key = srcNode.key;
@@ -426,15 +399,11 @@ class TreeCounter extends red_black_tree_1.RedBlackTree {
426
399
  return undefined;
427
400
  }
428
401
  /**
429
- * Time Complexity: O(1)
430
- * Space Complexity: O(1)
431
- *
432
- * The function replaces an old node with a new node and updates the count property of the new node.
433
- * @param {TreeCounterNode<K, V>} oldNode - The `oldNode` parameter is the node that you want to replace in the data
434
- * structure.
435
- * @param {TreeCounterNode<K, V>} newNode - The `newNode` parameter is an instance of the `TreeCounterNode<K, V>` class.
436
- * @returns The method is returning the result of calling the `_replaceNode` method from the
437
- * superclass, which is of type `TreeCounterNode<K, V>`.
402
+ * (Protected) Replace one node by another and adjust counters accordingly.
403
+ * @remarks Time O(1), Space O(1)
404
+ * @param oldNode - Node being replaced.
405
+ * @param newNode - Replacement node.
406
+ * @returns The new node after replacement.
438
407
  */
439
408
  _replaceNode(oldNode, newNode) {
440
409
  newNode.count = oldNode.count + newNode.count;