graph-typed 2.0.5 → 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 +598 -869
  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 +1 -0
  64. package/dist/utils/utils.d.ts +1 -1
  65. package/dist/utils/utils.js +2 -1
  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 +664 -893
  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 +2 -0
  101. package/src/utils/utils.ts +9 -14
@@ -1,17 +1,28 @@
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.AVLTreeCounter = exports.AVLTreeCounterNode = void 0;
4
11
  const avl_tree_1 = require("./avl-tree");
12
+ /**
13
+ * AVL 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 AVLTreeCounterNode extends avl_tree_1.AVLTreeNode {
6
19
  /**
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.
20
+ * Create an AVL counter node.
21
+ * @remarks Time O(1), Space O(1)
22
+ * @param key - Key of the node.
23
+ * @param [value] - Associated value (ignored in map mode).
24
+ * @param [count] - Initial count for this node (default 1).
25
+ * @returns New AVLTreeCounterNode instance.
15
26
  */
16
27
  constructor(key, value, count = 1) {
17
28
  super(key, value);
@@ -20,18 +31,40 @@ class AVLTreeCounterNode extends avl_tree_1.AVLTreeNode {
20
31
  this._right = undefined;
21
32
  this.count = count;
22
33
  }
34
+ /**
35
+ * Get the left child pointer.
36
+ * @remarks Time O(1), Space O(1)
37
+ * @returns Left child node, or null/undefined.
38
+ */
23
39
  get left() {
24
40
  return this._left;
25
41
  }
42
+ /**
43
+ * Set the left child and update its parent pointer.
44
+ * @remarks Time O(1), Space O(1)
45
+ * @param v - New left child node, or null/undefined.
46
+ * @returns void
47
+ */
26
48
  set left(v) {
27
49
  if (v) {
28
50
  v.parent = this;
29
51
  }
30
52
  this._left = v;
31
53
  }
54
+ /**
55
+ * Get the right child pointer.
56
+ * @remarks Time O(1), Space O(1)
57
+ * @returns Right child node, or null/undefined.
58
+ */
32
59
  get right() {
33
60
  return this._right;
34
61
  }
62
+ /**
63
+ * Set the right child and update its parent pointer.
64
+ * @remarks Time O(1), Space O(1)
65
+ * @param v - New right child node, or null/undefined.
66
+ * @returns void
67
+ */
35
68
  set right(v) {
36
69
  if (v) {
37
70
  v.parent = this;
@@ -41,16 +74,19 @@ class AVLTreeCounterNode extends avl_tree_1.AVLTreeNode {
41
74
  }
42
75
  exports.AVLTreeCounterNode = AVLTreeCounterNode;
43
76
  /**
44
- * 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.
77
+ * AVL tree that tracks an aggregate 'count' across nodes; supports balanced insert/delete and map-like mode.
78
+ * @remarks Time O(1), Space O(1)
79
+ * @template K
80
+ * @template V
81
+ * @template R
45
82
  */
46
83
  class AVLTreeCounter extends avl_tree_1.AVLTree {
47
84
  /**
48
- * The constructor initializes a new AVLTreeCounter object with optional initial elements.
49
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
50
- * iterable object that can contain either keys, nodes, entries, or raw elements.
51
- * @param [options] - The `options` parameter is an optional object that can be used to customize the
52
- * behavior of the AVLTreeCounter. It can include properties such as `compareKeys` and
53
- * `compareValues` functions to define custom comparison logic for keys and values, respectively.
85
+ * Create a AVLTreeCounter instance
86
+ * @remarks Time O(n), Space O(n)
87
+ * @param keysNodesEntriesOrRaws
88
+ * @param options
89
+ * @returns New AVLTreeCounterNode instance.
54
90
  */
55
91
  constructor(keysNodesEntriesOrRaws = [], options) {
56
92
  super([], options);
@@ -58,77 +94,37 @@ class AVLTreeCounter extends avl_tree_1.AVLTree {
58
94
  if (keysNodesEntriesOrRaws)
59
95
  this.addMany(keysNodesEntriesOrRaws);
60
96
  }
61
- /**
62
- * The function calculates the sum of the count property of all nodes in a tree using depth-first
63
- * search.
64
- * @returns the sum of the count property of all nodes in the tree.
65
- */
66
97
  get count() {
67
98
  return this._count;
68
99
  }
69
100
  /**
70
- * Time Complexity: O(n)
71
- * Space Complexity: O(1)
72
- *
73
- * The function calculates the sum of the count property of all nodes in a tree using depth-first
74
- * search.
75
- * @returns the sum of the count property of all nodes in the tree.
101
+ * Compute the total count by traversing the tree (sums node.count).
102
+ * @remarks Time O(N), Space O(H)
103
+ * @returns Total count recomputed from nodes.
76
104
  */
77
105
  getComputedCount() {
78
106
  let sum = 0;
79
107
  this.dfs(node => (sum += node.count));
80
108
  return sum;
81
109
  }
82
- /**
83
- * The function creates a new AVLTreeCounterNode with the specified key, value, and count.
84
- * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
85
- * which is a generic type that can be replaced with any specific type when using the function.
86
- * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
87
- * associated with the key in the node. It is of type `V`, which can be any data type.
88
- * @param {number} [count] - The `count` parameter represents the number of occurrences of a
89
- * key-value pair in the AVLTreeCounterNode. It is an optional parameter, so it can be omitted when
90
- * calling the `createNode` method. If provided, it specifies the initial count for the node.
91
- * @returns a new instance of the AVLTreeCounterNode class, casted as AVLTreeCounterNode<K, V>.
92
- */
93
- createNode(key, value, count) {
110
+ _createNode(key, value, count) {
94
111
  return new AVLTreeCounterNode(key, this._isMapMode ? undefined : value, count);
95
112
  }
96
113
  /**
97
- * The function creates a new AVLTreeCounter object with the specified options and returns it.
98
- * @param [options] - The `options` parameter is an optional object that contains additional
99
- * configuration options for creating the AVLTreeCounter. It can have the following properties:
100
- * @returns a new instance of the AVLTreeCounter class, with the specified options, as a TREE
101
- * object.
102
- */
103
- createTree(options) {
104
- return new AVLTreeCounter([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
105
- }
106
- /**
107
- * The function checks if the input is an instance of AVLTreeCounterNode.
108
- * @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
109
- * `keyNodeOrEntry` can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
110
- * @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
111
- * an instance of the `AVLTreeCounterNode` class.
114
+ * Type guard: check whether the input is an AVLTreeCounterNode.
115
+ * @remarks Time O(1), Space O(1)
116
+ * @returns True if the value is an AVLTreeCounterNode.
112
117
  */
113
118
  isNode(keyNodeOrEntry) {
114
119
  return keyNodeOrEntry instanceof AVLTreeCounterNode;
115
120
  }
116
121
  /**
117
- * Time Complexity: O(log n)
118
- * Space Complexity: O(1)
119
- *
120
- * The function overrides the add method of a TypeScript class to add a new node to a data structure
121
- * and update the count.
122
- * @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The
123
- * `keyNodeOrEntry` parameter can accept a value of type `R`, which can be any type. It
124
- * can also accept a value of type `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`, which represents a key, node,
125
- * entry, or raw element
126
- * @param {V} [value] - The `value` parameter represents the value associated with the key in the
127
- * data structure. It is an optional parameter, so it can be omitted if not needed.
128
- * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
129
- * be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
130
- * be added once. However, you can specify a different value for `count` if you want to add
131
- * @returns a boolean value.
122
+ * Insert or increment a node and update aggregate count.
123
+ * @remarks Time O(log N), Space O(1)
124
+ * @param keyNodeOrEntry - Key, node, or [key, value] entry to insert.
125
+ * @param [value] - Value when a bare key is provided (ignored in map mode).
126
+ * @param [count] - How much to increase the node's count (default 1).
127
+ * @returns True if inserted/updated; false if ignored.
132
128
  */
133
129
  add(keyNodeOrEntry, value, count = 1) {
134
130
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
@@ -142,23 +138,11 @@ class AVLTreeCounter extends avl_tree_1.AVLTree {
142
138
  return true;
143
139
  }
144
140
  /**
145
- * Time Complexity: O(log n)
146
- * Space Complexity: O(1)
147
- *
148
- * The function overrides the delete method in a binary tree data structure, handling deletion of
149
- * nodes and maintaining balance in the tree.
150
- * @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `predicate`
151
- * parameter in the `delete` method is used to specify the condition for deleting a node from the
152
- * binary tree. It can be a key, node, or entry that determines which
153
- * node(s) should be deleted.
154
- * @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
155
- * boolean flag that determines whether to ignore the count of the node being deleted. If
156
- * `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
157
- * `ignoreCount` is set to
158
- * @returns The `delete` method overrides the default delete behavior in a binary tree data
159
- * structure. It takes a predicate or node to be deleted and an optional flag to ignore count. The
160
- * method returns an array of `BinaryTreeDeleteResult` objects, each containing information about the
161
- * deleted node and whether balancing is needed in the tree.
141
+ * Delete a node (or decrement its count) and rebalance if needed.
142
+ * @remarks Time O(log N), Space O(1)
143
+ * @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node.
144
+ * @param [ignoreCount] - If true, remove the node regardless of its count.
145
+ * @returns Array of deletion results including deleted node and a rebalance hint when present.
162
146
  */
163
147
  delete(keyNodeOrEntry, ignoreCount = false) {
164
148
  var _a;
@@ -208,7 +192,6 @@ class AVLTreeCounter extends avl_tree_1.AVLTree {
208
192
  }
209
193
  }
210
194
  this._size = this._size - 1;
211
- // TODO How to handle when the count of target node is lesser than current node's count
212
195
  if (orgCurrent)
213
196
  this._count -= orgCurrent.count;
214
197
  }
@@ -219,125 +202,118 @@ class AVLTreeCounter extends avl_tree_1.AVLTree {
219
202
  return deletedResult;
220
203
  }
221
204
  /**
222
- * Time Complexity: O(1)
223
- * Space Complexity: O(1)
224
- *
225
- * The "clear" function overrides the parent class's "clear" function and also resets the count to
226
- * zero.
205
+ * Remove all nodes and reset aggregate counters.
206
+ * @remarks Time O(N), Space O(1)
207
+ * @returns void
227
208
  */
228
209
  clear() {
229
210
  super.clear();
230
211
  this._count = 0;
231
212
  }
232
213
  /**
233
- * Time Complexity: O(n log n)
234
- * Space Complexity: O(log n)
235
- *
236
- * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
237
- * tree using either a recursive or iterative approach.
238
- * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
239
- * specifies the type of iteration to use when building the balanced binary search tree. It has a
240
- * default value of `this.iterationType`, which means it will use the iteration type currently set in
241
- * the object.
242
- * @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
243
- * balancing operation is successful, and `false` if there are no nodes to balance.
214
+ * Rebuild the tree into a perfectly balanced form using in-order nodes.
215
+ * @remarks Time O(N), Space O(N)
216
+ * @param [iterationType] - Traversal style to use when constructing the balanced tree.
217
+ * @returns True if rebalancing succeeded (tree not empty).
244
218
  */
245
219
  perfectlyBalance(iterationType = this.iterationType) {
246
- const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
247
- if (sorted.length < 1)
220
+ const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
221
+ const n = nodes.length;
222
+ if (n === 0)
248
223
  return false;
249
- this.clear();
250
- if (iterationType === 'RECURSIVE') {
251
- const buildBalanceBST = (l, r) => {
252
- if (l > r)
253
- return;
254
- const m = l + Math.floor((r - l) / 2);
255
- const midNode = sorted[m];
256
- if (this._isMapMode)
257
- this.add(midNode.key, undefined, midNode.count);
258
- else
259
- this.add(midNode.key, midNode.value, midNode.count);
260
- buildBalanceBST(l, m - 1);
261
- buildBalanceBST(m + 1, r);
262
- };
263
- buildBalanceBST(0, n - 1);
264
- return true;
265
- }
266
- else {
267
- const stack = [[0, n - 1]];
268
- while (stack.length > 0) {
269
- const popped = stack.pop();
270
- if (popped) {
271
- const [l, r] = popped;
272
- if (l <= r) {
273
- const m = l + Math.floor((r - l) / 2);
274
- const midNode = sorted[m];
275
- if (this._isMapMode)
276
- this.add(midNode.key, undefined, midNode.count);
277
- else
278
- this.add(midNode.key, midNode.value, midNode.count);
279
- stack.push([m + 1, r]);
280
- stack.push([l, m - 1]);
281
- }
282
- }
283
- }
284
- return true;
285
- }
224
+ let total = 0;
225
+ for (const nd of nodes)
226
+ total += nd ? nd.count : 0;
227
+ this._clearNodes();
228
+ const build = (l, r, parent) => {
229
+ if (l > r)
230
+ return undefined;
231
+ const m = l + ((r - l) >> 1);
232
+ const root = nodes[m];
233
+ root.left = build(l, m - 1, root);
234
+ root.right = build(m + 1, r, root);
235
+ root.parent = parent;
236
+ const lh = root.left ? root.left.height : -1;
237
+ const rh = root.right ? root.right.height : -1;
238
+ root.height = Math.max(lh, rh) + 1;
239
+ return root;
240
+ };
241
+ const newRoot = build(0, n - 1, undefined);
242
+ this._setRoot(newRoot);
243
+ this._size = n;
244
+ this._count = total;
245
+ return true;
286
246
  }
287
247
  /**
288
- * Time complexity: O(n)
289
- * Space complexity: O(n)
290
- *
291
- * The function overrides the clone method to create a deep copy of a tree object.
292
- * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
248
+ * Deep copy this tree, preserving map mode and aggregate counts.
249
+ * @remarks Time O(N), Space O(N)
250
+ * @returns A deep copy of this tree.
293
251
  */
294
252
  clone() {
295
- const cloned = this.createTree();
296
- if (this._isMapMode)
297
- this.bfs(node => cloned.add(node.key, undefined, node.count));
298
- else
299
- this.bfs(node => cloned.add(node.key, node.value, node.count));
253
+ const out = this._createInstance();
254
+ if (this._isMapMode) {
255
+ this.bfs(node => out.add(node.key, undefined, node.count));
256
+ }
257
+ else {
258
+ this.bfs(node => out.add(node.key, node.value, node.count));
259
+ }
300
260
  if (this._isMapMode)
301
- cloned._store = this._store;
302
- return cloned;
261
+ out._store = this._store;
262
+ return out;
303
263
  }
304
264
  /**
305
- * The `map` function in TypeScript overrides the default behavior to create a new AVLTreeCounter
306
- * with modified entries based on a provided callback.
307
- * @param callback - The `callback` parameter is a function that will be called for each entry in the
308
- * AVLTreeCounter. It takes four arguments:
309
- * @param [options] - The `options` parameter in the `override map` function is of type
310
- * `AVLTreeCounterOptions<MK, MV, MR>`. This parameter allows you to provide additional
311
- * configuration options when creating a new `AVLTreeCounter` instance within the `map` function.
312
- * These options
313
- * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
314
- * the value of `this` when executing the `callback` function. It allows you to set the context
315
- * (value of `this`) for the callback function. This can be useful when you want to access properties
316
- * or
317
- * @returns The `map` method is returning a new `AVLTreeCounter` instance with the entries
318
- * transformed by the provided `callback` function. Each entry in the original tree is passed to the
319
- * `callback` function along with the index and the original tree itself. The transformed entries are
320
- * then added to the new `AVLTreeCounter` instance, which is returned at the end.
265
+ * Create a new AVLTreeCounter by mapping each [key, value] entry.
266
+ * @remarks Time O(N log N), Space O(N)
267
+ * @template MK
268
+ * @template MV
269
+ * @template MR
270
+ * @param callback - Function mapping (key, value, index, tree) → [newKey, newValue].
271
+ * @param [options] - Options for the output tree.
272
+ * @param [thisArg] - Value for `this` inside the callback.
273
+ * @returns A new AVLTreeCounter with mapped entries.
321
274
  */
322
275
  map(callback, options, thisArg) {
323
- const newTree = new AVLTreeCounter([], options);
276
+ const out = this._createLike([], options);
324
277
  let index = 0;
325
278
  for (const [key, value] of this) {
326
- newTree.add(callback.call(thisArg, key, value, index++, this));
279
+ out.add(callback.call(thisArg, key, value, index++, this));
327
280
  }
328
- return newTree;
281
+ return out;
282
+ }
283
+ /**
284
+ * (Protected) Create an empty instance of the same concrete class.
285
+ * @remarks Time O(1), Space O(1)
286
+ * @template TK
287
+ * @template TV
288
+ * @template TR
289
+ * @param [options] - Optional constructor options for the like-kind instance.
290
+ * @returns An empty like-kind instance.
291
+ */
292
+ _createInstance(options) {
293
+ const Ctor = this.constructor;
294
+ return new Ctor([], Object.assign(Object.assign({}, this._snapshotOptions()), (options !== null && options !== void 0 ? options : {})));
295
+ }
296
+ /**
297
+ * (Protected) Create a like-kind instance and seed it from an iterable.
298
+ * @remarks Time O(N log N), Space O(N)
299
+ * @template TK
300
+ * @template TV
301
+ * @template TR
302
+ * @param iter - Iterable used to seed the new tree.
303
+ * @param [options] - Options merged with the current snapshot.
304
+ * @returns A like-kind AVLTreeCounter built from the iterable.
305
+ */
306
+ _createLike(iter = [], options) {
307
+ const Ctor = this.constructor;
308
+ return new Ctor(iter, Object.assign(Object.assign({}, this._snapshotOptions()), (options !== null && options !== void 0 ? options : {})));
329
309
  }
330
310
  /**
331
- * The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
332
- * a node object.
333
- * @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The
334
- * `keyNodeOrEntry` parameter can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
335
- * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
336
- * `override` function. It represents the value associated with the key in the data structure. If no
337
- * value is provided, it will default to `undefined`.
338
- * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
339
- * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
340
- * @returns either a AVLTreeCounterNode<K, V> object or undefined.
311
+ * (Protected) Normalize input into a node plus its effective value and count.
312
+ * @remarks Time O(1), Space O(1)
313
+ * @param keyNodeOrEntry - Key, node, or [key, value] entry.
314
+ * @param [value] - Value used when a bare key is provided.
315
+ * @param [count] - Count increment to apply (default 1).
316
+ * @returns Tuple [node, value] where node may be undefined.
341
317
  */
342
318
  _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count = 1) {
343
319
  if (keyNodeOrEntry === undefined || keyNodeOrEntry === null)
@@ -349,29 +325,23 @@ class AVLTreeCounter extends avl_tree_1.AVLTree {
349
325
  if (key === undefined || key === null)
350
326
  return [undefined, undefined];
351
327
  const finalValue = value !== null && value !== void 0 ? value : entryValue;
352
- return [this.createNode(key, finalValue, count), finalValue];
328
+ return [this._createNode(key, finalValue, count), finalValue];
353
329
  }
354
- return [this.createNode(keyNodeOrEntry, value, count), value];
330
+ return [this._createNode(keyNodeOrEntry, value, count), value];
355
331
  }
356
332
  /**
357
- * Time Complexity: O(1)
358
- * Space Complexity: O(1)
359
- *
360
- * The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
361
- * in a binary search tree.
362
- * @param {BSTNOptKeyOrNode<K, AVLTreeCounterNode<K, V>>} srcNode - The `srcNode` parameter represents the source node
363
- * that will be swapped with the `destNode`.
364
- * @param {BSTNOptKeyOrNode<K, AVLTreeCounterNode<K, V>>} destNode - The `destNode` parameter represents the destination
365
- * node where the properties will be swapped with the source node.
366
- * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
367
- * If either `srcNode` or `destNode` is undefined, it returns `undefined`.
333
+ * (Protected) Swap keys/values/counters between the source and destination nodes.
334
+ * @remarks Time O(1), Space O(1)
335
+ * @param srcNode - Source node (or key) whose properties will be moved.
336
+ * @param destNode - Destination node (or key) to receive properties.
337
+ * @returns Destination node after swap, or undefined.
368
338
  */
369
339
  _swapProperties(srcNode, destNode) {
370
340
  srcNode = this.ensureNode(srcNode);
371
341
  destNode = this.ensureNode(destNode);
372
342
  if (srcNode && destNode) {
373
343
  const { key, value, count, height } = destNode;
374
- const tempNode = this.createNode(key, value, count);
344
+ const tempNode = this._createNode(key, value, count);
375
345
  if (tempNode) {
376
346
  tempNode.height = height;
377
347
  destNode.key = srcNode.key;
@@ -390,15 +360,11 @@ class AVLTreeCounter extends avl_tree_1.AVLTree {
390
360
  return undefined;
391
361
  }
392
362
  /**
393
- * Time Complexity: O(1)
394
- * Space Complexity: O(1)
395
- *
396
- * The function replaces an old node with a new node and updates the count property of the new node.
397
- * @param {AVLTreeCounterNode<K, V>} oldNode - The oldNode parameter represents the node that needs to be replaced in the
398
- * data structure. It is of type AVLTreeCounterNode<K, V>.
399
- * @param {AVLTreeCounterNode<K, V>} newNode - The `newNode` parameter is an instance of the `AVLTreeCounterNode<K, V>` class.
400
- * @returns The method is returning the result of calling the `_replaceNode` method from the
401
- * superclass, which is of type `AVLTreeCounterNode<K, V>`.
363
+ * (Protected) Replace one node by another and adjust counters accordingly.
364
+ * @remarks Time O(1), Space O(1)
365
+ * @param oldNode - Node being replaced.
366
+ * @param newNode - Replacement node.
367
+ * @returns The new node after replacement.
402
368
  */
403
369
  _replaceNode(oldNode, newNode) {
404
370
  newNode.count = oldNode.count + newNode.count;