binary-tree-typed 1.53.8 → 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 (96) hide show
  1. package/dist/data-structures/base/iterable-entry-base.js +4 -4
  2. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
  3. package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
  4. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -170
  5. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -328
  6. package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
  7. package/dist/data-structures/binary-tree/avl-tree.js +130 -70
  8. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
  9. package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
  10. package/dist/data-structures/binary-tree/binary-tree.d.ts +268 -202
  11. package/dist/data-structures/binary-tree/binary-tree.js +311 -263
  12. package/dist/data-structures/binary-tree/bst.d.ts +145 -121
  13. package/dist/data-structures/binary-tree/bst.js +195 -145
  14. package/dist/data-structures/binary-tree/index.d.ts +2 -0
  15. package/dist/data-structures/binary-tree/index.js +2 -0
  16. package/dist/data-structures/binary-tree/red-black-tree.d.ts +100 -72
  17. package/dist/data-structures/binary-tree/red-black-tree.js +127 -107
  18. package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
  19. package/dist/data-structures/binary-tree/tree-counter.js +444 -0
  20. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -170
  21. package/dist/data-structures/binary-tree/tree-multi-map.js +140 -362
  22. package/dist/data-structures/graph/abstract-graph.js +2 -2
  23. package/dist/data-structures/graph/directed-graph.d.ts +3 -0
  24. package/dist/data-structures/graph/directed-graph.js +3 -0
  25. package/dist/data-structures/graph/map-graph.d.ts +3 -0
  26. package/dist/data-structures/graph/map-graph.js +3 -0
  27. package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
  28. package/dist/data-structures/graph/undirected-graph.js +3 -0
  29. package/dist/data-structures/hash/hash-map.d.ts +1 -1
  30. package/dist/data-structures/hash/hash-map.js +5 -5
  31. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +10 -10
  32. package/dist/data-structures/linked-list/doubly-linked-list.js +12 -12
  33. package/dist/data-structures/linked-list/singly-linked-list.d.ts +13 -10
  34. package/dist/data-structures/linked-list/singly-linked-list.js +19 -16
  35. package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
  36. package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
  37. package/dist/data-structures/matrix/matrix.d.ts +3 -0
  38. package/dist/data-structures/matrix/matrix.js +3 -0
  39. package/dist/data-structures/matrix/navigator.d.ts +3 -0
  40. package/dist/data-structures/matrix/navigator.js +3 -0
  41. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
  42. package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
  43. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
  44. package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
  45. package/dist/data-structures/trie/trie.d.ts +0 -4
  46. package/dist/data-structures/trie/trie.js +0 -4
  47. package/dist/interfaces/binary-tree.d.ts +8 -8
  48. package/dist/types/data-structures/base/base.d.ts +1 -1
  49. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  50. package/dist/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
  51. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
  52. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
  53. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -3
  54. package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
  55. package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
  56. package/dist/types/data-structures/binary-tree/index.js +2 -0
  57. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -5
  58. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  59. package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
  60. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
  61. package/package.json +2 -2
  62. package/src/data-structures/base/iterable-entry-base.ts +4 -4
  63. package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
  64. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +151 -370
  65. package/src/data-structures/binary-tree/avl-tree.ts +162 -105
  66. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
  67. package/src/data-structures/binary-tree/binary-tree.ts +488 -416
  68. package/src/data-structures/binary-tree/bst.ts +270 -234
  69. package/src/data-structures/binary-tree/index.ts +2 -0
  70. package/src/data-structures/binary-tree/red-black-tree.ts +170 -145
  71. package/src/data-structures/binary-tree/tree-counter.ts +504 -0
  72. package/src/data-structures/binary-tree/tree-multi-map.ts +159 -401
  73. package/src/data-structures/graph/abstract-graph.ts +2 -2
  74. package/src/data-structures/graph/directed-graph.ts +3 -0
  75. package/src/data-structures/graph/map-graph.ts +3 -0
  76. package/src/data-structures/graph/undirected-graph.ts +3 -0
  77. package/src/data-structures/hash/hash-map.ts +7 -7
  78. package/src/data-structures/linked-list/doubly-linked-list.ts +13 -13
  79. package/src/data-structures/linked-list/singly-linked-list.ts +20 -17
  80. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  81. package/src/data-structures/matrix/matrix.ts +3 -0
  82. package/src/data-structures/matrix/navigator.ts +3 -0
  83. package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
  84. package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
  85. package/src/data-structures/trie/trie.ts +0 -4
  86. package/src/interfaces/binary-tree.ts +10 -21
  87. package/src/types/data-structures/base/base.ts +1 -1
  88. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
  89. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
  90. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
  91. package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
  92. package/src/types/data-structures/binary-tree/bst.ts +6 -6
  93. package/src/types/data-structures/binary-tree/index.ts +2 -0
  94. package/src/types/data-structures/binary-tree/rb-tree.ts +2 -7
  95. package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
  96. package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
