bst-typed 1.53.9 → 1.54.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
  2. package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -189
  4. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -357
  5. package/dist/data-structures/binary-tree/avl-tree.d.ts +108 -78
  6. package/dist/data-structures/binary-tree/avl-tree.js +126 -79
  7. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
  8. package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
  9. package/dist/data-structures/binary-tree/binary-tree.d.ts +243 -190
  10. package/dist/data-structures/binary-tree/binary-tree.js +273 -229
  11. package/dist/data-structures/binary-tree/bst.d.ts +141 -122
  12. package/dist/data-structures/binary-tree/bst.js +170 -134
  13. package/dist/data-structures/binary-tree/index.d.ts +2 -0
  14. package/dist/data-structures/binary-tree/index.js +2 -0
  15. package/dist/data-structures/binary-tree/red-black-tree.d.ts +84 -80
  16. package/dist/data-structures/binary-tree/red-black-tree.js +101 -79
  17. package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
  18. package/dist/data-structures/binary-tree/tree-counter.js +444 -0
  19. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -186
  20. package/dist/data-structures/binary-tree/tree-multi-map.js +140 -388
  21. package/dist/data-structures/graph/directed-graph.d.ts +3 -0
  22. package/dist/data-structures/graph/directed-graph.js +3 -0
  23. package/dist/data-structures/graph/map-graph.d.ts +3 -0
  24. package/dist/data-structures/graph/map-graph.js +3 -0
  25. package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
  26. package/dist/data-structures/graph/undirected-graph.js +3 -0
  27. package/dist/data-structures/linked-list/singly-linked-list.d.ts +3 -0
  28. package/dist/data-structures/linked-list/singly-linked-list.js +3 -0
  29. package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
  30. package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
  31. package/dist/data-structures/matrix/matrix.d.ts +3 -0
  32. package/dist/data-structures/matrix/matrix.js +3 -0
  33. package/dist/data-structures/matrix/navigator.d.ts +3 -0
  34. package/dist/data-structures/matrix/navigator.js +3 -0
  35. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
  36. package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
  37. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
  38. package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
  39. package/dist/data-structures/trie/trie.d.ts +0 -4
  40. package/dist/data-structures/trie/trie.js +0 -4
  41. package/dist/interfaces/binary-tree.d.ts +7 -6
  42. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  43. package/dist/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
  44. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -3
  45. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -2
  46. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
  47. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
  48. package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
  49. package/dist/types/data-structures/binary-tree/index.js +2 -0
  50. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -3
  51. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  52. package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
  53. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -3
  54. package/package.json +2 -2
  55. package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
  56. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +148 -394
  57. package/src/data-structures/binary-tree/avl-tree.ts +152 -112
  58. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
  59. package/src/data-structures/binary-tree/binary-tree.ts +446 -379
  60. package/src/data-structures/binary-tree/bst.ts +224 -201
  61. package/src/data-structures/binary-tree/index.ts +2 -0
  62. package/src/data-structures/binary-tree/red-black-tree.ts +138 -114
  63. package/src/data-structures/binary-tree/tree-counter.ts +504 -0
  64. package/src/data-structures/binary-tree/tree-multi-map.ts +156 -428
  65. package/src/data-structures/graph/directed-graph.ts +3 -0
  66. package/src/data-structures/graph/map-graph.ts +3 -0
  67. package/src/data-structures/graph/undirected-graph.ts +3 -0
  68. package/src/data-structures/linked-list/singly-linked-list.ts +3 -0
  69. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  70. package/src/data-structures/matrix/matrix.ts +3 -0
  71. package/src/data-structures/matrix/navigator.ts +3 -0
  72. package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
  73. package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
  74. package/src/data-structures/trie/trie.ts +0 -4
  75. package/src/interfaces/binary-tree.ts +10 -11
  76. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
  77. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -4
  78. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -3
  79. package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
  80. package/src/types/data-structures/binary-tree/bst.ts +5 -3
  81. package/src/types/data-structures/binary-tree/index.ts +2 -0
  82. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -4
  83. package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
  84. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -4
