data-structure-typed 1.34.6 → 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/README.md +74 -35
- 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/heap/heap.d.ts +1 -1
- 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/heap/heap.ts +1 -1
- 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
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { CP, LoopType } from '../../types';
|
|
2
2
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
3
3
|
export class BSTNode extends BinaryTreeNode {
|
|
4
|
-
constructor(
|
|
5
|
-
super(
|
|
4
|
+
constructor(key, val) {
|
|
5
|
+
super(key, val);
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
8
|
export class BST extends BinaryTree {
|
|
@@ -21,36 +21,36 @@ export class BST extends BinaryTree {
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
|
-
* The function creates a new binary search tree node with the given
|
|
25
|
-
* @param {
|
|
24
|
+
* The function creates a new binary search tree node with the given key and value.
|
|
25
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
26
26
|
* identify each node in the binary tree.
|
|
27
27
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
28
28
|
* that will be stored in the node.
|
|
29
|
-
* @returns a new instance of the BSTNode class with the specified
|
|
29
|
+
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
30
30
|
*/
|
|
31
|
-
createNode(
|
|
32
|
-
return new BSTNode(
|
|
31
|
+
createNode(key, val) {
|
|
32
|
+
return new BSTNode(key, val);
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
35
|
* The `add` function adds a new node to a binary search tree, either by creating a new node or by updating an existing
|
|
36
36
|
* node with the same ID.
|
|
37
|
-
* @param {
|
|
37
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N`
|
|
38
38
|
* (which represents a binary tree node) or `null`.
|
|
39
39
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
|
|
40
40
|
* being added to the binary search tree.
|
|
41
41
|
* @returns The function `add` returns the inserted node (`inserted`) which can be of type `N`, `null`, or `undefined`.
|
|
42
42
|
*/
|
|
43
|
-
add(
|
|
43
|
+
add(keyOrNode, val) {
|
|
44
44
|
// TODO support node as a param
|
|
45
45
|
let inserted = null;
|
|
46
46
|
let newNode = null;
|
|
47
|
-
if (
|
|
48
|
-
newNode =
|
|
47
|
+
if (keyOrNode instanceof BSTNode) {
|
|
48
|
+
newNode = keyOrNode;
|
|
49
49
|
}
|
|
50
|
-
else if (typeof
|
|
51
|
-
newNode = this.createNode(
|
|
50
|
+
else if (typeof keyOrNode === 'number') {
|
|
51
|
+
newNode = this.createNode(keyOrNode, val);
|
|
52
52
|
}
|
|
53
|
-
else if (
|
|
53
|
+
else if (keyOrNode === null) {
|
|
54
54
|
newNode = null;
|
|
55
55
|
}
|
|
56
56
|
if (this.root === null) {
|
|
@@ -63,7 +63,7 @@ export class BST extends BinaryTree {
|
|
|
63
63
|
let traversing = true;
|
|
64
64
|
while (traversing) {
|
|
65
65
|
if (cur !== null && newNode !== null) {
|
|
66
|
-
if (this._compare(cur.
|
|
66
|
+
if (this._compare(cur.key, newNode.key) === CP.eq) {
|
|
67
67
|
if (newNode) {
|
|
68
68
|
cur.val = newNode.val;
|
|
69
69
|
}
|
|
@@ -71,7 +71,7 @@ export class BST extends BinaryTree {
|
|
|
71
71
|
traversing = false;
|
|
72
72
|
inserted = cur;
|
|
73
73
|
}
|
|
74
|
-
else if (this._compare(cur.
|
|
74
|
+
else if (this._compare(cur.key, newNode.key) === CP.gt) {
|
|
75
75
|
// Traverse left of the node
|
|
76
76
|
if (cur.left === undefined) {
|
|
77
77
|
if (newNode) {
|
|
@@ -89,7 +89,7 @@ export class BST extends BinaryTree {
|
|
|
89
89
|
cur = cur.left;
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
|
-
else if (this._compare(cur.
|
|
92
|
+
else if (this._compare(cur.key, newNode.key) === CP.lt) {
|
|
93
93
|
// Traverse right of the node
|
|
94
94
|
if (cur.right === undefined) {
|
|
95
95
|
if (newNode) {
|
|
@@ -118,8 +118,8 @@ export class BST extends BinaryTree {
|
|
|
118
118
|
/**
|
|
119
119
|
* The `addMany` function overrides the base class method to add multiple nodes to a binary search tree in a balanced
|
|
120
120
|
* manner.
|
|
121
|
-
* @param {[
|
|
122
|
-
* `
|
|
121
|
+
* @param {[BinaryTreeNodeKey | N , N['val']][]} idsOrNodes - The `idsOrNodes` parameter in the `addMany` function is an array of
|
|
122
|
+
* `BinaryTreeNodeKey` or `N` (node) objects, or `null` values. It represents the nodes or node IDs that need to be added
|
|
123
123
|
* to the binary search tree.
|
|
124
124
|
* @param {N['val'][]} data - The values of tree nodes
|
|
125
125
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
@@ -136,28 +136,28 @@ export class BST extends BinaryTree {
|
|
|
136
136
|
const combinedArr = idsOrNodes.map((value, index) => [value, data === null || data === void 0 ? void 0 : data[index]]);
|
|
137
137
|
let sorted = [];
|
|
138
138
|
function isNodeOrNullTuple(arr) {
|
|
139
|
-
for (const [
|
|
140
|
-
if (
|
|
139
|
+
for (const [keyOrNode] of arr)
|
|
140
|
+
if (keyOrNode instanceof BSTNode)
|
|
141
141
|
return true;
|
|
142
142
|
return false;
|
|
143
143
|
}
|
|
144
|
-
function
|
|
145
|
-
for (const [
|
|
146
|
-
if (typeof
|
|
144
|
+
function isBinaryTreeKeyOrNullTuple(arr) {
|
|
145
|
+
for (const [keyOrNode] of arr)
|
|
146
|
+
if (typeof keyOrNode === 'number')
|
|
147
147
|
return true;
|
|
148
148
|
return false;
|
|
149
149
|
}
|
|
150
|
-
let
|
|
150
|
+
let sortedKeysOrNodes = [], sortedData = [];
|
|
151
151
|
if (isNodeOrNullTuple(combinedArr)) {
|
|
152
|
-
sorted = combinedArr.sort((a, b) => a[0].
|
|
152
|
+
sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
|
|
153
153
|
}
|
|
154
|
-
else if (
|
|
154
|
+
else if (isBinaryTreeKeyOrNullTuple(combinedArr)) {
|
|
155
155
|
sorted = combinedArr.sort((a, b) => a[0] - b[0]);
|
|
156
156
|
}
|
|
157
157
|
else {
|
|
158
158
|
throw new Error('Invalid input idsOrNodes');
|
|
159
159
|
}
|
|
160
|
-
|
|
160
|
+
sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
|
|
161
161
|
sortedData = sorted.map(([, val]) => val);
|
|
162
162
|
const recursive = (arr, data) => {
|
|
163
163
|
if (arr.length === 0)
|
|
@@ -177,7 +177,7 @@ export class BST extends BinaryTree {
|
|
|
177
177
|
const [l, r] = popped;
|
|
178
178
|
if (l <= r) {
|
|
179
179
|
const m = l + Math.floor((r - l) / 2);
|
|
180
|
-
const newNode = this.add(
|
|
180
|
+
const newNode = this.add(sortedKeysOrNodes[m], sortedData === null || sortedData === void 0 ? void 0 : sortedData[m]);
|
|
181
181
|
inserted.push(newNode);
|
|
182
182
|
stack.push([m + 1, r]);
|
|
183
183
|
stack.push([l, m - 1]);
|
|
@@ -186,7 +186,7 @@ export class BST extends BinaryTree {
|
|
|
186
186
|
}
|
|
187
187
|
};
|
|
188
188
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
189
|
-
recursive(
|
|
189
|
+
recursive(sortedKeysOrNodes, sortedData);
|
|
190
190
|
}
|
|
191
191
|
else {
|
|
192
192
|
iterative();
|
|
@@ -195,45 +195,45 @@ export class BST extends BinaryTree {
|
|
|
195
195
|
}
|
|
196
196
|
/**
|
|
197
197
|
* The function returns the first node in a binary tree that matches the given property name and value.
|
|
198
|
-
* @param {
|
|
198
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
199
199
|
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
200
200
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
201
|
-
* specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'
|
|
202
|
-
* @returns The method is returning either a
|
|
201
|
+
* specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'key'`.
|
|
202
|
+
* @returns The method is returning either a BinaryTreeNodeKey or N (generic type) or null.
|
|
203
203
|
*/
|
|
204
204
|
get(nodeProperty, propertyName) {
|
|
205
205
|
var _a;
|
|
206
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : '
|
|
206
|
+
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'key';
|
|
207
207
|
return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
208
208
|
}
|
|
209
209
|
/**
|
|
210
|
-
* The function returns the
|
|
211
|
-
* leftmost node if the comparison is greater than, and the
|
|
212
|
-
* @returns The method `lastKey()` returns the
|
|
213
|
-
* the values at index 0 and 1 is less than, otherwise it returns the
|
|
214
|
-
* equal, it returns the
|
|
210
|
+
* The function returns the key of the rightmost node if the comparison between two values is less than, the key of the
|
|
211
|
+
* leftmost node if the comparison is greater than, and the key of the rightmost node otherwise.
|
|
212
|
+
* @returns The method `lastKey()` returns the key of the rightmost node in the binary tree if the comparison between
|
|
213
|
+
* the values at index 0 and 1 is less than, otherwise it returns the key of the leftmost node. If the comparison is
|
|
214
|
+
* equal, it returns the key of the rightmost node. If there are no nodes in the tree, it returns 0.
|
|
215
215
|
*/
|
|
216
216
|
lastKey() {
|
|
217
217
|
var _a, _b, _c, _d, _e, _f;
|
|
218
218
|
if (this._compare(0, 1) === CP.lt)
|
|
219
|
-
return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.
|
|
219
|
+
return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.key) !== null && _b !== void 0 ? _b : 0;
|
|
220
220
|
else if (this._compare(0, 1) === CP.gt)
|
|
221
|
-
return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.
|
|
221
|
+
return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.key) !== null && _d !== void 0 ? _d : 0;
|
|
222
222
|
else
|
|
223
|
-
return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.
|
|
223
|
+
return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.key) !== null && _f !== void 0 ? _f : 0;
|
|
224
224
|
}
|
|
225
225
|
/**
|
|
226
226
|
* The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
|
|
227
|
-
* @param {
|
|
227
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or an
|
|
228
228
|
* `N` type. It represents the property of the binary tree node that you want to compare with.
|
|
229
229
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
230
|
-
* specifies the property name to use for comparison. If not provided, it defaults to `'
|
|
230
|
+
* specifies the property name to use for comparison. If not provided, it defaults to `'key'`.
|
|
231
231
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
232
232
|
* return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
|
|
233
233
|
* is set to `true`, the function will return an array with only one node (if
|
|
234
234
|
* @returns an array of nodes (type N).
|
|
235
235
|
*/
|
|
236
|
-
getNodes(nodeProperty, propertyName = '
|
|
236
|
+
getNodes(nodeProperty, propertyName = 'key', onlyOne) {
|
|
237
237
|
if (!this.root)
|
|
238
238
|
return [];
|
|
239
239
|
const result = [];
|
|
@@ -243,10 +243,10 @@ export class BST extends BinaryTree {
|
|
|
243
243
|
return;
|
|
244
244
|
if (!cur.left && !cur.right)
|
|
245
245
|
return;
|
|
246
|
-
if (propertyName === '
|
|
247
|
-
if (this._compare(cur.
|
|
246
|
+
if (propertyName === 'key') {
|
|
247
|
+
if (this._compare(cur.key, nodeProperty) === CP.gt)
|
|
248
248
|
cur.left && _traverse(cur.left);
|
|
249
|
-
if (this._compare(cur.
|
|
249
|
+
if (this._compare(cur.key, nodeProperty) === CP.lt)
|
|
250
250
|
cur.right && _traverse(cur.right);
|
|
251
251
|
}
|
|
252
252
|
else {
|
|
@@ -263,10 +263,10 @@ export class BST extends BinaryTree {
|
|
|
263
263
|
if (cur) {
|
|
264
264
|
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
|
|
265
265
|
return result;
|
|
266
|
-
if (propertyName === '
|
|
267
|
-
if (this._compare(cur.
|
|
266
|
+
if (propertyName === 'key') {
|
|
267
|
+
if (this._compare(cur.key, nodeProperty) === CP.gt)
|
|
268
268
|
cur.left && queue.push(cur.left);
|
|
269
|
-
if (this._compare(cur.
|
|
269
|
+
if (this._compare(cur.key, nodeProperty) === CP.lt)
|
|
270
270
|
cur.right && queue.push(cur.right);
|
|
271
271
|
}
|
|
272
272
|
else {
|
|
@@ -282,29 +282,29 @@ export class BST extends BinaryTree {
|
|
|
282
282
|
/**
|
|
283
283
|
* The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
|
|
284
284
|
* less than a given node.
|
|
285
|
-
* @param {N |
|
|
285
|
+
* @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
286
286
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
287
|
-
* specifies the property name to use for calculating the sum. If not provided, it defaults to `'
|
|
287
|
+
* specifies the property name to use for calculating the sum. If not provided, it defaults to `'key'`.
|
|
288
288
|
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
|
|
289
289
|
* binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
|
|
290
290
|
*/
|
|
291
291
|
lesserSum(beginNode, propertyName) {
|
|
292
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : '
|
|
292
|
+
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'key';
|
|
293
293
|
if (typeof beginNode === 'number')
|
|
294
|
-
beginNode = this.get(beginNode, '
|
|
294
|
+
beginNode = this.get(beginNode, 'key');
|
|
295
295
|
if (!beginNode)
|
|
296
296
|
return 0;
|
|
297
297
|
if (!this.root)
|
|
298
298
|
return 0;
|
|
299
|
-
const
|
|
299
|
+
const key = beginNode.key;
|
|
300
300
|
const getSumByPropertyName = (cur) => {
|
|
301
301
|
let needSum;
|
|
302
302
|
switch (propertyName) {
|
|
303
|
-
case '
|
|
304
|
-
needSum = cur.
|
|
303
|
+
case 'key':
|
|
304
|
+
needSum = cur.key;
|
|
305
305
|
break;
|
|
306
306
|
default:
|
|
307
|
-
needSum = cur.
|
|
307
|
+
needSum = cur.key;
|
|
308
308
|
break;
|
|
309
309
|
}
|
|
310
310
|
return needSum;
|
|
@@ -312,7 +312,7 @@ export class BST extends BinaryTree {
|
|
|
312
312
|
let sum = 0;
|
|
313
313
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
314
314
|
const _traverse = (cur) => {
|
|
315
|
-
const compared = this._compare(cur.
|
|
315
|
+
const compared = this._compare(cur.key, key);
|
|
316
316
|
if (compared === CP.eq) {
|
|
317
317
|
if (cur.right)
|
|
318
318
|
sum += this.subTreeSum(cur.right, propertyName);
|
|
@@ -341,7 +341,7 @@ export class BST extends BinaryTree {
|
|
|
341
341
|
while (queue.length > 0) {
|
|
342
342
|
const cur = queue.shift();
|
|
343
343
|
if (cur) {
|
|
344
|
-
const compared = this._compare(cur.
|
|
344
|
+
const compared = this._compare(cur.key, key);
|
|
345
345
|
if (compared === CP.eq) {
|
|
346
346
|
if (cur.right)
|
|
347
347
|
sum += this.subTreeSum(cur.right, propertyName);
|
|
@@ -371,44 +371,44 @@ export class BST extends BinaryTree {
|
|
|
371
371
|
/**
|
|
372
372
|
* The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
|
|
373
373
|
* have a greater value than a given node.
|
|
374
|
-
* @param {N |
|
|
375
|
-
* `
|
|
374
|
+
* @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be either of type `N` (a generic type),
|
|
375
|
+
* `BinaryTreeNodeKey`, or `null`. It represents the node in the binary tree to which the delta value will be added.
|
|
376
376
|
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
|
|
377
377
|
* each greater node should be increased.
|
|
378
378
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
379
379
|
* specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
|
|
380
|
-
* '
|
|
380
|
+
* 'key'.
|
|
381
381
|
* @returns a boolean value.
|
|
382
382
|
*/
|
|
383
383
|
allGreaterNodesAdd(node, delta, propertyName) {
|
|
384
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : '
|
|
384
|
+
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'key';
|
|
385
385
|
if (typeof node === 'number')
|
|
386
|
-
node = this.get(node, '
|
|
386
|
+
node = this.get(node, 'key');
|
|
387
387
|
if (!node)
|
|
388
388
|
return false;
|
|
389
|
-
const
|
|
389
|
+
const key = node.key;
|
|
390
390
|
if (!this.root)
|
|
391
391
|
return false;
|
|
392
392
|
const _sumByPropertyName = (cur) => {
|
|
393
393
|
switch (propertyName) {
|
|
394
|
-
case '
|
|
395
|
-
cur.
|
|
394
|
+
case 'key':
|
|
395
|
+
cur.key += delta;
|
|
396
396
|
break;
|
|
397
397
|
default:
|
|
398
|
-
cur.
|
|
398
|
+
cur.key += delta;
|
|
399
399
|
break;
|
|
400
400
|
}
|
|
401
401
|
};
|
|
402
402
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
403
403
|
const _traverse = (cur) => {
|
|
404
|
-
const compared = this._compare(cur.
|
|
404
|
+
const compared = this._compare(cur.key, key);
|
|
405
405
|
if (compared === CP.gt)
|
|
406
406
|
_sumByPropertyName(cur);
|
|
407
407
|
if (!cur.left && !cur.right)
|
|
408
408
|
return;
|
|
409
|
-
if (cur.left && this._compare(cur.left.
|
|
409
|
+
if (cur.left && this._compare(cur.left.key, key) === CP.gt)
|
|
410
410
|
_traverse(cur.left);
|
|
411
|
-
if (cur.right && this._compare(cur.right.
|
|
411
|
+
if (cur.right && this._compare(cur.right.key, key) === CP.gt)
|
|
412
412
|
_traverse(cur.right);
|
|
413
413
|
};
|
|
414
414
|
_traverse(this.root);
|
|
@@ -419,12 +419,12 @@ export class BST extends BinaryTree {
|
|
|
419
419
|
while (queue.length > 0) {
|
|
420
420
|
const cur = queue.shift();
|
|
421
421
|
if (cur) {
|
|
422
|
-
const compared = this._compare(cur.
|
|
422
|
+
const compared = this._compare(cur.key, key);
|
|
423
423
|
if (compared === CP.gt)
|
|
424
424
|
_sumByPropertyName(cur);
|
|
425
|
-
if (cur.left && this._compare(cur.left.
|
|
425
|
+
if (cur.left && this._compare(cur.left.key, key) === CP.gt)
|
|
426
426
|
queue.push(cur.left);
|
|
427
|
-
if (cur.right && this._compare(cur.right.
|
|
427
|
+
if (cur.right && this._compare(cur.right.key, key) === CP.gt)
|
|
428
428
|
queue.push(cur.right);
|
|
429
429
|
}
|
|
430
430
|
}
|
|
@@ -456,7 +456,7 @@ export class BST extends BinaryTree {
|
|
|
456
456
|
return;
|
|
457
457
|
const m = l + Math.floor((r - l) / 2);
|
|
458
458
|
const midNode = sorted[m];
|
|
459
|
-
this.add(midNode.
|
|
459
|
+
this.add(midNode.key, midNode.val);
|
|
460
460
|
buildBalanceBST(l, m - 1);
|
|
461
461
|
buildBalanceBST(m + 1, r);
|
|
462
462
|
};
|
|
@@ -472,7 +472,7 @@ export class BST extends BinaryTree {
|
|
|
472
472
|
if (l <= r) {
|
|
473
473
|
const m = l + Math.floor((r - l) / 2);
|
|
474
474
|
const midNode = sorted[m];
|
|
475
|
-
this.add(midNode.
|
|
475
|
+
this.add(midNode.key, midNode.val);
|
|
476
476
|
stack.push([m + 1, r]);
|
|
477
477
|
stack.push([l, m - 1]);
|
|
478
478
|
}
|
|
@@ -534,8 +534,8 @@ export class BST extends BinaryTree {
|
|
|
534
534
|
/**
|
|
535
535
|
* The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
|
|
536
536
|
* greater than, less than, or equal to the second ID.
|
|
537
|
-
* @param {
|
|
538
|
-
* @param {
|
|
537
|
+
* @param {BinaryTreeNodeKey} a - a is a BinaryTreeNodeKey, which represents the identifier of a binary tree node.
|
|
538
|
+
* @param {BinaryTreeNodeKey} b - The parameter "b" in the above code refers to a BinaryTreeNodeKey.
|
|
539
539
|
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
|
|
540
540
|
* than), or CP.eq (equal).
|
|
541
541
|
*/
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
|
|
2
2
|
import { IRBTree, IRBTreeNode } from '../../interfaces';
|
|
3
3
|
import { BST, BSTNode } from './bst';
|
|
4
4
|
export declare class RBTreeNode<V = any, NEIGHBOR extends RBTreeNode<V, NEIGHBOR> = RBTreeNodeNested<V>> extends BSTNode<V, NEIGHBOR> implements IRBTreeNode<V, NEIGHBOR> {
|
|
5
5
|
private _color;
|
|
6
|
-
constructor(
|
|
6
|
+
constructor(key: BinaryTreeNodeKey, val?: V);
|
|
7
7
|
get color(): RBColor;
|
|
8
8
|
set color(value: RBColor);
|
|
9
9
|
}
|
|
10
10
|
export declare class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IRBTree<N> {
|
|
11
11
|
constructor(options?: RBTreeOptions);
|
|
12
|
-
createNode(
|
|
12
|
+
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
|
|
13
13
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { RBColor } from '../../types';
|
|
2
2
|
import { BST, BSTNode } from './bst';
|
|
3
3
|
export class RBTreeNode extends BSTNode {
|
|
4
|
-
constructor(
|
|
5
|
-
super(
|
|
4
|
+
constructor(key, val) {
|
|
5
|
+
super(key, val);
|
|
6
6
|
this._color = RBColor.RED;
|
|
7
7
|
}
|
|
8
8
|
get color() {
|
|
@@ -16,7 +16,7 @@ export class RBTree extends BST {
|
|
|
16
16
|
constructor(options) {
|
|
17
17
|
super(options);
|
|
18
18
|
}
|
|
19
|
-
createNode(
|
|
20
|
-
return new RBTreeNode(
|
|
19
|
+
createNode(key, val) {
|
|
20
|
+
return new RBTreeNode(key, val);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -5,14 +5,14 @@
|
|
|
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, DFSOrderPattern } from '../../types';
|
|
10
10
|
import { ITreeMultiset, ITreeMultisetNode } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultisetNode<V = any, NEIGHBOR extends TreeMultisetNode<V, NEIGHBOR> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, NEIGHBOR> implements ITreeMultisetNode<V, NEIGHBOR> {
|
|
13
13
|
/**
|
|
14
|
-
* The constructor function initializes a BinaryTreeNode object with an
|
|
15
|
-
* @param {
|
|
14
|
+
* The constructor function initializes a BinaryTreeNode object with an key, value, and count.
|
|
15
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
|
|
16
16
|
* of the binary tree node.
|
|
17
17
|
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
18
18
|
* tree node. If no value is provided, it will be `undefined`.
|
|
@@ -20,7 +20,7 @@ export declare class TreeMultisetNode<V = any, NEIGHBOR extends TreeMultisetNode
|
|
|
20
20
|
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
21
21
|
* parameter when creating a new instance of the `BinaryTreeNode` class,
|
|
22
22
|
*/
|
|
23
|
-
constructor(
|
|
23
|
+
constructor(key: BinaryTreeNodeKey, val?: V, count?: number);
|
|
24
24
|
private _count;
|
|
25
25
|
get count(): number;
|
|
26
26
|
set count(v: number);
|
|
@@ -39,15 +39,15 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
39
39
|
private _count;
|
|
40
40
|
get count(): number;
|
|
41
41
|
/**
|
|
42
|
-
* The function creates a new BSTNode with the given
|
|
43
|
-
* @param {
|
|
42
|
+
* The function creates a new BSTNode with the given key, value, and count.
|
|
43
|
+
* @param {BinaryTreeNodeKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
44
44
|
* distinguish one node from another in the tree.
|
|
45
45
|
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
46
46
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
47
47
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
48
|
-
* @returns A new instance of the BSTNode class with the specified
|
|
48
|
+
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
49
49
|
*/
|
|
50
|
-
createNode(
|
|
50
|
+
createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N;
|
|
51
51
|
/**
|
|
52
52
|
* The function swaps the location of two nodes in a tree data structure.
|
|
53
53
|
* @param {N} srcNode - The source node that we want to swap with the destination node.
|
|
@@ -59,14 +59,14 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
59
59
|
/**
|
|
60
60
|
* The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
|
|
61
61
|
* necessary.
|
|
62
|
-
* @param {
|
|
62
|
+
* @param {BinaryTreeNodeKey | N} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N` (which
|
|
63
63
|
* represents a `BinaryTreeNode`).
|
|
64
64
|
* @param [val] - The `val` parameter represents the value to be added to the binary tree node.
|
|
65
65
|
* @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
|
|
66
66
|
* value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
|
|
67
67
|
* @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
|
68
68
|
*/
|
|
69
|
-
add(
|
|
69
|
+
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count?: number): N | null | undefined;
|
|
70
70
|
/**
|
|
71
71
|
* The function adds a new node to a binary tree if there is an available slot on the left or right side of the parent
|
|
72
72
|
* node.
|
|
@@ -80,14 +80,14 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
80
80
|
/**
|
|
81
81
|
* The `addMany` function takes an array of node IDs or nodes and adds them to the tree multiset, returning an array of
|
|
82
82
|
* the inserted nodes.
|
|
83
|
-
* @param {(
|
|
83
|
+
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} idsOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
84
84
|
* objects, or null values.
|
|
85
85
|
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
|
|
86
|
-
* the nodes being added. It is used when adding nodes using the `
|
|
86
|
+
* the nodes being added. It is used when adding nodes using the `keyOrNode` and `data` arguments in the `this.add()`
|
|
87
87
|
* method. If provided, the `data` array should
|
|
88
88
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
89
89
|
*/
|
|
90
|
-
addMany(idsOrNodes: (
|
|
90
|
+
addMany(idsOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][]): (N | null | undefined)[];
|
|
91
91
|
/**
|
|
92
92
|
* The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
|
|
93
93
|
* constructs a balanced binary search tree using either a recursive or iterative approach.
|
|
@@ -97,13 +97,13 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
97
97
|
/**
|
|
98
98
|
* The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
99
99
|
* node that needs to be balanced.
|
|
100
|
-
* @param {N |
|
|
100
|
+
* @param {N | BinaryTreeNodeKey | null} nodeOrKey - The `nodeOrKey` parameter can be one of the following:
|
|
101
101
|
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
102
102
|
* whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
|
|
103
103
|
* not be taken into account when removing it. If `ignoreCount` is set to `false
|
|
104
104
|
* @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
105
105
|
*/
|
|
106
|
-
remove(
|
|
106
|
+
remove(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
107
107
|
/**
|
|
108
108
|
* The function `getSubTreeCount` calculates the number of nodes and the sum of their counts in a subtree, using either
|
|
109
109
|
* recursive or iterative traversal.
|
|
@@ -115,34 +115,34 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
115
115
|
/**
|
|
116
116
|
* The function `subTreeSumCount` calculates the sum of the `count` property of each node in a subtree, either
|
|
117
117
|
* recursively or iteratively.
|
|
118
|
-
* @param {N |
|
|
119
|
-
* in a binary tree. It can be either a `
|
|
118
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
119
|
+
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree) or
|
|
120
120
|
* `null` if the subtree is empty.
|
|
121
121
|
* @returns the sum of the count values of all nodes in the subtree rooted at `subTreeRoot`.
|
|
122
122
|
*/
|
|
123
|
-
subTreeSumCount(subTreeRoot: N |
|
|
123
|
+
subTreeSumCount(subTreeRoot: N | BinaryTreeNodeKey | null): number;
|
|
124
124
|
/**
|
|
125
125
|
* The function `subTreeAddCount` recursively or iteratively traverses a binary tree and adds a given delta value to
|
|
126
126
|
* the `count` property of each node.
|
|
127
|
-
* @param {N |
|
|
128
|
-
* in a binary tree. It can be either a `
|
|
127
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
128
|
+
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree), a
|
|
129
129
|
* `BinaryTreeNode` object, or `null` if the subtree is empty.
|
|
130
130
|
* @param {number} delta - The delta parameter is a number that represents the amount by which the count of each node
|
|
131
131
|
* in the subtree should be increased or decreased.
|
|
132
132
|
* @returns a boolean value.
|
|
133
133
|
*/
|
|
134
|
-
subTreeAddCount(subTreeRoot: N |
|
|
134
|
+
subTreeAddCount(subTreeRoot: N | BinaryTreeNodeKey | null, delta: number): boolean;
|
|
135
135
|
/**
|
|
136
136
|
* The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
|
|
137
137
|
* using a queue.
|
|
138
|
-
* @param {
|
|
138
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
139
139
|
* `N`. It represents the property of the nodes that you want to search for.
|
|
140
140
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
141
141
|
* return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set
|
|
142
142
|
* to `true`, the function will return only one node. If `onlyOne`
|
|
143
143
|
* @returns an array of nodes that match the given nodeProperty.
|
|
144
144
|
*/
|
|
145
|
-
getNodesByCount(nodeProperty:
|
|
145
|
+
getNodesByCount(nodeProperty: BinaryTreeNodeKey | N, onlyOne?: boolean): N[];
|
|
146
146
|
/**
|
|
147
147
|
* The BFSCount function returns an array of counts from a breadth-first search of nodes.
|
|
148
148
|
* @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
|
|
@@ -186,19 +186,19 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
186
186
|
/**
|
|
187
187
|
* The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
|
|
188
188
|
* value than a given node.
|
|
189
|
-
* @param {N |
|
|
189
|
+
* @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
190
190
|
* @returns the sum of the counts of nodes in the binary tree that have a lesser value than the given beginNode.
|
|
191
191
|
*/
|
|
192
|
-
lesserSumCount(beginNode: N |
|
|
192
|
+
lesserSumCount(beginNode: N | BinaryTreeNodeKey | null): number;
|
|
193
193
|
/**
|
|
194
194
|
* The function `allGreaterNodesAddCount` updates the count property of all nodes in a binary tree that have an ID
|
|
195
195
|
* greater than a given ID by a specified delta value.
|
|
196
|
-
* @param {N |
|
|
196
|
+
* @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be one of the following:
|
|
197
197
|
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the `count` property
|
|
198
198
|
* of each node should be increased.
|
|
199
199
|
* @returns a boolean value.
|
|
200
200
|
*/
|
|
201
|
-
allGreaterNodesAddCount(node: N |
|
|
201
|
+
allGreaterNodesAddCount(node: N | BinaryTreeNodeKey | null, delta: number): boolean;
|
|
202
202
|
/**
|
|
203
203
|
* The clear() function clears the data and sets the count to 0.
|
|
204
204
|
*/
|