@@ -4,33 +4,39 @@ exports.RedBlackTree = exports.RedBlackTreeNode = void 0;
4
4
  const bst_1 = require("./bst");
5
5
  class RedBlackTreeNode extends bst_1.BSTNode {
6
6
  /**
7
- * The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a
8
- * color.
9
- * @param {K} key - The key parameter is of type K and represents the key of the node in the
10
- * Red-Black Tree.
11
- * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
12
- * associated with the key in the Red-Black Tree Node. It is not required and can be omitted when
13
- * creating a new instance of the Red-Black Tree Node.
14
- * @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black
15
- * Tree Node. It is an optional parameter with a default value of `'BLACK'`.
7
+ * The constructor initializes a node with a key, value, and color for a Red-Black Tree.
8
+ * @param {K} key - The `key` parameter is a key of type `K` that is used to identify the node in a
9
+ * Red-Black Tree data structure.
10
+ * @param {V} [value] - The `value` parameter in the constructor is an optional parameter of type
11
+ * `V`. It represents the value associated with the key in the data structure being constructed.
12
+ * @param {RBTNColor} [color=BLACK] - The `color` parameter in the constructor is used to specify the
13
+ * color of the node in a Red-Black Tree. It has a default value of 'BLACK' if not provided
14
+ * explicitly.
16
15
  */
17
16
  constructor(key, value, color = 'BLACK') {
18
17
  super(key, value);
18
+ this.parent = undefined;
19
+ this._left = undefined;
20
+ this._right = undefined;
19
21
  this._color = color;
20
22
  }
21
- /**
22
- * The function returns the color value of a variable.
23
- * @returns The color value stored in the private variable `_color`.
24
- */
25
- get color() {
26
- return this._color;
23
+ get left() {
24
+ return this._left;
27
25
  }
28
- /**
29
- * The function sets the color property to the specified value.
30
- * @param {RBTNColor} value - The value parameter is of type RBTNColor.
31
- */
32
- set color(value) {
33
- this._color = value;
26
+ set left(v) {
27
+ if (v) {
28
+ v.parent = this;
29
+ }
30
+ this._left = v;
31
+ }
32
+ get right() {
33
+ return this._right;
34
+ }
35
+ set right(v) {
36
+ if (v) {
37
+ v.parent = this;
38
+ }
39
+ this._right = v;
34
40
  }
35
41
  }
36
42
  exports.RedBlackTreeNode = RedBlackTreeNode;
@@ -89,14 +95,15 @@ exports.RedBlackTreeNode = RedBlackTreeNode;
89
95
  */
90
96
  class RedBlackTree extends bst_1.BST {
91
97
  /**
92
- * This is the constructor function for a Red-Black Tree data structure in TypeScript.
93
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
94
- * iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
95
- * initialize the RBTree with the provided elements.
96
- * @param [options] - The `options` parameter is an optional object that can be passed to the
97
- * constructor. It is of type `RBTreeOptions<K, V, R>`. This object can contain various options for
98
- * configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
99
- * depend on the implementation
98
+ * This TypeScript constructor initializes a Red-Black Tree with optional keys, nodes, entries, or
99
+ * raw data.
100
+ * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
101
+ * iterable that can contain either `BTNRep<K, V, RedBlackTreeNode<K, V>>` objects or `R` objects. It
102
+ * is used to initialize the Red-Black Tree with keys, nodes, entries, or
103
+ * @param [options] - The `options` parameter in the constructor is of type `RedBlackTreeOptions<K,
104
+ * V, R>`. It is an optional parameter that allows you to specify additional options for the
105
+ * RedBlackTree class. These options could include configuration settings, behavior customization, or
106
+ * any other parameters that are specific to
100
107
  */
101
108
  constructor(keysNodesEntriesOrRaws = [], options) {
102
109
  super([], options);
@@ -105,14 +112,13 @@ class RedBlackTree extends bst_1.BST {
105
112
  this.addMany(keysNodesEntriesOrRaws);
106
113
  }
107
114
  }
108
- /**
109
- * The function returns the root node of a tree or undefined if there is no root.
110
- * @returns The root node of the tree structure, or undefined if there is no root node.
111
- */
112
115
  get root() {
113
116
  return this._root;
114
117
  }
115
118
  /**
119
+ * Time Complexity: O(1)
120
+ * Space Complexity: O(1)
121
+ *
116
122
  * The function creates a new Red-Black Tree node with the specified key, value, and color.
117
123
  * @param {K} key - The key parameter represents the key value of the node being created. It is of
118
124
  * type K, which is a generic type that can be replaced with any specific type when using the
@@ -130,62 +136,30 @@ class RedBlackTree extends bst_1.BST {
130
136
  return new RedBlackTreeNode(key, this._isMapMode ? undefined : value, color);
131
137
  }
132
138
  /**
139
+ * Time Complexity: O(1)
140
+ * Space Complexity: O(1)
141
+ *
133
142
  * The function creates a new Red-Black Tree with the specified options.
134
143
  * @param [options] - The `options` parameter is an optional object that contains additional
135
144
  * configuration options for creating the Red-Black Tree. It has the following properties:
136
145
  * @returns a new instance of a RedBlackTree object.
137
146
  */
138
147
  createTree(options) {
139
- return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, extractComparable: this._extractComparable, toEntryFn: this._toEntryFn }, options));
148
+ return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn }, options));
140
149
  }