@@ -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 RedBlackTree 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 `RedBlackTreeOptions<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,15 +136,14 @@ class RedBlackTree extends bst_1.BST {
130
136
  return new RedBlackTreeNode(key, this._isMapMode ? undefined : value, color);
131
137
  }
132
138
  /**
133
- * The function `createTree` overrides the default implementation to create a Red-Black Tree with
134
- * specified options in TypeScript.
135
- * @param [options] - The `options` parameter in the `createTree` method is of type `RedBlackTreeOptions<K,
136
- * V, R>`, which is a generic type with three type parameters `K`, `V`, and `R`. This parameter
137
- * allows you to pass additional configuration options when creating a new Red-
138
- * @returns A new instance of a RedBlackTree with the specified options and properties from the
139
- * current object is being returned.
139
+ * Time Complexity: O(1)
140
+ * Space Complexity: O(1)
141
+ *
142
+ * The function creates a new Red-Black Tree with the specified options.
143
+ * @param [options] - The `options` parameter is an optional object that contains additional
144
+ * configuration options for creating the Red-Black Tree. It has the following properties:
145
+ * @returns a new instance of a RedBlackTree object.
140
146
  */
141
- // @ts-ignore
142
147
  createTree(options) {
143
148
  return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn }, options));
144
149
  }
@@ -147,13 +152,13 @@ class RedBlackTree extends bst_1.BST {
147
152
  * Space Complexity: O(1)
148
153
  *
149
154
  * The function checks if the input is an instance of the RedBlackTreeNode class.
150
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
151
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
152
- * @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
153
158
  * an instance of the `RedBlackTreeNode` class.
154
159
  */
155
- isNode(keyNodeEntryOrRaw) {
156
- return keyNodeEntryOrRaw instanceof RedBlackTreeNode;
160
+ isNode(keyNodeOrEntry) {
161
+ return keyNodeOrEntry instanceof RedBlackTreeNode;
157
162
  }
158
163
  /**
159
164
  * Time Complexity: O(1)
@@ -168,12 +173,12 @@ class RedBlackTree extends bst_1.BST {
168
173
  }
169
174
  /**
170
175
  * Time Complexity: O(log n)
171
- * Space Complexity: O(1)
176
+ * Space Complexity: O(log n)
172
177
  *
173
178
  * The function adds a new node to a binary search tree and returns true if the node was successfully
174
179
  * added.
175
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
176
- * `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>>`.
177
182
  * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
178
183
  * the key in the data structure. It represents the value that you want to add or update in the data
179
184
  * structure.
@@ -181,8 +186,8 @@ class RedBlackTree extends bst_1.BST {
181
186
  * the method returns true. If the node already exists and its value is updated, the method also
182
187
  * returns true. If the node cannot be added or updated, the method returns false.
183
188
  */
184
- add(keyNodeEntryOrRaw, value) {
185
- const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
189
+ add(keyNodeOrEntry, value) {
190
+ const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
186
191
  if (!this.isRealNode(newNode))
187
192
  return false;
188
193
  const insertStatus = this._insert(newNode);
@@ -208,35 +213,37 @@ class RedBlackTree extends bst_1.BST {
208
213
  }
209
214
  /**
210
215
  * Time Complexity: O(log n)
211
- * Space Complexity: O(1)
216
+ * Space Complexity: O(log n)
212
217
  *
213
218
  * The function overrides the delete method in a binary tree data structure to remove a node based on
214
219
  * a given predicate and maintain the binary search tree properties.
215
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
220
+ * @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
216
221
  * parameter in the `override delete` method is used to specify the condition or key based on which a
217
222
  * node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
218
223
  * function that determines which node(s) should be deleted.
219
- * @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>>`
220
225
  * objects. Each object in the array contains information about the deleted node and whether
221
226
  * balancing is needed.
222
227
  */
