data-structure-typed 1.18.8 → 1.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +169 -168
  2. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +182 -148
  3. package/dist/data-structures/binary-tree/abstract-binary-tree.js +288 -316
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +22 -14
  5. package/dist/data-structures/binary-tree/avl-tree.js +23 -17
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +12 -26
  7. package/dist/data-structures/binary-tree/binary-tree.js +11 -26
  8. package/dist/data-structures/binary-tree/bst.d.ts +62 -74
  9. package/dist/data-structures/binary-tree/bst.js +72 -96
  10. package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -14
  11. package/dist/data-structures/binary-tree/rb-tree.js +5 -17
  12. package/dist/data-structures/binary-tree/tree-multiset.d.ts +186 -17
  13. package/dist/data-structures/binary-tree/tree-multiset.js +712 -28
  14. package/dist/data-structures/graph/abstract-graph.d.ts +107 -49
  15. package/dist/data-structures/graph/abstract-graph.js +104 -55
  16. package/dist/data-structures/graph/directed-graph.d.ts +95 -94
  17. package/dist/data-structures/graph/directed-graph.js +95 -95
  18. package/dist/data-structures/graph/undirected-graph.d.ts +62 -61
  19. package/dist/data-structures/graph/undirected-graph.js +62 -61
  20. package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +10 -15
  21. package/dist/data-structures/interfaces/avl-tree.d.ts +2 -2
  22. package/dist/data-structures/interfaces/binary-tree.d.ts +1 -1
  23. package/dist/data-structures/interfaces/bst.d.ts +3 -4
  24. package/dist/data-structures/interfaces/rb-tree.d.ts +1 -2
  25. package/dist/data-structures/interfaces/tree-multiset.d.ts +3 -3
  26. package/dist/data-structures/types/abstract-binary-tree.d.ts +1 -1
  27. package/dist/data-structures/types/tree-multiset.d.ts +3 -3
  28. package/dist/utils/index.d.ts +1 -0
  29. package/dist/utils/index.js +1 -0
  30. package/dist/utils/types/index.d.ts +1 -0
  31. package/dist/utils/types/index.js +1 -0
  32. package/dist/utils/types/utils.d.ts +0 -18
  33. package/dist/utils/types/validate-type.d.ts +19 -0
  34. package/dist/utils/types/validate-type.js +2 -0
  35. package/dist/utils/utils.d.ts +3 -8
  36. package/dist/utils/utils.js +1 -83
  37. package/dist/utils/validate-type.d.ts +45 -0
  38. package/dist/utils/validate-type.js +58 -0
  39. package/package.json +6 -2
  40. package/tsconfig.json +0 -17
@@ -25,38 +25,76 @@ var __assign = (this && this.__assign) || function () {
25
25
  };
26
26
  return __assign.apply(this, arguments);
27
27
  };
28
+ var __values = (this && this.__values) || function(o) {
29
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
30
+ if (m) return m.call(o);
31
+ if (o && typeof o.length === "number") return {
32
+ next: function () {
33
+ if (o && i >= o.length) o = void 0;
34
+ return { value: o && o[i++], done: !o };
35
+ }
36
+ };
37
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
38
+ };
28
39
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.TreeMultiSet = exports.TreeMultiSetNode = void 0;
40
+ exports.TreeMultiset = exports.TreeMultisetNode = void 0;
41
+ var types_1 = require("../types");
30
42
  var avl_tree_1 = require("./avl-tree");