141
150
  /**
142
151
  * Time Complexity: O(1)
143
152
  * Space Complexity: O(1)
144
153
  *
145
154
  * The function checks if the input is an instance of the RedBlackTreeNode class.
146
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
147
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
148
- * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
155
+ * @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter
156
+ * `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`.
157
+ * @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
149
158
  * an instance of the `RedBlackTreeNode` class.
150
159
  */
151
- isNode(keyNodeEntryOrRaw) {
152
- return keyNodeEntryOrRaw instanceof RedBlackTreeNode;
160
+ isNode(keyNodeOrEntry) {
161
+ return keyNodeOrEntry instanceof RedBlackTreeNode;
153
162
  }
154
- // /**
155
- // * Time Complexity: O(1)
156
- // * Space Complexity: O(1)
157
- // */
158
- //
159
- // /**
160
- // * Time Complexity: O(1)
161
- // * Space Complexity: O(1)
162
- // *
163
- // * The function `keyValueNodeEntryRawToNodeAndValue` takes a key, value, or entry and returns a node if it is
164
- // * valid, otherwise it returns undefined.
165
- // * @param {BTNRep<K, V, NODE>} keyNodeEntryOrRaw - The key, value, or entry to convert.
166
- // * @param {V} [value] - The value associated with the key (if `keyNodeEntryOrRaw` is a key).
167
- // * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
168
- // */
169
- // override keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): NODE | undefined {
170
- //
171
- // if (keyNodeEntryOrRaw === null || keyNodeEntryOrRaw === undefined) return;
172
- // if (this.isNode(keyNodeEntryOrRaw)) return keyNodeEntryOrRaw;
173
- //
174
- // if (this._toEntryFn) {
175
- // const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw as R);
176
- // if (this.isKey(key)) return this.createNode(key, value ?? entryValue, 'RED');
177
- // }
178
- //
179
- // if (this.isEntry(keyNodeEntryOrRaw)) {
180
- // const [key, value] = keyNodeEntryOrRaw;
181
- // if (key === undefined || key === null) return;
182
- // else return this.createNode(key, value, 'RED');
183
- // }
184
- //
185
- // if (this.isKey(keyNodeEntryOrRaw)) return this.createNode(keyNodeEntryOrRaw, value, 'RED');
186
- //
187
- // return ;
188
- // }
189
163
  /**
190
164
  * Time Complexity: O(1)
191
165
  * Space Complexity: O(1)
@@ -199,12 +173,12 @@ class RedBlackTree extends bst_1.BST {
199
173
  }
200
174
  /**
201
175
  * Time Complexity: O(log n)
202
- * Space Complexity: O(1)
176
+ * Space Complexity: O(log n)
203
177
  *
204
178
  * The function adds a new node to a binary search tree and returns true if the node was successfully
205
179
  * added.
206
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
207
- * `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`.
180
+ * @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter
181
+ * `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`.
208
182
  * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
209
183
  * the key in the data structure. It represents the value that you want to add or update in the data
210
184
  * structure.
@@ -212,8 +186,8 @@ class RedBlackTree extends bst_1.BST {
212
186
  * the method returns true. If the node already exists and its value is updated, the method also
213
187
  * returns true. If the node cannot be added or updated, the method returns false.
214
188
  */
215
- add(keyNodeEntryOrRaw, value) {
216
- const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
189
+ add(keyNodeOrEntry, value) {
190
+ const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
217
191
  if (!this.isRealNode(newNode))
218
192
  return false;
219
193
  const insertStatus = this._insert(newNode);
@@ -239,35 +213,37 @@ class RedBlackTree extends bst_1.BST {
239
213
  }
240
214
  /**
241
215
  * Time Complexity: O(log n)
242
- * Space Complexity: O(1)
216
+ * Space Complexity: O(log n)
243
217
  *
244
218
  * The function overrides the delete method in a binary tree data structure to remove a node based on
245
219
  * a given predicate and maintain the binary search tree properties.
246
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
220
+ * @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
247
221
  * parameter in the `override delete` method is used to specify the condition or key based on which a
248
222
  * node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
249
223
  * function that determines which node(s) should be deleted.
250
- * @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<NODE>`
224
+ * @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>`
251
225
  * objects. Each object in the array contains information about the deleted node and whether
252
226
  * balancing is needed.
253
227
  */
254
- delete(keyNodeEntryOrRaw) {
255
- if (keyNodeEntryOrRaw === null)
228
+ delete(keyNodeOrEntry) {
229
+ if (keyNodeOrEntry === null)
256
230
  return [];
257
231
  const results = [];
258
232
  let nodeToDelete;
259
- if (this._isPredicate(keyNodeEntryOrRaw))
260
- nodeToDelete = this.getNode(keyNodeEntryOrRaw);
233
+ if (this._isPredicate(keyNodeOrEntry))
234
+ nodeToDelete = this.getNode(keyNodeOrEntry);
261
235
  else
262
- nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw);
236
+ nodeToDelete = this.isRealNode(keyNodeOrEntry) ? keyNodeOrEntry : this.getNode(keyNodeOrEntry);
263
237
  if (!nodeToDelete) {
264
238
  return results;
265
239
  }
266
240
  let originalColor = nodeToDelete.color;
267
241
  let replacementNode;
268
242
  if (!this.isRealNode(nodeToDelete.left)) {
269
- replacementNode = nodeToDelete.right;
270
- this._transplant(nodeToDelete, nodeToDelete.right);
243
+ if (nodeToDelete.right !== null) {
244
+ replacementNode = nodeToDelete.right;
245
+ this._transplant(nodeToDelete, nodeToDelete.right);
246
+ }
271
247
  }
272
248
  else if (!this.isRealNode(nodeToDelete.right)) {
273
249
  replacementNode = nodeToDelete.left;
@@ -277,15 +253,18 @@ class RedBlackTree extends bst_1.BST {
277
253
  const successor = this.getLeftMost(node => node, nodeToDelete.right);
278
254
  if (successor) {
279
255
  originalColor = successor.color;
280
- replacementNode = successor.right;
256
+ if (successor.right !== null)
257
+ replacementNode = successor.right;
281
258
  if (successor.parent === nodeToDelete) {
282
259
  if (this.isRealNode(replacementNode)) {
283
260
  replacementNode.parent = successor;
284
261
  }
285
262
  }
286
263
  else {
287
- this._transplant(successor, successor.right);
288
- successor.right = nodeToDelete.right;
264
+ if (successor.right !== null) {
265
+ this._transplant(successor, successor.right);
266
+ successor.right = nodeToDelete.right;
267
+ }
289
268
  if (this.isRealNode(successor.right)) {
290
269
  successor.right.parent = successor;
291
270
  }
@@ -308,13 +287,54 @@ class RedBlackTree extends bst_1.BST {
308
287
  results.push({ deleted: nodeToDelete, needBalanced: undefined });
309
288
  return results;
310
289
  }
290
+ /**
291
+ * Time Complexity: O(n)
292
+ * Space Complexity: O(n)
293
+ *
294
+ * The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
295
+ * applying a callback to each entry in the original tree.
296
+ * @param callback - A function that will be called for each entry in the tree, with parameters
297
+ * representing the key, value, index, and the tree itself. It should return an entry for the new
298
+ * tree.
299
+ * @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
300
+ * MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
301
+ * Tree that will be created during the mapping process. These options could include things like
302
+ * custom comparators
303
+ * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
304
+ * the value of `this` when executing the `callback` function. It allows you to set the context
305
+ * (value of `this`) for the callback function. This can be useful when you want to access properties
306
+ * or
307
+ * @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
308
+ * provided callback function.
309
+ */
310
+ map(callback, options, thisArg) {
311
+ const newTree = new RedBlackTree([], options);
312
+ let index = 0;
313
+ for (const [key, value] of this) {
314
+ newTree.add(callback.call(thisArg, key, value, index++, this));
315
+ }
316
+ return newTree;
317
+ }
318
+ /**
319
+ * Time Complexity: O(n)
320
+ * Space Complexity: O(n)
321
+ *
322
+ * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
323
+ * structure.
324
+ * @returns The `cloned` object is being returned.
325
+ */
326
+ clone() {
327
+ const cloned = this.createTree();
328
+ this._clone(cloned);
329
+ return cloned;
330
+ }
311
331
  /**
312
332
  * Time Complexity: O(1)
313
333
  * Space Complexity: O(1)
314
334
  *
315
335
  * The function sets the root of a tree-like structure and updates the parent property of the new
316
336
  * root.
317
- * @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
337
+ * @param {RedBlackTreeNode<K, V> | undefined} v - v is a parameter of type RedBlackTreeNode<K, V> or undefined.
318
338
  */
319
339
  _setRoot(v) {
320
340
  if (v) {
@@ -327,9 +347,9 @@ class RedBlackTree extends bst_1.BST {
327
347
  * Space Complexity: O(1)
328
348
  *
329
349
  * The function replaces an old node with a new node while preserving the color of the old node.
330
- * @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
350
+ * @param {RedBlackTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
331
351
  * the data structure.
332
- * @param {NODE} newNode - The `newNode` parameter is of type `NODE`, which represents a node in a
352
+ * @param {RedBlackTreeNode<K, V>} newNode - The `newNode` parameter is of type `RedBlackTreeNode<K, V>`, which represents a node in a
333
353
  * data structure.
334
354
  * @returns The method is returning the result of calling the `_replaceNode` method from the
335
355
  * superclass, with the `oldNode` and `newNode` parameters.
@@ -340,11 +360,11 @@ class RedBlackTree extends bst_1.BST {
340
360
  }
341
361
  /**
342
362
  * Time Complexity: O(log n)
343
- * Space Complexity: O(1)
363
+ * Space Complexity: O(log n)
344
364
  *
345
365
  * The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to
346
366
  * maintain the red-black tree properties.
347
- * @param {NODE} node - The `node` parameter represents the node that needs to be inserted into the
367
+ * @param {RedBlackTreeNode<K, V>} node - The `node` parameter represents the node that needs to be inserted into the
348
368
  * binary search tree.
349
369
  * @returns a string value indicating the result of the insertion operation. It can return either
350
370
  * 'UPDATED' if the node with the same key already exists and was updated, or 'CREATED' if a new node
@@ -372,7 +392,7 @@ class RedBlackTree extends bst_1.BST {
372
392
  if (!parent) {
373
393
  this._setRoot(node);
374
394
  }
375
- else if (node.key < parent.key) {
395
+ else if (this._compare(node.key, parent.key) < 0) {
376
396
  parent.left = node;
377
397
  }
378
398
  else {
@@ -389,9 +409,9 @@ class RedBlackTree extends bst_1.BST {
389
409
  * Space Complexity: O(1)
390
410
  *
391
411
  * The function `_transplant` is used to replace a node `u` with another node `v` in a binary tree.
392
- * @param {NODE} u - The parameter "u" represents a node in a binary tree.
393
- * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`, which means it can
394
- * either be a `NODE` object or `undefined`.
412
+ * @param {RedBlackTreeNode<K, V>} u - The parameter "u" represents a node in a binary tree.
413
+ * @param {RedBlackTreeNode<K, V> | undefined} v - The parameter `v` is of type `RedBlackTreeNode<K, V> | undefined`, which means it can
414
+ * either be a `RedBlackTreeNode<K, V>` object or `undefined`.
395
415
  */
396
416
  _transplant(u, v) {
397
417
  if (!u.parent) {
@@ -412,11 +432,11 @@ class RedBlackTree extends bst_1.BST {
412
432
  * Space Complexity: O(1)
413
433
  *
414
434
  * The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
415
- * @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
435
+ * @param {RedBlackTreeNode<K, V> | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
416
436
  * structure. It can either be a valid node or `undefined`.
417
437
  */
418
438
  _insertFixup(z) {
419
- var _a, _b, _c, _d;
439
+ var _a, _b, _c, _d, _e;
420
440
  // Continue fixing the tree as long as the parent of z is red
421
441
  while (((_a = z === null || z === void 0 ? void 0 : z.parent) === null || _a === void 0 ? void 0 : _a.color) === 'RED') {
422
442
  // Check if the parent of z is the left child of its parent
@@ -450,7 +470,7 @@ class RedBlackTree extends bst_1.BST {
450
470
  else {
451
471
  // Symmetric case for the right child (left and right exchanged)
452
472
  // Follow the same logic as above with left and right exchanged
453
- const y = (_d = (_c = z === null || z === void 0 ? void 0 : z.parent) === null || _c === void 0 ? void 0 : _c.parent) === null || _d === void 0 ? void 0 : _d.left;
473
+ const y = (_e = (_d = (_c = z === null || z === void 0 ? void 0 : z.parent) === null || _c === void 0 ? void 0 : _c.parent) === null || _d === void 0 ? void 0 : _d.left) !== null && _e !== void 0 ? _e : undefined;
454
474
  if ((y === null || y === void 0 ? void 0 : y.color) === 'RED') {
455
475
  z.parent.color = 'BLACK';
456
476
  y.color = 'BLACK';
@@ -480,7 +500,7 @@ class RedBlackTree extends bst_1.BST {
480
500
  *
481
501
  * The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
482
502
  * the colors and performing rotations.
483
- * @param {NODE | undefined} node - The `node` parameter represents a node in a binary tree. It can
503
+ * @param {RedBlackTreeNode<K, V> | undefined} node - The `node` parameter represents a node in a binary tree. It can
484
504
  * be either a valid node object or `undefined`.
485
505
  * @returns The function does not return any value. It has a return type of `void`, which means it
486
506
  * does not return anything.
@@ -566,7 +586,7 @@ class RedBlackTree extends bst_1.BST {
566
586
  * Space Complexity: O(1)
567
587
  *
568
588
  * The `_leftRotate` function performs a left rotation on a given node in a binary tree.
569
- * @param {NODE | undefined} x - The parameter `x` is of type `NODE | undefined`. It represents a
589
+ * @param {RedBlackTreeNode<K, V> | undefined} x - The parameter `x` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a
570
590
  * node in a binary tree or `undefined` if there is no node.
571
591
  * @returns void, which means it does not return any value.
572
592
  */
@@ -597,7 +617,7 @@ class RedBlackTree extends bst_1.BST {
597
617
  * Space Complexity: O(1)
598
618
  *
599
619
  * The `_rightRotate` function performs a right rotation on a given node in a binary tree.
600
- * @param {NODE | undefined} y - The parameter `y` is of type `NODE | undefined`. It represents a
620
+ * @param {RedBlackTreeNode<K, V> | undefined} y - The parameter `y` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a
601
621
  * node in a binary tree or `undefined` if there is no node.
602
622
  * @returns void, which means it does not return any value.
603
623
  */
@@ -0,0 +1,212 @@
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 { BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback, IterationType, OptNodeOrNull, RBTNColor, TreeCounterOptions } from '../../types';
9
+ import { IBinaryTree } from '../../interfaces';
10
+ import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
11
+ export declare class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
12
+ /**
13
+ * The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
14
+ * @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
15
+ * used to identify and locate the node within the tree.
16
+ * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
17
+ * associated with the key in the Red-Black Tree node. It is not required and can be omitted when
18
+ * creating a new node.
19
+ * @param [count=1] - The `count` parameter represents the number of occurrences of a particular key
20
+ * in the Red-Black Tree. It is an optional parameter with a default value of 1.
21
+ * @param {RBTNColor} [color=BLACK] - The `color` parameter is used to specify the color of the node
22
+ * in a Red-Black Tree. It is optional and has a default value of `'BLACK'`.
23
+ */
24
+ constructor(key: K, value?: V, count?: number, color?: RBTNColor);
25
+ parent?: TreeCounterNode<K, V>;
26
+ _left?: OptNodeOrNull<TreeCounterNode<K, V>>;
27
+ get left(): OptNodeOrNull<TreeCounterNode<K, V>>;
28
+ set left(v: OptNodeOrNull<TreeCounterNode<K, V>>);
29
+ _right?: OptNodeOrNull<TreeCounterNode<K, V>>;
30
+ get right(): OptNodeOrNull<TreeCounterNode<K, V>>;
31
+ set right(v: OptNodeOrNull<TreeCounterNode<K, V>>);
32
+ }
33
+ /**
34
+ *
35
+ */
36
+ export declare class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends RedBlackTree<K, V, R, MK, MV, MR> implements IBinaryTree<K, V, R, MK, MV, MR> {
37
+ /**
38
+ * The constructor function initializes a TreeCounter object with optional initial data.
39
+ * @param keysNodesEntriesOrRaws - The parameter `keysNodesEntriesOrRaws` is an
40
+ * iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
41
+ * TreeCounter with initial data.
42
+ * @param [options] - The `options` parameter is an optional object that can be used to customize the
43
+ * behavior of the `TreeCounter` constructor. It can include properties such as `compareKeys` and
44
+ * `compareValues`, which are functions used to compare keys and values respectively.
45
+ */
46
+ constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, TreeCounterNode<K, V>> | R>, options?: TreeCounterOptions<K, V, R>);
47
+ protected _count: number;
48
+ /**
49
+ * The function calculates the sum of the count property of all nodes in a tree structure.
50
+ * @returns the sum of the count property of all nodes in the tree.
51
+ */
52
+ get count(): number;
53
+ /**
54
+ * Time Complexity: O(n)
55
+ * Space Complexity: O(1)
56
+ *
57
+ * The function calculates the sum of the count property of all nodes in a tree using depth-first
58
+ * search.
59
+ * @returns the sum of the count property of all nodes in the tree.
60
+ */
61
+ getComputedCount(): number;
62
+ /**
63
+ * The function creates a new TreeCounterNode with the specified key, value, color, and count.
64
+ * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
65
+ * which is a generic type representing the type of keys in the tree.
66
+ * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
67
+ * associated with the key in the node. It is of type `V`, which can be any data type.
68
+ * @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
69
+ * a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
70
+ * @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
71
+ * the tree. It is an optional parameter and is used to keep track of the number of values associated
72
+ * with a key in the tree.
73
+ * @returns A new instance of the TreeCounterNode class, casted as TreeCounterNode<K, V>.
74
+ */
75
+ createNode(key: K, value?: V, color?: RBTNColor, count?: number): TreeCounterNode<K, V>;
76
+ /**
77
+ * The function creates a new instance of a TreeCounter with the specified options and returns it.
78
+ * @param [options] - The `options` parameter is an optional object that contains additional
79
+ * configuration options for creating the `TreeCounter`. It is of type `TreeCounterOptions<K, V,
80
+ * R>`.
81
+ * @returns a new instance of the `TreeCounter` class, with the provided options merged with the
82
+ * existing `iterationType` property. The returned value is casted as `TREE`.
83
+ */
84
+ createTree(options?: TreeCounterOptions<K, V, R>): TreeCounter<K, V, R, MK, MV, MR>;
85
+ /**
86
+ * The function checks if the input is an instance of the TreeCounterNode class.
87
+ * @param {BTNRep<K, V, TreeCounterNode<K, V>>} keyNodeOrEntry - The parameter
88
+ * `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, TreeCounterNode<K, V>>`.
89
+ * @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
90
+ * an instance of the `TreeCounterNode` class.
91
+ */
92
+ isNode(keyNodeOrEntry: BTNRep<K, V, TreeCounterNode<K, V>>): keyNodeOrEntry is TreeCounterNode<K, V>;
93
+ /**
94
+ * Time Complexity: O(log n)
95
+ * Space Complexity: O(1)
96
+ *
97
+ * The function overrides the add method of a class and adds a new node to a data structure, updating
98
+ * the count and returning a boolean indicating success.
99
+ * @param {BTNRep<K, V, TreeCounterNode<K, V>>} keyNodeOrEntry - The
100
+ * `keyNodeOrEntry` parameter can accept one of the following types:
101
+ * @param {V} [value] - The `value` parameter represents the value associated with the key in the
102
+ * data structure. It is an optional parameter, so it can be omitted if not needed.
103
+ * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
104
+ * be added to the data structure. By default, it is set to 1, meaning that if no value is provided
105
+ * for `count`, the key-value pair will be added once.
106
+ * @returns The method is returning a boolean value. It returns true if the addition of the new node
107
+ * was successful, and false otherwise.
108
+ */
109
+ add(keyNodeOrEntry: BTNRep<K, V, TreeCounterNode<K, V>>, value?: V, count?: number): boolean;
110
+ /**
111
+ * Time Complexity: O(log n)
112
+ * Space Complexity: O(1)
113
+ *
114
+ * The function `delete` in TypeScript overrides the deletion operation in a binary tree data
115
+ * structure, handling cases where nodes have children and maintaining balance in the tree.
116
+ * @param {BTNRep<K, V, TreeCounterNode<K, V>>} keyNodeOrEntry - The `predicate`
117
+ * parameter in the `delete` method is used to specify the condition or key based on which a node
118
+ * should be deleted from the binary tree. It can be a key, a node, or an entry.
119
+ * @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
120
+ * boolean flag that determines whether to ignore the count of nodes when performing deletion. If
121
+ * `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
122
+ * `ignoreCount` is `false
123
+ * @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<TreeCounterNode<K, V>>` objects.
124
+ */
125
+ delete(keyNodeOrEntry: BTNRep<K, V, TreeCounterNode<K, V>>, ignoreCount?: boolean): BinaryTreeDeleteResult<TreeCounterNode<K, V>>[];
126
+ /**
127
+ * Time Complexity: O(1)
128
+ * Space Complexity: O(1)
129
+ *
130
+ * The "clear" function overrides the parent class's "clear" function and also resets the count to
131
+ * zero.
132
+ */
133
+ clear(): void;
134
+ /**
135
+ * Time Complexity: O(n log n)
136
+ * Space Complexity: O(log n)
137
+ *
138
+ * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
139
+ * tree using either a recursive or iterative approach.
140
+ * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
141
+ * specifies the type of iteration to use when building the balanced binary search tree. It has a
142
+ * default value of `this.iterationType`, which means it will use the iteration type specified by the
143
+ * `iterationType` property of the current object.
144
+ * @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
145
+ * balancing operation is successful, and `false` if there are no nodes to balance.
146
+ */
147
+ perfectlyBalance(iterationType?: IterationType): boolean;
148
+ /**
149
+ * Time complexity: O(n)
150
+ * Space complexity: O(n)
151
+ *
152
+ * The function overrides the clone method to create a deep copy of a tree object.
153
+ * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
154
+ */
155
+ clone(): TreeCounter<K, V, R, MK, MV, MR>;
156
+ /**
157
+ * The `map` function in TypeScript overrides the default behavior to create a new TreeCounter with
158
+ * modified entries based on a provided callback.
159
+ * @param callback - The `callback` parameter is a function that will be called for each entry in the
160
+ * map. It takes four arguments:
161
+ * @param [options] - The `options` parameter in the `override map` function is of type
162
+ * `TreeCounterOptions<MK, MV, MR>`. This parameter allows you to provide additional configuration
163
+ * options when creating a new `TreeCounter` instance within the `map` function. These options could
164
+ * include things like
165
+ * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
166
+ * the value of `this` when executing the `callback` function. It allows you to set the context
167
+ * (value of `this`) for the callback function when it is called within the `map` function. This
168
+ * @returns A new TreeCounter instance is being returned, which is populated with entries generated
169
+ * by the provided callback function.
170
+ */
171
+ map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: TreeCounterOptions<MK, MV, MR>, thisArg?: any): TreeCounter<MK, MV, MR>;
172
+ /**
173
+ * The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
174
+ * node based on the input.
175
+ * @param {BTNRep<K, V, TreeCounterNode<K, V>>} keyNodeOrEntry - The parameter
176
+ * `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, TreeCounterNode<K, V>>`.
177
+ * @param {V} [value] - The `value` parameter is an optional value that represents the value
178
+ * associated with the key in the node. It is used when creating a new node or updating the value of
179
+ * an existing node.
180
+ * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
181
+ * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
182
+ * @returns either a TreeCounterNode<K, V> object or undefined.
183
+ */
184
+ protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: BTNRep<K, V, TreeCounterNode<K, V>>, value?: V, count?: number): [TreeCounterNode<K, V> | undefined, V | undefined];
185
+ /**
186
+ * Time Complexity: O(1)
187
+ * Space Complexity: O(1)
188
+ *
189
+ * The `_swapProperties` function swaps the properties (key, value, count, color) between two nodes
190
+ * in a binary search tree.
191
+ * @param {R | BSTNOptKeyOrNode<K, TreeCounterNode<K, V>>} srcNode - The `srcNode` parameter represents the source node
192
+ * that will be swapped with the `destNode`. It can be either an instance of the `R` class or an
193
+ * instance of the `BSTNOptKeyOrNode<K, TreeCounterNode<K, V>>` class.
194
+ * @param {R | BSTNOptKeyOrNode<K, TreeCounterNode<K, V>>} destNode - The `destNode` parameter represents the destination
195
+ * node where the properties will be swapped with the source node.
196
+ * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
197
+ * If either `srcNode` or `destNode` is undefined, it returns undefined.
198
+ */
199
+ protected _swapProperties(srcNode: BSTNOptKeyOrNode<K, TreeCounterNode<K, V>>, destNode: BSTNOptKeyOrNode<K, TreeCounterNode<K, V>>): TreeCounterNode<K, V> | undefined;
200
+ /**
201
+ * Time Complexity: O(1)
202
+ * Space Complexity: O(1)
203
+ *
204
+ * The function replaces an old node with a new node and updates the count property of the new node.
205
+ * @param {TreeCounterNode<K, V>} oldNode - The `oldNode` parameter is the node that you want to replace in the data
206
+ * structure.
207
+ * @param {TreeCounterNode<K, V>} newNode - The `newNode` parameter is an instance of the `TreeCounterNode<K, V>` class.
208
+ * @returns The method is returning the result of calling the `_replaceNode` method from the
209
+ * superclass, which is of type `TreeCounterNode<K, V>`.
210
+ */
211
+ protected _replaceNode(oldNode: TreeCounterNode<K, V>, newNode: TreeCounterNode<K, V>): TreeCounterNode<K, V>;
212
+ }