223
- delete(keyNodeEntryOrRaw) {
224
- if (keyNodeEntryOrRaw === null)
228
+ delete(keyNodeOrEntry) {
229
+ if (keyNodeOrEntry === null)
225
230
  return [];
226
231
  const results = [];
227
232
  let nodeToDelete;
228
- if (this._isPredicate(keyNodeEntryOrRaw))
229
- nodeToDelete = this.getNode(keyNodeEntryOrRaw);
233
+ if (this._isPredicate(keyNodeOrEntry))
234
+ nodeToDelete = this.getNode(keyNodeOrEntry);
230
235
  else
231
- nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw);
236
+ nodeToDelete = this.isRealNode(keyNodeOrEntry) ? keyNodeOrEntry : this.getNode(keyNodeOrEntry);
232
237
  if (!nodeToDelete) {
233
238
  return results;
234
239
  }
235
240
  let originalColor = nodeToDelete.color;
236
241
  let replacementNode;
237
242
  if (!this.isRealNode(nodeToDelete.left)) {
238
- replacementNode = nodeToDelete.right;
239
- this._transplant(nodeToDelete, nodeToDelete.right);
243
+ if (nodeToDelete.right !== null) {
244
+ replacementNode = nodeToDelete.right;
245
+ this._transplant(nodeToDelete, nodeToDelete.right);
246
+ }
240
247
  }
241
248
  else if (!this.isRealNode(nodeToDelete.right)) {
242
249
  replacementNode = nodeToDelete.left;
@@ -246,15 +253,18 @@ class RedBlackTree extends bst_1.BST {
246
253
  const successor = this.getLeftMost(node => node, nodeToDelete.right);
247
254
  if (successor) {
248
255
  originalColor = successor.color;
249
- replacementNode = successor.right;
256
+ if (successor.right !== null)
257
+ replacementNode = successor.right;
250
258
  if (successor.parent === nodeToDelete) {
251
259
  if (this.isRealNode(replacementNode)) {
252
260
  replacementNode.parent = successor;
253
261
  }
254
262
  }
255
263
  else {
256
- this._transplant(successor, successor.right);
257
- successor.right = nodeToDelete.right;
264
+ if (successor.right !== null) {
265
+ this._transplant(successor, successor.right);
266
+ successor.right = nodeToDelete.right;
267
+ }
258
268
  if (this.isRealNode(successor.right)) {
259
269
  successor.right.parent = successor;
260
270
  }
@@ -297,7 +307,6 @@ class RedBlackTree extends bst_1.BST {
297
307
  * @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
298
308
  * provided callback function.
299
309
  */
300
- // @ts-ignore
301
310
  map(callback, options, thisArg) {
302
311
  const newTree = new RedBlackTree([], options);
303
312
  let index = 0;
@@ -306,13 +315,26 @@ class RedBlackTree extends bst_1.BST {
306
315
  }
307
316
  return newTree;
308
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
+ }
309
331
  /**
310
332
  * Time Complexity: O(1)
311
333
  * Space Complexity: O(1)
312
334
  *
313
335
  * The function sets the root of a tree-like structure and updates the parent property of the new
314
336
  * root.
315
- * @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.
316
338
  */
317
339
  _setRoot(v) {
318
340
  if (v) {
@@ -325,9 +347,9 @@ class RedBlackTree extends bst_1.BST {
325
347
  * Space Complexity: O(1)
326
348
  *
327
349
  * The function replaces an old node with a new node while preserving the color of the old node.
328
- * @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
329
351
  * the data structure.
330
- * @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
331
353
  * data structure.
332
354
  * @returns The method is returning the result of calling the `_replaceNode` method from the
333
355
  * superclass, with the `oldNode` and `newNode` parameters.
@@ -338,11 +360,11 @@ class RedBlackTree extends bst_1.BST {
338
360
  }
339
361
  /**
340
362
  * Time Complexity: O(log n)
341
- * Space Complexity: O(1)
363
+ * Space Complexity: O(log n)
342
364
  *
343
365
  * The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to
344
366
  * maintain the red-black tree properties.
345
- * @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
346
368
  * binary search tree.
347
369
  * @returns a string value indicating the result of the insertion operation. It can return either
348
370
  * 'UPDATED' if the node with the same key already exists and was updated, or 'CREATED' if a new node
@@ -387,9 +409,9 @@ class RedBlackTree extends bst_1.BST {
387
409
  * Space Complexity: O(1)
388
410
  *
389
411
  * The function `_transplant` is used to replace a node `u` with another node `v` in a binary tree.
390
- * @param {NODE} u - The parameter "u" represents a node in a binary tree.
391
- * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`, which means it can
392
- * 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`.
393
415
  */
394
416
  _transplant(u, v) {
395
417
  if (!u.parent) {
@@ -410,11 +432,11 @@ class RedBlackTree extends bst_1.BST {
410
432
  * Space Complexity: O(1)
411
433
  *
412
434
  * The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
413
- * @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
414
436
  * structure. It can either be a valid node or `undefined`.
415
437
  */
416
438
  _insertFixup(z) {
417
- var _a, _b, _c, _d;
439
+ var _a, _b, _c, _d, _e;
418
440
  // Continue fixing the tree as long as the parent of z is red
419
441
  while (((_a = z === null || z === void 0 ? void 0 : z.parent) === null || _a === void 0 ? void 0 : _a.color) === 'RED') {
420
442
  // Check if the parent of z is the left child of its parent
@@ -448,7 +470,7 @@ class RedBlackTree extends bst_1.BST {
448
470
  else {
449
471
  // Symmetric case for the right child (left and right exchanged)
450
472
  // Follow the same logic as above with left and right exchanged
451
- 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;
452
474
  if ((y === null || y === void 0 ? void 0 : y.color) === 'RED') {
453
475
  z.parent.color = 'BLACK';
454
476
  y.color = 'BLACK';
@@ -478,7 +500,7 @@ class RedBlackTree extends bst_1.BST {
478
500
  *
479
501
  * The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
480
502
  * the colors and performing rotations.
481
- * @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
482
504
  * be either a valid node object or `undefined`.
483
505
  * @returns The function does not return any value. It has a return type of `void`, which means it
484
506
  * does not return anything.
@@ -564,7 +586,7 @@ class RedBlackTree extends bst_1.BST {
564
586
  * Space Complexity: O(1)
565
587
  *
566
588
  * The `_leftRotate` function performs a left rotation on a given node in a binary tree.
567
- * @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
568
590
  * node in a binary tree or `undefined` if there is no node.
569
591
  * @returns void, which means it does not return any value.
570
592
  */
@@ -595,7 +617,7 @@ class RedBlackTree extends bst_1.BST {
595
617
  * Space Complexity: O(1)
596
618
  *
597
619
  * The `_rightRotate` function performs a right rotation on a given node in a binary tree.
598
- * @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
599
621
  * node in a binary tree or `undefined` if there is no node.
600
622
  * @returns void, which means it does not return any value.
601
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
+ }