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,20 +39,16 @@ var utils_1 = require("../../utils");
|
|
|
39
39
|
var types_1 = require("../types");
|
|
40
40
|
var AbstractBinaryTreeNode = /** @class */ (function () {
|
|
41
41
|
/**
|
|
42
|
-
* The constructor function initializes a BinaryTreeNode object with an id
|
|
42
|
+
* The constructor function initializes a BinaryTreeNode object with an id and an optional value.
|
|
43
43
|
* @param {BinaryTreeNodeId} id - The `id` parameter is of type `BinaryTreeNodeId` and represents the unique identifier
|
|
44
|
-
*
|
|
45
|
-
* @param {T} [val] - The
|
|
46
|
-
* tree node. If no value is provided, it will be
|
|
47
|
-
* @param {number} [count] - The `count` parameter is an optional parameter that represents the number of times the
|
|
48
|
-
* value `val` appears in the binary tree node. If the `count` parameter is not provided, it defaults to 1.
|
|
44
|
+
* of the binary tree node. It is used to distinguish one node from another in the binary tree.
|
|
45
|
+
* @param {T} [val] - The "val" parameter is an optional parameter of type T. It represents the value that will be
|
|
46
|
+
* stored in the binary tree node. If no value is provided, it will be set to undefined.
|
|
49
47
|
*/
|
|
50
|
-
function AbstractBinaryTreeNode(id, val
|
|
51
|
-
this._count = 1;
|
|
48
|
+
function AbstractBinaryTreeNode(id, val) {
|
|
52
49
|
this._height = 0;
|
|
53
50
|
this._id = id;
|
|
54
51
|
this._val = val;
|
|
55
|
-
this._count = count !== null && count !== void 0 ? count : 1;
|
|
56
52
|
}
|
|
57
53
|
Object.defineProperty(AbstractBinaryTreeNode.prototype, "id", {
|
|
58
54
|
get: function () {
|
|
@@ -110,16 +106,6 @@ var AbstractBinaryTreeNode = /** @class */ (function () {
|
|
|
110
106
|
enumerable: false,
|
|
111
107
|
configurable: true
|
|
112
108
|
});
|
|
113
|
-
Object.defineProperty(AbstractBinaryTreeNode.prototype, "count", {
|
|
114
|
-
get: function () {
|
|
115
|
-
return this._count;
|
|
116
|
-
},
|
|
117
|
-
set: function (v) {
|
|
118
|
-
this._count = v;
|
|
119
|
-
},
|
|
120
|
-
enumerable: false,
|
|
121
|
-
configurable: true
|
|
122
|
-
});
|
|
123
109
|
Object.defineProperty(AbstractBinaryTreeNode.prototype, "height", {
|
|
124
110
|
get: function () {
|
|
125
111
|
return this._height;
|
|
@@ -131,6 +117,10 @@ var AbstractBinaryTreeNode = /** @class */ (function () {
|
|
|
131
117
|
configurable: true
|
|
132
118
|
});
|
|
133
119
|
Object.defineProperty(AbstractBinaryTreeNode.prototype, "familyPosition", {
|
|
120
|
+
/**
|
|
121
|
+
* The function determines the position of a node in a family tree structure.
|
|
122
|
+
* @returns a value of type `FamilyPosition`.
|
|
123
|
+
*/
|
|
134
124
|
get: function () {
|
|
135
125
|
var that = this;
|
|
136
126
|
if (that.parent) {
|
|
@@ -166,36 +156,6 @@ var AbstractBinaryTreeNode = /** @class */ (function () {
|
|
|
166
156
|
enumerable: false,
|
|
167
157
|
configurable: true
|
|
168
158
|
});
|
|
169
|
-
/**
|
|
170
|
-
* The function swaps the location of two nodes in a binary tree.
|
|
171
|
-
* @param {FAMILY} destNode - The `swapNode` parameter is of type `FAMILY`, which represents a node in a family tree.
|
|
172
|
-
* @returns the `swapNode` object after swapping its properties with the properties of `this` object.
|
|
173
|
-
*/
|
|
174
|
-
AbstractBinaryTreeNode.prototype.swapLocation = function (destNode) {
|
|
175
|
-
var val = destNode.val, count = destNode.count, height = destNode.height, id = destNode.id;
|
|
176
|
-
var tempNode = this.createNode(id, val, count);
|
|
177
|
-
tempNode.height = height;
|
|
178
|
-
if (tempNode instanceof AbstractBinaryTreeNode) {
|
|
179
|
-
// TODO should we consider the left, right children?
|
|
180
|
-
destNode.id = this.id;
|
|
181
|
-
destNode.val = this.val;
|
|
182
|
-
destNode.count = this.count;
|
|
183
|
-
destNode.height = this.height;
|
|
184
|
-
this.id = tempNode.id;
|
|
185
|
-
this.val = tempNode.val;
|
|
186
|
-
this.count = tempNode.count;
|
|
187
|
-
this.height = tempNode.height;
|
|
188
|
-
}
|
|
189
|
-
return destNode;
|
|
190
|
-
};
|
|
191
|
-
/**
|
|
192
|
-
* The `clone` function returns a new instance of the `FAMILY` class with the same `id`, `val`, and `count` properties.
|
|
193
|
-
* @returns The `clone()` method is returning a new instance of the `FAMILY` class with the same `id`, `val`, and
|
|
194
|
-
* `count` values as the current instance.
|
|
195
|
-
*/
|
|
196
|
-
AbstractBinaryTreeNode.prototype.clone = function () {
|
|
197
|
-
return this.createNode(this.id, this.val, this.count);
|
|
198
|
-
};
|
|
199
159
|
return AbstractBinaryTreeNode;
|
|
200
160
|
}());
|
|
201
161
|
exports.AbstractBinaryTreeNode = AbstractBinaryTreeNode;
|
|
@@ -206,18 +166,18 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
206
166
|
* tree.
|
|
207
167
|
*/
|
|
208
168
|
function AbstractBinaryTree(options) {
|
|
169
|
+
this._root = null;
|
|
170
|
+
this._size = 0;
|
|
209
171
|
this._loopType = types_1.LoopType.ITERATIVE;
|
|
172
|
+
this._autoIncrementId = false;
|
|
173
|
+
this._maxId = -1;
|
|
174
|
+
// TODO this variable may be moved to TreeMultiset
|
|
175
|
+
this._isMergeDuplicatedVal = true;
|
|
210
176
|
this._visitedId = [];
|
|
211
177
|
this._visitedVal = [];
|
|
212
178
|
this._visitedNode = [];
|
|
213
179
|
this._visitedCount = [];
|
|
214
180
|
this._visitedLeftSum = [];
|
|
215
|
-
this._autoIncrementId = false;
|
|
216
|
-
this._maxId = -1;
|
|
217
|
-
this._isMergeDuplicatedVal = true;
|
|
218
|
-
this._root = null;
|
|
219
|
-
this._size = 0;
|
|
220
|
-
this._count = 0;
|
|
221
181
|
if (options !== undefined) {
|
|
222
182
|
var _a = options.loopType, loopType = _a === void 0 ? types_1.LoopType.ITERATIVE : _a, _b = options.autoIncrementId, autoIncrementId = _b === void 0 ? false : _b, _c = options.isMergeDuplicatedVal, isMergeDuplicatedVal = _c === void 0 ? true : _c;
|
|
223
183
|
this._isMergeDuplicatedVal = isMergeDuplicatedVal;
|
|
@@ -225,97 +185,113 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
225
185
|
this._loopType = loopType;
|
|
226
186
|
}
|
|
227
187
|
}
|
|
228
|
-
Object.defineProperty(AbstractBinaryTree.prototype, "
|
|
229
|
-
get: function () {
|
|
230
|
-
return this._loopType;
|
|
231
|
-
},
|
|
232
|
-
enumerable: false,
|
|
233
|
-
configurable: true
|
|
234
|
-
});
|
|
235
|
-
Object.defineProperty(AbstractBinaryTree.prototype, "visitedId", {
|
|
188
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "root", {
|
|
236
189
|
get: function () {
|
|
237
|
-
return this.
|
|
190
|
+
return this._root;
|
|
238
191
|
},
|
|
239
192
|
enumerable: false,
|
|
240
193
|
configurable: true
|
|
241
194
|
});
|
|
242
|
-
Object.defineProperty(AbstractBinaryTree.prototype, "
|
|
195
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "size", {
|
|
243
196
|
get: function () {
|
|
244
|
-
return this.
|
|
197
|
+
return this._size;
|
|
245
198
|
},
|
|
246
199
|
enumerable: false,
|
|
247
200
|
configurable: true
|
|
248
201
|
});
|
|
249
|
-
Object.defineProperty(AbstractBinaryTree.prototype, "
|
|
202
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "loopType", {
|
|
250
203
|
get: function () {
|
|
251
|
-
return this.
|
|
204
|
+
return this._loopType;
|
|
252
205
|
},
|
|
253
206
|
enumerable: false,
|
|
254
207
|
configurable: true
|
|
255
208
|
});
|
|
256
|
-
Object.defineProperty(AbstractBinaryTree.prototype, "
|
|
209
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "autoIncrementId", {
|
|
257
210
|
get: function () {
|
|
258
|
-
return this.
|
|
211
|
+
return this._autoIncrementId;
|
|
259
212
|
},
|
|
260
213
|
enumerable: false,
|
|
261
214
|
configurable: true
|
|
262
215
|
});
|
|
263
|
-
Object.defineProperty(AbstractBinaryTree.prototype, "
|
|
216
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "maxId", {
|
|
264
217
|
get: function () {
|
|
265
|
-
return this.
|
|
218
|
+
return this._maxId;
|
|
266
219
|
},
|
|
267
220
|
enumerable: false,
|
|
268
221
|
configurable: true
|
|
269
222
|
});
|
|
270
|
-
Object.defineProperty(AbstractBinaryTree.prototype, "
|
|
223
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "isMergeDuplicatedVal", {
|
|
271
224
|
get: function () {
|
|
272
|
-
return this.
|
|
225
|
+
return this._isMergeDuplicatedVal;
|
|
273
226
|
},
|
|
274
227
|
enumerable: false,
|
|
275
228
|
configurable: true
|
|
276
229
|
});
|
|
277
|
-
Object.defineProperty(AbstractBinaryTree.prototype, "
|
|
230
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "visitedId", {
|
|
278
231
|
get: function () {
|
|
279
|
-
return this.
|
|
232
|
+
return this._visitedId;
|
|
280
233
|
},
|
|
281
234
|
enumerable: false,
|
|
282
235
|
configurable: true
|
|
283
236
|
});
|
|
284
|
-
Object.defineProperty(AbstractBinaryTree.prototype, "
|
|
237
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "visitedVal", {
|
|
285
238
|
get: function () {
|
|
286
|
-
return this.
|
|
239
|
+
return this._visitedVal;
|
|
287
240
|
},
|
|
288
241
|
enumerable: false,
|
|
289
242
|
configurable: true
|
|
290
243
|
});
|
|
291
|
-
Object.defineProperty(AbstractBinaryTree.prototype, "
|
|
244
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "visitedNode", {
|
|
292
245
|
get: function () {
|
|
293
|
-
return this.
|
|
246
|
+
return this._visitedNode;
|
|
294
247
|
},
|
|
295
248
|
enumerable: false,
|
|
296
249
|
configurable: true
|
|
297
250
|
});
|
|
298
|
-
Object.defineProperty(AbstractBinaryTree.prototype, "
|
|
251
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "visitedCount", {
|
|
299
252
|
get: function () {
|
|
300
|
-
return this.
|
|
253
|
+
return this._visitedCount;
|
|
301
254
|
},
|
|
302
255
|
enumerable: false,
|
|
303
256
|
configurable: true
|
|
304
257
|
});
|
|
305
|
-
Object.defineProperty(AbstractBinaryTree.prototype, "
|
|
258
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "visitedLeftSum", {
|
|
306
259
|
get: function () {
|
|
307
|
-
return this.
|
|
260
|
+
return this._visitedLeftSum;
|
|
308
261
|
},
|
|
309
262
|
enumerable: false,
|
|
310
263
|
configurable: true
|
|
311
264
|
});
|
|
312
265
|
/**
|
|
313
|
-
* The
|
|
266
|
+
* The `swapLocation` function swaps the location of two nodes in a binary tree.
|
|
267
|
+
* @param {N} srcNode - The source node that you want to swap with the destination node.
|
|
268
|
+
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
269
|
+
* be swapped to.
|
|
270
|
+
* @returns The `destNode` is being returned.
|
|
271
|
+
*/
|
|
272
|
+
AbstractBinaryTree.prototype.swapLocation = function (srcNode, destNode) {
|
|
273
|
+
var val = destNode.val, height = destNode.height, id = destNode.id;
|
|
274
|
+
var tempNode = this.createNode(id, val);
|
|
275
|
+
if (tempNode) {
|
|
276
|
+
tempNode.height = height;
|
|
277
|
+
if (tempNode instanceof AbstractBinaryTreeNode) {
|
|
278
|
+
// TODO should we consider the left, right children?
|
|
279
|
+
destNode.id = srcNode.id;
|
|
280
|
+
destNode.val = srcNode.val;
|
|
281
|
+
destNode.height = srcNode.height;
|
|
282
|
+
srcNode.id = tempNode.id;
|
|
283
|
+
srcNode.val = tempNode.val;
|
|
284
|
+
srcNode.height = tempNode.height;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return destNode;
|
|
288
|
+
};
|
|
289
|
+
/**
|
|
290
|
+
* The clear() function resets the root, size, and maxId properties to their initial values.
|
|
314
291
|
*/
|
|
315
292
|
AbstractBinaryTree.prototype.clear = function () {
|
|
316
293
|
this._setRoot(null);
|
|
317
294
|
this._setSize(0);
|
|
318
|
-
this._setCount(0);
|
|
319
295
|
this._setMaxId(-1);
|
|
320
296
|
};
|
|
321
297
|
/**
|
|
@@ -326,19 +302,16 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
326
302
|
return this.size === 0;
|
|
327
303
|
};
|
|
328
304
|
/**
|
|
329
|
-
* The `add` function
|
|
330
|
-
*
|
|
331
|
-
* @param
|
|
332
|
-
*
|
|
333
|
-
* @param {
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
* @returns The function `add` returns a `N` object if a new node is inserted, or `null` if no new node
|
|
337
|
-
* is inserted, or `undefined` if the insertion fails.
|
|
305
|
+
* The `add` function adds a new node to a binary tree, updating the value of an existing node if it already exists.
|
|
306
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that you want to add.
|
|
307
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. If no value is
|
|
308
|
+
* provided, the default value will be the same as the `id` parameter.
|
|
309
|
+
* @param {number} [count] - The `count` parameter is an optional number that represents the number of times the value
|
|
310
|
+
* should be added to the binary tree. If not provided, the default value is `undefined`.
|
|
311
|
+
* @returns The function `add` returns either a `BinaryTreeNode` object (`N`), `null`, or `undefined`.
|
|
338
312
|
*/
|
|
339
313
|
AbstractBinaryTree.prototype.add = function (id, val, count) {
|
|
340
314
|
var _this = this;
|
|
341
|
-
count = count !== null && count !== void 0 ? count : 1;
|
|
342
315
|
var _bfs = function (root, newNode) {
|
|
343
316
|
var queue = [root];
|
|
344
317
|
while (queue.length > 0) {
|
|
@@ -358,14 +331,12 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
358
331
|
return;
|
|
359
332
|
};
|
|
360
333
|
var inserted;
|
|
361
|
-
var needInsert = val !== null ? this.createNode(id, val
|
|
334
|
+
var needInsert = val !== null ? this.createNode(id, val) : null;
|
|
362
335
|
var existNode = val !== null ? this.get(id, 'id') : null;
|
|
363
336
|
if (this.root) {
|
|
364
337
|
if (existNode) {
|
|
365
|
-
existNode.count += count;
|
|
366
338
|
existNode.val = val !== null && val !== void 0 ? val : id;
|
|
367
339
|
if (needInsert !== null) {
|
|
368
|
-
this._setCount(this.count + count);
|
|
369
340
|
inserted = existNode;
|
|
370
341
|
}
|
|
371
342
|
}
|
|
@@ -374,26 +345,24 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
374
345
|
}
|
|
375
346
|
}
|
|
376
347
|
else {
|
|
377
|
-
this._setRoot(val !== null ? this.createNode(id, val
|
|
348
|
+
this._setRoot(val !== null ? this.createNode(id, val) : null);
|
|
378
349
|
if (needInsert !== null) {
|
|
379
350
|
this._setSize(1);
|
|
380
|
-
this._setCount(count);
|
|
381
351
|
}
|
|
382
352
|
inserted = this.root;
|
|
383
353
|
}
|
|
384
354
|
return inserted;
|
|
385
355
|
};
|
|
386
356
|
/**
|
|
387
|
-
* The function adds a new node to
|
|
357
|
+
* The function adds a new node to the left or right child of a parent node, updating the size of the tree if
|
|
358
|
+
* necessary.
|
|
388
359
|
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to the tree. It can be
|
|
389
360
|
* either a node object (`N`) or `null`.
|
|
390
361
|
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
|
|
391
362
|
* child.
|
|
392
|
-
* @returns either the left
|
|
393
|
-
* `undefined` in certain cases.
|
|
363
|
+
* @returns either the left child node, the right child node, or undefined.
|
|
394
364
|
*/
|
|
395
365
|
AbstractBinaryTree.prototype.addTo = function (newNode, parent) {
|
|
396
|
-
var _a, _b;
|
|
397
366
|
if (parent) {
|
|
398
367
|
if (parent.left === undefined) {
|
|
399
368
|
if (newNode) {
|
|
@@ -402,7 +371,6 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
402
371
|
parent.left = newNode;
|
|
403
372
|
if (newNode !== null) {
|
|
404
373
|
this._setSize(this.size + 1);
|
|
405
|
-
this._setCount((_a = this.count + newNode.count) !== null && _a !== void 0 ? _a : 0);
|
|
406
374
|
}
|
|
407
375
|
return parent.left;
|
|
408
376
|
}
|
|
@@ -413,7 +381,6 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
413
381
|
parent.right = newNode;
|
|
414
382
|
if (newNode !== null) {
|
|
415
383
|
this._setSize(this.size + 1);
|
|
416
|
-
this._setCount((_b = this.count + newNode.count) !== null && _b !== void 0 ? _b : 0);
|
|
417
384
|
}
|
|
418
385
|
return parent.right;
|
|
419
386
|
}
|
|
@@ -426,11 +393,11 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
426
393
|
}
|
|
427
394
|
};
|
|
428
395
|
/**
|
|
429
|
-
* The `addMany` function
|
|
396
|
+
* The `addMany` function adds multiple nodes to a binary tree and returns an array of the inserted nodes or
|
|
430
397
|
* null/undefined values.
|
|
431
|
-
* @param {N[] | N[]} data - The `data` parameter can be either an array of
|
|
432
|
-
*
|
|
433
|
-
* @returns The function `addMany` returns an array of `N
|
|
398
|
+
* @param {N[] | Array<N['val']>} data - The `data` parameter can be either an array of `N` objects or an array of
|
|
399
|
+
* `N['val']` values.
|
|
400
|
+
* @returns The function `addMany` returns an array of values of type `N | null | undefined`.
|
|
434
401
|
*/
|
|
435
402
|
AbstractBinaryTree.prototype.addMany = function (data) {
|
|
436
403
|
var e_1, _a, e_2, _b;
|
|
@@ -457,7 +424,7 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
457
424
|
for (var data_2 = __values(data), data_2_1 = data_2.next(); !data_2_1.done; data_2_1 = data_2.next()) {
|
|
458
425
|
var nodeOrId = data_2_1.value;
|
|
459
426
|
if (nodeOrId instanceof AbstractBinaryTreeNode) {
|
|
460
|
-
inserted.push(this.add(nodeOrId.id, nodeOrId.val
|
|
427
|
+
inserted.push(this.add(nodeOrId.id, nodeOrId.val));
|
|
461
428
|
continue;
|
|
462
429
|
}
|
|
463
430
|
if (nodeOrId === null) {
|
|
@@ -510,11 +477,11 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
510
477
|
return inserted;
|
|
511
478
|
};
|
|
512
479
|
/**
|
|
513
|
-
* The `fill` function clears the current data and
|
|
514
|
-
*
|
|
515
|
-
* @param {N[] | N[]} data - The `data` parameter can be either an array of
|
|
516
|
-
* array
|
|
517
|
-
* @returns
|
|
480
|
+
* The `fill` function clears the current data and adds new data, returning a boolean indicating if the operation was
|
|
481
|
+
* successful.
|
|
482
|
+
* @param {N[] | Array<N['val']>} data - The `data` parameter can be either an array of objects or an array of arrays.
|
|
483
|
+
* Each object or array should have a property called `val`.
|
|
484
|
+
* @returns a boolean value.
|
|
518
485
|
*/
|
|
519
486
|
AbstractBinaryTree.prototype.fill = function (data) {
|
|
520
487
|
this.clear();
|
|
@@ -523,10 +490,11 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
523
490
|
/**
|
|
524
491
|
* The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
525
492
|
* node that needs to be balanced.
|
|
526
|
-
* @param {N | BinaryTreeNodeId
|
|
493
|
+
* @param {N | BinaryTreeNodeId} nodeOrId - The `nodeOrId` parameter can be either a node object (`N`) or a binary tree
|
|
494
|
+
* node ID (`BinaryTreeNodeId`).
|
|
527
495
|
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
528
|
-
* whether to ignore the count of the
|
|
529
|
-
* not be
|
|
496
|
+
* whether to ignore the count of the nodes in the binary tree. If `ignoreCount` is set to `true`, the count of the
|
|
497
|
+
* nodes in the binary tree will not be updated after removing a node. If `ignoreCount`
|
|
530
498
|
* @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
531
499
|
*/
|
|
532
500
|
AbstractBinaryTree.prototype.remove = function (nodeOrId, ignoreCount) {
|
|
@@ -538,44 +506,37 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
538
506
|
return bstDeletedResult;
|
|
539
507
|
var parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
|
|
540
508
|
var needBalanced = null, orgCurrent = curr;
|
|
541
|
-
if (curr.
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
509
|
+
if (!curr.left) {
|
|
510
|
+
if (!parent) {
|
|
511
|
+
if (curr.right !== undefined)
|
|
512
|
+
this._setRoot(curr.right);
|
|
513
|
+
}
|
|
514
|
+
else {
|
|
515
|
+
var fp = curr.familyPosition;
|
|
516
|
+
if (fp === types_1.FamilyPosition.LEFT || fp === types_1.FamilyPosition.ROOT_LEFT) {
|
|
517
|
+
parent.left = curr.right;
|
|
550
518
|
}
|
|
551
|
-
else {
|
|
552
|
-
|
|
553
|
-
if (fp === types_1.FamilyPosition.LEFT || fp === types_1.FamilyPosition.ROOT_LEFT) {
|
|
554
|
-
parent.left = curr.right;
|
|
555
|
-
}
|
|
556
|
-
else if (fp === types_1.FamilyPosition.RIGHT || fp === types_1.FamilyPosition.ROOT_RIGHT) {
|
|
557
|
-
parent.right = curr.right;
|
|
558
|
-
}
|
|
559
|
-
needBalanced = parent;
|
|
519
|
+
else if (fp === types_1.FamilyPosition.RIGHT || fp === types_1.FamilyPosition.ROOT_RIGHT) {
|
|
520
|
+
parent.right = curr.right;
|
|
560
521
|
}
|
|
522
|
+
needBalanced = parent;
|
|
561
523
|
}
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
524
|
+
}
|
|
525
|
+
else {
|
|
526
|
+
var leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
|
|
527
|
+
if (leftSubTreeRightMost) {
|
|
528
|
+
var parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
529
|
+
orgCurrent = this.swapLocation(curr, leftSubTreeRightMost);
|
|
530
|
+
if (parentOfLeftSubTreeMax) {
|
|
531
|
+
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
532
|
+
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
533
|
+
else
|
|
534
|
+
parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
535
|
+
needBalanced = parentOfLeftSubTreeMax;
|
|
574
536
|
}
|
|
575
537
|
}
|
|
576
|
-
this._setSize(this.size - 1);
|
|
577
|
-
this._setCount(this.count - orgCurrent.count);
|
|
578
538
|
}
|
|
539
|
+
this._setSize(this.size - 1);
|
|
579
540
|
bstDeletedResult.push({ deleted: orgCurrent, needBalanced: needBalanced });
|
|
580
541
|
return bstDeletedResult;
|
|
581
542
|
};
|
|
@@ -595,15 +556,13 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
595
556
|
return depth;
|
|
596
557
|
};
|
|
597
558
|
/**
|
|
598
|
-
* The `getHeight` function calculates the maximum height of a binary tree
|
|
599
|
-
*
|
|
600
|
-
*
|
|
601
|
-
*
|
|
602
|
-
* If no value is provided for `beginRoot`, the function will use the `root` property of the class instance as
|
|
559
|
+
* The `getHeight` function calculates the maximum height of a binary tree, either recursively or iteratively.
|
|
560
|
+
* @param {N | BinaryTreeNodeId | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
561
|
+
* generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
|
|
562
|
+
* node), or `null`.
|
|
603
563
|
* @returns the height of the binary tree.
|
|
604
564
|
*/
|
|
605
565
|
AbstractBinaryTree.prototype.getHeight = function (beginRoot) {
|
|
606
|
-
var _a, _b, _c;
|
|
607
566
|
beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
|
|
608
567
|
if (typeof beginRoot === 'number')
|
|
609
568
|
beginRoot = this.get(beginRoot, 'id');
|
|
@@ -620,39 +579,30 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
620
579
|
return _getMaxHeight_1(beginRoot);
|
|
621
580
|
}
|
|
622
581
|
else {
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
582
|
+
if (!beginRoot) {
|
|
583
|
+
return -1;
|
|
584
|
+
}
|
|
585
|
+
var stack = [{ node: beginRoot, depth: 0 }];
|
|
586
|
+
var maxHeight = 0;
|
|
587
|
+
while (stack.length > 0) {
|
|
588
|
+
var _a = stack.pop(), node = _a.node, depth = _a.depth;
|
|
589
|
+
if (node.left) {
|
|
590
|
+
stack.push({ node: node.left, depth: depth + 1 });
|
|
630
591
|
}
|
|
631
|
-
|
|
632
|
-
node
|
|
633
|
-
if (!node.right || last === node.right) {
|
|
634
|
-
node = stack.pop();
|
|
635
|
-
if (node) {
|
|
636
|
-
var leftHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
|
|
637
|
-
var rightHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
|
|
638
|
-
depths.set(node, 1 + Math.max(leftHeight, rightHeight));
|
|
639
|
-
last = node;
|
|
640
|
-
node = null;
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
else
|
|
644
|
-
node = node.right;
|
|
592
|
+
if (node.right) {
|
|
593
|
+
stack.push({ node: node.right, depth: depth + 1 });
|
|
645
594
|
}
|
|
595
|
+
maxHeight = Math.max(maxHeight, depth);
|
|
646
596
|
}
|
|
647
|
-
return
|
|
597
|
+
return maxHeight;
|
|
648
598
|
}
|
|
649
599
|
};
|
|
650
600
|
/**
|
|
651
601
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
|
|
652
602
|
* approach.
|
|
653
|
-
* @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type
|
|
654
|
-
*
|
|
655
|
-
*
|
|
603
|
+
* @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It
|
|
604
|
+
* represents the starting node from which to calculate the minimum height of a binary tree. If no value is provided
|
|
605
|
+
* for `beginRoot`, the `this.root` property is used as the default value.
|
|
656
606
|
* @returns The function `getMinHeight` returns the minimum height of the binary tree.
|
|
657
607
|
*/
|
|
658
608
|
AbstractBinaryTree.prototype.getMinHeight = function (beginRoot) {
|
|
@@ -701,25 +651,25 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
701
651
|
}
|
|
702
652
|
};
|
|
703
653
|
/**
|
|
704
|
-
* The function checks if a binary tree is balanced by comparing the minimum height and the
|
|
705
|
-
*
|
|
706
|
-
*
|
|
654
|
+
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the height of the
|
|
655
|
+
* tree.
|
|
656
|
+
* @param {N | null} [beginRoot] - The parameter `beginRoot` is of type `N` or `null`. It represents the root node of a
|
|
657
|
+
* tree or null if the tree is empty.
|
|
707
658
|
* @returns The method is returning a boolean value.
|
|
708
659
|
*/
|
|
709
|
-
AbstractBinaryTree.prototype.
|
|
660
|
+
AbstractBinaryTree.prototype.isPerfectlyBalanced = function (beginRoot) {
|
|
710
661
|
return (this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot));
|
|
711
662
|
};
|
|
712
663
|
/**
|
|
713
|
-
* The function `getNodes` returns an array of
|
|
714
|
-
* searching recursively or iteratively.
|
|
664
|
+
* The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
|
|
715
665
|
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
716
666
|
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
717
667
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
718
668
|
* specifies the property name to use when searching for nodes. If not provided, it defaults to 'id'.
|
|
719
669
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
720
|
-
* return only one node that matches the `nodeProperty` or `propertyName
|
|
721
|
-
* function will stop traversing the tree and return the first matching node. If `
|
|
722
|
-
* @returns
|
|
670
|
+
* return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
|
|
671
|
+
* function will stop traversing the tree and return the first matching node. If `only
|
|
672
|
+
* @returns an array of nodes (type N).
|
|
723
673
|
*/
|
|
724
674
|
AbstractBinaryTree.prototype.getNodes = function (nodeProperty, propertyName, onlyOne) {
|
|
725
675
|
var _this = this;
|
|
@@ -727,7 +677,7 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
727
677
|
return [];
|
|
728
678
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
729
679
|
var result = [];
|
|
730
|
-
if (this.
|
|
680
|
+
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
731
681
|
var _traverse_1 = function (cur) {
|
|
732
682
|
if (_this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
|
|
733
683
|
return;
|
|
@@ -753,25 +703,27 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
753
703
|
return result;
|
|
754
704
|
};
|
|
755
705
|
/**
|
|
756
|
-
* The function checks if a binary tree node has a specific property
|
|
757
|
-
*
|
|
758
|
-
*
|
|
759
|
-
* generic type `N`. It represents the property of a binary tree node that you want to check.
|
|
706
|
+
* The function checks if a binary tree node has a specific property.
|
|
707
|
+
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or `N`.
|
|
708
|
+
* It represents the property of the binary tree node that you want to check.
|
|
760
709
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
761
|
-
* specifies the name of the property to
|
|
710
|
+
* specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'id'.
|
|
762
711
|
* @returns a boolean value.
|
|
763
712
|
*/
|
|
764
713
|
AbstractBinaryTree.prototype.has = function (nodeProperty, propertyName) {
|
|
714
|
+
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
765
715
|
return this.getNodes(nodeProperty, propertyName).length > 0;
|
|
766
716
|
};
|
|
767
717
|
/**
|
|
768
|
-
* The function returns the first
|
|
769
|
-
*
|
|
770
|
-
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or
|
|
771
|
-
*
|
|
718
|
+
* The function returns the first node that matches the given property name and value, or null if no matching node is
|
|
719
|
+
* found.
|
|
720
|
+
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or `N`.
|
|
721
|
+
* It represents the property of the binary tree node that you want to search for.
|
|
772
722
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
773
|
-
* specifies the property
|
|
774
|
-
*
|
|
723
|
+
* specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
|
|
724
|
+
* default value is set to `'id'`.
|
|
725
|
+
* @returns either the value of the specified property of the node, or the node itself if no property name is provided.
|
|
726
|
+
* If no matching node is found, it returns null.
|
|
775
727
|
*/
|
|
776
728
|
AbstractBinaryTree.prototype.get = function (nodeProperty, propertyName) {
|
|
777
729
|
var _a;
|
|
@@ -779,11 +731,10 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
779
731
|
return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
780
732
|
};
|
|
781
733
|
/**
|
|
782
|
-
* The function getPathToRoot returns an array of
|
|
783
|
-
* root
|
|
784
|
-
* @param node - The `node`
|
|
785
|
-
* @returns The function `getPathToRoot` returns an array of `N`
|
|
786
|
-
* the given `node` to the root of the binary tree.
|
|
734
|
+
* The function getPathToRoot takes a node and returns an array of nodes representing the path from the given node to
|
|
735
|
+
* the root node.
|
|
736
|
+
* @param {N} node - The parameter `node` represents a node in a tree data structure.
|
|
737
|
+
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
|
787
738
|
*/
|
|
788
739
|
AbstractBinaryTree.prototype.getPathToRoot = function (node) {
|
|
789
740
|
var result = [];
|
|
@@ -831,10 +782,12 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
831
782
|
/**
|
|
832
783
|
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using
|
|
833
784
|
* tail recursion optimization.
|
|
834
|
-
* @param {N | null} [node] - The `node` parameter is an optional parameter of type `N
|
|
835
|
-
*
|
|
836
|
-
*
|
|
837
|
-
* @returns The `getRightMost`
|
|
785
|
+
* @param {N | null} [node] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
786
|
+
* starting node from which we want to find the rightmost node. If no node is provided, the `node` parameter defaults
|
|
787
|
+
* to `this.root`, which is the root node of the data structure
|
|
788
|
+
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If the `node` parameter is
|
|
789
|
+
* not provided, it defaults to the root node of the tree. If the tree is empty or the `node` parameter is `null`, the
|
|
790
|
+
* function returns `null`.
|
|
838
791
|
*/
|
|
839
792
|
AbstractBinaryTree.prototype.getRightMost = function (node) {
|
|
840
793
|
node = node !== null && node !== void 0 ? node : this.root;
|
|
@@ -859,7 +812,7 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
859
812
|
}
|
|
860
813
|
};
|
|
861
814
|
/**
|
|
862
|
-
* The function
|
|
815
|
+
* The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
|
|
863
816
|
* @param {N | null} node - The `node` parameter represents the root node of a binary search tree (BST).
|
|
864
817
|
* @returns a boolean value.
|
|
865
818
|
*/
|
|
@@ -904,49 +857,43 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
904
857
|
return this.isBSTByRooted(this.root);
|
|
905
858
|
};
|
|
906
859
|
/**
|
|
907
|
-
* The function calculates the size
|
|
908
|
-
*
|
|
909
|
-
*
|
|
910
|
-
*
|
|
911
|
-
* @returns The function `getSubTreeSizeAndCount` returns an array `[number, number]`. The first element of the array
|
|
912
|
-
* represents the size of the subtree, and the second element represents the count of the nodes in the subtree.
|
|
860
|
+
* The function calculates the size of a subtree by traversing it either recursively or iteratively.
|
|
861
|
+
* @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
|
|
862
|
+
* binary tree.
|
|
863
|
+
* @returns the size of the subtree rooted at `subTreeRoot`.
|
|
913
864
|
*/
|
|
914
|
-
AbstractBinaryTree.prototype.
|
|
915
|
-
var
|
|
865
|
+
AbstractBinaryTree.prototype.getSubTreeSize = function (subTreeRoot) {
|
|
866
|
+
var size = 0;
|
|
916
867
|
if (!subTreeRoot)
|
|
917
|
-
return
|
|
868
|
+
return size;
|
|
918
869
|
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
919
870
|
var _traverse_6 = function (cur) {
|
|
920
|
-
|
|
921
|
-
res[1] += cur.count;
|
|
871
|
+
size++;
|
|
922
872
|
cur.left && _traverse_6(cur.left);
|
|
923
873
|
cur.right && _traverse_6(cur.right);
|
|
924
874
|
};
|
|
925
875
|
_traverse_6(subTreeRoot);
|
|
926
|
-
return
|
|
876
|
+
return size;
|
|
927
877
|
}
|
|
928
878
|
else {
|
|
929
879
|
var stack = [subTreeRoot];
|
|
930
880
|
while (stack.length > 0) {
|
|
931
881
|
var cur = stack.pop();
|
|
932
|
-
|
|
933
|
-
res[1] += cur.count;
|
|
882
|
+
size++;
|
|
934
883
|
cur.right && stack.push(cur.right);
|
|
935
884
|
cur.left && stack.push(cur.left);
|
|
936
885
|
}
|
|
937
|
-
return
|
|
886
|
+
return size;
|
|
938
887
|
}
|
|
939
888
|
};
|
|
940
|
-
// --- start additional methods ---
|
|
941
889
|
/**
|
|
942
|
-
* The function `subTreeSum` calculates the sum of a specified property in a binary tree
|
|
943
|
-
*
|
|
944
|
-
*
|
|
945
|
-
*
|
|
946
|
-
*
|
|
947
|
-
*
|
|
948
|
-
*
|
|
949
|
-
* @returns a number, which is the sum of the values of the nodes in the subtree rooted at `subTreeRoot`.
|
|
890
|
+
* The function `subTreeSum` calculates the sum of a specified property in a binary tree or subtree.
|
|
891
|
+
* @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
|
|
892
|
+
* tree or the ID of a binary tree node. It can also be `null` if there is no subtree.
|
|
893
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
|
|
894
|
+
* property of the binary tree node to use for calculating the sum. It can be either 'id' or 'val'. If propertyName is
|
|
895
|
+
* not provided, it defaults to 'id'.
|
|
896
|
+
* @returns a number, which is the sum of the values of the specified property in the subtree rooted at `subTreeRoot`.
|
|
950
897
|
*/
|
|
951
898
|
AbstractBinaryTree.prototype.subTreeSum = function (subTreeRoot, propertyName) {
|
|
952
899
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
@@ -961,9 +908,6 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
961
908
|
case 'id':
|
|
962
909
|
needSum = cur.id;
|
|
963
910
|
break;
|
|
964
|
-
case 'count':
|
|
965
|
-
needSum = cur.count;
|
|
966
|
-
break;
|
|
967
911
|
case 'val':
|
|
968
912
|
needSum = typeof cur.val === 'number' ? cur.val : 0;
|
|
969
913
|
break;
|
|
@@ -995,16 +939,14 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
995
939
|
/**
|
|
996
940
|
* The function `subTreeAdd` adds a delta value to a specified property of each node in a subtree.
|
|
997
941
|
* @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
|
|
998
|
-
* tree or the ID of a binary tree
|
|
942
|
+
* tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
|
|
999
943
|
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
|
|
1000
|
-
* each node in the subtree should be incremented
|
|
944
|
+
* each node in the subtree should be incremented.
|
|
1001
945
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
1002
|
-
* specifies the property of the binary tree node that should be modified.
|
|
1003
|
-
* value is provided for `propertyName`, it defaults to 'id'.
|
|
946
|
+
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'id'.
|
|
1004
947
|
* @returns a boolean value.
|
|
1005
948
|
*/
|
|
1006
949
|
AbstractBinaryTree.prototype.subTreeAdd = function (subTreeRoot, delta, propertyName) {
|
|
1007
|
-
var _this = this;
|
|
1008
950
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
1009
951
|
if (typeof subTreeRoot === 'number')
|
|
1010
952
|
subTreeRoot = this.get(subTreeRoot, 'id');
|
|
@@ -1015,10 +957,6 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
1015
957
|
case 'id':
|
|
1016
958
|
cur.id += delta;
|
|
1017
959
|
break;
|
|
1018
|
-
case 'count':
|
|
1019
|
-
cur.count += delta;
|
|
1020
|
-
_this._setCount(_this.count + delta);
|
|
1021
|
-
break;
|
|
1022
960
|
default:
|
|
1023
961
|
cur.id += delta;
|
|
1024
962
|
break;
|
|
@@ -1044,13 +982,13 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
1044
982
|
return true;
|
|
1045
983
|
};
|
|
1046
984
|
/**
|
|
1047
|
-
* The BFS function performs a breadth-first search on a binary tree
|
|
1048
|
-
*
|
|
985
|
+
* The BFS function performs a breadth-first search on a binary tree, accumulating properties of each node based on a
|
|
986
|
+
* specified property name.
|
|
1049
987
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
|
|
1050
|
-
* represents either a node or a property name. If a node is provided, the breadth-first search algorithm will be
|
|
1051
|
-
* performed starting from that node. If a property name is provided, the
|
|
1052
|
-
*
|
|
1053
|
-
* @returns an
|
|
988
|
+
* represents either a node or a property name. If a node is provided, the breadth-first search (BFS) algorithm will be
|
|
989
|
+
* performed starting from that node. If a property name is provided, the BFS algorithm will be performed starting from
|
|
990
|
+
* the
|
|
991
|
+
* @returns an instance of the `AbstractBinaryTreeNodeProperties` class with generic type `N`.
|
|
1054
992
|
*/
|
|
1055
993
|
AbstractBinaryTree.prototype.BFS = function (nodeOrPropertyName) {
|
|
1056
994
|
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
@@ -1069,16 +1007,15 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
1069
1007
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1070
1008
|
};
|
|
1071
1009
|
/**
|
|
1072
|
-
* The DFS function performs a depth-first search traversal on a binary tree and returns the
|
|
1073
|
-
* specified pattern and
|
|
1074
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The "pattern" parameter is used to specify the order
|
|
1075
|
-
*
|
|
1076
|
-
* 'pre', or 'post'.
|
|
1010
|
+
* The DFS function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
|
|
1011
|
+
* each node based on the specified pattern and property name.
|
|
1012
|
+
* @param {'in' | 'pre' | 'post'} [pattern] - The "pattern" parameter is used to specify the traversal order of the
|
|
1013
|
+
* binary tree. It can have three possible values:
|
|
1077
1014
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is a string that represents
|
|
1078
|
-
*
|
|
1079
|
-
*
|
|
1080
|
-
*
|
|
1081
|
-
*
|
|
1015
|
+
* the name of a property of the nodes in the binary tree. This property will be used to accumulate values during the
|
|
1016
|
+
* depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'id'`.
|
|
1017
|
+
* @returns an instance of the AbstractBinaryTreeNodeProperties class, which contains the accumulated properties of the
|
|
1018
|
+
* binary tree nodes based on the specified pattern and node or property name.
|
|
1082
1019
|
*/
|
|
1083
1020
|
AbstractBinaryTree.prototype.DFS = function (pattern, nodeOrPropertyName) {
|
|
1084
1021
|
var _this = this;
|
|
@@ -1114,11 +1051,15 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
1114
1051
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1115
1052
|
};
|
|
1116
1053
|
/**
|
|
1117
|
-
*
|
|
1118
|
-
*
|
|
1119
|
-
* @param pattern
|
|
1120
|
-
*
|
|
1121
|
-
* @
|
|
1054
|
+
* The DFSIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
|
|
1055
|
+
* specify the traversal pattern and the property name to accumulate results by.
|
|
1056
|
+
* @param {'in' | 'pre' | 'post'} [pattern] - The "pattern" parameter determines the order in which the nodes of the
|
|
1057
|
+
* binary tree are visited during the depth-first search. It can have one of the following values:
|
|
1058
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is used to specify the
|
|
1059
|
+
* property of the nodes that you want to retrieve or perform operations on during the depth-first search traversal. By
|
|
1060
|
+
* default, it is set to `'id'`, which means that the traversal will accumulate results based on the `id` property of
|
|
1061
|
+
* the
|
|
1062
|
+
* @returns an object of type AbstractBinaryTreeNodeProperties<N>.
|
|
1122
1063
|
*/
|
|
1123
1064
|
AbstractBinaryTree.prototype.DFSIterative = function (pattern, nodeOrPropertyName) {
|
|
1124
1065
|
pattern = pattern || 'in';
|
|
@@ -1221,15 +1162,12 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
1221
1162
|
case 'node':
|
|
1222
1163
|
levelsNodes[level].push(node);
|
|
1223
1164
|
break;
|
|
1224
|
-
case 'count':
|
|
1225
|
-
levelsNodes[level].push(node.count);
|
|
1226
|
-
break;
|
|
1227
1165
|
default:
|
|
1228
1166
|
levelsNodes[level].push(node.id);
|
|
1229
1167
|
break;
|
|
1230
1168
|
}
|
|
1231
1169
|
};
|
|
1232
|
-
if (this.
|
|
1170
|
+
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
1233
1171
|
var _recursive_1 = function (node, level) {
|
|
1234
1172
|
if (!levelsNodes[level])
|
|
1235
1173
|
levelsNodes[level] = [];
|
|
@@ -1278,15 +1216,13 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
1278
1216
|
};
|
|
1279
1217
|
/**
|
|
1280
1218
|
* The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris
|
|
1281
|
-
* traversal algorithm
|
|
1282
|
-
*
|
|
1283
|
-
*
|
|
1284
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that determines the
|
|
1285
|
-
* traversal pattern of the binary tree. It can have one of three values:
|
|
1219
|
+
* traversal algorithm.
|
|
1220
|
+
* @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter determines the traversal pattern for the binary
|
|
1221
|
+
* tree. It can have one of three values:
|
|
1286
1222
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is used to specify the
|
|
1287
|
-
* property of the nodes that you want to retrieve
|
|
1288
|
-
*
|
|
1289
|
-
* @returns
|
|
1223
|
+
* property name of the nodes that you want to retrieve. It can be any valid property name of the nodes in the binary
|
|
1224
|
+
* tree.
|
|
1225
|
+
* @returns an array of AbstractBinaryTreeNodeProperties<N> objects.
|
|
1290
1226
|
*/
|
|
1291
1227
|
AbstractBinaryTree.prototype.morris = function (pattern, nodeOrPropertyName) {
|
|
1292
1228
|
var _this = this;
|
|
@@ -1375,70 +1311,114 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
1375
1311
|
}
|
|
1376
1312
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1377
1313
|
};
|
|
1314
|
+
/**
|
|
1315
|
+
* The function sets the loop type for a protected variable.
|
|
1316
|
+
* @param {LoopType} value - The value parameter is of type LoopType.
|
|
1317
|
+
*/
|
|
1378
1318
|
AbstractBinaryTree.prototype._setLoopType = function (value) {
|
|
1379
1319
|
this._loopType = value;
|
|
1380
1320
|
};
|
|
1321
|
+
/**
|
|
1322
|
+
* The function sets the value of the `_visitedId` property in a protected manner.
|
|
1323
|
+
* @param {BinaryTreeNodeId[]} value - value is an array of BinaryTreeNodeId values.
|
|
1324
|
+
*/
|
|
1381
1325
|
AbstractBinaryTree.prototype._setVisitedId = function (value) {
|
|
1382
1326
|
this._visitedId = value;
|
|
1383
1327
|
};
|
|
1328
|
+
/**
|
|
1329
|
+
* The function sets the value of the "_visitedVal" property to the given array.
|
|
1330
|
+
* @param value - An array of type N.
|
|
1331
|
+
*/
|
|
1384
1332
|
AbstractBinaryTree.prototype._setVisitedVal = function (value) {
|
|
1385
1333
|
this._visitedVal = value;
|
|
1386
1334
|
};
|
|
1335
|
+
/**
|
|
1336
|
+
* The function sets the value of the _visitedNode property.
|
|
1337
|
+
* @param {N[]} value - N[] is an array of elements of type N.
|
|
1338
|
+
*/
|
|
1387
1339
|
AbstractBinaryTree.prototype._setVisitedNode = function (value) {
|
|
1388
1340
|
this._visitedNode = value;
|
|
1389
1341
|
};
|
|
1342
|
+
/**
|
|
1343
|
+
* The function sets the value of the visitedCount property.
|
|
1344
|
+
* @param {number[]} value - The value parameter is an array of numbers.
|
|
1345
|
+
*/
|
|
1390
1346
|
AbstractBinaryTree.prototype.setVisitedCount = function (value) {
|
|
1391
1347
|
this._visitedCount = value;
|
|
1392
1348
|
};
|
|
1349
|
+
/**
|
|
1350
|
+
* The function sets the value of the `_visitedLeftSum` property to the provided array.
|
|
1351
|
+
* @param {number[]} value - An array of numbers that represents the visited left sum.
|
|
1352
|
+
*/
|
|
1393
1353
|
AbstractBinaryTree.prototype._setVisitedLeftSum = function (value) {
|
|
1394
1354
|
this._visitedLeftSum = value;
|
|
1395
1355
|
};
|
|
1356
|
+
/**
|
|
1357
|
+
* The function sets the value of the _autoIncrementId property.
|
|
1358
|
+
* @param {boolean} value - The value parameter is a boolean that determines whether the id should be automatically
|
|
1359
|
+
* incremented or not. If value is true, the id will be automatically incremented. If value is false, the id will not
|
|
1360
|
+
* be automatically incremented.
|
|
1361
|
+
*/
|
|
1396
1362
|
AbstractBinaryTree.prototype._setAutoIncrementId = function (value) {
|
|
1397
1363
|
this._autoIncrementId = value;
|
|
1398
1364
|
};
|
|
1365
|
+
/**
|
|
1366
|
+
* The function sets the maximum ID value.
|
|
1367
|
+
* @param {number} value - The value parameter is a number that represents the new maximum ID value.
|
|
1368
|
+
*/
|
|
1399
1369
|
AbstractBinaryTree.prototype._setMaxId = function (value) {
|
|
1400
1370
|
this._maxId = value;
|
|
1401
1371
|
};
|
|
1372
|
+
/**
|
|
1373
|
+
* The function sets the value of a protected property called "_isMergeDuplicatedVal".
|
|
1374
|
+
* @param {boolean} value - The value parameter is a boolean value that determines whether the isMergeDuplicatedVal
|
|
1375
|
+
* property should be set to true or false.
|
|
1376
|
+
*/
|
|
1402
1377
|
AbstractBinaryTree.prototype._setIsDuplicatedVal = function (value) {
|
|
1403
1378
|
this._isMergeDuplicatedVal = value;
|
|
1404
1379
|
};
|
|
1380
|
+
/**
|
|
1381
|
+
* The function sets the root property of an object to a given value, and if the value is not null, it also sets the
|
|
1382
|
+
* parent property of the value to undefined.
|
|
1383
|
+
* @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of type `N` or `null`.
|
|
1384
|
+
*/
|
|
1405
1385
|
AbstractBinaryTree.prototype._setRoot = function (v) {
|
|
1406
1386
|
if (v) {
|
|
1407
1387
|
v.parent = undefined;
|
|
1408
1388
|
}
|
|
1409
1389
|
this._root = v;
|
|
1410
1390
|
};
|
|
1391
|
+
/**
|
|
1392
|
+
* The function sets the size of a protected variable.
|
|
1393
|
+
* @param {number} v - number
|
|
1394
|
+
*/
|
|
1411
1395
|
AbstractBinaryTree.prototype._setSize = function (v) {
|
|
1412
1396
|
this._size = v;
|
|
1413
1397
|
};
|
|
1414
|
-
AbstractBinaryTree.prototype._setCount = function (v) {
|
|
1415
|
-
this._count = v;
|
|
1416
|
-
};
|
|
1417
1398
|
/**
|
|
1418
|
-
* The function resets the values of several arrays used for tracking visited nodes and their
|
|
1399
|
+
* The function `_resetResults` resets the values of several arrays used for tracking visited nodes and their
|
|
1400
|
+
* properties.
|
|
1419
1401
|
*/
|
|
1420
1402
|
AbstractBinaryTree.prototype._resetResults = function () {
|
|
1421
1403
|
this._visitedId = [];
|
|
1422
1404
|
this._visitedVal = [];
|
|
1423
1405
|
this._visitedNode = [];
|
|
1424
|
-
this._visitedCount = [];
|
|
1425
1406
|
this._visitedLeftSum = [];
|
|
1426
1407
|
};
|
|
1427
1408
|
/**
|
|
1428
1409
|
* The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
|
|
1429
1410
|
* a result array.
|
|
1430
|
-
* @param cur - The current
|
|
1431
|
-
* @param {(N | null | undefined)[]} result - An array that stores the matching nodes
|
|
1432
|
-
*
|
|
1433
|
-
*
|
|
1434
|
-
* the binary tree nodes. It can be either the `id`, `count`, or `val` property of the node.
|
|
1411
|
+
* @param {N} cur - The current node being processed.
|
|
1412
|
+
* @param {(N | null | undefined)[]} result - An array that stores the matching nodes.
|
|
1413
|
+
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter is either a `BinaryTreeNodeId` or a `N`
|
|
1414
|
+
* type. It represents the property value that we are comparing against in the switch statement.
|
|
1435
1415
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
1436
|
-
* specifies the property
|
|
1437
|
-
*
|
|
1416
|
+
* specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'id'`
|
|
1417
|
+
* or `'val'`. If it is not provided or is not equal to `'id'` or `'val'`, the
|
|
1438
1418
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
1439
1419
|
* stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
|
|
1440
1420
|
* `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
|
|
1441
|
-
* @returns a boolean value indicating whether
|
|
1421
|
+
* @returns a boolean value indicating whether only one matching node should be pushed into the result array.
|
|
1442
1422
|
*/
|
|
1443
1423
|
AbstractBinaryTree.prototype._pushByPropertyNameStopOrNot = function (cur, result, nodeProperty, propertyName, onlyOne) {
|
|
1444
1424
|
switch (propertyName) {
|
|
@@ -1448,12 +1428,6 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
1448
1428
|
return !!onlyOne;
|
|
1449
1429
|
}
|
|
1450
1430
|
break;
|
|
1451
|
-
case 'count':
|
|
1452
|
-
if (cur.count === nodeProperty) {
|
|
1453
|
-
result.push(cur);
|
|
1454
|
-
return !!onlyOne;
|
|
1455
|
-
}
|
|
1456
|
-
break;
|
|
1457
1431
|
case 'val':
|
|
1458
1432
|
if (cur.val === nodeProperty) {
|
|
1459
1433
|
result.push(cur);
|
|
@@ -1469,12 +1443,11 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
1469
1443
|
}
|
|
1470
1444
|
};
|
|
1471
1445
|
/**
|
|
1472
|
-
* The function `_accumulatedByPropertyName`
|
|
1473
|
-
*
|
|
1474
|
-
* @param
|
|
1475
|
-
*
|
|
1476
|
-
*
|
|
1477
|
-
* the property name of the node that should be accumulated. If it is a node object, it specifies the node itself
|
|
1446
|
+
* The function `_accumulatedByPropertyName` accumulates values from a given node based on the specified property name.
|
|
1447
|
+
* @param {N} node - The `node` parameter is of type `N`, which represents a node in a data structure.
|
|
1448
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
|
|
1449
|
+
* can be either a string representing a property name or a reference to a `Node` object. If it is a string, it
|
|
1450
|
+
* specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
|
|
1478
1451
|
*/
|
|
1479
1452
|
AbstractBinaryTree.prototype._accumulatedByPropertyName = function (node, nodeOrPropertyName) {
|
|
1480
1453
|
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
@@ -1488,20 +1461,21 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
1488
1461
|
case 'node':
|
|
1489
1462
|
this._visitedNode.push(node);
|
|
1490
1463
|
break;
|
|
1491
|
-
case 'count':
|
|
1492
|
-
this._visitedCount.push(node.count);
|
|
1493
|
-
break;
|
|
1494
1464
|
default:
|
|
1495
1465
|
this._visitedId.push(node.id);
|
|
1496
1466
|
break;
|
|
1497
1467
|
}
|
|
1498
1468
|
};
|
|
1499
1469
|
/**
|
|
1500
|
-
* The
|
|
1501
|
-
*
|
|
1470
|
+
* The time complexity of Morris traversal is O(n), it's may slower than others
|
|
1471
|
+
* The space complexity Morris traversal is O(1) because no using stack
|
|
1472
|
+
*/
|
|
1473
|
+
/**
|
|
1474
|
+
* The function `_getResultByPropertyName` returns the corresponding property value based on the given node or property
|
|
1475
|
+
* name.
|
|
1502
1476
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
|
|
1503
|
-
* can accept a
|
|
1504
|
-
* @returns The method returns an
|
|
1477
|
+
* can accept either a `NodeOrPropertyName` type or be undefined.
|
|
1478
|
+
* @returns The method `_getResultByPropertyName` returns an instance of `AbstractBinaryTreeNodeProperties<N>`.
|
|
1505
1479
|
*/
|
|
1506
1480
|
AbstractBinaryTree.prototype._getResultByPropertyName = function (nodeOrPropertyName) {
|
|
1507
1481
|
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
@@ -1512,8 +1486,6 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
1512
1486
|
return this._visitedVal;
|
|
1513
1487
|
case 'node':
|
|
1514
1488
|
return this._visitedNode;
|
|
1515
|
-
case 'count':
|
|
1516
|
-
return this._visitedCount;
|
|
1517
1489
|
default:
|
|
1518
1490
|
return this._visitedId;
|
|
1519
1491
|
}
|