linked-list-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/dist/data-structures/binary-tree/avl-tree.d.ts +14 -8
- package/dist/data-structures/binary-tree/avl-tree.js +10 -5
- package/dist/data-structures/binary-tree/binary-tree.d.ts +59 -107
- package/dist/data-structures/binary-tree/binary-tree.js +72 -81
- package/dist/data-structures/binary-tree/bst.d.ts +13 -13
- package/dist/data-structures/binary-tree/bst.js +14 -14
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +15 -11
- package/dist/data-structures/binary-tree/tree-multiset.js +11 -7
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/helpers.d.ts +2 -0
- package/package.json +2 -2
- 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
|
@@ -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
|
/**
|
|
@@ -34,7 +34,7 @@ class BinaryTreeNode {
|
|
|
34
34
|
}
|
|
35
35
|
/**
|
|
36
36
|
* Set the left child node.
|
|
37
|
-
* @param {
|
|
37
|
+
* @param {N | null | undefined} v - The left child node.
|
|
38
38
|
*/
|
|
39
39
|
set left(v) {
|
|
40
40
|
if (v) {
|
|
@@ -50,7 +50,7 @@ class BinaryTreeNode {
|
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
52
|
* Set the right child node.
|
|
53
|
-
* @param {
|
|
53
|
+
* @param {N | null | undefined} v - The right child node.
|
|
54
54
|
*/
|
|
55
55
|
set right(v) {
|
|
56
56
|
if (v) {
|
|
@@ -64,35 +64,16 @@ class BinaryTreeNode {
|
|
|
64
64
|
*/
|
|
65
65
|
get familyPosition() {
|
|
66
66
|
const that = this;
|
|
67
|
-
if (
|
|
68
|
-
|
|
69
|
-
if (that.left || that.right) {
|
|
70
|
-
return types_1.FamilyPosition.ROOT_LEFT;
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
return types_1.FamilyPosition.LEFT;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
else if (that.parent.right === that) {
|
|
77
|
-
if (that.left || that.right) {
|
|
78
|
-
return types_1.FamilyPosition.ROOT_RIGHT;
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
return types_1.FamilyPosition.RIGHT;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
return types_1.FamilyPosition.MAL_NODE;
|
|
86
|
-
}
|
|
67
|
+
if (!this.parent) {
|
|
68
|
+
return this.left || this.right ? types_1.FamilyPosition.ROOT : types_1.FamilyPosition.ISOLATED;
|
|
87
69
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return types_1.FamilyPosition.ROOT;
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
return types_1.FamilyPosition.ISOLATED;
|
|
94
|
-
}
|
|
70
|
+
if (this.parent.left === that) {
|
|
71
|
+
return this.left || this.right ? types_1.FamilyPosition.ROOT_LEFT : types_1.FamilyPosition.LEFT;
|
|
95
72
|
}
|
|
73
|
+
else if (this.parent.right === that) {
|
|
74
|
+
return this.left || this.right ? types_1.FamilyPosition.ROOT_RIGHT : types_1.FamilyPosition.RIGHT;
|
|
75
|
+
}
|
|
76
|
+
return types_1.FamilyPosition.MAL_NODE;
|
|
96
77
|
}
|
|
97
78
|
}
|
|
98
79
|
exports.BinaryTreeNode = BinaryTreeNode;
|
|
@@ -106,7 +87,7 @@ class BinaryTree {
|
|
|
106
87
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
107
88
|
*/
|
|
108
89
|
constructor(options) {
|
|
109
|
-
this.
|
|
90
|
+
this._iterationType = types_1.IterationType.ITERATIVE;
|
|
110
91
|
this._root = null;
|
|
111
92
|
this._size = 0;
|
|
112
93
|
/**
|
|
@@ -119,38 +100,38 @@ class BinaryTree {
|
|
|
119
100
|
this._defaultCallbackByKey = node => node.key;
|
|
120
101
|
if (options !== undefined) {
|
|
121
102
|
const { iterationType = types_1.IterationType.ITERATIVE } = options;
|
|
122
|
-
this.
|
|
103
|
+
this._iterationType = iterationType;
|
|
123
104
|
}
|
|
124
105
|
}
|
|
125
106
|
/**
|
|
126
|
-
* Get the
|
|
107
|
+
* Get the iteration type used in the binary tree.
|
|
127
108
|
*/
|
|
128
|
-
get
|
|
129
|
-
return this.
|
|
109
|
+
get iterationType() {
|
|
110
|
+
return this._iterationType;
|
|
130
111
|
}
|
|
131
112
|
/**
|
|
132
|
-
*
|
|
113
|
+
* Set the iteration type for the binary tree.
|
|
114
|
+
* @param {IterationType} v - The new iteration type to set.
|
|
133
115
|
*/
|
|
134
|
-
|
|
135
|
-
|
|
116
|
+
set iterationType(v) {
|
|
117
|
+
this._iterationType = v;
|
|
136
118
|
}
|
|
137
119
|
/**
|
|
138
|
-
* Get the
|
|
120
|
+
* Get the root node of the binary tree.
|
|
139
121
|
*/
|
|
140
|
-
get
|
|
141
|
-
return this.
|
|
122
|
+
get root() {
|
|
123
|
+
return this._root;
|
|
142
124
|
}
|
|
143
125
|
/**
|
|
144
|
-
*
|
|
145
|
-
* @param {IterationType} v - The new iteration type to set.
|
|
126
|
+
* Get the number of nodes in the binary tree.
|
|
146
127
|
*/
|
|
147
|
-
|
|
148
|
-
this.
|
|
128
|
+
get size() {
|
|
129
|
+
return this._size;
|
|
149
130
|
}
|
|
150
131
|
/**
|
|
151
132
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
152
133
|
* @param {BinaryTreeNodeKey} key - The key for the new node.
|
|
153
|
-
* @param {
|
|
134
|
+
* @param {V} val - The value for the new node.
|
|
154
135
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
155
136
|
*/
|
|
156
137
|
createNode(key, val) {
|
|
@@ -173,7 +154,7 @@ class BinaryTree {
|
|
|
173
154
|
/**
|
|
174
155
|
* Add a node with the given key and value to the binary tree.
|
|
175
156
|
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
176
|
-
* @param {
|
|
157
|
+
* @param {V} val - The value for the new node (optional).
|
|
177
158
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
178
159
|
*/
|
|
179
160
|
add(keyOrNode, val) {
|
|
@@ -210,7 +191,8 @@ class BinaryTree {
|
|
|
210
191
|
else {
|
|
211
192
|
return;
|
|
212
193
|
}
|
|
213
|
-
const
|
|
194
|
+
const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined;
|
|
195
|
+
const existNode = key !== undefined ? this.get(key, this._defaultCallbackByKey) : undefined;
|
|
214
196
|
if (this.root) {
|
|
215
197
|
if (existNode) {
|
|
216
198
|
existNode.val = val;
|
|
@@ -237,34 +219,29 @@ class BinaryTree {
|
|
|
237
219
|
* values, and adds them to the binary tree.
|
|
238
220
|
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
239
221
|
* objects, or null values.
|
|
240
|
-
* @param {
|
|
222
|
+
* @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
|
|
241
223
|
* the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
|
|
242
224
|
* the value of the nodes will be `undefined`.
|
|
243
225
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
244
226
|
*/
|
|
245
227
|
addMany(keysOrNodes, values) {
|
|
246
228
|
// TODO not sure addMany not be run multi times
|
|
247
|
-
|
|
248
|
-
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
249
|
-
const keyOrNode = keysOrNodes[i];
|
|
229
|
+
return keysOrNodes.map((keyOrNode, i) => {
|
|
250
230
|
if (keyOrNode instanceof BinaryTreeNode) {
|
|
251
|
-
|
|
252
|
-
continue;
|
|
231
|
+
return this.add(keyOrNode.key, keyOrNode.val);
|
|
253
232
|
}
|
|
254
233
|
if (keyOrNode === null) {
|
|
255
|
-
|
|
256
|
-
continue;
|
|
234
|
+
return this.add(null);
|
|
257
235
|
}
|
|
258
236
|
const val = values === null || values === void 0 ? void 0 : values[i];
|
|
259
|
-
|
|
260
|
-
}
|
|
261
|
-
return inserted;
|
|
237
|
+
return this.add(keyOrNode, val);
|
|
238
|
+
});
|
|
262
239
|
}
|
|
263
240
|
/**
|
|
264
241
|
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
265
242
|
* @param {(BinaryTreeNodeKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
266
243
|
* `BinaryTreeNodeKey` or `N` values.
|
|
267
|
-
* @param {N[] | Array<
|
|
244
|
+
* @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
268
245
|
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
269
246
|
* array. Each value in the `data` array will be assigned to the
|
|
270
247
|
* @returns The method is returning a boolean value.
|
|
@@ -276,16 +253,24 @@ class BinaryTree {
|
|
|
276
253
|
/**
|
|
277
254
|
* The `delete` function removes a node from a binary search tree and returns the deleted node along
|
|
278
255
|
* with the parent node that needs to be balanced.
|
|
279
|
-
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node (`N`) or
|
|
280
256
|
* a key (`BinaryTreeNodeKey`). If it is a key, the function will find the corresponding node in the
|
|
281
257
|
* binary tree.
|
|
282
258
|
* @returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
259
|
+
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
260
|
+
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
|
|
261
|
+
* searching for. It can be a specific key value or any other property of the node.
|
|
262
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
263
|
+
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
264
|
+
* included in the result. The `callback` parameter has a default value of
|
|
265
|
+
* `this._defaultCallbackByKey`, which
|
|
283
266
|
*/
|
|
284
|
-
delete(
|
|
267
|
+
delete(identifier, callback = this._defaultCallbackByKey) {
|
|
285
268
|
const bstDeletedResult = [];
|
|
286
269
|
if (!this.root)
|
|
287
270
|
return bstDeletedResult;
|
|
288
|
-
|
|
271
|
+
if (identifier instanceof BinaryTreeNode)
|
|
272
|
+
callback = (node => node);
|
|
273
|
+
const curr = this.get(identifier, callback);
|
|
289
274
|
if (!curr)
|
|
290
275
|
return bstDeletedResult;
|
|
291
276
|
const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
|
|
@@ -327,10 +312,10 @@ class BinaryTree {
|
|
|
327
312
|
/**
|
|
328
313
|
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a
|
|
329
314
|
* specified root node.
|
|
330
|
-
* @param {
|
|
315
|
+
* @param {BinaryTreeNodeKey | N | null} distNode - The `distNode` parameter represents the node
|
|
331
316
|
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
|
|
332
317
|
* of the node (`BinaryTreeNodeKey`), or `null`.
|
|
333
|
-
* @param {
|
|
318
|
+
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
334
319
|
* starting node from which we want to calculate the depth. It can be either a node object or the key
|
|
335
320
|
* of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
|
|
336
321
|
* node of the binary tree.
|
|
@@ -354,7 +339,7 @@ class BinaryTree {
|
|
|
354
339
|
/**
|
|
355
340
|
* The `getHeight` function calculates the maximum height of a binary tree using either recursive or
|
|
356
341
|
* iterative approach.
|
|
357
|
-
* @param {
|
|
342
|
+
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
358
343
|
* starting node from which the height of the binary tree is calculated. It can be either a node
|
|
359
344
|
* object (`N`), a key value of a node in the tree (`BinaryTreeNodeKey`), or `null` if no starting
|
|
360
345
|
* node is specified. If `
|
|
@@ -464,15 +449,15 @@ class BinaryTree {
|
|
|
464
449
|
/**
|
|
465
450
|
* The function `getNodes` returns an array of nodes that match a given node property, using either
|
|
466
451
|
* recursive or iterative traversal.
|
|
467
|
-
* @param {
|
|
452
|
+
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
468
453
|
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
|
|
469
454
|
* searching for. It can be a specific key value or any other property of the node.
|
|
470
455
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
471
|
-
* value. This value is compared with the `
|
|
456
|
+
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
472
457
|
* included in the result. The `callback` parameter has a default value of
|
|
473
458
|
* `this._defaultCallbackByKey`, which
|
|
474
459
|
* @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
|
|
475
|
-
* first node that matches the
|
|
460
|
+
* first node that matches the identifier. If set to true, the function will return an array with
|
|
476
461
|
* only one element (or an empty array if no matching node is found). If set to false (default), the
|
|
477
462
|
* function will continue searching for all
|
|
478
463
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the
|
|
@@ -482,13 +467,15 @@ class BinaryTree {
|
|
|
482
467
|
* traverse the binary tree. It can have two possible values:
|
|
483
468
|
* @returns The function `getNodes` returns an array of nodes (`N[]`).
|
|
484
469
|
*/
|
|
485
|
-
getNodes(
|
|
470
|
+
getNodes(identifier, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
486
471
|
if (!beginRoot)
|
|
487
472
|
return [];
|
|
473
|
+
if (identifier instanceof BinaryTreeNode)
|
|
474
|
+
callback = (node => node);
|
|
488
475
|
const ans = [];
|
|
489
476
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
490
477
|
const _traverse = (cur) => {
|
|
491
|
-
if (callback(cur) ===
|
|
478
|
+
if (callback(cur) === identifier) {
|
|
492
479
|
ans.push(cur);
|
|
493
480
|
if (onlyOne)
|
|
494
481
|
return;
|
|
@@ -505,7 +492,7 @@ class BinaryTree {
|
|
|
505
492
|
while (queue.size > 0) {
|
|
506
493
|
const cur = queue.shift();
|
|
507
494
|
if (cur) {
|
|
508
|
-
if (callback(cur) ===
|
|
495
|
+
if (callback(cur) === identifier) {
|
|
509
496
|
ans.push(cur);
|
|
510
497
|
if (onlyOne)
|
|
511
498
|
return ans;
|
|
@@ -519,7 +506,7 @@ class BinaryTree {
|
|
|
519
506
|
}
|
|
520
507
|
/**
|
|
521
508
|
* The function checks if a binary tree has a node with a given property or key.
|
|
522
|
-
* @param {BinaryTreeNodeKey | N}
|
|
509
|
+
* @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
|
|
523
510
|
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or a
|
|
524
511
|
* generic type `N`.
|
|
525
512
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
@@ -534,13 +521,15 @@ class BinaryTree {
|
|
|
534
521
|
* performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
535
522
|
* @returns a boolean value.
|
|
536
523
|
*/
|
|
537
|
-
has(
|
|
524
|
+
has(identifier, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
525
|
+
if (identifier instanceof BinaryTreeNode)
|
|
526
|
+
callback = (node => node);
|
|
538
527
|
// TODO may support finding node by value equal
|
|
539
|
-
return this.getNodes(
|
|
528
|
+
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
540
529
|
}
|
|
541
530
|
/**
|
|
542
531
|
* The function `get` returns the first node in a binary tree that matches the given property or key.
|
|
543
|
-
* @param {BinaryTreeNodeKey | N}
|
|
532
|
+
* @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
|
|
544
533
|
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or `N`
|
|
545
534
|
* type.
|
|
546
535
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
@@ -553,10 +542,12 @@ class BinaryTree {
|
|
|
553
542
|
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
554
543
|
* @returns either the found node (of type N) or null if no node is found.
|
|
555
544
|
*/
|
|
556
|
-
get(
|
|
545
|
+
get(identifier, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
557
546
|
var _a;
|
|
547
|
+
if (identifier instanceof BinaryTreeNode)
|
|
548
|
+
callback = (node => node);
|
|
558
549
|
// TODO may support finding node by value equal
|
|
559
|
-
return (_a = this.getNodes(
|
|
550
|
+
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
560
551
|
}
|
|
561
552
|
/**
|
|
562
553
|
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
@@ -583,7 +574,7 @@ class BinaryTree {
|
|
|
583
574
|
/**
|
|
584
575
|
* The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
|
|
585
576
|
* iterative traversal.
|
|
586
|
-
* @param {
|
|
577
|
+
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
587
578
|
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
|
|
588
579
|
* of a node (`BinaryTreeNodeKey`), or `null` if the tree is empty.
|
|
589
580
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
@@ -707,7 +698,7 @@ class BinaryTree {
|
|
|
707
698
|
* subtree traversal. It takes a single argument, which is the current node being traversed, and
|
|
708
699
|
* returns a value. The return values from each callback invocation will be collected and returned as
|
|
709
700
|
* an array.
|
|
710
|
-
* @param {
|
|
701
|
+
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
711
702
|
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
|
|
712
703
|
* start from the root of the tree.
|
|
713
704
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -831,7 +822,7 @@ class BinaryTree {
|
|
|
831
822
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
832
823
|
* `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
|
|
833
824
|
* @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines
|
|
834
|
-
* whether
|
|
825
|
+
* whether to include the level of each node in the callback function. If `withLevel` is set
|
|
835
826
|
* to `true`, the level of each node will be passed as an argument to the callback function. If
|
|
836
827
|
* `withLevel` is
|
|
837
828
|
* @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.
|
|
@@ -126,11 +126,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
126
126
|
/**
|
|
127
127
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
128
128
|
* maintaining balance.
|
|
129
|
-
* @param {[BinaryTreeNodeKey | N, N['val']][]}
|
|
129
|
+
* @param {[BinaryTreeNodeKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
130
130
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
131
131
|
* array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
|
|
132
132
|
* `null
|
|
133
|
-
* @param {
|
|
133
|
+
* @param {V[]} data - The values of tree nodes
|
|
134
134
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
135
135
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
|
|
136
136
|
* It can have two possible values:
|
|
@@ -208,7 +208,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
208
208
|
/**
|
|
209
209
|
* The function returns the first node in the binary tree that matches the given node property and
|
|
210
210
|
* callback.
|
|
211
|
-
* @param {
|
|
211
|
+
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
|
|
212
212
|
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
213
213
|
* value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines
|
|
214
214
|
* whether a node matches the desired property.
|
|
@@ -223,9 +223,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
223
223
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
224
224
|
* matching node is found.
|
|
225
225
|
*/
|
|
226
|
-
get(
|
|
226
|
+
get(identifier, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
227
227
|
var _a;
|
|
228
|
-
return (_a = this.getNodes(
|
|
228
|
+
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
229
229
|
}
|
|
230
230
|
/**
|
|
231
231
|
* The function `lastKey` returns the key of the rightmost node if the comparison result is less
|
|
@@ -254,7 +254,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
254
254
|
/**
|
|
255
255
|
* The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
|
|
256
256
|
* using either recursive or iterative traversal.
|
|
257
|
-
* @param {
|
|
257
|
+
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
|
|
258
258
|
* of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a
|
|
259
259
|
* generic type `N`.
|
|
260
260
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
@@ -272,14 +272,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
272
272
|
* traverse the binary tree. It can have one of the following values:
|
|
273
273
|
* @returns an array of nodes (N[]).
|
|
274
274
|
*/
|
|
275
|
-
getNodes(
|
|
275
|
+
getNodes(identifier, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
276
276
|
if (!beginRoot)
|
|
277
277
|
return [];
|
|
278
278
|
const ans = [];
|
|
279
279
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
280
280
|
const _traverse = (cur) => {
|
|
281
281
|
const callbackResult = callback(cur);
|
|
282
|
-
if (callbackResult ===
|
|
282
|
+
if (callbackResult === identifier) {
|
|
283
283
|
ans.push(cur);
|
|
284
284
|
if (onlyOne)
|
|
285
285
|
return;
|
|
@@ -288,9 +288,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
288
288
|
return;
|
|
289
289
|
// TODO potential bug
|
|
290
290
|
if (callback === this._defaultCallbackByKey) {
|
|
291
|
-
if (this._compare(cur.key,
|
|
291
|
+
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
292
292
|
cur.left && _traverse(cur.left);
|
|
293
|
-
if (this._compare(cur.key,
|
|
293
|
+
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
294
294
|
cur.right && _traverse(cur.right);
|
|
295
295
|
}
|
|
296
296
|
else {
|
|
@@ -306,16 +306,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
306
306
|
const cur = queue.shift();
|
|
307
307
|
if (cur) {
|
|
308
308
|
const callbackResult = callback(cur);
|
|
309
|
-
if (callbackResult ===
|
|
309
|
+
if (callbackResult === identifier) {
|
|
310
310
|
ans.push(cur);
|
|
311
311
|
if (onlyOne)
|
|
312
312
|
return ans;
|
|
313
313
|
}
|
|
314
314
|
// TODO potential bug
|
|
315
315
|
if (callback === this._defaultCallbackByKey) {
|
|
316
|
-
if (this._compare(cur.key,
|
|
316
|
+
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
317
317
|
cur.left && queue.push(cur.left);
|
|
318
|
-
if (this._compare(cur.key,
|
|
318
|
+
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
319
319
|
cur.right && queue.push(cur.right);
|
|
320
320
|
}
|
|
321
321
|
else {
|
|
@@ -337,7 +337,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
337
337
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
338
338
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
|
|
339
339
|
* of the following values:
|
|
340
|
-
* @param {
|
|
340
|
+
* @param {BinaryTreeNodeKey | N | null} targetNode - The `targetNode` parameter in the
|
|
341
341
|
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
|
|
342
342
|
* start. It can be either a reference to a specific node (`N`), the key of a node
|
|
343
343
|
* (`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
|
}
|