data-structure-typed 1.38.5 → 1.38.7
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/cjs/data-structures/binary-tree/avl-tree.d.ts +14 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +10 -5
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +59 -107
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +72 -81
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +13 -13
- package/dist/cjs/data-structures/binary-tree/bst.js +14 -14
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +15 -11
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js +11 -7
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/helpers.d.ts +2 -0
- package/dist/cjs/types/helpers.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +14 -8
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +10 -5
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +59 -107
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +72 -81
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +13 -13
- package/dist/mjs/data-structures/binary-tree/bst.js +14 -14
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +15 -11
- package/dist/mjs/data-structures/binary-tree/tree-multiset.js +11 -7
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/helpers.d.ts +2 -0
- package/dist/umd/index.global.js +1 -1
- package/dist/umd/index.global.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +22 -13
- package/src/data-structures/binary-tree/binary-tree.ts +155 -111
- package/src/data-structures/binary-tree/bst.ts +26 -26
- package/src/data-structures/binary-tree/rb-tree.ts +6 -9
- package/src/data-structures/binary-tree/tree-multiset.ts +24 -19
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -1
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/helpers.ts +4 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +15 -15
|
@@ -14,7 +14,7 @@ const queue_1 = require("../queue");
|
|
|
14
14
|
/**
|
|
15
15
|
* Represents a node in a binary tree.
|
|
16
16
|
* @template V - The type of data stored in the node.
|
|
17
|
-
* @template
|
|
17
|
+
* @template N - The type of the family relationship in the binary tree.
|
|
18
18
|
*/
|
|
19
19
|
class BinaryTreeNode {
|
|
20
20
|
/**
|
|
@@ -47,7 +47,7 @@ class BinaryTreeNode {
|
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
49
|
* Set the left child node.
|
|
50
|
-
* @param {
|
|
50
|
+
* @param {N | null | undefined} v - The left child node.
|
|
51
51
|
*/
|
|
52
52
|
set left(v) {
|
|
53
53
|
if (v) {
|
|
@@ -64,7 +64,7 @@ class BinaryTreeNode {
|
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
66
66
|
* Set the right child node.
|
|
67
|
-
* @param {
|
|
67
|
+
* @param {N | null | undefined} v - The right child node.
|
|
68
68
|
*/
|
|
69
69
|
set right(v) {
|
|
70
70
|
if (v) {
|
|
@@ -78,35 +78,16 @@ class BinaryTreeNode {
|
|
|
78
78
|
*/
|
|
79
79
|
get familyPosition() {
|
|
80
80
|
const that = this;
|
|
81
|
-
if (
|
|
82
|
-
|
|
83
|
-
if (that.left || that.right) {
|
|
84
|
-
return types_1.FamilyPosition.ROOT_LEFT;
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
return types_1.FamilyPosition.LEFT;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
else if (that.parent.right === that) {
|
|
91
|
-
if (that.left || that.right) {
|
|
92
|
-
return types_1.FamilyPosition.ROOT_RIGHT;
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
return types_1.FamilyPosition.RIGHT;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
return types_1.FamilyPosition.MAL_NODE;
|
|
100
|
-
}
|
|
81
|
+
if (!this.parent) {
|
|
82
|
+
return this.left || this.right ? types_1.FamilyPosition.ROOT : types_1.FamilyPosition.ISOLATED;
|
|
101
83
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
return types_1.FamilyPosition.ROOT;
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
return types_1.FamilyPosition.ISOLATED;
|
|
108
|
-
}
|
|
84
|
+
if (this.parent.left === that) {
|
|
85
|
+
return this.left || this.right ? types_1.FamilyPosition.ROOT_LEFT : types_1.FamilyPosition.LEFT;
|
|
109
86
|
}
|
|
87
|
+
else if (this.parent.right === that) {
|
|
88
|
+
return this.left || this.right ? types_1.FamilyPosition.ROOT_RIGHT : types_1.FamilyPosition.RIGHT;
|
|
89
|
+
}
|
|
90
|
+
return types_1.FamilyPosition.MAL_NODE;
|
|
110
91
|
}
|
|
111
92
|
}
|
|
112
93
|
exports.BinaryTreeNode = BinaryTreeNode;
|
|
@@ -115,7 +96,6 @@ exports.BinaryTreeNode = BinaryTreeNode;
|
|
|
115
96
|
* @template N - The type of the binary tree's nodes.
|
|
116
97
|
*/
|
|
117
98
|
class BinaryTree {
|
|
118
|
-
_loopType = types_1.IterationType.ITERATIVE;
|
|
119
99
|
/**
|
|
120
100
|
* Creates a new instance of BinaryTree.
|
|
121
101
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
@@ -123,9 +103,23 @@ class BinaryTree {
|
|
|
123
103
|
constructor(options) {
|
|
124
104
|
if (options !== undefined) {
|
|
125
105
|
const { iterationType = types_1.IterationType.ITERATIVE } = options;
|
|
126
|
-
this.
|
|
106
|
+
this._iterationType = iterationType;
|
|
127
107
|
}
|
|
128
108
|
}
|
|
109
|
+
_iterationType = types_1.IterationType.ITERATIVE;
|
|
110
|
+
/**
|
|
111
|
+
* Get the iteration type used in the binary tree.
|
|
112
|
+
*/
|
|
113
|
+
get iterationType() {
|
|
114
|
+
return this._iterationType;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Set the iteration type for the binary tree.
|
|
118
|
+
* @param {IterationType} v - The new iteration type to set.
|
|
119
|
+
*/
|
|
120
|
+
set iterationType(v) {
|
|
121
|
+
this._iterationType = v;
|
|
122
|
+
}
|
|
129
123
|
_root = null;
|
|
130
124
|
/**
|
|
131
125
|
* Get the root node of the binary tree.
|
|
@@ -140,23 +134,10 @@ class BinaryTree {
|
|
|
140
134
|
get size() {
|
|
141
135
|
return this._size;
|
|
142
136
|
}
|
|
143
|
-
/**
|
|
144
|
-
* Get the iteration type used in the binary tree.
|
|
145
|
-
*/
|
|
146
|
-
get iterationType() {
|
|
147
|
-
return this._loopType;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Set the iteration type for the binary tree.
|
|
151
|
-
* @param {IterationType} v - The new iteration type to set.
|
|
152
|
-
*/
|
|
153
|
-
set iterationType(v) {
|
|
154
|
-
this._loopType = v;
|
|
155
|
-
}
|
|
156
137
|
/**
|
|
157
138
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
158
139
|
* @param {BinaryTreeNodeKey} key - The key for the new node.
|
|
159
|
-
* @param {
|
|
140
|
+
* @param {V} val - The value for the new node.
|
|
160
141
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
161
142
|
*/
|
|
162
143
|
createNode(key, val) {
|
|
@@ -179,7 +160,7 @@ class BinaryTree {
|
|
|
179
160
|
/**
|
|
180
161
|
* Add a node with the given key and value to the binary tree.
|
|
181
162
|
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
182
|
-
* @param {
|
|
163
|
+
* @param {V} val - The value for the new node (optional).
|
|
183
164
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
184
165
|
*/
|
|
185
166
|
add(keyOrNode, val) {
|
|
@@ -216,7 +197,8 @@ class BinaryTree {
|
|
|
216
197
|
else {
|
|
217
198
|
return;
|
|
218
199
|
}
|
|
219
|
-
const
|
|
200
|
+
const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined;
|
|
201
|
+
const existNode = key !== undefined ? this.get(key, this._defaultCallbackByKey) : undefined;
|
|
220
202
|
if (this.root) {
|
|
221
203
|
if (existNode) {
|
|
222
204
|
existNode.val = val;
|
|
@@ -243,34 +225,29 @@ class BinaryTree {
|
|
|
243
225
|
* values, and adds them to the binary tree.
|
|
244
226
|
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
245
227
|
* objects, or null values.
|
|
246
|
-
* @param {
|
|
228
|
+
* @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
|
|
247
229
|
* the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
|
|
248
230
|
* the value of the nodes will be `undefined`.
|
|
249
231
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
250
232
|
*/
|
|
251
233
|
addMany(keysOrNodes, values) {
|
|
252
234
|
// TODO not sure addMany not be run multi times
|
|
253
|
-
|
|
254
|
-
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
255
|
-
const keyOrNode = keysOrNodes[i];
|
|
235
|
+
return keysOrNodes.map((keyOrNode, i) => {
|
|
256
236
|
if (keyOrNode instanceof BinaryTreeNode) {
|
|
257
|
-
|
|
258
|
-
continue;
|
|
237
|
+
return this.add(keyOrNode.key, keyOrNode.val);
|
|
259
238
|
}
|
|
260
239
|
if (keyOrNode === null) {
|
|
261
|
-
|
|
262
|
-
continue;
|
|
240
|
+
return this.add(null);
|
|
263
241
|
}
|
|
264
242
|
const val = values?.[i];
|
|
265
|
-
|
|
266
|
-
}
|
|
267
|
-
return inserted;
|
|
243
|
+
return this.add(keyOrNode, val);
|
|
244
|
+
});
|
|
268
245
|
}
|
|
269
246
|
/**
|
|
270
247
|
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
271
248
|
* @param {(BinaryTreeNodeKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
272
249
|
* `BinaryTreeNodeKey` or `N` values.
|
|
273
|
-
* @param {N[] | Array<
|
|
250
|
+
* @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
274
251
|
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
275
252
|
* array. Each value in the `data` array will be assigned to the
|
|
276
253
|
* @returns The method is returning a boolean value.
|
|
@@ -282,16 +259,24 @@ class BinaryTree {
|
|
|
282
259
|
/**
|
|
283
260
|
* The `delete` function removes a node from a binary search tree and returns the deleted node along
|
|
284
261
|
* with the parent node that needs to be balanced.
|
|
285
|
-
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node (`N`) or
|
|
286
262
|
* a key (`BinaryTreeNodeKey`). If it is a key, the function will find the corresponding node in the
|
|
287
263
|
* binary tree.
|
|
288
264
|
* @returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
265
|
+
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
266
|
+
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
|
|
267
|
+
* searching for. It can be a specific key value or any other property of the node.
|
|
268
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
269
|
+
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
270
|
+
* included in the result. The `callback` parameter has a default value of
|
|
271
|
+
* `this._defaultCallbackByKey`, which
|
|
289
272
|
*/
|
|
290
|
-
delete(
|
|
273
|
+
delete(identifier, callback = this._defaultCallbackByKey) {
|
|
291
274
|
const bstDeletedResult = [];
|
|
292
275
|
if (!this.root)
|
|
293
276
|
return bstDeletedResult;
|
|
294
|
-
|
|
277
|
+
if (identifier instanceof BinaryTreeNode)
|
|
278
|
+
callback = (node => node);
|
|
279
|
+
const curr = this.get(identifier, callback);
|
|
295
280
|
if (!curr)
|
|
296
281
|
return bstDeletedResult;
|
|
297
282
|
const parent = curr?.parent ? curr.parent : null;
|
|
@@ -333,10 +318,10 @@ class BinaryTree {
|
|
|
333
318
|
/**
|
|
334
319
|
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a
|
|
335
320
|
* specified root node.
|
|
336
|
-
* @param {
|
|
321
|
+
* @param {BinaryTreeNodeKey | N | null} distNode - The `distNode` parameter represents the node
|
|
337
322
|
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
|
|
338
323
|
* of the node (`BinaryTreeNodeKey`), or `null`.
|
|
339
|
-
* @param {
|
|
324
|
+
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
340
325
|
* starting node from which we want to calculate the depth. It can be either a node object or the key
|
|
341
326
|
* of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
|
|
342
327
|
* node of the binary tree.
|
|
@@ -360,7 +345,7 @@ class BinaryTree {
|
|
|
360
345
|
/**
|
|
361
346
|
* The `getHeight` function calculates the maximum height of a binary tree using either recursive or
|
|
362
347
|
* iterative approach.
|
|
363
|
-
* @param {
|
|
348
|
+
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
364
349
|
* starting node from which the height of the binary tree is calculated. It can be either a node
|
|
365
350
|
* object (`N`), a key value of a node in the tree (`BinaryTreeNodeKey`), or `null` if no starting
|
|
366
351
|
* node is specified. If `
|
|
@@ -469,15 +454,15 @@ class BinaryTree {
|
|
|
469
454
|
/**
|
|
470
455
|
* The function `getNodes` returns an array of nodes that match a given node property, using either
|
|
471
456
|
* recursive or iterative traversal.
|
|
472
|
-
* @param {
|
|
457
|
+
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
473
458
|
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
|
|
474
459
|
* searching for. It can be a specific key value or any other property of the node.
|
|
475
460
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
476
|
-
* value. This value is compared with the `
|
|
461
|
+
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
477
462
|
* included in the result. The `callback` parameter has a default value of
|
|
478
463
|
* `this._defaultCallbackByKey`, which
|
|
479
464
|
* @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
|
|
480
|
-
* first node that matches the
|
|
465
|
+
* first node that matches the identifier. If set to true, the function will return an array with
|
|
481
466
|
* only one element (or an empty array if no matching node is found). If set to false (default), the
|
|
482
467
|
* function will continue searching for all
|
|
483
468
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the
|
|
@@ -487,13 +472,15 @@ class BinaryTree {
|
|
|
487
472
|
* traverse the binary tree. It can have two possible values:
|
|
488
473
|
* @returns The function `getNodes` returns an array of nodes (`N[]`).
|
|
489
474
|
*/
|
|
490
|
-
getNodes(
|
|
475
|
+
getNodes(identifier, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
491
476
|
if (!beginRoot)
|
|
492
477
|
return [];
|
|
478
|
+
if (identifier instanceof BinaryTreeNode)
|
|
479
|
+
callback = (node => node);
|
|
493
480
|
const ans = [];
|
|
494
481
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
495
482
|
const _traverse = (cur) => {
|
|
496
|
-
if (callback(cur) ===
|
|
483
|
+
if (callback(cur) === identifier) {
|
|
497
484
|
ans.push(cur);
|
|
498
485
|
if (onlyOne)
|
|
499
486
|
return;
|
|
@@ -510,7 +497,7 @@ class BinaryTree {
|
|
|
510
497
|
while (queue.size > 0) {
|
|
511
498
|
const cur = queue.shift();
|
|
512
499
|
if (cur) {
|
|
513
|
-
if (callback(cur) ===
|
|
500
|
+
if (callback(cur) === identifier) {
|
|
514
501
|
ans.push(cur);
|
|
515
502
|
if (onlyOne)
|
|
516
503
|
return ans;
|
|
@@ -524,7 +511,7 @@ class BinaryTree {
|
|
|
524
511
|
}
|
|
525
512
|
/**
|
|
526
513
|
* The function checks if a binary tree has a node with a given property or key.
|
|
527
|
-
* @param {BinaryTreeNodeKey | N}
|
|
514
|
+
* @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
|
|
528
515
|
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or a
|
|
529
516
|
* generic type `N`.
|
|
530
517
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
@@ -539,13 +526,15 @@ class BinaryTree {
|
|
|
539
526
|
* performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
540
527
|
* @returns a boolean value.
|
|
541
528
|
*/
|
|
542
|
-
has(
|
|
529
|
+
has(identifier, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
530
|
+
if (identifier instanceof BinaryTreeNode)
|
|
531
|
+
callback = (node => node);
|
|
543
532
|
// TODO may support finding node by value equal
|
|
544
|
-
return this.getNodes(
|
|
533
|
+
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
545
534
|
}
|
|
546
535
|
/**
|
|
547
536
|
* The function `get` returns the first node in a binary tree that matches the given property or key.
|
|
548
|
-
* @param {BinaryTreeNodeKey | N}
|
|
537
|
+
* @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
|
|
549
538
|
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or `N`
|
|
550
539
|
* type.
|
|
551
540
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
@@ -558,9 +547,11 @@ class BinaryTree {
|
|
|
558
547
|
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
559
548
|
* @returns either the found node (of type N) or null if no node is found.
|
|
560
549
|
*/
|
|
561
|
-
get(
|
|
550
|
+
get(identifier, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
551
|
+
if (identifier instanceof BinaryTreeNode)
|
|
552
|
+
callback = (node => node);
|
|
562
553
|
// TODO may support finding node by value equal
|
|
563
|
-
return this.getNodes(
|
|
554
|
+
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
564
555
|
}
|
|
565
556
|
/**
|
|
566
557
|
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
@@ -587,7 +578,7 @@ class BinaryTree {
|
|
|
587
578
|
/**
|
|
588
579
|
* The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
|
|
589
580
|
* iterative traversal.
|
|
590
|
-
* @param {
|
|
581
|
+
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
591
582
|
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
|
|
592
583
|
* of a node (`BinaryTreeNodeKey`), or `null` if the tree is empty.
|
|
593
584
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
@@ -711,7 +702,7 @@ class BinaryTree {
|
|
|
711
702
|
* subtree traversal. It takes a single argument, which is the current node being traversed, and
|
|
712
703
|
* returns a value. The return values from each callback invocation will be collected and returned as
|
|
713
704
|
* an array.
|
|
714
|
-
* @param {
|
|
705
|
+
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
715
706
|
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
|
|
716
707
|
* start from the root of the tree.
|
|
717
708
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -835,7 +826,7 @@ class BinaryTree {
|
|
|
835
826
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
836
827
|
* `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
|
|
837
828
|
* @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines
|
|
838
|
-
* whether
|
|
829
|
+
* whether to include the level of each node in the callback function. If `withLevel` is set
|
|
839
830
|
* to `true`, the level of each node will be passed as an argument to the callback function. If
|
|
840
831
|
* `withLevel` is
|
|
841
832
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
@@ -9,10 +9,10 @@ import type { BinaryTreeNodeKey, BSTComparator, BSTNodeNested, BSTOptions, MapCa
|
|
|
9
9
|
import { CP, IterationType } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
|
-
export declare class BSTNode<V = any,
|
|
12
|
+
export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
|
13
13
|
constructor(key: BinaryTreeNodeKey, val?: V);
|
|
14
14
|
}
|
|
15
|
-
export declare class BST<N extends BSTNode<
|
|
15
|
+
export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
|
|
16
16
|
/**
|
|
17
17
|
* The constructor function initializes a binary search tree object with an optional comparator
|
|
18
18
|
* function.
|
|
@@ -28,7 +28,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
28
28
|
* represents the value associated with the node in a binary search tree.
|
|
29
29
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
30
30
|
*/
|
|
31
|
-
createNode(key: BinaryTreeNodeKey, val?:
|
|
31
|
+
createNode(key: BinaryTreeNodeKey, val?: V): N;
|
|
32
32
|
/**
|
|
33
33
|
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
34
34
|
* into the tree.
|
|
@@ -39,25 +39,25 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
39
39
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
40
40
|
* was not added or if the parameters were invalid, it returns null or undefined.
|
|
41
41
|
*/
|
|
42
|
-
add(keyOrNode: BinaryTreeNodeKey | N | null, val?:
|
|
42
|
+
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined;
|
|
43
43
|
/**
|
|
44
44
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
45
45
|
* maintaining balance.
|
|
46
|
-
* @param {[BinaryTreeNodeKey | N, N['val']][]}
|
|
46
|
+
* @param {[BinaryTreeNodeKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
47
47
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
48
48
|
* array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
|
|
49
49
|
* `null
|
|
50
|
-
* @param {
|
|
50
|
+
* @param {V[]} data - The values of tree nodes
|
|
51
51
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
52
52
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
|
|
53
53
|
* It can have two possible values:
|
|
54
54
|
* @returns The `addMany` function returns an array of `N`, `null`, or `undefined` values.
|
|
55
55
|
*/
|
|
56
|
-
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?:
|
|
56
|
+
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: V[], isBalanceAdd?: boolean, iterationType?: IterationType): (N | null | undefined)[];
|
|
57
57
|
/**
|
|
58
58
|
* The function returns the first node in the binary tree that matches the given node property and
|
|
59
59
|
* callback.
|
|
60
|
-
* @param {
|
|
60
|
+
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
|
|
61
61
|
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
62
62
|
* value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines
|
|
63
63
|
* whether a node matches the desired property.
|
|
@@ -72,7 +72,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
72
72
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
73
73
|
* matching node is found.
|
|
74
74
|
*/
|
|
75
|
-
get<C extends MapCallback<N
|
|
75
|
+
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
76
76
|
/**
|
|
77
77
|
* The function `lastKey` returns the key of the rightmost node if the comparison result is less
|
|
78
78
|
* than, the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
@@ -92,7 +92,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
92
92
|
/**
|
|
93
93
|
* The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
|
|
94
94
|
* using either recursive or iterative traversal.
|
|
95
|
-
* @param {
|
|
95
|
+
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
|
|
96
96
|
* of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a
|
|
97
97
|
* generic type `N`.
|
|
98
98
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
@@ -110,7 +110,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
110
110
|
* traverse the binary tree. It can have one of the following values:
|
|
111
111
|
* @returns an array of nodes (N[]).
|
|
112
112
|
*/
|
|
113
|
-
getNodes<C extends MapCallback<N
|
|
113
|
+
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
114
114
|
/**
|
|
115
115
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
|
|
116
116
|
* nodes that have a key value lesser or greater than a target key value.
|
|
@@ -120,7 +120,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
120
120
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
121
121
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
|
|
122
122
|
* of the following values:
|
|
123
|
-
* @param {
|
|
123
|
+
* @param {BinaryTreeNodeKey | N | null} targetNode - The `targetNode` parameter in the
|
|
124
124
|
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
|
|
125
125
|
* start. It can be either a reference to a specific node (`N`), the key of a node
|
|
126
126
|
* (`BinaryTreeNodeKey`), or `null` to
|
|
@@ -128,7 +128,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
128
128
|
* done recursively or iteratively. It can have two possible values:
|
|
129
129
|
* @returns The function `lesserOrGreaterTraverse` returns an array of `MapCallbackReturn<N>`.
|
|
130
130
|
*/
|
|
131
|
-
lesserOrGreaterTraverse<C extends MapCallback<N
|
|
131
|
+
lesserOrGreaterTraverse<C extends MapCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BinaryTreeNodeKey | N | null, iterationType?: IterationType): ReturnType<C>[];
|
|
132
132
|
/**
|
|
133
133
|
* Balancing Adjustment:
|
|
134
134
|
* Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
|
|
@@ -125,11 +125,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
125
125
|
/**
|
|
126
126
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
127
127
|
* maintaining balance.
|
|
128
|
-
* @param {[BinaryTreeNodeKey | N, N['val']][]}
|
|
128
|
+
* @param {[BinaryTreeNodeKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
129
129
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
130
130
|
* array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
|
|
131
131
|
* `null
|
|
132
|
-
* @param {
|
|
132
|
+
* @param {V[]} data - The values of tree nodes
|
|
133
133
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
134
134
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
|
|
135
135
|
* It can have two possible values:
|
|
@@ -207,7 +207,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
207
207
|
/**
|
|
208
208
|
* The function returns the first node in the binary tree that matches the given node property and
|
|
209
209
|
* callback.
|
|
210
|
-
* @param {
|
|
210
|
+
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
|
|
211
211
|
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
212
212
|
* value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines
|
|
213
213
|
* whether a node matches the desired property.
|
|
@@ -222,8 +222,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
222
222
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
223
223
|
* matching node is found.
|
|
224
224
|
*/
|
|
225
|
-
get(
|
|
226
|
-
return this.getNodes(
|
|
225
|
+
get(identifier, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
226
|
+
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
227
227
|
}
|
|
228
228
|
/**
|
|
229
229
|
* The function `lastKey` returns the key of the rightmost node if the comparison result is less
|
|
@@ -251,7 +251,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
251
251
|
/**
|
|
252
252
|
* The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
|
|
253
253
|
* using either recursive or iterative traversal.
|
|
254
|
-
* @param {
|
|
254
|
+
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
|
|
255
255
|
* of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a
|
|
256
256
|
* generic type `N`.
|
|
257
257
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
@@ -269,14 +269,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
269
269
|
* traverse the binary tree. It can have one of the following values:
|
|
270
270
|
* @returns an array of nodes (N[]).
|
|
271
271
|
*/
|
|
272
|
-
getNodes(
|
|
272
|
+
getNodes(identifier, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
273
273
|
if (!beginRoot)
|
|
274
274
|
return [];
|
|
275
275
|
const ans = [];
|
|
276
276
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
277
277
|
const _traverse = (cur) => {
|
|
278
278
|
const callbackResult = callback(cur);
|
|
279
|
-
if (callbackResult ===
|
|
279
|
+
if (callbackResult === identifier) {
|
|
280
280
|
ans.push(cur);
|
|
281
281
|
if (onlyOne)
|
|
282
282
|
return;
|
|
@@ -285,9 +285,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
285
285
|
return;
|
|
286
286
|
// TODO potential bug
|
|
287
287
|
if (callback === this._defaultCallbackByKey) {
|
|
288
|
-
if (this._compare(cur.key,
|
|
288
|
+
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
289
289
|
cur.left && _traverse(cur.left);
|
|
290
|
-
if (this._compare(cur.key,
|
|
290
|
+
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
291
291
|
cur.right && _traverse(cur.right);
|
|
292
292
|
}
|
|
293
293
|
else {
|
|
@@ -303,16 +303,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
303
303
|
const cur = queue.shift();
|
|
304
304
|
if (cur) {
|
|
305
305
|
const callbackResult = callback(cur);
|
|
306
|
-
if (callbackResult ===
|
|
306
|
+
if (callbackResult === identifier) {
|
|
307
307
|
ans.push(cur);
|
|
308
308
|
if (onlyOne)
|
|
309
309
|
return ans;
|
|
310
310
|
}
|
|
311
311
|
// TODO potential bug
|
|
312
312
|
if (callback === this._defaultCallbackByKey) {
|
|
313
|
-
if (this._compare(cur.key,
|
|
313
|
+
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
314
314
|
cur.left && queue.push(cur.left);
|
|
315
|
-
if (this._compare(cur.key,
|
|
315
|
+
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
316
316
|
cur.right && queue.push(cur.right);
|
|
317
317
|
}
|
|
318
318
|
else {
|
|
@@ -334,7 +334,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
334
334
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
335
335
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
|
|
336
336
|
* of the following values:
|
|
337
|
-
* @param {
|
|
337
|
+
* @param {BinaryTreeNodeKey | N | null} targetNode - The `targetNode` parameter in the
|
|
338
338
|
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
|
|
339
339
|
* start. It can be either a reference to a specific node (`N`), the key of a node
|
|
340
340
|
* (`BinaryTreeNodeKey`), or `null` to
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
|
|
2
2
|
import { IBinaryTree } from '../../interfaces';
|
|
3
3
|
import { BST, BSTNode } from './bst';
|
|
4
|
-
export declare class RBTreeNode<V = any,
|
|
4
|
+
export declare class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
5
5
|
constructor(key: BinaryTreeNodeKey, val?: V);
|
|
6
6
|
private _color;
|
|
7
7
|
get color(): RBColor;
|
|
8
8
|
set color(value: RBColor);
|
|
9
9
|
}
|
|
10
|
-
export declare class RBTree<N extends RBTreeNode<
|
|
10
|
+
export declare class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode> extends BST<V, N> implements IBinaryTree<V, N> {
|
|
11
11
|
constructor(options?: RBTreeOptions);
|
|
12
|
-
createNode(key: BinaryTreeNodeKey, val?:
|
|
12
|
+
createNode(key: BinaryTreeNodeKey, val?: V): N;
|
|
13
13
|
}
|
|
@@ -6,10 +6,10 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
|
|
9
|
-
import { BinaryTreeDeletedResult, IterationType } from '../../types';
|
|
9
|
+
import { BinaryTreeDeletedResult, IterationType, MapCallback } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
|
-
export declare class TreeMultisetNode<V = any,
|
|
12
|
+
export declare class TreeMultisetNode<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, N> {
|
|
13
13
|
count: number;
|
|
14
14
|
/**
|
|
15
15
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
@@ -26,7 +26,7 @@ export declare class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V
|
|
|
26
26
|
/**
|
|
27
27
|
* 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.
|
|
28
28
|
*/
|
|
29
|
-
export declare class TreeMultiset<N extends TreeMultisetNode<
|
|
29
|
+
export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode> extends AVLTree<V, N> implements IBinaryTree<V, N> {
|
|
30
30
|
/**
|
|
31
31
|
* The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
|
|
32
32
|
* merge duplicated values.
|
|
@@ -45,7 +45,7 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
45
45
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
46
46
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
47
47
|
*/
|
|
48
|
-
createNode(key: BinaryTreeNodeKey, val?:
|
|
48
|
+
createNode(key: BinaryTreeNodeKey, val?: V, count?: number): N;
|
|
49
49
|
/**
|
|
50
50
|
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
51
51
|
* exists, and balancing the tree if necessary.
|
|
@@ -59,7 +59,7 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
59
59
|
* count is specified, the default count will be 1.
|
|
60
60
|
* @returns The function `add` returns a value of type `N | null | undefined`.
|
|
61
61
|
*/
|
|
62
|
-
add(keyOrNode: BinaryTreeNodeKey | N | null, val?:
|
|
62
|
+
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V, count?: number): N | null | undefined;
|
|
63
63
|
/**
|
|
64
64
|
* The function adds a new node to a binary tree if there is an available slot in the parent node.
|
|
65
65
|
* @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to
|
|
@@ -74,12 +74,12 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
74
74
|
* inserted nodes.
|
|
75
75
|
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
|
|
76
76
|
* added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode.
|
|
77
|
-
* @param {
|
|
77
|
+
* @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
|
|
78
78
|
* to the keys or nodes being added to the multiset. It is used to associate additional data with
|
|
79
79
|
* each key or node.
|
|
80
80
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
81
81
|
*/
|
|
82
|
-
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?:
|
|
82
|
+
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: V[]): (N | null | undefined)[];
|
|
83
83
|
/**
|
|
84
84
|
* The `perfectlyBalance` function in TypeScript takes a sorted array of nodes and builds a balanced
|
|
85
85
|
* binary search tree using either a recursive or iterative approach.
|
|
@@ -92,16 +92,20 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
92
92
|
/**
|
|
93
93
|
* The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
|
|
94
94
|
* node along with the parent node that needs to be balanced.
|
|
95
|
-
* @param {
|
|
96
|
-
*
|
|
97
|
-
*
|
|
95
|
+
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
96
|
+
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
|
|
97
|
+
* searching for. It can be a specific key value or any other property of the node.
|
|
98
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
99
|
+
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
100
|
+
* included in the result. The `callback` parameter has a default value of
|
|
101
|
+
* `this._defaultCallbackByKey`
|
|
98
102
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
99
103
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
100
104
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
101
105
|
* decremented by 1 and
|
|
102
106
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
103
107
|
*/
|
|
104
|
-
delete(
|
|
108
|
+
delete<C extends MapCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
105
109
|
/**
|
|
106
110
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
107
111
|
*/
|