deque-typed 1.54.0 → 1.54.2
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.
- package/LICENSE +1 -1
- package/coverage/lcov-report/index.ts.html +2 -2
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
- package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -177
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -340
- package/dist/data-structures/binary-tree/avl-tree.d.ts +102 -57
- package/dist/data-structures/binary-tree/avl-tree.js +110 -47
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +240 -190
- package/dist/data-structures/binary-tree/binary-tree.js +269 -240
- package/dist/data-structures/binary-tree/bst.d.ts +145 -112
- package/dist/data-structures/binary-tree/bst.js +180 -129
- package/dist/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/data-structures/binary-tree/index.js +2 -0
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +100 -82
- package/dist/data-structures/binary-tree/red-black-tree.js +115 -79
- package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
- package/dist/data-structures/binary-tree/tree-counter.js +444 -0
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -174
- package/dist/data-structures/binary-tree/tree-multi-map.js +142 -377
- package/dist/data-structures/graph/directed-graph.d.ts +3 -0
- package/dist/data-structures/graph/directed-graph.js +3 -0
- package/dist/data-structures/graph/map-graph.d.ts +3 -0
- package/dist/data-structures/graph/map-graph.js +3 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
- package/dist/data-structures/graph/undirected-graph.js +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
- package/dist/data-structures/matrix/matrix.d.ts +3 -0
- package/dist/data-structures/matrix/matrix.js +3 -0
- package/dist/data-structures/matrix/navigator.d.ts +3 -0
- package/dist/data-structures/matrix/navigator.js +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
- package/dist/data-structures/trie/trie.d.ts +0 -4
- package/dist/data-structures/trie/trie.js +0 -4
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -3
- package/dist/interfaces/binary-tree.d.ts +8 -8
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -1
- package/dist/types/data-structures/binary-tree/index.js +3 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
- package/package.json +3 -3
- package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +155 -393
- package/src/data-structures/binary-tree/avl-tree.ts +144 -93
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +433 -405
- package/src/data-structures/binary-tree/bst.ts +261 -239
- package/src/data-structures/binary-tree/index.ts +2 -0
- package/src/data-structures/binary-tree/red-black-tree.ts +163 -134
- package/src/data-structures/binary-tree/tree-counter.ts +504 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +161 -429
- package/src/data-structures/graph/directed-graph.ts +3 -0
- package/src/data-structures/graph/map-graph.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +3 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +3 -0
- package/src/data-structures/matrix/navigator.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
- package/src/data-structures/trie/trie.ts +0 -4
- package/src/index.ts +2 -3
- package/src/interfaces/binary-tree.ts +10 -24
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
- package/src/types/data-structures/binary-tree/index.ts +3 -1
- package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
- /package/dist/types/data-structures/binary-tree/{rb-tree.js → avl-tree-counter.js} +0 -0
|
@@ -4,20 +4,40 @@ 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
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
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
|
}
|
|
23
|
+
get left() {
|
|
24
|
+
return this._left;
|
|
25
|
+
}
|
|
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;
|
|
40
|
+
}
|
|
21
41
|
}
|
|
22
42
|
exports.RedBlackTreeNode = RedBlackTreeNode;
|
|
23
43
|
/**
|
|
@@ -75,14 +95,15 @@ exports.RedBlackTreeNode = RedBlackTreeNode;
|
|
|
75
95
|
*/
|
|
76
96
|
class RedBlackTree extends bst_1.BST {
|
|
77
97
|
/**
|
|
78
|
-
* This
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
* constructor
|
|
84
|
-
*
|
|
85
|
-
*
|
|
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
|
|
86
107
|
*/
|
|
87
108
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
88
109
|
super([], options);
|
|
@@ -91,14 +112,13 @@ class RedBlackTree extends bst_1.BST {
|
|
|
91
112
|
this.addMany(keysNodesEntriesOrRaws);
|
|
92
113
|
}
|
|
93
114
|
}
|
|
94
|
-
/**
|
|
95
|
-
* The function returns the root node of a tree or undefined if there is no root.
|
|
96
|
-
* @returns The root node of the tree structure, or undefined if there is no root node.
|
|
97
|
-
*/
|
|
98
115
|
get root() {
|
|
99
116
|
return this._root;
|
|
100
117
|
}
|
|
101
118
|
/**
|
|
119
|
+
* Time Complexity: O(1)
|
|
120
|
+
* Space Complexity: O(1)
|
|
121
|
+
*
|
|
102
122
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
103
123
|
* @param {K} key - The key parameter represents the key value of the node being created. It is of
|
|
104
124
|
* type K, which is a generic type that can be replaced with any specific type when using the
|
|
@@ -116,6 +136,9 @@ class RedBlackTree extends bst_1.BST {
|
|
|
116
136
|
return new RedBlackTreeNode(key, this._isMapMode ? undefined : value, color);
|
|
117
137
|
}
|
|
118
138
|
/**
|
|
139
|
+
* Time Complexity: O(1)
|
|
140
|
+
* Space Complexity: O(1)
|
|
141
|
+
*
|
|
119
142
|
* The function creates a new Red-Black Tree with the specified options.
|
|
120
143
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
121
144
|
* configuration options for creating the Red-Black Tree. It has the following properties:
|
|
@@ -129,13 +152,13 @@ class RedBlackTree extends bst_1.BST {
|
|
|
129
152
|
* Space Complexity: O(1)
|
|
130
153
|
*
|
|
131
154
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
132
|
-
* @param {BTNRep<K, V,
|
|
133
|
-
* `
|
|
134
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
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
|
|
135
158
|
* an instance of the `RedBlackTreeNode` class.
|
|
136
159
|
*/
|
|
137
|
-
isNode(
|
|
138
|
-
return
|
|
160
|
+
isNode(keyNodeOrEntry) {
|
|
161
|
+
return keyNodeOrEntry instanceof RedBlackTreeNode;
|
|
139
162
|
}
|
|
140
163
|
/**
|
|
141
164
|
* Time Complexity: O(1)
|
|
@@ -150,12 +173,12 @@ class RedBlackTree extends bst_1.BST {
|
|
|
150
173
|
}
|
|
151
174
|
/**
|
|
152
175
|
* Time Complexity: O(log n)
|
|
153
|
-
* Space Complexity: O(
|
|
176
|
+
* Space Complexity: O(log n)
|
|
154
177
|
*
|
|
155
178
|
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
156
179
|
* added.
|
|
157
|
-
* @param {BTNRep<K, V,
|
|
158
|
-
* `
|
|
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>>`.
|
|
159
182
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
160
183
|
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
161
184
|
* structure.
|
|
@@ -163,8 +186,8 @@ class RedBlackTree extends bst_1.BST {
|
|
|
163
186
|
* the method returns true. If the node already exists and its value is updated, the method also
|
|
164
187
|
* returns true. If the node cannot be added or updated, the method returns false.
|
|
165
188
|
*/
|
|
166
|
-
add(
|
|
167
|
-
const [newNode, newValue] = this.
|
|
189
|
+
add(keyNodeOrEntry, value) {
|
|
190
|
+
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
168
191
|
if (!this.isRealNode(newNode))
|
|
169
192
|
return false;
|
|
170
193
|
const insertStatus = this._insert(newNode);
|
|
@@ -190,27 +213,27 @@ class RedBlackTree extends bst_1.BST {
|
|
|
190
213
|
}
|
|
191
214
|
/**
|
|
192
215
|
* Time Complexity: O(log n)
|
|
193
|
-
* Space Complexity: O(
|
|
216
|
+
* Space Complexity: O(log n)
|
|
194
217
|
*
|
|
195
218
|
* The function overrides the delete method in a binary tree data structure to remove a node based on
|
|
196
219
|
* a given predicate and maintain the binary search tree properties.
|
|
197
|
-
* @param {BTNRep<K, V,
|
|
220
|
+
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
198
221
|
* parameter in the `override delete` method is used to specify the condition or key based on which a
|
|
199
222
|
* node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
|
|
200
223
|
* function that determines which node(s) should be deleted.
|
|
201
|
-
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<
|
|
224
|
+
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>`
|
|
202
225
|
* objects. Each object in the array contains information about the deleted node and whether
|
|
203
226
|
* balancing is needed.
|
|
204
227
|
*/
|
|
205
|
-
delete(
|
|
206
|
-
if (
|
|
228
|
+
delete(keyNodeOrEntry) {
|
|
229
|
+
if (keyNodeOrEntry === null)
|
|
207
230
|
return [];
|
|
208
231
|
const results = [];
|
|
209
232
|
let nodeToDelete;
|
|
210
|
-
if (this._isPredicate(
|
|
211
|
-
nodeToDelete = this.getNode(
|
|
233
|
+
if (this._isPredicate(keyNodeOrEntry))
|
|
234
|
+
nodeToDelete = this.getNode(keyNodeOrEntry);
|
|
212
235
|
else
|
|
213
|
-
nodeToDelete = this.isRealNode(
|
|
236
|
+
nodeToDelete = this.isRealNode(keyNodeOrEntry) ? keyNodeOrEntry : this.getNode(keyNodeOrEntry);
|
|
214
237
|
if (!nodeToDelete) {
|
|
215
238
|
return results;
|
|
216
239
|
}
|
|
@@ -264,13 +287,54 @@ class RedBlackTree extends bst_1.BST {
|
|
|
264
287
|
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
265
288
|
return results;
|
|
266
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
|
+
}
|
|
267
331
|
/**
|
|
268
332
|
* Time Complexity: O(1)
|
|
269
333
|
* Space Complexity: O(1)
|
|
270
334
|
*
|
|
271
335
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
272
336
|
* root.
|
|
273
|
-
* @param {
|
|
337
|
+
* @param {RedBlackTreeNode<K, V> | undefined} v - v is a parameter of type RedBlackTreeNode<K, V> or undefined.
|
|
274
338
|
*/
|
|
275
339
|
_setRoot(v) {
|
|
276
340
|
if (v) {
|
|
@@ -283,9 +347,9 @@ class RedBlackTree extends bst_1.BST {
|
|
|
283
347
|
* Space Complexity: O(1)
|
|
284
348
|
*
|
|
285
349
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
286
|
-
* @param {
|
|
350
|
+
* @param {RedBlackTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
287
351
|
* the data structure.
|
|
288
|
-
* @param {
|
|
352
|
+
* @param {RedBlackTreeNode<K, V>} newNode - The `newNode` parameter is of type `RedBlackTreeNode<K, V>`, which represents a node in a
|
|
289
353
|
* data structure.
|
|
290
354
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
291
355
|
* superclass, with the `oldNode` and `newNode` parameters.
|
|
@@ -296,11 +360,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
296
360
|
}
|
|
297
361
|
/**
|
|
298
362
|
* Time Complexity: O(log n)
|
|
299
|
-
* Space Complexity: O(
|
|
363
|
+
* Space Complexity: O(log n)
|
|
300
364
|
*
|
|
301
365
|
* The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to
|
|
302
366
|
* maintain the red-black tree properties.
|
|
303
|
-
* @param {
|
|
367
|
+
* @param {RedBlackTreeNode<K, V>} node - The `node` parameter represents the node that needs to be inserted into the
|
|
304
368
|
* binary search tree.
|
|
305
369
|
* @returns a string value indicating the result of the insertion operation. It can return either
|
|
306
370
|
* 'UPDATED' if the node with the same key already exists and was updated, or 'CREATED' if a new node
|
|
@@ -345,9 +409,9 @@ class RedBlackTree extends bst_1.BST {
|
|
|
345
409
|
* Space Complexity: O(1)
|
|
346
410
|
*
|
|
347
411
|
* The function `_transplant` is used to replace a node `u` with another node `v` in a binary tree.
|
|
348
|
-
* @param {
|
|
349
|
-
* @param {
|
|
350
|
-
* either be a `
|
|
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`.
|
|
351
415
|
*/
|
|
352
416
|
_transplant(u, v) {
|
|
353
417
|
if (!u.parent) {
|
|
@@ -368,7 +432,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
368
432
|
* Space Complexity: O(1)
|
|
369
433
|
*
|
|
370
434
|
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
|
|
371
|
-
* @param {
|
|
435
|
+
* @param {RedBlackTreeNode<K, V> | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
|
|
372
436
|
* structure. It can either be a valid node or `undefined`.
|
|
373
437
|
*/
|
|
374
438
|
_insertFixup(z) {
|
|
@@ -436,7 +500,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
436
500
|
*
|
|
437
501
|
* The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
|
|
438
502
|
* the colors and performing rotations.
|
|
439
|
-
* @param {
|
|
503
|
+
* @param {RedBlackTreeNode<K, V> | undefined} node - The `node` parameter represents a node in a binary tree. It can
|
|
440
504
|
* be either a valid node object or `undefined`.
|
|
441
505
|
* @returns The function does not return any value. It has a return type of `void`, which means it
|
|
442
506
|
* does not return anything.
|
|
@@ -522,7 +586,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
522
586
|
* Space Complexity: O(1)
|
|
523
587
|
*
|
|
524
588
|
* The `_leftRotate` function performs a left rotation on a given node in a binary tree.
|
|
525
|
-
* @param {
|
|
589
|
+
* @param {RedBlackTreeNode<K, V> | undefined} x - The parameter `x` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a
|
|
526
590
|
* node in a binary tree or `undefined` if there is no node.
|
|
527
591
|
* @returns void, which means it does not return any value.
|
|
528
592
|
*/
|
|
@@ -553,7 +617,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
553
617
|
* Space Complexity: O(1)
|
|
554
618
|
*
|
|
555
619
|
* The `_rightRotate` function performs a right rotation on a given node in a binary tree.
|
|
556
|
-
* @param {
|
|
620
|
+
* @param {RedBlackTreeNode<K, V> | undefined} y - The parameter `y` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a
|
|
557
621
|
* node in a binary tree or `undefined` if there is no node.
|
|
558
622
|
* @returns void, which means it does not return any value.
|
|
559
623
|
*/
|
|
@@ -579,33 +643,5 @@ class RedBlackTree extends bst_1.BST {
|
|
|
579
643
|
x.right = y;
|
|
580
644
|
y.parent = x;
|
|
581
645
|
}
|
|
582
|
-
/**
|
|
583
|
-
* Time Complexity: O(n)
|
|
584
|
-
* Space Complexity: O(n)
|
|
585
|
-
*
|
|
586
|
-
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
|
|
587
|
-
* applying a callback to each entry in the original tree.
|
|
588
|
-
* @param callback - A function that will be called for each entry in the tree, with parameters
|
|
589
|
-
* representing the key, value, index, and the tree itself. It should return an entry for the new
|
|
590
|
-
* tree.
|
|
591
|
-
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
|
|
592
|
-
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
|
|
593
|
-
* Tree that will be created during the mapping process. These options could include things like
|
|
594
|
-
* custom comparators
|
|
595
|
-
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
596
|
-
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
597
|
-
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
598
|
-
* or
|
|
599
|
-
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
|
|
600
|
-
* provided callback function.
|
|
601
|
-
*/
|
|
602
|
-
map(callback, options, thisArg) {
|
|
603
|
-
const newTree = new RedBlackTree([], options);
|
|
604
|
-
let index = 0;
|
|
605
|
-
for (const [key, value] of this) {
|
|
606
|
-
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
607
|
-
}
|
|
608
|
-
return newTree;
|
|
609
|
-
}
|
|
610
646
|
}
|
|
611
647
|
exports.RedBlackTree = RedBlackTree;
|
|
@@ -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
|
+
}
|