31
- var TreeMultiSetNode = /** @class */ (function (_super) {
32
- __extends(TreeMultiSetNode, _super);
33
- function TreeMultiSetNode() {
34
- return _super !== null && _super.apply(this, arguments) || this;
35
- }
43
+ var TreeMultisetNode = /** @class */ (function (_super) {
44
+ __extends(TreeMultisetNode, _super);
36
45
  /**
37
- * The function creates a new node in a binary tree with an optional value and count.
38
- * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
39
- * identify each node in the tree.
40
- * @param {T} [val] - The `val` parameter represents the value to be stored in the node. It is an optional parameter,
41
- * meaning it can be omitted when calling the `createNode` method.
42
- * @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary tree
43
- * node. It is an optional parameter, so it can be omitted when calling the `createNode` method.
44
- * @returns The method is returning a new instance of the TreeMultiSetNode class, casted as the FAMILY type.
46
+ * The constructor function initializes a BinaryTreeNode object with an id, value, and count.
47
+ * @param {BinaryTreeNodeId} id - The `id` parameter is of type `BinaryTreeNodeId` and represents the unique identifier
48
+ * of the binary tree node.
49
+ * @param {T} [val] - The `val` parameter is an optional parameter of type `T`. It represents the value of the binary
50
+ * tree node. If no value is provided, it will be `undefined`.
51
+ * @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
52
+ * occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
53
+ * parameter when creating a new instance of the `BinaryTreeNode` class,
45
54
  */
46
- TreeMultiSetNode.prototype.createNode = function (id, val, count) {
47
- return new TreeMultiSetNode(id, val, count);
48
- };
49
- return TreeMultiSetNode;
55
+ function TreeMultisetNode(id, val, count) {
56
+ if (count === void 0) { count = 1; }
57
+ var _this = _super.call(this, id, val) || this;
58
+ _this._count = 1;
59
+ _this._count = count;
60
+ return _this;
61
+ }
62
+ Object.defineProperty(TreeMultisetNode.prototype, "count", {
63
+ get: function () {
64
+ return this._count;
65
+ },
66
+ set: function (v) {
67
+ this._count = v;
68
+ },
69
+ enumerable: false,
70
+ configurable: true
71
+ });
72
+ return TreeMultisetNode;
50
73
  }(avl_tree_1.AVLTreeNode));
51
- exports.TreeMultiSetNode = TreeMultiSetNode;
74
+ exports.TreeMultisetNode = TreeMultisetNode;
52
75
  /**
53
- * 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.
76
+ * 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.
54
77
  */
55
- var TreeMultiSet = /** @class */ (function (_super) {
56
- __extends(TreeMultiSet, _super);
57
- function TreeMultiSet(options) {
58
- return _super.call(this, __assign(__assign({}, options), { isMergeDuplicatedVal: true })) || this;
78
+ var TreeMultiset = /** @class */ (function (_super) {
79
+ __extends(TreeMultiset, _super);
80
+ /**
81
+ * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
82
+ * merge duplicated values.
83
+ * @param {TreeMultisetOptions} [options] - An optional object that contains additional configuration options for the
84
+ * TreeMultiset.
85
+ */
86
+ function TreeMultiset(options) {
87
+ var _this = _super.call(this, __assign(__assign({}, options), { isMergeDuplicatedVal: true })) || this;
88
+ _this._count = 0;
89
+ return _this;
59
90
  }
91
+ Object.defineProperty(TreeMultiset.prototype, "count", {
92
+ get: function () {
93
+ return this._count;
94
+ },
95
+ enumerable: false,
96
+ configurable: true
97
+ });
60
98
  /**
61
99
  * The function creates a new BSTNode with the given id, value, and count.
62
100
  * @param {BinaryTreeNodeId} id - The id parameter is the unique identifier for the binary tree node. It is used to
@@ -66,9 +104,655 @@ var TreeMultiSet = /** @class */ (function (_super) {
66
104
  * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
67
105
  * @returns A new instance of the BSTNode class with the specified id, value, and count (if provided).
68
106
  */
69
- TreeMultiSet.prototype.createNode = function (id, val, count) {
70
- return new TreeMultiSetNode(id, val, count);
107
+ TreeMultiset.prototype.createNode = function (id, val, count) {
108
+ return new TreeMultisetNode(id, val, count);
109
+ };
110
+ /**
111
+ * The function swaps the location of two nodes in a tree data structure.
112
+ * @param {N} srcNode - The source node that we want to swap with the destination node.
113
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
114
+ * be swapped with.
115
+ * @returns the `destNode` after swapping its values with the `srcNode`.
116
+ */
117
+ TreeMultiset.prototype.swapLocation = function (srcNode, destNode) {
118
+ var val = destNode.val, count = destNode.count, height = destNode.height, id = destNode.id;
119
+ var tempNode = this.createNode(id, val, count);
120
+ if (tempNode) {
121
+ tempNode.height = height;
122
+ if (tempNode instanceof TreeMultisetNode) {
123
+ destNode.id = srcNode.id;
124
+ destNode.val = srcNode.val;
125
+ destNode.count = srcNode.count;
126
+ destNode.height = srcNode.height;
127
+ srcNode.id = tempNode.id;
128
+ srcNode.val = tempNode.val;
129
+ srcNode.count = tempNode.count;
130
+ srcNode.height = tempNode.height;
131
+ }
132
+ }
133
+ return destNode;
134
+ };
135
+ /**
136
+ * The `add` function adds a new node to a binary tree, updating the size and count properties accordingly, and
137
+ * balancing the tree if necessary.
138
+ * @param {BinaryTreeNodeId} id - The id parameter represents the identifier of the binary tree node that we want to
139
+ * add. It is of type BinaryTreeNodeId.
140
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. If no value is
141
+ * provided, it will default to `undefined`.
142
+ * @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the node
143
+ * with the given `id` should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
144
+ * @returns The `add` method returns the inserted node (`N`), `null`, or `undefined`.
145
+ */
146
+ TreeMultiset.prototype.add = function (id, val, count) {
147
+ count = count !== null && count !== void 0 ? count : 1;
148
+ var inserted = null;
149
+ var newNode = this.createNode(id, val, count);
150
+ if (this.root === null) {
151
+ this._setRoot(newNode);
152
+ this._setSize(this.size + 1);
153
+ this._setCount(this.count + count);
154
+ inserted = (this.root);
155
+ }
156
+ else {
157
+ var cur = this.root;
158
+ var traversing = true;
159
+ while (traversing) {
160
+ if (cur !== null && newNode !== null) {
161
+ if (this._compare(cur.id, id) === types_1.CP.eq) {
162
+ if (newNode) {
163
+ cur.val = newNode.val;
164
+ cur.count += count;
165
+ this._setCount(this.count + newNode.count);
166
+ }
167
+ //Duplicates are not accepted.
168
+ traversing = false;
169
+ inserted = cur;
170
+ }
171
+ else if (this._compare(cur.id, id) === types_1.CP.gt) {
172
+ // Traverse left of the node
173
+ if (cur.left === undefined) {
174
+ if (newNode) {
175
+ newNode.parent = cur;
176
+ }
177
+ //Add to the left of the current node
178
+ cur.left = newNode;
179
+ this._setSize(this.size + 1);
180
+ this._setCount(this.count + newNode.count);
181
+ traversing = false;
182
+ inserted = cur.left;
183
+ }
184
+ else {
185
+ //Traverse the left of the current node
186
+ if (cur.left)
187
+ cur = cur.left;
188
+ }
189
+ }
190
+ else if (this._compare(cur.id, id) === types_1.CP.lt) {
191
+ // Traverse right of the node
192
+ if (cur.right === undefined) {
193
+ if (newNode) {
194
+ newNode.parent = cur;
195
+ }
196
+ //Add to the right of the current node
197
+ cur.right = newNode;
198
+ this._setSize(this.size + 1);
199
+ this._setCount(this.count + newNode.count);
200
+ traversing = false;
201
+ inserted = (cur.right);
202
+ }
203
+ else {
204
+ //Traverse the left of the current node
205
+ if (cur.right)
206
+ cur = cur.right;
207
+ }
208
+ }
209
+ }
210
+ else {
211
+ traversing = false;
212
+ }
213
+ }
214
+ }
215
+ if (inserted)
216
+ this.balancePath(inserted);
217
+ return inserted;
218
+ };
219
+ /**
220
+ * The function adds a new node to a binary tree as the left or right child of a given parent node.
221
+ * @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to the tree. It can be
222
+ * either a node object (`N`) or `null`.
223
+ * @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
224
+ * child.
225
+ * @returns either the left or right child node that was added to the parent node. It can also return `null` or
226
+ * `undefined` in certain cases.
227
+ */
228
+ TreeMultiset.prototype.addTo = function (newNode, parent) {
229
+ var _a, _b;
230
+ if (parent) {
231
+ if (parent.left === undefined) {
232
+ if (newNode) {
233
+ newNode.parent = parent;
234
+ }
235
+ parent.left = newNode;
236
+ if (newNode !== null) {
237
+ this._setSize(this.size + 1);
238
+ this._setCount((_a = this.count + newNode.count) !== null && _a !== void 0 ? _a : 0);
239
+ }
240
+ return parent.left;
241
+ }
242
+ else if (parent.right === undefined) {
243
+ if (newNode) {
244
+ newNode.parent = parent;
245
+ }
246
+ parent.right = newNode;
247
+ if (newNode !== null) {
248
+ this._setSize(this.size + 1);
249
+ this._setCount((_b = this.count + newNode.count) !== null && _b !== void 0 ? _b : 0);
250
+ }
251
+ return parent.right;
252
+ }
253
+ else {
254
+ return;
255
+ }
256
+ }
257
+ else {
258
+ return;
259
+ }
260
+ };
261
+ /**
262
+ * The `addMany` function inserts multiple items into a binary tree and returns an array of the inserted nodes or
263
+ * null/undefined values.
264
+ * @param {N[] | N[]} data - The `data` parameter can be either an array of elements of type `N` or an
265
+ * array of `N` objects.
266
+ * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
267
+ */
268
+ TreeMultiset.prototype.addMany = function (data) {
269
+ var e_1, _a, e_2, _b;
270
+ var _c;
271
+ // TODO not sure addMany not be run multi times
272
+ var inserted = [];
273
+ var map = new Map();
274
+ if (this.isMergeDuplicatedVal) {
275
+ try {
276
+ for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
277
+ var nodeOrId = data_1_1.value;
278
+ map.set(nodeOrId, ((_c = map.get(nodeOrId)) !== null && _c !== void 0 ? _c : 0) + 1);
279
+ }
280
+ }
281
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
282
+ finally {
283
+ try {
284
+ if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
285
+ }
286
+ finally { if (e_1) throw e_1.error; }
287
+ }
288
+ }
289
+ try {
290
+ for (var data_2 = __values(data), data_2_1 = data_2.next(); !data_2_1.done; data_2_1 = data_2.next()) {
291
+ var nodeOrId = data_2_1.value;
292
+ if (nodeOrId instanceof TreeMultisetNode) {
293
+ inserted.push(this.add(nodeOrId.id, nodeOrId.val, nodeOrId.count));
294
+ continue;
295
+ }
296
+ if (nodeOrId === null) {
297
+ inserted.push(this.add(NaN, null, 0));
298
+ continue;
299
+ }
300
+ // TODO will this cause an issue?
301
+ var count = this.isMergeDuplicatedVal ? map.get(nodeOrId) : 1;
302
+ var newId = void 0;
303
+ if (typeof nodeOrId === 'number') {
304
+ newId = this.autoIncrementId ? this.maxId + 1 : nodeOrId;
305
+ }
306
+ else if (nodeOrId instanceof Object) {
307
+ if (this.autoIncrementId) {
308
+ newId = this.maxId + 1;
309
+ }
310
+ else {
311
+ if (Object.keys(nodeOrId).includes('id')) {
312
+ newId = nodeOrId.id;
313
+ }
314
+ else {
315
+ console.warn(nodeOrId, 'Object value must has an id property when the autoIncrementId is false');
316
+ continue;
317
+ }
318
+ }
319
+ }
320
+ else {
321
+ console.warn(nodeOrId, " is not added");
322
+ continue;
323
+ }
324
+ if (this.isMergeDuplicatedVal) {
325
+ if (map.has(nodeOrId)) {
326
+ inserted.push(this.add(newId, nodeOrId, count));
327
+ map.delete(nodeOrId);
328
+ }
329
+ }
330
+ else {
331
+ inserted.push(this.add(newId, nodeOrId, 1));
332
+ }
333
+ this._setMaxId(newId);
334
+ }
335
+ }
336
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
337
+ finally {
338
+ try {
339
+ if (data_2_1 && !data_2_1.done && (_b = data_2.return)) _b.call(data_2);
340
+ }
341
+ finally { if (e_2) throw e_2.error; }
342
+ }
343
+ return inserted;
344
+ };
345
+ /**
346
+ * The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
347
+ * node that needs to be balanced.
348
+ * @param {N | BinaryTreeNodeId | null} nodeOrId - The `nodeOrId` parameter can be one of the following:
349
+ * @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
350
+ * whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
351
+ * not be taken into account when removing it. If `ignoreCount` is set to `false
352
+ * @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
353
+ */
354
+ TreeMultiset.prototype.remove = function (nodeOrId, ignoreCount) {
355
+ var bstDeletedResult = [];
356
+ if (!this.root)
357
+ return bstDeletedResult;
358
+ var curr = (typeof nodeOrId === 'number') ? this.get(nodeOrId) : nodeOrId;
359
+ if (!curr)
360
+ return bstDeletedResult;
361
+ var parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
362
+ var needBalanced = null, orgCurrent = curr;
363
+ if (curr.count > 1 && !ignoreCount) {
364
+ curr.count--;
365
+ this._setCount(this.count - 1);
366
+ }
367
+ else {
368
+ if (!curr.left) {
369
+ if (!parent) {
370
+ if (curr.right !== undefined)
371
+ this._setRoot(curr.right);
372
+ }
373
+ else {
374
+ var fp = curr.familyPosition;
375
+ if (fp === types_1.FamilyPosition.LEFT || fp === types_1.FamilyPosition.ROOT_LEFT) {
376
+ parent.left = curr.right;
377
+ }
378
+ else if (fp === types_1.FamilyPosition.RIGHT || fp === types_1.FamilyPosition.ROOT_RIGHT) {
379
+ parent.right = curr.right;
380
+ }
381
+ needBalanced = parent;
382
+ }
383
+ }
384
+ else {
385
+ var leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
386
+ if (leftSubTreeRightMost) {
387
+ var parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
388
+ orgCurrent = this.swapLocation(curr, leftSubTreeRightMost);
389
+ if (parentOfLeftSubTreeMax) {
390
+ if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
391
+ parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
392
+ else
393
+ parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
394
+ needBalanced = parentOfLeftSubTreeMax;
395
+ }
396
+ }
397
+ }
398
+ this._setSize(this.size - 1);
399
+ this._setCount(this.count - orgCurrent.count);
400
+ }
401
+ bstDeletedResult.push({ deleted: orgCurrent, needBalanced: needBalanced });
402
+ if (needBalanced) {
403
+ this.balancePath(needBalanced);
404
+ }
405
+ return bstDeletedResult;
406
+ };
407
+ /**
408
+ * The function `getSubTreeCount` calculates the number of nodes and the sum of their counts in a subtree, using either
409
+ * recursive or iterative traversal.
410
+ * @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
411
+ * binary tree.
412
+ * @returns The function `getSubTreeCount` returns an array `[number, number]`.
413
+ */
414
+ TreeMultiset.prototype.getSubTreeCount = function (subTreeRoot) {
415
+ var res = [0, 0];
416
+ if (!subTreeRoot)
417
+ return res;
418
+ if (this.loopType === types_1.LoopType.RECURSIVE) {
419
+ var _traverse_1 = function (cur) {
420
+ res[0]++;
421
+ res[1] += cur.count;
422
+ cur.left && _traverse_1(cur.left);
423
+ cur.right && _traverse_1(cur.right);
424
+ };
425
+ _traverse_1(subTreeRoot);
426
+ return res;
427
+ }
428
+ else {
429
+ var stack = [subTreeRoot];
430
+ while (stack.length > 0) {
431
+ var cur = stack.pop();
432
+ res[0]++;
433
+ res[1] += cur.count;
434
+ cur.right && stack.push(cur.right);
435
+ cur.left && stack.push(cur.left);
436
+ }
437
+ return res;
438
+ }
439
+ };
440
+ /**
441
+ * The function `subTreeSumCount` calculates the sum of the `count` property of each node in a subtree, either
442
+ * recursively or iteratively.
443
+ * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
444
+ * in a binary tree. It can be either a `BinaryTreeNodeId` (a unique identifier for a node in the binary tree) or
445
+ * `null` if the subtree is empty.
446
+ * @returns the sum of the count values of all nodes in the subtree rooted at `subTreeRoot`.
447
+ */
448
+ TreeMultiset.prototype.subTreeSumCount = function (subTreeRoot) {
449
+ if (typeof subTreeRoot === 'number')
450
+ subTreeRoot = this.get(subTreeRoot, 'id');
451
+ if (!subTreeRoot)
452
+ return 0;
453
+ var sum = 0;
454
+ if (this.loopType === types_1.LoopType.RECURSIVE) {
455
+ var _traverse_2 = function (cur) {
456
+ sum += cur.count;
457
+ cur.left && _traverse_2(cur.left);
458
+ cur.right && _traverse_2(cur.right);
459
+ };
460
+ _traverse_2(subTreeRoot);
461
+ }
462
+ else {
463
+ var stack = [subTreeRoot];
464
+ while (stack.length > 0) {
465
+ var cur = stack.pop();
466
+ sum += cur.count;
467
+ cur.right && stack.push(cur.right);
468
+ cur.left && stack.push(cur.left);
469
+ }
470
+ }
471
+ return sum;
472
+ };
473
+ /**
474
+ * The function `subTreeAddCount` recursively or iteratively traverses a binary tree and adds a given delta value to
475
+ * the `count` property of each node.
476
+ * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
477
+ * in a binary tree. It can be either a `BinaryTreeNodeId` (a unique identifier for a node in the binary tree), a
478
+ * `BinaryTreeNode` object, or `null` if the subtree is empty.
479
+ * @param {number} delta - The delta parameter is a number that represents the amount by which the count of each node
480
+ * in the subtree should be increased or decreased.
481
+ * @returns a boolean value.
482
+ */
483
+ TreeMultiset.prototype.subTreeAddCount = function (subTreeRoot, delta) {
484
+ var _this = this;
485
+ if (typeof subTreeRoot === 'number')
486
+ subTreeRoot = this.get(subTreeRoot, 'id');
487
+ if (!subTreeRoot)
488
+ return false;
489
+ var _addByProperty = function (cur) {
490
+ cur.count += delta;
491
+ _this._setCount(_this.count + delta);
492
+ };
493
+ if (this.loopType === types_1.LoopType.RECURSIVE) {
494
+ var _traverse_3 = function (cur) {
495
+ _addByProperty(cur);
496
+ cur.left && _traverse_3(cur.left);
497
+ cur.right && _traverse_3(cur.right);
498
+ };
499
+ _traverse_3(subTreeRoot);
500
+ }
501
+ else {
502
+ var stack = [subTreeRoot];
503
+ while (stack.length > 0) {
504
+ var cur = stack.pop();
505
+ _addByProperty(cur);
506
+ cur.right && stack.push(cur.right);
507
+ cur.left && stack.push(cur.left);
508
+ }
509
+ }
510
+ return true;
511
+ };
512
+ /**
513
+ * The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
514
+ * using a queue.
515
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
516
+ * `N`. It represents the property of the nodes that you want to search for.
517
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
518
+ * return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set
519
+ * to `true`, the function will return only one node. If `onlyOne`
520
+ * @returns an array of nodes that match the given nodeProperty.
521
+ */
522
+ TreeMultiset.prototype.getNodesByCount = function (nodeProperty, onlyOne) {
523
+ if (!this.root)
524
+ return [];
525
+ var result = [];
526
+ if (this.loopType === types_1.LoopType.RECURSIVE) {
527
+ var _traverse_4 = function (cur) {
528
+ if (cur.count === nodeProperty) {
529
+ result.push(cur);
530
+ if (onlyOne)
531
+ return;
532
+ }
533
+ if (!cur.left && !cur.right)
534
+ return;
535
+ cur.left && _traverse_4(cur.left);
536
+ cur.right && _traverse_4(cur.right);
537
+ };
538
+ _traverse_4(this.root);
539
+ }
540
+ else {
541
+ var queue = [this.root];
542
+ while (queue.length > 0) {
543
+ var cur = queue.shift();
544
+ if (cur) {
545
+ if (cur.count === nodeProperty) {
546
+ result.push(cur);
547
+ if (onlyOne)
548
+ return result;
549
+ }
550
+ cur.left && queue.push(cur.left);
551
+ cur.right && queue.push(cur.right);
552
+ }
553
+ }
554
+ }
555
+ return result;
556
+ };
557
+ /**
558
+ * The BFSCount function returns an array of counts from a breadth-first search of nodes.
559
+ * @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
560
+ * BFS traversal.
561
+ */
562
+ TreeMultiset.prototype.BFSCount = function () {
563
+ var nodes = _super.prototype.BFS.call(this, 'node');
564
+ return nodes.map(function (node) { return node.count; });
565
+ };
566
+ /**
567
+ * The function "listLevelsCount" takes a node and returns an array of arrays, where each inner array contains the
568
+ * count property of each node at that level.
569
+ * @param {N | null} node - The parameter `node` is of type `N | null`. This means that it can either be an instance of
570
+ * the class `N` or `null`.
571
+ * @returns a 2D array of numbers. Each inner array represents a level in the binary tree, and each number in the inner
572
+ * array represents the count property of a node in that level.
573
+ */
574
+ TreeMultiset.prototype.listLevelsCount = function (node) {
575
+ var levels = _super.prototype.listLevels.call(this, node, 'node');
576
+ return levels.map(function (level) { return level.map(function (node) { return node.count; }); });
577
+ };
578
+ /**
579
+ * The `morrisCount` function returns an array of counts for each node in a binary tree, based on a specified traversal
580
+ * pattern.
581
+ * @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that specifies the
582
+ * traversal pattern for the Morris traversal algorithm. It can have one of three values: 'in', 'pre', or 'post'.
583
+ * @returns The function `morrisCount` returns an array of numbers.
584
+ */
585
+ TreeMultiset.prototype.morrisCount = function (pattern) {
586
+ pattern = pattern || 'in';
587
+ var nodes = _super.prototype.morris.call(this, pattern, 'node');
588
+ return nodes.map(function (node) { return node.count; });
589
+ };
590
+ /**
591
+ * The function DFSIterativeCount performs a depth-first search iteratively and returns an array of count values for
592
+ * each node.
593
+ * @param {'in' | 'pre' | 'post'} [pattern] - The "pattern" parameter is a string that specifies the traversal order
594
+ * for the Depth-First Search (DFS) algorithm. It can have one of three values: 'in', 'pre', or 'post'.
595
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
596
+ * specifies whether to return the nodes or the property names during the depth-first search traversal. If it is set to
597
+ * `'node'`, the function will return the nodes. If it is set to `'property'`, the function will return the property
598
+ * @returns The DFSIterativeCount method returns an array of numbers.
599
+ */
600
+ TreeMultiset.prototype.DFSIterativeCount = function (pattern, nodeOrPropertyName) {
601
+ pattern = pattern !== null && pattern !== void 0 ? pattern : 'in';
602
+ var nodes = _super.prototype.DFSIterative.call(this, pattern, 'node');
603
+ return nodes.map(function (node) { return node.count; });
604
+ };
605
+ /**
606
+ * The DFSCount function returns an array of counts for each node in a depth-first search traversal.
607
+ * @param {DFSOrderPattern} [pattern] - The `pattern` parameter is an optional parameter that specifies the order in
608
+ * which the Depth-First Search (DFS) algorithm should traverse the nodes. It can have one of the following values:
609
+ * @param [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is used to specify whether you want to retrieve the
610
+ * nodes themselves or a specific property of the nodes. If you pass `'count'` as the value for `nodeOrPropertyName`,
611
+ * the function will return an array of the `count` property of each node.
612
+ * @returns The DFSCount method returns an array of numbers representing the count property of each node in the DFS
613
+ * traversal.
614
+ */
615
+ TreeMultiset.prototype.DFSCount = function (pattern, nodeOrPropertyName) {
616
+ pattern = pattern !== null && pattern !== void 0 ? pattern : 'in';
617
+ var nodes = _super.prototype.DFS.call(this, pattern, 'node');
618
+ return nodes.map(function (node) { return node.count; });
619
+ };
620
+ /**
621
+ * The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
622
+ * value than a given node.
623
+ * @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
624
+ * @returns the sum of the counts of nodes in the binary tree that have a lesser value than the given beginNode.
625
+ */
626
+ TreeMultiset.prototype.lesserSumCount = function (beginNode) {
627
+ var _this = this;
628
+ if (typeof beginNode === 'number')
629
+ beginNode = this.get(beginNode, 'id');
630
+ if (!beginNode)
631
+ return 0;
632
+ if (!this.root)
633
+ return 0;
634
+ var id = beginNode.id;
635
+ var sum = 0;
636
+ if (this.loopType === types_1.LoopType.RECURSIVE) {
637
+ var _traverse_5 = function (cur) {
638
+ var compared = _this._compare(cur.id, id);
639
+ if (compared === types_1.CP.eq) {
640
+ if (cur.right)
641
+ sum += _this.subTreeSumCount(cur.right);
642
+ return;
643
+ }
644
+ else if (compared === types_1.CP.lt) {
645
+ if (cur.left)
646
+ sum += _this.subTreeSumCount(cur.left);
647
+ sum += cur.count;
648
+ if (cur.right)
649
+ _traverse_5(cur.right);
650
+ else
651
+ return;
652
+ }
653
+ else {
654
+ if (cur.left)
655
+ _traverse_5(cur.left);
656
+ else
657
+ return;
658
+ }
659
+ };
660
+ _traverse_5(this.root);
661
+ }
662
+ else {
663
+ var queue = [this.root];
664
+ while (queue.length > 0) {
665
+ var cur = queue.shift();
666
+ if (cur) {
667
+ var compared = this._compare(cur.id, id);
668
+ if (compared === types_1.CP.eq) {
669
+ if (cur.right)
670
+ sum += this.subTreeSumCount(cur.right);
671
+ return sum;
672
+ }
673
+ else if (compared === types_1.CP.lt) { // todo maybe a bug
674
+ if (cur.left)
675
+ sum += this.subTreeSumCount(cur.left);
676
+ sum += cur.count;
677
+ if (cur.right)
678
+ queue.push(cur.right);
679
+ else
680
+ return sum;
681
+ }
682
+ else {
683
+ if (cur.left)
684
+ queue.push(cur.left);
685
+ else
686
+ return sum;
687
+ }
688
+ }
689
+ }
690
+ }
691
+ return sum;
692
+ };
693
+ /**
694
+ * The function `allGreaterNodesAddCount` updates the count property of all nodes in a binary tree that have an ID
695
+ * greater than a given ID by a specified delta value.
696
+ * @param {N | BinaryTreeNodeId | null} node - The `node` parameter can be one of the following:
697
+ * @param {number} delta - The `delta` parameter is a number that represents the amount by which the `count` property
698
+ * of each node should be increased.
699
+ * @returns a boolean value.
700
+ */
701
+ TreeMultiset.prototype.allGreaterNodesAddCount = function (node, delta) {
702
+ var _this = this;
703
+ if (typeof node === 'number')
704
+ node = this.get(node, 'id');
705
+ if (!node)
706
+ return false;
707
+ var id = node.id;
708
+ if (!this.root)
709
+ return false;
710
+ if (this.loopType === types_1.LoopType.RECURSIVE) {
711
+ var _traverse_6 = function (cur) {
712
+ var compared = _this._compare(cur.id, id);
713
+ if (compared === types_1.CP.gt)
714
+ cur.count += delta;
715
+ if (!cur.left && !cur.right)
716
+ return;
717
+ if (cur.left && _this._compare(cur.left.id, id) === types_1.CP.gt)
718
+ _traverse_6(cur.left);
719
+ if (cur.right && _this._compare(cur.right.id, id) === types_1.CP.gt)
720
+ _traverse_6(cur.right);
721
+ };
722
+ _traverse_6(this.root);
723
+ return true;
724
+ }
725
+ else {
726
+ var queue = [this.root];
727
+ while (queue.length > 0) {
728
+ var cur = queue.shift();
729
+ if (cur) {
730
+ var compared = this._compare(cur.id, id);
731
+ if (compared === types_1.CP.gt)
732
+ cur.count += delta;
733
+ if (cur.left && this._compare(cur.left.id, id) === types_1.CP.gt)
734
+ queue.push(cur.left);
735
+ if (cur.right && this._compare(cur.right.id, id) === types_1.CP.gt)
736
+ queue.push(cur.right);
737
+ }
738
+ }
739
+ return true;
740
+ }
741
+ };
742
+ /**
743
+ * The clear() function clears the data and sets the count to 0.
744
+ */
745
+ TreeMultiset.prototype.clear = function () {
746
+ _super.prototype.clear.call(this);
747
+ this._setCount(0);
748
+ };
749
+ /**
750
+ * The function "_setCount" is used to set the value of the "_count" property.
751
+ * @param {number} v - number
752
+ */
753
+ TreeMultiset.prototype._setCount = function (v) {
754
+ this._count = v;
71
755
  };
72
- return TreeMultiSet;
756
+ return TreeMultiset;
73
757
  }(avl_tree_1.AVLTree));
74
- exports.TreeMultiSet = TreeMultiSet;
758
+ exports.TreeMultiset = TreeMultiset;