min-heap-typed 1.42.5 → 1.42.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 +5 -5
- package/dist/data-structures/binary-tree/avl-tree.js +19 -14
- package/dist/data-structures/binary-tree/binary-tree.d.ts +108 -60
- package/dist/data-structures/binary-tree/binary-tree.js +189 -89
- package/dist/data-structures/binary-tree/bst.d.ts +30 -8
- package/dist/data-structures/binary-tree/bst.js +77 -28
- package/dist/data-structures/binary-tree/rb-tree.d.ts +35 -28
- package/dist/data-structures/binary-tree/rb-tree.js +44 -45
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +7 -12
- package/dist/data-structures/binary-tree/tree-multimap.js +38 -37
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/binary-tree.js +6 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/package.json +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +24 -18
- package/src/data-structures/binary-tree/binary-tree.ts +248 -142
- package/src/data-structures/binary-tree/bst.ts +88 -38
- package/src/data-structures/binary-tree/rb-tree.ts +52 -58
- package/src/data-structures/binary-tree/tree-multimap.ts +50 -54
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +7 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
|
@@ -10,9 +10,9 @@ import { CP, IterationType } from '../../types';
|
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
|
13
|
-
parent
|
|
13
|
+
parent?: N;
|
|
14
14
|
constructor(key: BTNKey, value?: V);
|
|
15
|
-
protected _left
|
|
15
|
+
protected _left?: N;
|
|
16
16
|
/**
|
|
17
17
|
* Get the left child node.
|
|
18
18
|
*/
|
|
@@ -22,7 +22,7 @@ export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>
|
|
|
22
22
|
* @param {N | undefined} v - The left child node.
|
|
23
23
|
*/
|
|
24
24
|
set left(v: N | undefined);
|
|
25
|
-
protected _right
|
|
25
|
+
protected _right?: N;
|
|
26
26
|
/**
|
|
27
27
|
* Get the right child node.
|
|
28
28
|
*/
|
|
@@ -41,7 +41,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
41
41
|
* binary search tree.
|
|
42
42
|
*/
|
|
43
43
|
constructor(options?: BSTOptions);
|
|
44
|
-
protected _root
|
|
44
|
+
protected _root?: N;
|
|
45
45
|
/**
|
|
46
46
|
* Get the root node of the binary tree.
|
|
47
47
|
*/
|
|
@@ -79,7 +79,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
79
79
|
* It can have two possible values:
|
|
80
80
|
* @returns The `addMany` function returns an array of `N`, `undefined`, or `undefined` values.
|
|
81
81
|
*/
|
|
82
|
-
addMany(keysOrNodes: (BTNKey |
|
|
82
|
+
addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: (V | undefined)[], isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
|
|
83
83
|
/**
|
|
84
84
|
* The function `lastKey` returns the key of the rightmost node if the comparison result is less
|
|
85
85
|
* than, the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
@@ -95,7 +95,29 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
95
95
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
96
96
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
97
97
|
*/
|
|
98
|
-
lastKey(beginRoot?: N | undefined, iterationType?: IterationType): BTNKey;
|
|
98
|
+
lastKey(beginRoot?: BTNKey | N | undefined, iterationType?: IterationType): BTNKey;
|
|
99
|
+
/**
|
|
100
|
+
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
101
|
+
* either recursive or iterative methods.
|
|
102
|
+
* @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
103
|
+
* It is used to find the node with the matching key value.
|
|
104
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
105
|
+
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
106
|
+
* values:
|
|
107
|
+
* @returns The function `getNodeByKey` returns a node (`N`) if a node with the specified key is
|
|
108
|
+
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
109
|
+
*/
|
|
110
|
+
getNodeByKey(key: BTNKey, iterationType?: IterationType): N | undefined;
|
|
111
|
+
/**
|
|
112
|
+
* The function `ensureNotKey` returns the node corresponding to the given key if it is a node key,
|
|
113
|
+
* otherwise it returns the key itself.
|
|
114
|
+
* @param {BTNKey | N | undefined} key - The `key` parameter can be of type `BTNKey`, `N`, or
|
|
115
|
+
* `undefined`.
|
|
116
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
117
|
+
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
118
|
+
* @returns either a node object (N) or undefined.
|
|
119
|
+
*/
|
|
120
|
+
ensureNotKey(key: BTNKey | N | undefined, iterationType?: IterationType): N | undefined;
|
|
99
121
|
/**
|
|
100
122
|
* The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
|
|
101
123
|
* using either recursive or iterative traversal.
|
|
@@ -104,7 +126,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
104
126
|
* generic type `N`.
|
|
105
127
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
106
128
|
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
107
|
-
* included in the result. The default value for `callback` is `this.
|
|
129
|
+
* included in the result. The default value for `callback` is `this._defaultOneParamCallback`, which is
|
|
108
130
|
* a
|
|
109
131
|
* @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
|
|
110
132
|
* the first node that matches the nodeProperty. If set to true, the function will return an array
|
|
@@ -117,7 +139,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
117
139
|
* traverse the binary tree. It can have one of the following values:
|
|
118
140
|
* @returns an array of nodes (N[]).
|
|
119
141
|
*/
|
|
120
|
-
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: N | undefined, iterationType?: IterationType): N[];
|
|
142
|
+
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BTNKey | N | undefined, iterationType?: IterationType): N[];
|
|
121
143
|
/**
|
|
122
144
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
|
|
123
145
|
* nodes that have a key value lesser or greater than a target key value.
|
|
@@ -54,7 +54,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
54
54
|
*/
|
|
55
55
|
constructor(options) {
|
|
56
56
|
super(options);
|
|
57
|
-
this._root = undefined;
|
|
58
57
|
this._comparator = (a, b) => a - b;
|
|
59
58
|
this._root = undefined;
|
|
60
59
|
if (options !== undefined) {
|
|
@@ -92,9 +91,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
92
91
|
* was not added or if the parameters were invalid, it returns undefined or undefined.
|
|
93
92
|
*/
|
|
94
93
|
add(keyOrNode, value) {
|
|
95
|
-
if (keyOrNode === 8) {
|
|
96
|
-
debugger;
|
|
97
|
-
}
|
|
98
94
|
if (keyOrNode === null)
|
|
99
95
|
return undefined;
|
|
100
96
|
// TODO support node as a parameter
|
|
@@ -103,7 +99,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
103
99
|
if (keyOrNode instanceof BSTNode) {
|
|
104
100
|
newNode = keyOrNode;
|
|
105
101
|
}
|
|
106
|
-
else if (
|
|
102
|
+
else if (this.isNodeKey(keyOrNode)) {
|
|
107
103
|
newNode = this.createNode(keyOrNode, value);
|
|
108
104
|
}
|
|
109
105
|
else {
|
|
@@ -186,32 +182,32 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
186
182
|
*/
|
|
187
183
|
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
188
184
|
// TODO this addMany function is inefficient, it should be optimized
|
|
189
|
-
function
|
|
185
|
+
function hasNoUndefined(arr) {
|
|
190
186
|
return arr.indexOf(undefined) === -1;
|
|
191
187
|
}
|
|
192
|
-
if (!isBalanceAdd || !
|
|
188
|
+
if (!isBalanceAdd || !hasNoUndefined(keysOrNodes)) {
|
|
193
189
|
return super.addMany(keysOrNodes, data).map(n => n !== null && n !== void 0 ? n : undefined);
|
|
194
190
|
}
|
|
195
191
|
const inserted = [];
|
|
196
192
|
const combinedArr = keysOrNodes.map((value, index) => [value, data === null || data === void 0 ? void 0 : data[index]]);
|
|
197
193
|
let sorted = [];
|
|
198
|
-
function
|
|
194
|
+
function _isNodeOrUndefinedTuple(arr) {
|
|
199
195
|
for (const [keyOrNode] of arr)
|
|
200
196
|
if (keyOrNode instanceof BSTNode)
|
|
201
197
|
return true;
|
|
202
198
|
return false;
|
|
203
199
|
}
|
|
204
|
-
|
|
200
|
+
const _isBinaryTreeKeyOrNullTuple = (arr) => {
|
|
205
201
|
for (const [keyOrNode] of arr)
|
|
206
|
-
if (
|
|
202
|
+
if (this.isNodeKey(keyOrNode))
|
|
207
203
|
return true;
|
|
208
204
|
return false;
|
|
209
|
-
}
|
|
205
|
+
};
|
|
210
206
|
let sortedKeysOrNodes = [], sortedData = [];
|
|
211
|
-
if (
|
|
207
|
+
if (_isNodeOrUndefinedTuple(combinedArr)) {
|
|
212
208
|
sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
|
|
213
209
|
}
|
|
214
|
-
else if (
|
|
210
|
+
else if (_isBinaryTreeKeyOrNullTuple(combinedArr)) {
|
|
215
211
|
sorted = combinedArr.sort((a, b) => a[0] - b[0]);
|
|
216
212
|
}
|
|
217
213
|
else {
|
|
@@ -219,16 +215,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
219
215
|
}
|
|
220
216
|
sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
|
|
221
217
|
sortedData = sorted.map(([, value]) => value);
|
|
222
|
-
const
|
|
218
|
+
const _dfs = (arr, data) => {
|
|
223
219
|
if (arr.length === 0)
|
|
224
220
|
return;
|
|
225
221
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
226
222
|
const newNode = this.add(arr[mid], data === null || data === void 0 ? void 0 : data[mid]);
|
|
227
223
|
inserted.push(newNode);
|
|
228
|
-
|
|
229
|
-
|
|
224
|
+
_dfs(arr.slice(0, mid), data === null || data === void 0 ? void 0 : data.slice(0, mid));
|
|
225
|
+
_dfs(arr.slice(mid + 1), data === null || data === void 0 ? void 0 : data.slice(mid + 1));
|
|
230
226
|
};
|
|
231
|
-
const
|
|
227
|
+
const _iterate = () => {
|
|
232
228
|
const n = sorted.length;
|
|
233
229
|
const stack = [[0, n - 1]];
|
|
234
230
|
while (stack.length > 0) {
|
|
@@ -246,10 +242,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
246
242
|
}
|
|
247
243
|
};
|
|
248
244
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
249
|
-
|
|
245
|
+
_dfs(sortedKeysOrNodes, sortedData);
|
|
250
246
|
}
|
|
251
247
|
else {
|
|
252
|
-
|
|
248
|
+
_iterate();
|
|
253
249
|
}
|
|
254
250
|
return inserted;
|
|
255
251
|
}
|
|
@@ -277,6 +273,60 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
277
273
|
else
|
|
278
274
|
return (_f = (_e = this.getRightMost(beginRoot, iterationType)) === null || _e === void 0 ? void 0 : _e.key) !== null && _f !== void 0 ? _f : 0;
|
|
279
275
|
}
|
|
276
|
+
/**
|
|
277
|
+
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
278
|
+
* either recursive or iterative methods.
|
|
279
|
+
* @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
280
|
+
* It is used to find the node with the matching key value.
|
|
281
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
282
|
+
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
283
|
+
* values:
|
|
284
|
+
* @returns The function `getNodeByKey` returns a node (`N`) if a node with the specified key is
|
|
285
|
+
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
286
|
+
*/
|
|
287
|
+
getNodeByKey(key, iterationType = types_1.IterationType.ITERATIVE) {
|
|
288
|
+
if (!this.root)
|
|
289
|
+
return undefined;
|
|
290
|
+
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
291
|
+
const _dfs = (cur) => {
|
|
292
|
+
if (cur.key === key)
|
|
293
|
+
return cur;
|
|
294
|
+
if (!cur.left && !cur.right)
|
|
295
|
+
return;
|
|
296
|
+
if (this._compare(cur.key, key) === types_1.CP.gt && cur.left)
|
|
297
|
+
return _dfs(cur.left);
|
|
298
|
+
if (this._compare(cur.key, key) === types_1.CP.lt && cur.right)
|
|
299
|
+
return _dfs(cur.right);
|
|
300
|
+
};
|
|
301
|
+
return _dfs(this.root);
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
const queue = new queue_1.Queue([this.root]);
|
|
305
|
+
while (queue.size > 0) {
|
|
306
|
+
const cur = queue.shift();
|
|
307
|
+
if (cur) {
|
|
308
|
+
if (this._compare(cur.key, key) === types_1.CP.eq)
|
|
309
|
+
return cur;
|
|
310
|
+
if (this._compare(cur.key, key) === types_1.CP.gt)
|
|
311
|
+
cur.left && queue.push(cur.left);
|
|
312
|
+
if (this._compare(cur.key, key) === types_1.CP.lt)
|
|
313
|
+
cur.right && queue.push(cur.right);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* The function `ensureNotKey` returns the node corresponding to the given key if it is a node key,
|
|
320
|
+
* otherwise it returns the key itself.
|
|
321
|
+
* @param {BTNKey | N | undefined} key - The `key` parameter can be of type `BTNKey`, `N`, or
|
|
322
|
+
* `undefined`.
|
|
323
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
324
|
+
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
325
|
+
* @returns either a node object (N) or undefined.
|
|
326
|
+
*/
|
|
327
|
+
ensureNotKey(key, iterationType = types_1.IterationType.ITERATIVE) {
|
|
328
|
+
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
329
|
+
}
|
|
280
330
|
/**
|
|
281
331
|
* The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
|
|
282
332
|
* using either recursive or iterative traversal.
|
|
@@ -285,7 +335,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
285
335
|
* generic type `N`.
|
|
286
336
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
287
337
|
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
288
|
-
* included in the result. The default value for `callback` is `this.
|
|
338
|
+
* included in the result. The default value for `callback` is `this._defaultOneParamCallback`, which is
|
|
289
339
|
* a
|
|
290
340
|
* @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
|
|
291
341
|
* the first node that matches the nodeProperty. If set to true, the function will return an array
|
|
@@ -298,7 +348,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
298
348
|
* traverse the binary tree. It can have one of the following values:
|
|
299
349
|
* @returns an array of nodes (N[]).
|
|
300
350
|
*/
|
|
301
|
-
getNodes(identifier, callback = this.
|
|
351
|
+
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
352
|
+
beginRoot = this.ensureNotKey(beginRoot);
|
|
302
353
|
if (!beginRoot)
|
|
303
354
|
return [];
|
|
304
355
|
const ans = [];
|
|
@@ -313,7 +364,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
313
364
|
if (!cur.left && !cur.right)
|
|
314
365
|
return;
|
|
315
366
|
// TODO potential bug
|
|
316
|
-
if (callback === this.
|
|
367
|
+
if (callback === this._defaultOneParamCallback) {
|
|
317
368
|
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
318
369
|
cur.left && _traverse(cur.left);
|
|
319
370
|
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
@@ -338,7 +389,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
338
389
|
return ans;
|
|
339
390
|
}
|
|
340
391
|
// TODO potential bug
|
|
341
|
-
if (callback === this.
|
|
392
|
+
if (callback === this._defaultOneParamCallback) {
|
|
342
393
|
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
343
394
|
cur.left && queue.push(cur.left);
|
|
344
395
|
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
@@ -371,16 +422,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
371
422
|
* done recursively or iteratively. It can have two possible values:
|
|
372
423
|
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
373
424
|
*/
|
|
374
|
-
lesserOrGreaterTraverse(callback = this.
|
|
375
|
-
|
|
376
|
-
if (typeof targetNode === 'number')
|
|
377
|
-
targetNode = (_a = this.getNode(targetNode)) !== null && _a !== void 0 ? _a : undefined;
|
|
425
|
+
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
426
|
+
targetNode = this.ensureNotKey(targetNode);
|
|
378
427
|
const ans = [];
|
|
379
428
|
if (!targetNode)
|
|
380
429
|
return ans;
|
|
381
|
-
const targetKey = targetNode.key;
|
|
382
430
|
if (!this.root)
|
|
383
431
|
return ans;
|
|
432
|
+
const targetKey = targetNode.key;
|
|
384
433
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
385
434
|
const _traverse = (cur) => {
|
|
386
435
|
const compared = this._compare(cur.key, targetKey);
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
8
|
+
import { BiTreeDeleteResult, BTNCallback, BTNKey, IterationType, RBTNColor, RedBlackTreeNodeNested, RBTreeOptions } from '../../types';
|
|
9
9
|
import { BST, BSTNode } from "./bst";
|
|
10
10
|
import { IBinaryTree } from "../../interfaces";
|
|
11
|
-
export declare class
|
|
11
|
+
export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
12
12
|
color: RBTNColor;
|
|
13
13
|
constructor(key: BTNKey, value?: V, color?: RBTNColor);
|
|
14
14
|
}
|
|
@@ -19,74 +19,81 @@ export declare class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNode
|
|
|
19
19
|
* 4. Red nodes must have black children.
|
|
20
20
|
* 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
|
|
21
21
|
*/
|
|
22
|
-
export declare class RedBlackTree<V = any, N extends
|
|
22
|
+
export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNode<V, RedBlackTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
|
|
23
23
|
constructor(options?: RBTreeOptions);
|
|
24
24
|
protected _root: N;
|
|
25
25
|
get root(): N;
|
|
26
26
|
protected _size: number;
|
|
27
27
|
get size(): number;
|
|
28
28
|
NIL: N;
|
|
29
|
+
/**
|
|
30
|
+
* The `add` function adds a new node to a Red-Black Tree data structure.
|
|
31
|
+
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
32
|
+
* following types:
|
|
33
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
34
|
+
* key in the node being added to the Red-Black Tree.
|
|
35
|
+
* @returns The method returns either a node (`N`) or `undefined`.
|
|
36
|
+
*/
|
|
29
37
|
add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined;
|
|
30
38
|
createNode(key: BTNKey, value?: V, color?: RBTNColor): N;
|
|
31
|
-
|
|
32
|
-
|
|
39
|
+
/**
|
|
40
|
+
* The `delete` function removes a node from a binary tree based on a given identifier and updates
|
|
41
|
+
* the tree accordingly.
|
|
42
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
43
|
+
* that you want to use to identify the node that you want to delete from the binary tree. It can be
|
|
44
|
+
* of any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
|
|
45
|
+
* you don't want to
|
|
46
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` and
|
|
47
|
+
* returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
|
|
48
|
+
* on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
|
|
49
|
+
* @returns an array of `BiTreeDeleteResult<N>`.
|
|
50
|
+
*/
|
|
51
|
+
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback?: C): BiTreeDeleteResult<N>[];
|
|
52
|
+
isRealNode(node: N | undefined): node is N;
|
|
33
53
|
getNode<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
34
54
|
getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
35
55
|
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
36
|
-
/**
|
|
37
|
-
* The function returns the leftmost node in a red-black tree.
|
|
38
|
-
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode, which represents a node in
|
|
39
|
-
* a Red-Black Tree.
|
|
40
|
-
* @returns The leftmost node in the given RBTreeNode.
|
|
41
|
-
*/
|
|
42
|
-
getLeftMost(node?: N): N;
|
|
43
|
-
/**
|
|
44
|
-
* The function returns the rightmost node in a red-black tree.
|
|
45
|
-
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
|
|
46
|
-
* @returns the rightmost node in a red-black tree.
|
|
47
|
-
*/
|
|
48
|
-
getRightMost(node: N): N;
|
|
49
56
|
/**
|
|
50
57
|
* The function returns the successor of a given node in a red-black tree.
|
|
51
|
-
* @param {
|
|
52
|
-
* @returns the successor of the given
|
|
58
|
+
* @param {RedBlackTreeNode} x - RedBlackTreeNode - The node for which we want to find the successor.
|
|
59
|
+
* @returns the successor of the given RedBlackTreeNode.
|
|
53
60
|
*/
|
|
54
61
|
getSuccessor(x: N): N | undefined;
|
|
55
62
|
/**
|
|
56
63
|
* The function returns the predecessor of a given node in a red-black tree.
|
|
57
|
-
* @param {
|
|
64
|
+
* @param {RedBlackTreeNode} x - The parameter `x` is of type `RedBlackTreeNode`, which represents a node in a
|
|
58
65
|
* Red-Black Tree.
|
|
59
|
-
* @returns the predecessor of the given
|
|
66
|
+
* @returns the predecessor of the given RedBlackTreeNode 'x'.
|
|
60
67
|
*/
|
|
61
68
|
getPredecessor(x: N): N;
|
|
62
69
|
clear(): void;
|
|
63
70
|
protected _setRoot(v: N): void;
|
|
64
71
|
/**
|
|
65
72
|
* The function performs a left rotation on a red-black tree node.
|
|
66
|
-
* @param {
|
|
73
|
+
* @param {RedBlackTreeNode} x - The parameter `x` is a RedBlackTreeNode object.
|
|
67
74
|
*/
|
|
68
75
|
protected _leftRotate(x: N): void;
|
|
69
76
|
/**
|
|
70
77
|
* The function performs a right rotation on a red-black tree node.
|
|
71
|
-
* @param {
|
|
78
|
+
* @param {RedBlackTreeNode} x - x is a RedBlackTreeNode, which represents the node that needs to be right
|
|
72
79
|
* rotated.
|
|
73
80
|
*/
|
|
74
81
|
protected _rightRotate(x: N): void;
|
|
75
82
|
/**
|
|
76
83
|
* The _fixDelete function is used to rebalance the Red-Black Tree after a node deletion.
|
|
77
|
-
* @param {
|
|
84
|
+
* @param {RedBlackTreeNode} x - The parameter `x` is of type `RedBlackTreeNode`, which represents a node in a
|
|
78
85
|
* red-black tree.
|
|
79
86
|
*/
|
|
80
87
|
protected _fixDelete(x: N): void;
|
|
81
88
|
/**
|
|
82
89
|
* The function `_rbTransplant` replaces one node in a red-black tree with another node.
|
|
83
|
-
* @param {
|
|
84
|
-
* @param {
|
|
90
|
+
* @param {RedBlackTreeNode} u - The parameter "u" represents a RedBlackTreeNode object.
|
|
91
|
+
* @param {RedBlackTreeNode} v - The parameter "v" is a RedBlackTreeNode object.
|
|
85
92
|
*/
|
|
86
93
|
protected _rbTransplant(u: N, v: N): void;
|
|
87
94
|
/**
|
|
88
95
|
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
89
|
-
* @param {
|
|
96
|
+
* @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
|
|
90
97
|
* red-black tree.
|
|
91
98
|
*/
|
|
92
99
|
protected _fixInsert(k: N): void;
|
|
@@ -7,17 +7,17 @@
|
|
|
7
7
|
* @license MIT License
|
|
8
8
|
*/
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.RedBlackTree = exports.
|
|
10
|
+
exports.RedBlackTree = exports.RedBlackTreeNode = void 0;
|
|
11
11
|
const types_1 = require("../../types");
|
|
12
12
|
const bst_1 = require("./bst");
|
|
13
13
|
const binary_tree_1 = require("./binary-tree");
|
|
14
|
-
class
|
|
14
|
+
class RedBlackTreeNode extends bst_1.BSTNode {
|
|
15
15
|
constructor(key, value, color = types_1.RBTNColor.BLACK) {
|
|
16
16
|
super(key, value);
|
|
17
17
|
this.color = color;
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
|
-
exports.
|
|
20
|
+
exports.RedBlackTreeNode = RedBlackTreeNode;
|
|
21
21
|
/**
|
|
22
22
|
* 1. Each node is either red or black.
|
|
23
23
|
* 2. The root node is always black.
|
|
@@ -29,7 +29,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
29
29
|
constructor(options) {
|
|
30
30
|
super(options);
|
|
31
31
|
this._size = 0;
|
|
32
|
-
this.NIL = new
|
|
32
|
+
this.NIL = new RedBlackTreeNode(NaN);
|
|
33
33
|
this._root = this.NIL;
|
|
34
34
|
}
|
|
35
35
|
get root() {
|
|
@@ -38,12 +38,20 @@ class RedBlackTree extends bst_1.BST {
|
|
|
38
38
|
get size() {
|
|
39
39
|
return this._size;
|
|
40
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* The `add` function adds a new node to a Red-Black Tree data structure.
|
|
43
|
+
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
44
|
+
* following types:
|
|
45
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
46
|
+
* key in the node being added to the Red-Black Tree.
|
|
47
|
+
* @returns The method returns either a node (`N`) or `undefined`.
|
|
48
|
+
*/
|
|
41
49
|
add(keyOrNode, value) {
|
|
42
50
|
let node;
|
|
43
|
-
if (
|
|
51
|
+
if (this.isNodeKey(keyOrNode)) {
|
|
44
52
|
node = this.createNode(keyOrNode, value, types_1.RBTNColor.RED);
|
|
45
53
|
}
|
|
46
|
-
else if (keyOrNode instanceof
|
|
54
|
+
else if (keyOrNode instanceof RedBlackTreeNode) {
|
|
47
55
|
node = keyOrNode;
|
|
48
56
|
}
|
|
49
57
|
else if (keyOrNode === null) {
|
|
@@ -91,9 +99,21 @@ class RedBlackTree extends bst_1.BST {
|
|
|
91
99
|
this._size++;
|
|
92
100
|
}
|
|
93
101
|
createNode(key, value, color = types_1.RBTNColor.BLACK) {
|
|
94
|
-
return new
|
|
102
|
+
return new RedBlackTreeNode(key, value, color);
|
|
95
103
|
}
|
|
96
|
-
|
|
104
|
+
/**
|
|
105
|
+
* The `delete` function removes a node from a binary tree based on a given identifier and updates
|
|
106
|
+
* the tree accordingly.
|
|
107
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
108
|
+
* that you want to use to identify the node that you want to delete from the binary tree. It can be
|
|
109
|
+
* of any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
|
|
110
|
+
* you don't want to
|
|
111
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` and
|
|
112
|
+
* returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
|
|
113
|
+
* on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
|
|
114
|
+
* @returns an array of `BiTreeDeleteResult<N>`.
|
|
115
|
+
*/
|
|
116
|
+
delete(identifier, callback = this._defaultOneParamCallback) {
|
|
97
117
|
const ans = [];
|
|
98
118
|
if (identifier === null)
|
|
99
119
|
return ans;
|
|
@@ -151,7 +171,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
151
171
|
// TODO
|
|
152
172
|
return ans;
|
|
153
173
|
}
|
|
154
|
-
|
|
174
|
+
isRealNode(node) {
|
|
155
175
|
return node !== this.NIL && node !== undefined;
|
|
156
176
|
}
|
|
157
177
|
/**
|
|
@@ -162,50 +182,29 @@ class RedBlackTree extends bst_1.BST {
|
|
|
162
182
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
163
183
|
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
164
184
|
* whether the node matches the criteria or not. The default callback function
|
|
165
|
-
* (`this.
|
|
185
|
+
* (`this._defaultOneParamCallback`) is used if no callback function is
|
|
166
186
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
167
187
|
* the root node from which the search should begin.
|
|
168
188
|
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
169
189
|
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
170
190
|
* @returns either the found node (of type N) or null if no node is found.
|
|
171
191
|
*/
|
|
172
|
-
getNode(identifier, callback = this.
|
|
192
|
+
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
173
193
|
var _a;
|
|
174
194
|
if (identifier instanceof binary_tree_1.BinaryTreeNode)
|
|
175
195
|
callback = (node => node);
|
|
196
|
+
beginRoot = this.ensureNotKey(beginRoot);
|
|
176
197
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
177
198
|
}
|
|
178
|
-
/**
|
|
179
|
-
* The function returns the leftmost node in a red-black tree.
|
|
180
|
-
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode, which represents a node in
|
|
181
|
-
* a Red-Black Tree.
|
|
182
|
-
* @returns The leftmost node in the given RBTreeNode.
|
|
183
|
-
*/
|
|
184
|
-
getLeftMost(node = this.root) {
|
|
185
|
-
while (node.left !== undefined && node.left !== this.NIL) {
|
|
186
|
-
node = node.left;
|
|
187
|
-
}
|
|
188
|
-
return node;
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* The function returns the rightmost node in a red-black tree.
|
|
192
|
-
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
|
|
193
|
-
* @returns the rightmost node in a red-black tree.
|
|
194
|
-
*/
|
|
195
|
-
getRightMost(node) {
|
|
196
|
-
while (node.right !== undefined && node.right !== this.NIL) {
|
|
197
|
-
node = node.right;
|
|
198
|
-
}
|
|
199
|
-
return node;
|
|
200
|
-
}
|
|
201
199
|
/**
|
|
202
200
|
* The function returns the successor of a given node in a red-black tree.
|
|
203
|
-
* @param {
|
|
204
|
-
* @returns the successor of the given
|
|
201
|
+
* @param {RedBlackTreeNode} x - RedBlackTreeNode - The node for which we want to find the successor.
|
|
202
|
+
* @returns the successor of the given RedBlackTreeNode.
|
|
205
203
|
*/
|
|
206
204
|
getSuccessor(x) {
|
|
205
|
+
var _a;
|
|
207
206
|
if (x.right !== this.NIL) {
|
|
208
|
-
return this.getLeftMost(x.right);
|
|
207
|
+
return (_a = this.getLeftMost(x.right)) !== null && _a !== void 0 ? _a : undefined;
|
|
209
208
|
}
|
|
210
209
|
let y = x.parent;
|
|
211
210
|
while (y !== this.NIL && y !== undefined && x === y.right) {
|
|
@@ -216,9 +215,9 @@ class RedBlackTree extends bst_1.BST {
|
|
|
216
215
|
}
|
|
217
216
|
/**
|
|
218
217
|
* The function returns the predecessor of a given node in a red-black tree.
|
|
219
|
-
* @param {
|
|
218
|
+
* @param {RedBlackTreeNode} x - The parameter `x` is of type `RedBlackTreeNode`, which represents a node in a
|
|
220
219
|
* Red-Black Tree.
|
|
221
|
-
* @returns the predecessor of the given
|
|
220
|
+
* @returns the predecessor of the given RedBlackTreeNode 'x'.
|
|
222
221
|
*/
|
|
223
222
|
getPredecessor(x) {
|
|
224
223
|
if (x.left !== this.NIL) {
|
|
@@ -243,7 +242,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
243
242
|
}
|
|
244
243
|
/**
|
|
245
244
|
* The function performs a left rotation on a red-black tree node.
|
|
246
|
-
* @param {
|
|
245
|
+
* @param {RedBlackTreeNode} x - The parameter `x` is a RedBlackTreeNode object.
|
|
247
246
|
*/
|
|
248
247
|
_leftRotate(x) {
|
|
249
248
|
if (x.right) {
|
|
@@ -269,7 +268,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
269
268
|
}
|
|
270
269
|
/**
|
|
271
270
|
* The function performs a right rotation on a red-black tree node.
|
|
272
|
-
* @param {
|
|
271
|
+
* @param {RedBlackTreeNode} x - x is a RedBlackTreeNode, which represents the node that needs to be right
|
|
273
272
|
* rotated.
|
|
274
273
|
*/
|
|
275
274
|
_rightRotate(x) {
|
|
@@ -296,7 +295,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
296
295
|
}
|
|
297
296
|
/**
|
|
298
297
|
* The _fixDelete function is used to rebalance the Red-Black Tree after a node deletion.
|
|
299
|
-
* @param {
|
|
298
|
+
* @param {RedBlackTreeNode} x - The parameter `x` is of type `RedBlackTreeNode`, which represents a node in a
|
|
300
299
|
* red-black tree.
|
|
301
300
|
*/
|
|
302
301
|
_fixDelete(x) {
|
|
@@ -365,8 +364,8 @@ class RedBlackTree extends bst_1.BST {
|
|
|
365
364
|
}
|
|
366
365
|
/**
|
|
367
366
|
* The function `_rbTransplant` replaces one node in a red-black tree with another node.
|
|
368
|
-
* @param {
|
|
369
|
-
* @param {
|
|
367
|
+
* @param {RedBlackTreeNode} u - The parameter "u" represents a RedBlackTreeNode object.
|
|
368
|
+
* @param {RedBlackTreeNode} v - The parameter "v" is a RedBlackTreeNode object.
|
|
370
369
|
*/
|
|
371
370
|
_rbTransplant(u, v) {
|
|
372
371
|
if (u.parent === undefined) {
|
|
@@ -382,7 +381,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
382
381
|
}
|
|
383
382
|
/**
|
|
384
383
|
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
385
|
-
* @param {
|
|
384
|
+
* @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
|
|
386
385
|
* red-black tree.
|
|
387
386
|
*/
|
|
388
387
|
_fixInsert(k) {
|