data-structure-typed 1.18.8 → 1.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +185 -184
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +182 -148
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +288 -316
- package/dist/data-structures/binary-tree/avl-tree.d.ts +22 -14
- package/dist/data-structures/binary-tree/avl-tree.js +23 -17
- package/dist/data-structures/binary-tree/binary-tree.d.ts +12 -26
- package/dist/data-structures/binary-tree/binary-tree.js +11 -26
- package/dist/data-structures/binary-tree/bst.d.ts +62 -74
- package/dist/data-structures/binary-tree/bst.js +72 -96
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -14
- package/dist/data-structures/binary-tree/rb-tree.js +5 -17
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +186 -17
- package/dist/data-structures/binary-tree/tree-multiset.js +712 -28
- package/dist/data-structures/graph/abstract-graph.d.ts +107 -49
- package/dist/data-structures/graph/abstract-graph.js +104 -55
- package/dist/data-structures/graph/directed-graph.d.ts +95 -94
- package/dist/data-structures/graph/directed-graph.js +95 -95
- package/dist/data-structures/graph/undirected-graph.d.ts +62 -61
- package/dist/data-structures/graph/undirected-graph.js +62 -61
- package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +10 -15
- package/dist/data-structures/interfaces/avl-tree.d.ts +2 -2
- package/dist/data-structures/interfaces/binary-tree.d.ts +1 -1
- package/dist/data-structures/interfaces/bst.d.ts +3 -4
- package/dist/data-structures/interfaces/rb-tree.d.ts +1 -2
- package/dist/data-structures/interfaces/tree-multiset.d.ts +3 -3
- package/dist/data-structures/types/abstract-binary-tree.d.ts +1 -1
- package/dist/data-structures/types/tree-multiset.d.ts +3 -3
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/types/index.d.ts +1 -0
- package/dist/utils/types/index.js +1 -0
- package/dist/utils/types/utils.d.ts +0 -18
- package/dist/utils/types/validate-type.d.ts +19 -0
- package/dist/utils/types/validate-type.js +2 -0
- package/dist/utils/utils.d.ts +3 -8
- package/dist/utils/utils.js +1 -83
- package/dist/utils/validate-type.d.ts +45 -0
- package/dist/utils/validate-type.js +58 -0
- package/package.json +4 -1
|
@@ -39,27 +39,14 @@ var BSTNode = /** @class */ (function (_super) {
|
|
|
39
39
|
function BSTNode() {
|
|
40
40
|
return _super !== null && _super.apply(this, arguments) || this;
|
|
41
41
|
}
|
|
42
|
-
/**
|
|
43
|
-
* The function creates a new binary search tree node with the specified id, value, and count.
|
|
44
|
-
* @param {BinaryTreeNodeId} id - The id parameter is the identifier for the binary tree node. It is used to uniquely
|
|
45
|
-
* identify each node in the tree.
|
|
46
|
-
* @param {T} [val] - The "val" parameter represents the value that will be stored in the binary tree node. It is an
|
|
47
|
-
* optional parameter, meaning it can be omitted when calling the "createNode" function.
|
|
48
|
-
* @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary
|
|
49
|
-
* search tree node. It is an optional parameter, so it can be omitted when calling the `createNode` method.
|
|
50
|
-
* @returns The method is returning a new instance of the BSTNode class, casted as the FAMILY type.
|
|
51
|
-
*/
|
|
52
|
-
BSTNode.prototype.createNode = function (id, val, count) {
|
|
53
|
-
return new BSTNode(id, val, count);
|
|
54
|
-
};
|
|
55
42
|
return BSTNode;
|
|
56
43
|
}(binary_tree_1.BinaryTreeNode));
|
|
57
44
|
exports.BSTNode = BSTNode;
|
|
58
45
|
var BST = /** @class */ (function (_super) {
|
|
59
46
|
__extends(BST, _super);
|
|
60
47
|
/**
|
|
61
|
-
* The constructor function
|
|
62
|
-
* @param [options] - An optional object that
|
|
48
|
+
* The constructor function initializes a binary search tree object with an optional comparator function.
|
|
49
|
+
* @param {BSTOptions} [options] - An optional object that contains configuration options for the binary search tree.
|
|
63
50
|
*/
|
|
64
51
|
function BST(options) {
|
|
65
52
|
var _this = _super.call(this, options) || this;
|
|
@@ -73,38 +60,31 @@ var BST = /** @class */ (function (_super) {
|
|
|
73
60
|
return _this;
|
|
74
61
|
}
|
|
75
62
|
/**
|
|
76
|
-
* The function creates a new binary search tree node with the given id
|
|
77
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is
|
|
78
|
-
*
|
|
79
|
-
* @param
|
|
80
|
-
*
|
|
81
|
-
* @
|
|
82
|
-
* of a particular value in the binary search tree node.
|
|
83
|
-
* @returns a new instance of the BSTNode class, casted as type N.
|
|
63
|
+
* The function creates a new binary search tree node with the given id and value.
|
|
64
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
65
|
+
* identify each node in the binary tree.
|
|
66
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
67
|
+
* that will be stored in the node.
|
|
68
|
+
* @returns a new instance of the BSTNode class with the specified id and value.
|
|
84
69
|
*/
|
|
85
|
-
BST.prototype.createNode = function (id, val
|
|
86
|
-
return new BSTNode(id, val
|
|
70
|
+
BST.prototype.createNode = function (id, val) {
|
|
71
|
+
return new BSTNode(id, val);
|
|
87
72
|
};
|
|
88
73
|
/**
|
|
89
|
-
* The `add` function
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
* the binary search tree. By default, it is set to 1, meaning that if no count is specified, the value will be
|
|
97
|
-
* inserted once.
|
|
98
|
-
* @returns The method `add` returns a `N` object or `null`.
|
|
74
|
+
* The `add` function adds a new node to a binary tree, ensuring that duplicates are not accepted.
|
|
75
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add. It
|
|
76
|
+
* is of type `BinaryTreeNodeId`.
|
|
77
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It represents
|
|
78
|
+
* the value associated with the node.
|
|
79
|
+
* @returns The function `add` returns the inserted node (`inserted`) if it was successfully added to the binary tree.
|
|
80
|
+
* If the node was not added (e.g., due to a duplicate ID), it returns `null` or `undefined`.
|
|
99
81
|
*/
|
|
100
|
-
BST.prototype.add = function (id, val
|
|
101
|
-
if (count === void 0) { count = 1; }
|
|
82
|
+
BST.prototype.add = function (id, val) {
|
|
102
83
|
var inserted = null;
|
|
103
|
-
var newNode = this.createNode(id, val
|
|
84
|
+
var newNode = this.createNode(id, val);
|
|
104
85
|
if (this.root === null) {
|
|
105
86
|
this._setRoot(newNode);
|
|
106
87
|
this._setSize(this.size + 1);
|
|
107
|
-
this._setCount(this.count + count);
|
|
108
88
|
inserted = (this.root);
|
|
109
89
|
}
|
|
110
90
|
else {
|
|
@@ -114,8 +94,6 @@ var BST = /** @class */ (function (_super) {
|
|
|
114
94
|
if (cur !== null && newNode !== null) {
|
|
115
95
|
if (this._compare(cur.id, id) === types_1.CP.eq) {
|
|
116
96
|
if (newNode) {
|
|
117
|
-
cur.count += newNode.count;
|
|
118
|
-
this._setCount(this.count + newNode.count);
|
|
119
97
|
cur.val = newNode.val;
|
|
120
98
|
}
|
|
121
99
|
//Duplicates are not accepted.
|
|
@@ -131,7 +109,6 @@ var BST = /** @class */ (function (_super) {
|
|
|
131
109
|
//Add to the left of the current node
|
|
132
110
|
cur.left = newNode;
|
|
133
111
|
this._setSize(this.size + 1);
|
|
134
|
-
this._setCount(this.count + newNode.count);
|
|
135
112
|
traversing = false;
|
|
136
113
|
inserted = cur.left;
|
|
137
114
|
}
|
|
@@ -150,7 +127,6 @@ var BST = /** @class */ (function (_super) {
|
|
|
150
127
|
//Add to the right of the current node
|
|
151
128
|
cur.right = newNode;
|
|
152
129
|
this._setSize(this.size + 1);
|
|
153
|
-
this._setCount(this.count + newNode.count);
|
|
154
130
|
traversing = false;
|
|
155
131
|
inserted = (cur.right);
|
|
156
132
|
}
|
|
@@ -169,13 +145,12 @@ var BST = /** @class */ (function (_super) {
|
|
|
169
145
|
return inserted;
|
|
170
146
|
};
|
|
171
147
|
/**
|
|
172
|
-
* The
|
|
148
|
+
* The function returns the first node in a binary tree that matches the given property name and value.
|
|
173
149
|
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
174
|
-
* generic type `N`. It represents the
|
|
150
|
+
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
175
151
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
176
|
-
* specifies the property name to use for searching the binary
|
|
177
|
-
*
|
|
178
|
-
* @returns The method is returning a N object or null.
|
|
152
|
+
* specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'id'`.
|
|
153
|
+
* @returns The method is returning either a BinaryTreeNodeId or N (generic type) or null.
|
|
179
154
|
*/
|
|
180
155
|
BST.prototype.get = function (nodeProperty, propertyName) {
|
|
181
156
|
var _a;
|
|
@@ -185,10 +160,9 @@ var BST = /** @class */ (function (_super) {
|
|
|
185
160
|
/**
|
|
186
161
|
* The function returns the id of the rightmost node if the comparison between two values is less than, the id of the
|
|
187
162
|
* leftmost node if the comparison is greater than, and the id of the rightmost node otherwise.
|
|
188
|
-
* @returns The
|
|
189
|
-
* the
|
|
190
|
-
*
|
|
191
|
-
* there are no nodes in
|
|
163
|
+
* @returns The method `lastKey()` returns the id of the rightmost node in the binary tree if the comparison between
|
|
164
|
+
* the values at index 0 and 1 is less than, otherwise it returns the id of the leftmost node. If the comparison is
|
|
165
|
+
* equal, it returns the id of the rightmost node. If there are no nodes in the tree, it returns 0.
|
|
192
166
|
*/
|
|
193
167
|
BST.prototype.lastKey = function () {
|
|
194
168
|
var _a, _b, _c, _d, _e, _f;
|
|
@@ -200,17 +174,15 @@ var BST = /** @class */ (function (_super) {
|
|
|
200
174
|
return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.id) !== null && _f !== void 0 ? _f : 0;
|
|
201
175
|
};
|
|
202
176
|
/**
|
|
203
|
-
* The function `getNodes` returns an array of binary
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
* generic type `N`. It represents the property value that you want to search for in the binary search tree.
|
|
177
|
+
* The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
|
|
178
|
+
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or an
|
|
179
|
+
* `N` type. It represents the property of the binary tree node that you want to compare with.
|
|
207
180
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
208
|
-
* specifies the property
|
|
209
|
-
* `
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
* @returns an array of N objects.
|
|
181
|
+
* specifies the property name to use for comparison. If not provided, it defaults to `'id'`.
|
|
182
|
+
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
183
|
+
* return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
|
|
184
|
+
* is set to `true`, the function will return an array with only one node (if
|
|
185
|
+
* @returns an array of nodes (type N).
|
|
214
186
|
*/
|
|
215
187
|
BST.prototype.getNodes = function (nodeProperty, propertyName, onlyOne) {
|
|
216
188
|
var _this = this;
|
|
@@ -261,13 +233,13 @@ var BST = /** @class */ (function (_super) {
|
|
|
261
233
|
};
|
|
262
234
|
// --- start additional functions
|
|
263
235
|
/**
|
|
264
|
-
* The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a
|
|
265
|
-
* than a given node.
|
|
236
|
+
* The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
|
|
237
|
+
* less than a given node.
|
|
266
238
|
* @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
267
239
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
268
240
|
* specifies the property name to use for calculating the sum. If not provided, it defaults to `'id'`.
|
|
269
|
-
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in
|
|
270
|
-
* tree that have a lesser value than the specified `beginNode` based on the
|
|
241
|
+
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
|
|
242
|
+
* binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
|
|
271
243
|
*/
|
|
272
244
|
BST.prototype.lesserSum = function (beginNode, propertyName) {
|
|
273
245
|
var _this = this;
|
|
@@ -285,9 +257,6 @@ var BST = /** @class */ (function (_super) {
|
|
|
285
257
|
case 'id':
|
|
286
258
|
needSum = cur.id;
|
|
287
259
|
break;
|
|
288
|
-
case 'count':
|
|
289
|
-
needSum = cur.count;
|
|
290
|
-
break;
|
|
291
260
|
default:
|
|
292
261
|
needSum = cur.id;
|
|
293
262
|
break;
|
|
@@ -353,15 +322,15 @@ var BST = /** @class */ (function (_super) {
|
|
|
353
322
|
return sum;
|
|
354
323
|
};
|
|
355
324
|
/**
|
|
356
|
-
* The
|
|
357
|
-
*
|
|
358
|
-
* @param node - The `node` parameter
|
|
359
|
-
*
|
|
325
|
+
* The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
|
|
326
|
+
* have a greater value than a given node.
|
|
327
|
+
* @param {N | BinaryTreeNodeId | null} node - The `node` parameter can be either of type `N` (a generic type),
|
|
328
|
+
* `BinaryTreeNodeId`, or `null`. It represents the node in the binary tree to which the delta value will be added.
|
|
360
329
|
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
|
|
361
|
-
* each node should be increased.
|
|
362
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that
|
|
363
|
-
* property of the
|
|
364
|
-
*
|
|
330
|
+
* each greater node should be increased.
|
|
331
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
332
|
+
* specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
|
|
333
|
+
* 'id'.
|
|
365
334
|
* @returns a boolean value.
|
|
366
335
|
*/
|
|
367
336
|
BST.prototype.allGreaterNodesAdd = function (node, delta, propertyName) {
|
|
@@ -379,9 +348,6 @@ var BST = /** @class */ (function (_super) {
|
|
|
379
348
|
case 'id':
|
|
380
349
|
cur.id += delta;
|
|
381
350
|
break;
|
|
382
|
-
case 'count':
|
|
383
|
-
cur.count += delta;
|
|
384
|
-
break;
|
|
385
351
|
default:
|
|
386
352
|
cur.id += delta;
|
|
387
353
|
break;
|
|
@@ -390,12 +356,13 @@ var BST = /** @class */ (function (_super) {
|
|
|
390
356
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
391
357
|
var _traverse_3 = function (cur) {
|
|
392
358
|
var compared = _this._compare(cur.id, id);
|
|
393
|
-
|
|
359
|
+
if (compared === types_1.CP.gt)
|
|
360
|
+
_sumByPropertyName(cur);
|
|
394
361
|
if (!cur.left && !cur.right)
|
|
395
362
|
return;
|
|
396
|
-
if (cur.left &&
|
|
363
|
+
if (cur.left && _this._compare(cur.left.id, id) === types_1.CP.gt)
|
|
397
364
|
_traverse_3(cur.left);
|
|
398
|
-
|
|
365
|
+
if (cur.right && _this._compare(cur.right.id, id) === types_1.CP.gt)
|
|
399
366
|
_traverse_3(cur.right);
|
|
400
367
|
};
|
|
401
368
|
_traverse_3(this.root);
|
|
@@ -406,11 +373,12 @@ var BST = /** @class */ (function (_super) {
|
|
|
406
373
|
while (queue.length > 0) {
|
|
407
374
|
var cur = queue.shift();
|
|
408
375
|
if (cur) {
|
|
409
|
-
var compared = this._compare(cur.id,
|
|
410
|
-
|
|
411
|
-
|
|
376
|
+
var compared = this._compare(cur.id, id);
|
|
377
|
+
if (compared === types_1.CP.gt)
|
|
378
|
+
_sumByPropertyName(cur);
|
|
379
|
+
if (cur.left && this._compare(cur.left.id, id) === types_1.CP.gt)
|
|
412
380
|
queue.push(cur.left);
|
|
413
|
-
|
|
381
|
+
if (cur.right && this._compare(cur.right.id, id) === types_1.CP.gt)
|
|
414
382
|
queue.push(cur.right);
|
|
415
383
|
}
|
|
416
384
|
}
|
|
@@ -418,11 +386,20 @@ var BST = /** @class */ (function (_super) {
|
|
|
418
386
|
}
|
|
419
387
|
};
|
|
420
388
|
/**
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
*
|
|
389
|
+
* Balancing Adjustment:
|
|
390
|
+
* Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
|
|
391
|
+
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
|
|
392
|
+
*
|
|
393
|
+
* Use Cases and Efficiency:
|
|
394
|
+
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
|
|
395
|
+
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
424
396
|
*/
|
|
425
|
-
|
|
397
|
+
/**
|
|
398
|
+
* The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
|
|
399
|
+
* constructs a balanced binary search tree using either a recursive or iterative approach.
|
|
400
|
+
* @returns The function `perfectlyBalance()` returns a boolean value.
|
|
401
|
+
*/
|
|
402
|
+
BST.prototype.perfectlyBalance = function () {
|
|
426
403
|
var _this = this;
|
|
427
404
|
var sorted = this.DFS('in', 'node'), n = sorted.length;
|
|
428
405
|
this.clear();
|
|
@@ -434,7 +411,7 @@ var BST = /** @class */ (function (_super) {
|
|
|
434
411
|
return;
|
|
435
412
|
var m = l + Math.floor((r - l) / 2);
|
|
436
413
|
var midNode = sorted[m];
|
|
437
|
-
_this.add(midNode.id, midNode.val
|
|
414
|
+
_this.add(midNode.id, midNode.val);
|
|
438
415
|
buildBalanceBST_1(l, m - 1);
|
|
439
416
|
buildBalanceBST_1(m + 1, r);
|
|
440
417
|
};
|
|
@@ -450,7 +427,7 @@ var BST = /** @class */ (function (_super) {
|
|
|
450
427
|
if (l <= r) {
|
|
451
428
|
var m = l + Math.floor((r - l) / 2);
|
|
452
429
|
var midNode = sorted[m];
|
|
453
|
-
this.add(midNode.id, midNode.val
|
|
430
|
+
this.add(midNode.id, midNode.val);
|
|
454
431
|
stack.push([m + 1, r]);
|
|
455
432
|
stack.push([l, m - 1]);
|
|
456
433
|
}
|
|
@@ -460,9 +437,8 @@ var BST = /** @class */ (function (_super) {
|
|
|
460
437
|
}
|
|
461
438
|
};
|
|
462
439
|
/**
|
|
463
|
-
* The function `isAVLBalanced` checks if a binary
|
|
464
|
-
* @returns
|
|
465
|
-
* is balanced according to the AVL tree property, and `false` otherwise.
|
|
440
|
+
* The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
|
|
441
|
+
* @returns a boolean value.
|
|
466
442
|
*/
|
|
467
443
|
BST.prototype.isAVLBalanced = function () {
|
|
468
444
|
var _a, _b;
|
|
@@ -1,26 +1,15 @@
|
|
|
1
1
|
import { BinaryTreeNodeId, RBColor, RBTreeNodeNested, RBTreeOptions } from '../types';
|
|
2
2
|
import { IRBTree, IRBTreeNode } from '../interfaces/rb-tree';
|
|
3
3
|
import { BST, BSTNode } from './bst';
|
|
4
|
-
export declare class RBTreeNode<T = any,
|
|
5
|
-
constructor(id: BinaryTreeNodeId,
|
|
4
|
+
export declare class RBTreeNode<T = any, NEIGHBOR extends RBTreeNode<T, NEIGHBOR> = RBTreeNodeNested<T>> extends BSTNode<T, NEIGHBOR> implements IRBTreeNode<T, NEIGHBOR> {
|
|
5
|
+
constructor(id: BinaryTreeNodeId, color: RBColor, val?: T);
|
|
6
6
|
private _color;
|
|
7
7
|
get color(): RBColor;
|
|
8
8
|
set color(value: RBColor);
|
|
9
|
-
/**
|
|
10
|
-
* The function creates a new RBTreeNode with the given id, value, and count and returns it as a FAMILY object.
|
|
11
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
12
|
-
* identify each node in the tree.
|
|
13
|
-
* @param {T | null} [val] - The "val" parameter represents the value to be stored in the node. It can be of type T
|
|
14
|
-
* (generic type) or null.
|
|
15
|
-
* @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary tree
|
|
16
|
-
* node.
|
|
17
|
-
* @returns The method is returning a new instance of the RBTreeNode class, casted as a FAMILY type.
|
|
18
|
-
*/
|
|
19
|
-
createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY;
|
|
20
9
|
}
|
|
21
10
|
export declare class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IRBTree<N> {
|
|
22
11
|
constructor(options?: RBTreeOptions);
|
|
23
|
-
createNode(id: BinaryTreeNodeId, val?: N['val']
|
|
12
|
+
createNode(id: BinaryTreeNodeId, val?: N['val']): N;
|
|
24
13
|
insert(id: number, val?: N | null): void;
|
|
25
14
|
private leftRotate;
|
|
26
15
|
private rightRotate;
|
|
@@ -20,9 +20,10 @@ var types_1 = require("../types");
|
|
|
20
20
|
var bst_1 = require("./bst");
|
|
21
21
|
var RBTreeNode = /** @class */ (function (_super) {
|
|
22
22
|
__extends(RBTreeNode, _super);
|
|
23
|
-
function RBTreeNode(id,
|
|
24
|
-
var _this = _super.call(this, id, val
|
|
23
|
+
function RBTreeNode(id, color, val) {
|
|
24
|
+
var _this = _super.call(this, id, val) || this;
|
|
25
25
|
_this._color = types_1.RBColor.RED;
|
|
26
|
+
_this._color = color;
|
|
26
27
|
return _this;
|
|
27
28
|
}
|
|
28
29
|
Object.defineProperty(RBTreeNode.prototype, "color", {
|
|
@@ -35,19 +36,6 @@ var RBTreeNode = /** @class */ (function (_super) {
|
|
|
35
36
|
enumerable: false,
|
|
36
37
|
configurable: true
|
|
37
38
|
});
|
|
38
|
-
/**
|
|
39
|
-
* The function creates a new RBTreeNode with the given id, value, and count and returns it as a FAMILY object.
|
|
40
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
41
|
-
* identify each node in the tree.
|
|
42
|
-
* @param {T | null} [val] - The "val" parameter represents the value to be stored in the node. It can be of type T
|
|
43
|
-
* (generic type) or null.
|
|
44
|
-
* @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary tree
|
|
45
|
-
* node.
|
|
46
|
-
* @returns The method is returning a new instance of the RBTreeNode class, casted as a FAMILY type.
|
|
47
|
-
*/
|
|
48
|
-
RBTreeNode.prototype.createNode = function (id, val, count) {
|
|
49
|
-
return new RBTreeNode(id, val, count);
|
|
50
|
-
};
|
|
51
39
|
return RBTreeNode;
|
|
52
40
|
}(bst_1.BSTNode));
|
|
53
41
|
exports.RBTreeNode = RBTreeNode;
|
|
@@ -56,8 +44,8 @@ var RBTree = /** @class */ (function (_super) {
|
|
|
56
44
|
function RBTree(options) {
|
|
57
45
|
return _super.call(this, options) || this;
|
|
58
46
|
}
|
|
59
|
-
RBTree.prototype.createNode = function (id, val
|
|
60
|
-
return new RBTreeNode(id,
|
|
47
|
+
RBTree.prototype.createNode = function (id, val) {
|
|
48
|
+
return new RBTreeNode(id, types_1.RBColor.RED, val);
|
|
61
49
|
};
|
|
62
50
|
// private override _root: BinaryTreeNode<N> | null = null;
|
|
63
51
|
//
|
|
@@ -5,27 +5,39 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeNodeId,
|
|
9
|
-
import {
|
|
8
|
+
import type { BinaryTreeNodeId, TreeMultisetNodeNested, TreeMultisetOptions } from '../types';
|
|
9
|
+
import { BinaryTreeDeletedResult, DFSOrderPattern, NodeOrPropertyName } from '../types';
|
|
10
|
+
import { ITreeMultiset, ITreeMultisetNode } from '../interfaces';
|
|
10
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
11
|
-
export declare class
|
|
12
|
-
/**
|
|
13
|
-
* The function
|
|
14
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is
|
|
15
|
-
*
|
|
16
|
-
* @param {T} [val] - The `val` parameter
|
|
17
|
-
*
|
|
18
|
-
* @param {number} [count] - The `count` parameter represents the number of
|
|
19
|
-
* node. It
|
|
20
|
-
*
|
|
21
|
-
*/
|
|
22
|
-
|
|
12
|
+
export declare class TreeMultisetNode<T = any, NEIGHBOR extends TreeMultisetNode<T, NEIGHBOR> = TreeMultisetNodeNested<T>> extends AVLTreeNode<T, NEIGHBOR> implements ITreeMultisetNode<T, NEIGHBOR> {
|
|
13
|
+
/**
|
|
14
|
+
* The constructor function initializes a BinaryTreeNode object with an id, value, and count.
|
|
15
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is of type `BinaryTreeNodeId` and represents the unique identifier
|
|
16
|
+
* of the binary tree node.
|
|
17
|
+
* @param {T} [val] - The `val` parameter is an optional parameter of type `T`. It represents the value of the binary
|
|
18
|
+
* tree node. If no value is provided, it will be `undefined`.
|
|
19
|
+
* @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
|
|
20
|
+
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
21
|
+
* parameter when creating a new instance of the `BinaryTreeNode` class,
|
|
22
|
+
*/
|
|
23
|
+
constructor(id: BinaryTreeNodeId, val?: T, count?: number);
|
|
24
|
+
private _count;
|
|
25
|
+
get count(): number;
|
|
26
|
+
set count(v: number);
|
|
23
27
|
}
|
|
24
28
|
/**
|
|
25
|
-
* The only distinction between a
|
|
29
|
+
* The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
26
30
|
*/
|
|
27
|
-
export declare class
|
|
28
|
-
|
|
31
|
+
export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultisetNode> extends AVLTree<N> implements ITreeMultiset<N> {
|
|
32
|
+
/**
|
|
33
|
+
* The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
|
|
34
|
+
* merge duplicated values.
|
|
35
|
+
* @param {TreeMultisetOptions} [options] - An optional object that contains additional configuration options for the
|
|
36
|
+
* TreeMultiset.
|
|
37
|
+
*/
|
|
38
|
+
constructor(options?: TreeMultisetOptions);
|
|
39
|
+
private _count;
|
|
40
|
+
get count(): number;
|
|
29
41
|
/**
|
|
30
42
|
* The function creates a new BSTNode with the given id, value, and count.
|
|
31
43
|
* @param {BinaryTreeNodeId} id - The id parameter is the unique identifier for the binary tree node. It is used to
|
|
@@ -36,4 +48,161 @@ export declare class TreeMultiSet<N extends TreeMultiSetNode<N['val'], N> = Tree
|
|
|
36
48
|
* @returns A new instance of the BSTNode class with the specified id, value, and count (if provided).
|
|
37
49
|
*/
|
|
38
50
|
createNode(id: BinaryTreeNodeId, val?: N['val'], count?: number): N;
|
|
51
|
+
/**
|
|
52
|
+
* The function swaps the location of two nodes in a tree data structure.
|
|
53
|
+
* @param {N} srcNode - The source node that we want to swap with the destination node.
|
|
54
|
+
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
55
|
+
* be swapped with.
|
|
56
|
+
* @returns the `destNode` after swapping its values with the `srcNode`.
|
|
57
|
+
*/
|
|
58
|
+
swapLocation(srcNode: N, destNode: N): N;
|
|
59
|
+
/**
|
|
60
|
+
* The `add` function adds a new node to a binary tree, updating the size and count properties accordingly, and
|
|
61
|
+
* balancing the tree if necessary.
|
|
62
|
+
* @param {BinaryTreeNodeId} id - The id parameter represents the identifier of the binary tree node that we want to
|
|
63
|
+
* add. It is of type BinaryTreeNodeId.
|
|
64
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. If no value is
|
|
65
|
+
* provided, it will default to `undefined`.
|
|
66
|
+
* @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the node
|
|
67
|
+
* with the given `id` should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
|
|
68
|
+
* @returns The `add` method returns the inserted node (`N`), `null`, or `undefined`.
|
|
69
|
+
*/
|
|
70
|
+
add(id: BinaryTreeNodeId, val?: N['val'], count?: number): N | null | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* The function adds a new node to a binary tree as the left or right child of a given parent node.
|
|
73
|
+
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to the tree. It can be
|
|
74
|
+
* either a node object (`N`) or `null`.
|
|
75
|
+
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
|
|
76
|
+
* child.
|
|
77
|
+
* @returns either the left or right child node that was added to the parent node. It can also return `null` or
|
|
78
|
+
* `undefined` in certain cases.
|
|
79
|
+
*/
|
|
80
|
+
addTo(newNode: N | null, parent: N): N | null | undefined;
|
|
81
|
+
/**
|
|
82
|
+
* The `addMany` function inserts multiple items into a binary tree and returns an array of the inserted nodes or
|
|
83
|
+
* null/undefined values.
|
|
84
|
+
* @param {N[] | N[]} data - The `data` parameter can be either an array of elements of type `N` or an
|
|
85
|
+
* array of `N` objects.
|
|
86
|
+
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
87
|
+
*/
|
|
88
|
+
addMany(data: N[] | Array<N['val']>): (N | null | undefined)[];
|
|
89
|
+
/**
|
|
90
|
+
* The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
91
|
+
* node that needs to be balanced.
|
|
92
|
+
* @param {N | BinaryTreeNodeId | null} nodeOrId - The `nodeOrId` parameter can be one of the following:
|
|
93
|
+
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
94
|
+
* whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
|
|
95
|
+
* not be taken into account when removing it. If `ignoreCount` is set to `false
|
|
96
|
+
* @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
97
|
+
*/
|
|
98
|
+
remove(nodeOrId: N | BinaryTreeNodeId, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
99
|
+
/**
|
|
100
|
+
* The function `getSubTreeCount` calculates the number of nodes and the sum of their counts in a subtree, using either
|
|
101
|
+
* recursive or iterative traversal.
|
|
102
|
+
* @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
|
|
103
|
+
* binary tree.
|
|
104
|
+
* @returns The function `getSubTreeCount` returns an array `[number, number]`.
|
|
105
|
+
*/
|
|
106
|
+
getSubTreeCount(subTreeRoot: N | null | undefined): [number, number];
|
|
107
|
+
/**
|
|
108
|
+
* The function `subTreeSumCount` calculates the sum of the `count` property of each node in a subtree, either
|
|
109
|
+
* recursively or iteratively.
|
|
110
|
+
* @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
111
|
+
* in a binary tree. It can be either a `BinaryTreeNodeId` (a unique identifier for a node in the binary tree) or
|
|
112
|
+
* `null` if the subtree is empty.
|
|
113
|
+
* @returns the sum of the count values of all nodes in the subtree rooted at `subTreeRoot`.
|
|
114
|
+
*/
|
|
115
|
+
subTreeSumCount(subTreeRoot: N | BinaryTreeNodeId | null): number;
|
|
116
|
+
/**
|
|
117
|
+
* The function `subTreeAddCount` recursively or iteratively traverses a binary tree and adds a given delta value to
|
|
118
|
+
* the `count` property of each node.
|
|
119
|
+
* @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
120
|
+
* in a binary tree. It can be either a `BinaryTreeNodeId` (a unique identifier for a node in the binary tree), a
|
|
121
|
+
* `BinaryTreeNode` object, or `null` if the subtree is empty.
|
|
122
|
+
* @param {number} delta - The delta parameter is a number that represents the amount by which the count of each node
|
|
123
|
+
* in the subtree should be increased or decreased.
|
|
124
|
+
* @returns a boolean value.
|
|
125
|
+
*/
|
|
126
|
+
subTreeAddCount(subTreeRoot: N | BinaryTreeNodeId | null, delta: number): boolean;
|
|
127
|
+
/**
|
|
128
|
+
* The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
|
|
129
|
+
* using a queue.
|
|
130
|
+
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
131
|
+
* `N`. It represents the property of the nodes that you want to search for.
|
|
132
|
+
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
133
|
+
* return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set
|
|
134
|
+
* to `true`, the function will return only one node. If `onlyOne`
|
|
135
|
+
* @returns an array of nodes that match the given nodeProperty.
|
|
136
|
+
*/
|
|
137
|
+
getNodesByCount(nodeProperty: BinaryTreeNodeId | N, onlyOne?: boolean): N[];
|
|
138
|
+
/**
|
|
139
|
+
* The BFSCount function returns an array of counts from a breadth-first search of nodes.
|
|
140
|
+
* @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
|
|
141
|
+
* BFS traversal.
|
|
142
|
+
*/
|
|
143
|
+
BFSCount(): number[];
|
|
144
|
+
/**
|
|
145
|
+
* The function "listLevelsCount" takes a node and returns an array of arrays, where each inner array contains the
|
|
146
|
+
* count property of each node at that level.
|
|
147
|
+
* @param {N | null} node - The parameter `node` is of type `N | null`. This means that it can either be an instance of
|
|
148
|
+
* the class `N` or `null`.
|
|
149
|
+
* @returns a 2D array of numbers. Each inner array represents a level in the binary tree, and each number in the inner
|
|
150
|
+
* array represents the count property of a node in that level.
|
|
151
|
+
*/
|
|
152
|
+
listLevelsCount(node: N | null): number[][];
|
|
153
|
+
/**
|
|
154
|
+
* The `morrisCount` function returns an array of counts for each node in a binary tree, based on a specified traversal
|
|
155
|
+
* pattern.
|
|
156
|
+
* @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that specifies the
|
|
157
|
+
* traversal pattern for the Morris traversal algorithm. It can have one of three values: 'in', 'pre', or 'post'.
|
|
158
|
+
* @returns The function `morrisCount` returns an array of numbers.
|
|
159
|
+
*/
|
|
160
|
+
morrisCount(pattern?: 'in' | 'pre' | 'post'): number[];
|
|
161
|
+
/**
|
|
162
|
+
* The function DFSIterativeCount performs a depth-first search iteratively and returns an array of count values for
|
|
163
|
+
* each node.
|
|
164
|
+
* @param {'in' | 'pre' | 'post'} [pattern] - The "pattern" parameter is a string that specifies the traversal order
|
|
165
|
+
* for the Depth-First Search (DFS) algorithm. It can have one of three values: 'in', 'pre', or 'post'.
|
|
166
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
|
|
167
|
+
* specifies whether to return the nodes or the property names during the depth-first search traversal. If it is set to
|
|
168
|
+
* `'node'`, the function will return the nodes. If it is set to `'property'`, the function will return the property
|
|
169
|
+
* @returns The DFSIterativeCount method returns an array of numbers.
|
|
170
|
+
*/
|
|
171
|
+
DFSIterativeCount(pattern?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): number[];
|
|
172
|
+
/**
|
|
173
|
+
* The DFSCount function returns an array of counts for each node in a depth-first search traversal.
|
|
174
|
+
* @param {DFSOrderPattern} [pattern] - The `pattern` parameter is an optional parameter that specifies the order in
|
|
175
|
+
* which the Depth-First Search (DFS) algorithm should traverse the nodes. It can have one of the following values:
|
|
176
|
+
* @param [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is used to specify whether you want to retrieve the
|
|
177
|
+
* nodes themselves or a specific property of the nodes. If you pass `'count'` as the value for `nodeOrPropertyName`,
|
|
178
|
+
* the function will return an array of the `count` property of each node.
|
|
179
|
+
* @returns The DFSCount method returns an array of numbers representing the count property of each node in the DFS
|
|
180
|
+
* traversal.
|
|
181
|
+
*/
|
|
182
|
+
DFSCount(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[];
|
|
183
|
+
/**
|
|
184
|
+
* The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
|
|
185
|
+
* value than a given node.
|
|
186
|
+
* @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
187
|
+
* @returns the sum of the counts of nodes in the binary tree that have a lesser value than the given beginNode.
|
|
188
|
+
*/
|
|
189
|
+
lesserSumCount(beginNode: N | BinaryTreeNodeId | null): number;
|
|
190
|
+
/**
|
|
191
|
+
* The function `allGreaterNodesAddCount` updates the count property of all nodes in a binary tree that have an ID
|
|
192
|
+
* greater than a given ID by a specified delta value.
|
|
193
|
+
* @param {N | BinaryTreeNodeId | null} node - The `node` parameter can be one of the following:
|
|
194
|
+
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the `count` property
|
|
195
|
+
* of each node should be increased.
|
|
196
|
+
* @returns a boolean value.
|
|
197
|
+
*/
|
|
198
|
+
allGreaterNodesAddCount(node: N | BinaryTreeNodeId | null, delta: number): boolean;
|
|
199
|
+
/**
|
|
200
|
+
* The clear() function clears the data and sets the count to 0.
|
|
201
|
+
*/
|
|
202
|
+
clear(): void;
|
|
203
|
+
/**
|
|
204
|
+
* The function "_setCount" is used to set the value of the "_count" property.
|
|
205
|
+
* @param {number} v - number
|
|
206
|
+
*/
|
|
207
|
+
protected _setCount(v: number): void;
|
|
39
208
|
}
|