data-structure-typed 1.34.7 → 1.34.8
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/CHANGELOG.md +1 -1
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +70 -70
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +8 -8
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +4 -4
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.js +59 -59
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +39 -39
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +49 -49
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js +33 -33
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/map-graph.js +4 -4
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js +14 -14
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/tree/tree.js +5 -5
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +71 -71
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +110 -110
- package/lib/data-structures/binary-tree/avl-tree.d.ts +10 -10
- package/lib/data-structures/binary-tree/avl-tree.js +13 -13
- package/lib/data-structures/binary-tree/binary-tree.d.ts +6 -6
- package/lib/data-structures/binary-tree/binary-tree.js +7 -7
- package/lib/data-structures/binary-tree/bst.d.ts +34 -34
- package/lib/data-structures/binary-tree/bst.js +80 -80
- package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/lib/data-structures/binary-tree/rb-tree.js +4 -4
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +27 -27
- package/lib/data-structures/binary-tree/tree-multiset.js +55 -55
- package/lib/data-structures/graph/abstract-graph.d.ts +60 -60
- package/lib/data-structures/graph/abstract-graph.js +81 -81
- package/lib/data-structures/graph/directed-graph.d.ts +51 -51
- package/lib/data-structures/graph/directed-graph.js +63 -63
- package/lib/data-structures/graph/map-graph.d.ts +13 -13
- package/lib/data-structures/graph/map-graph.js +12 -12
- package/lib/data-structures/graph/undirected-graph.d.ts +30 -30
- package/lib/data-structures/graph/undirected-graph.js +32 -32
- package/lib/data-structures/tree/tree.d.ts +4 -4
- package/lib/data-structures/tree/tree.js +6 -6
- package/lib/interfaces/abstract-binary-tree.d.ts +24 -24
- package/lib/interfaces/abstract-graph.d.ts +13 -13
- package/lib/interfaces/avl-tree.d.ts +3 -3
- package/lib/interfaces/bst.d.ts +8 -8
- package/lib/interfaces/directed-graph.d.ts +5 -5
- package/lib/interfaces/rb-tree.d.ts +2 -2
- package/lib/interfaces/undirected-graph.d.ts +2 -2
- package/lib/types/data-structures/abstract-binary-tree.d.ts +3 -3
- package/lib/types/data-structures/abstract-graph.d.ts +2 -2
- package/lib/types/data-structures/bst.d.ts +2 -2
- package/lib/types/data-structures/tree-multiset.d.ts +1 -1
- package/lib/types/utils/validate-type.d.ts +8 -8
- package/package.json +1 -1
- package/scripts/rename_clear_files.sh +29 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +147 -147
- package/src/data-structures/binary-tree/avl-tree.ts +14 -14
- package/src/data-structures/binary-tree/binary-tree.ts +8 -8
- package/src/data-structures/binary-tree/bst.ts +98 -90
- package/src/data-structures/binary-tree/rb-tree.ts +9 -9
- package/src/data-structures/binary-tree/tree-multiset.ts +62 -62
- package/src/data-structures/graph/abstract-graph.ts +109 -104
- package/src/data-structures/graph/directed-graph.ts +77 -77
- package/src/data-structures/graph/map-graph.ts +20 -15
- package/src/data-structures/graph/undirected-graph.ts +39 -39
- package/src/data-structures/tree/tree.ts +7 -7
- package/src/interfaces/abstract-binary-tree.ts +24 -24
- package/src/interfaces/abstract-graph.ts +13 -13
- package/src/interfaces/avl-tree.ts +3 -3
- package/src/interfaces/bst.ts +8 -8
- package/src/interfaces/directed-graph.ts +5 -5
- package/src/interfaces/rb-tree.ts +2 -2
- package/src/interfaces/undirected-graph.ts +2 -2
- package/src/types/data-structures/abstract-binary-tree.ts +3 -3
- package/src/types/data-structures/abstract-graph.ts +2 -2
- package/src/types/data-structures/bst.ts +2 -2
- package/src/types/data-structures/tree-multiset.ts +1 -1
- package/src/types/utils/validate-type.ts +10 -10
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +24 -24
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/bst.test.ts +71 -71
- package/test/unit/data-structures/binary-tree/overall.test.ts +19 -19
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +72 -72
- package/test/unit/data-structures/graph/directed-graph.test.ts +8 -8
- package/test/unit/data-structures/graph/map-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
- package/test/unit/data-structures/tree/tree.test.ts +2 -2
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
8
|
+
import type {BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types';
|
|
9
9
|
import {BinaryTreeDeletedResult, CP, DFSOrderPattern, FamilyPosition, LoopType} from '../../types';
|
|
10
10
|
import {ITreeMultiset, ITreeMultisetNode} from '../../interfaces';
|
|
11
11
|
import {AVLTree, AVLTreeNode} from './avl-tree';
|
|
@@ -15,8 +15,8 @@ export class TreeMultisetNode<V = any, NEIGHBOR extends TreeMultisetNode<V, NEIG
|
|
|
15
15
|
implements ITreeMultisetNode<V, NEIGHBOR>
|
|
16
16
|
{
|
|
17
17
|
/**
|
|
18
|
-
* The constructor function initializes a BinaryTreeNode object with an
|
|
19
|
-
* @param {
|
|
18
|
+
* The constructor function initializes a BinaryTreeNode object with an key, value, and count.
|
|
19
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
|
|
20
20
|
* of the binary tree node.
|
|
21
21
|
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
22
22
|
* tree node. If no value is provided, it will be `undefined`.
|
|
@@ -24,8 +24,8 @@ export class TreeMultisetNode<V = any, NEIGHBOR extends TreeMultisetNode<V, NEIG
|
|
|
24
24
|
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
25
25
|
* parameter when creating a new instance of the `BinaryTreeNode` class,
|
|
26
26
|
*/
|
|
27
|
-
constructor(
|
|
28
|
-
super(
|
|
27
|
+
constructor(key: BinaryTreeNodeKey, val?: V, count = 1) {
|
|
28
|
+
super(key, val);
|
|
29
29
|
this._count = count;
|
|
30
30
|
}
|
|
31
31
|
|
|
@@ -64,16 +64,16 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
|
-
* The function creates a new BSTNode with the given
|
|
68
|
-
* @param {
|
|
67
|
+
* The function creates a new BSTNode with the given key, value, and count.
|
|
68
|
+
* @param {BinaryTreeNodeKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
69
69
|
* distinguish one node from another in the tree.
|
|
70
70
|
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
71
71
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
72
72
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
73
|
-
* @returns A new instance of the BSTNode class with the specified
|
|
73
|
+
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
74
74
|
*/
|
|
75
|
-
override createNode(
|
|
76
|
-
return new TreeMultisetNode(
|
|
75
|
+
override createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N {
|
|
76
|
+
return new TreeMultisetNode(key, val, count) as N;
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
/**
|
|
@@ -84,17 +84,17 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
84
84
|
* @returns the `destNode` after swapping its values with the `srcNode`.
|
|
85
85
|
*/
|
|
86
86
|
override swapLocation(srcNode: N, destNode: N): N {
|
|
87
|
-
const {
|
|
88
|
-
const tempNode = this.createNode(
|
|
87
|
+
const {key, val, count, height} = destNode;
|
|
88
|
+
const tempNode = this.createNode(key, val, count);
|
|
89
89
|
if (tempNode) {
|
|
90
90
|
tempNode.height = height;
|
|
91
91
|
|
|
92
|
-
destNode.
|
|
92
|
+
destNode.key = srcNode.key;
|
|
93
93
|
destNode.val = srcNode.val;
|
|
94
94
|
destNode.count = srcNode.count;
|
|
95
95
|
destNode.height = srcNode.height;
|
|
96
96
|
|
|
97
|
-
srcNode.
|
|
97
|
+
srcNode.key = tempNode.key;
|
|
98
98
|
srcNode.val = tempNode.val;
|
|
99
99
|
srcNode.count = tempNode.count;
|
|
100
100
|
srcNode.height = tempNode.height;
|
|
@@ -106,23 +106,23 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
106
106
|
/**
|
|
107
107
|
* The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
|
|
108
108
|
* necessary.
|
|
109
|
-
* @param {
|
|
109
|
+
* @param {BinaryTreeNodeKey | N} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N` (which
|
|
110
110
|
* represents a `BinaryTreeNode`).
|
|
111
111
|
* @param [val] - The `val` parameter represents the value to be added to the binary tree node.
|
|
112
112
|
* @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
|
|
113
113
|
* value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
|
|
114
114
|
* @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
|
115
115
|
*/
|
|
116
|
-
override add(
|
|
116
|
+
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count?: number): N | null | undefined {
|
|
117
117
|
count = count ?? 1;
|
|
118
118
|
let inserted: N | null | undefined = undefined,
|
|
119
119
|
newNode: N | null;
|
|
120
|
-
if (
|
|
121
|
-
newNode = this.createNode(
|
|
122
|
-
} else if (
|
|
120
|
+
if (keyOrNode instanceof TreeMultisetNode) {
|
|
121
|
+
newNode = this.createNode(keyOrNode.key, keyOrNode.val, keyOrNode.count);
|
|
122
|
+
} else if (keyOrNode === null) {
|
|
123
123
|
newNode = null;
|
|
124
124
|
} else {
|
|
125
|
-
newNode = this.createNode(
|
|
125
|
+
newNode = this.createNode(keyOrNode, val, count);
|
|
126
126
|
}
|
|
127
127
|
if (!this.root) {
|
|
128
128
|
this._setRoot(newNode);
|
|
@@ -135,13 +135,13 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
135
135
|
while (traversing) {
|
|
136
136
|
if (cur) {
|
|
137
137
|
if (newNode) {
|
|
138
|
-
if (this._compare(cur.
|
|
138
|
+
if (this._compare(cur.key, newNode.key) === CP.eq) {
|
|
139
139
|
cur.val = newNode.val;
|
|
140
140
|
cur.count += newNode.count;
|
|
141
141
|
this._setCount(this.count + newNode.count);
|
|
142
142
|
traversing = false;
|
|
143
143
|
inserted = cur;
|
|
144
|
-
} else if (this._compare(cur.
|
|
144
|
+
} else if (this._compare(cur.key, newNode.key) === CP.gt) {
|
|
145
145
|
// Traverse left of the node
|
|
146
146
|
if (cur.left === undefined) {
|
|
147
147
|
//Add to the left of the current node
|
|
@@ -155,7 +155,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
155
155
|
//Traverse the left of the current node
|
|
156
156
|
if (cur.left) cur = cur.left;
|
|
157
157
|
}
|
|
158
|
-
} else if (this._compare(cur.
|
|
158
|
+
} else if (this._compare(cur.key, newNode.key) === CP.lt) {
|
|
159
159
|
// Traverse right of the node
|
|
160
160
|
if (cur.right === undefined) {
|
|
161
161
|
//Add to the right of the current node
|
|
@@ -219,33 +219,33 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
219
219
|
/**
|
|
220
220
|
* The `addMany` function takes an array of node IDs or nodes and adds them to the tree multiset, returning an array of
|
|
221
221
|
* the inserted nodes.
|
|
222
|
-
* @param {(
|
|
222
|
+
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} idsOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
223
223
|
* objects, or null values.
|
|
224
224
|
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
|
|
225
|
-
* the nodes being added. It is used when adding nodes using the `
|
|
225
|
+
* the nodes being added. It is used when adding nodes using the `keyOrNode` and `data` arguments in the `this.add()`
|
|
226
226
|
* method. If provided, the `data` array should
|
|
227
227
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
228
228
|
*/
|
|
229
229
|
override addMany(
|
|
230
|
-
idsOrNodes: (
|
|
230
|
+
idsOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
|
|
231
231
|
data?: N['val'][]
|
|
232
232
|
): (N | null | undefined)[] {
|
|
233
233
|
const inserted: (N | null | undefined)[] = [];
|
|
234
234
|
|
|
235
235
|
for (let i = 0; i < idsOrNodes.length; i++) {
|
|
236
|
-
const
|
|
236
|
+
const keyOrNode = idsOrNodes[i];
|
|
237
237
|
|
|
238
|
-
if (
|
|
239
|
-
inserted.push(this.add(
|
|
238
|
+
if (keyOrNode instanceof TreeMultisetNode) {
|
|
239
|
+
inserted.push(this.add(keyOrNode.key, keyOrNode.val, keyOrNode.count));
|
|
240
240
|
continue;
|
|
241
241
|
}
|
|
242
242
|
|
|
243
|
-
if (
|
|
243
|
+
if (keyOrNode === null) {
|
|
244
244
|
inserted.push(this.add(NaN, null, 0));
|
|
245
245
|
continue;
|
|
246
246
|
}
|
|
247
247
|
|
|
248
|
-
inserted.push(this.add(
|
|
248
|
+
inserted.push(this.add(keyOrNode, data?.[i], 1));
|
|
249
249
|
}
|
|
250
250
|
return inserted;
|
|
251
251
|
}
|
|
@@ -267,7 +267,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
267
267
|
if (l > r) return;
|
|
268
268
|
const m = l + Math.floor((r - l) / 2);
|
|
269
269
|
const midNode = sorted[m];
|
|
270
|
-
this.add(midNode.
|
|
270
|
+
this.add(midNode.key, midNode.val, midNode.count);
|
|
271
271
|
buildBalanceBST(l, m - 1);
|
|
272
272
|
buildBalanceBST(m + 1, r);
|
|
273
273
|
};
|
|
@@ -283,7 +283,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
283
283
|
if (l <= r) {
|
|
284
284
|
const m = l + Math.floor((r - l) / 2);
|
|
285
285
|
const midNode = sorted[m];
|
|
286
|
-
this.add(midNode.
|
|
286
|
+
this.add(midNode.key, midNode.val, midNode.count);
|
|
287
287
|
stack.push([m + 1, r]);
|
|
288
288
|
stack.push([l, m - 1]);
|
|
289
289
|
}
|
|
@@ -296,17 +296,17 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
296
296
|
/**
|
|
297
297
|
* The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
298
298
|
* node that needs to be balanced.
|
|
299
|
-
* @param {N |
|
|
299
|
+
* @param {N | BinaryTreeNodeKey | null} nodeOrKey - The `nodeOrKey` parameter can be one of the following:
|
|
300
300
|
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
301
301
|
* whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
|
|
302
302
|
* not be taken into account when removing it. If `ignoreCount` is set to `false
|
|
303
303
|
* @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
304
304
|
*/
|
|
305
|
-
override remove(
|
|
305
|
+
override remove(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[] {
|
|
306
306
|
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
|
307
307
|
if (!this.root) return bstDeletedResult;
|
|
308
308
|
|
|
309
|
-
const curr: N | null = this.get(
|
|
309
|
+
const curr: N | null = this.get(nodeOrKey);
|
|
310
310
|
if (!curr) return bstDeletedResult;
|
|
311
311
|
|
|
312
312
|
const parent: N | null = curr?.parent ? curr.parent : null;
|
|
@@ -397,13 +397,13 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
397
397
|
/**
|
|
398
398
|
* The function `subTreeSumCount` calculates the sum of the `count` property of each node in a subtree, either
|
|
399
399
|
* recursively or iteratively.
|
|
400
|
-
* @param {N |
|
|
401
|
-
* in a binary tree. It can be either a `
|
|
400
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
401
|
+
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree) or
|
|
402
402
|
* `null` if the subtree is empty.
|
|
403
403
|
* @returns the sum of the count values of all nodes in the subtree rooted at `subTreeRoot`.
|
|
404
404
|
*/
|
|
405
|
-
subTreeSumCount(subTreeRoot: N |
|
|
406
|
-
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, '
|
|
405
|
+
subTreeSumCount(subTreeRoot: N | BinaryTreeNodeKey | null): number {
|
|
406
|
+
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'key');
|
|
407
407
|
|
|
408
408
|
if (!subTreeRoot) return 0;
|
|
409
409
|
|
|
@@ -434,15 +434,15 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
434
434
|
/**
|
|
435
435
|
* The function `subTreeAddCount` recursively or iteratively traverses a binary tree and adds a given delta value to
|
|
436
436
|
* the `count` property of each node.
|
|
437
|
-
* @param {N |
|
|
438
|
-
* in a binary tree. It can be either a `
|
|
437
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
438
|
+
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree), a
|
|
439
439
|
* `BinaryTreeNode` object, or `null` if the subtree is empty.
|
|
440
440
|
* @param {number} delta - The delta parameter is a number that represents the amount by which the count of each node
|
|
441
441
|
* in the subtree should be increased or decreased.
|
|
442
442
|
* @returns a boolean value.
|
|
443
443
|
*/
|
|
444
|
-
subTreeAddCount(subTreeRoot: N |
|
|
445
|
-
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, '
|
|
444
|
+
subTreeAddCount(subTreeRoot: N | BinaryTreeNodeKey | null, delta: number): boolean {
|
|
445
|
+
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'key');
|
|
446
446
|
|
|
447
447
|
if (!subTreeRoot) return false;
|
|
448
448
|
|
|
@@ -476,14 +476,14 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
476
476
|
/**
|
|
477
477
|
* The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
|
|
478
478
|
* using a queue.
|
|
479
|
-
* @param {
|
|
479
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
480
480
|
* `N`. It represents the property of the nodes that you want to search for.
|
|
481
481
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
482
482
|
* return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set
|
|
483
483
|
* to `true`, the function will return only one node. If `onlyOne`
|
|
484
484
|
* @returns an array of nodes that match the given nodeProperty.
|
|
485
485
|
*/
|
|
486
|
-
getNodesByCount(nodeProperty:
|
|
486
|
+
getNodesByCount(nodeProperty: BinaryTreeNodeKey | N, onlyOne?: boolean): N[] {
|
|
487
487
|
if (!this.root) return [];
|
|
488
488
|
const result: N[] = [];
|
|
489
489
|
|
|
@@ -585,20 +585,20 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
585
585
|
/**
|
|
586
586
|
* The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
|
|
587
587
|
* value than a given node.
|
|
588
|
-
* @param {N |
|
|
588
|
+
* @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
589
589
|
* @returns the sum of the counts of nodes in the binary tree that have a lesser value than the given beginNode.
|
|
590
590
|
*/
|
|
591
|
-
lesserSumCount(beginNode: N |
|
|
592
|
-
if (typeof beginNode === 'number') beginNode = this.get(beginNode, '
|
|
591
|
+
lesserSumCount(beginNode: N | BinaryTreeNodeKey | null): number {
|
|
592
|
+
if (typeof beginNode === 'number') beginNode = this.get(beginNode, 'key');
|
|
593
593
|
if (!beginNode) return 0;
|
|
594
594
|
if (!this.root) return 0;
|
|
595
|
-
const
|
|
595
|
+
const key = beginNode.key;
|
|
596
596
|
|
|
597
597
|
let sum = 0;
|
|
598
598
|
|
|
599
599
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
600
600
|
const _traverse = (cur: N): void => {
|
|
601
|
-
const compared = this._compare(cur.
|
|
601
|
+
const compared = this._compare(cur.key, key);
|
|
602
602
|
if (compared === CP.eq) {
|
|
603
603
|
if (cur.right) sum += this.subTreeSumCount(cur.right);
|
|
604
604
|
return;
|
|
@@ -619,7 +619,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
619
619
|
while (queue.length > 0) {
|
|
620
620
|
const cur = queue.shift();
|
|
621
621
|
if (cur) {
|
|
622
|
-
const compared = this._compare(cur.
|
|
622
|
+
const compared = this._compare(cur.key, key);
|
|
623
623
|
if (compared === CP.eq) {
|
|
624
624
|
if (cur.right) sum += this.subTreeSumCount(cur.right);
|
|
625
625
|
return sum;
|
|
@@ -643,25 +643,25 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
643
643
|
/**
|
|
644
644
|
* The function `allGreaterNodesAddCount` updates the count property of all nodes in a binary tree that have an ID
|
|
645
645
|
* greater than a given ID by a specified delta value.
|
|
646
|
-
* @param {N |
|
|
646
|
+
* @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be one of the following:
|
|
647
647
|
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the `count` property
|
|
648
648
|
* of each node should be increased.
|
|
649
649
|
* @returns a boolean value.
|
|
650
650
|
*/
|
|
651
|
-
allGreaterNodesAddCount(node: N |
|
|
652
|
-
if (typeof node === 'number') node = this.get(node, '
|
|
651
|
+
allGreaterNodesAddCount(node: N | BinaryTreeNodeKey | null, delta: number): boolean {
|
|
652
|
+
if (typeof node === 'number') node = this.get(node, 'key');
|
|
653
653
|
if (!node) return false;
|
|
654
|
-
const
|
|
654
|
+
const key = node.key;
|
|
655
655
|
if (!this.root) return false;
|
|
656
656
|
|
|
657
657
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
658
658
|
const _traverse = (cur: N) => {
|
|
659
|
-
const compared = this._compare(cur.
|
|
659
|
+
const compared = this._compare(cur.key, key);
|
|
660
660
|
if (compared === CP.gt) cur.count += delta;
|
|
661
661
|
|
|
662
662
|
if (!cur.left && !cur.right) return;
|
|
663
|
-
if (cur.left && this._compare(cur.left.
|
|
664
|
-
if (cur.right && this._compare(cur.right.
|
|
663
|
+
if (cur.left && this._compare(cur.left.key, key) === CP.gt) _traverse(cur.left);
|
|
664
|
+
if (cur.right && this._compare(cur.right.key, key) === CP.gt) _traverse(cur.right);
|
|
665
665
|
};
|
|
666
666
|
|
|
667
667
|
_traverse(this.root);
|
|
@@ -671,11 +671,11 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
671
671
|
while (queue.length > 0) {
|
|
672
672
|
const cur = queue.shift();
|
|
673
673
|
if (cur) {
|
|
674
|
-
const compared = this._compare(cur.
|
|
674
|
+
const compared = this._compare(cur.key, key);
|
|
675
675
|
if (compared === CP.gt) cur.count += delta;
|
|
676
676
|
|
|
677
|
-
if (cur.left && this._compare(cur.left.
|
|
678
|
-
if (cur.right && this._compare(cur.right.
|
|
677
|
+
if (cur.left && this._compare(cur.left.key, key) === CP.gt) queue.push(cur.left);
|
|
678
|
+
if (cur.right && this._compare(cur.right.key, key) === CP.gt) queue.push(cur.right);
|
|
679
679
|
}
|
|
680
680
|
}
|
|
681
681
|
return true;
|