min-heap-typed 1.48.9 → 1.49.0
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 +8 -1
- package/dist/data-structures/binary-tree/avl-tree.js +9 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +15 -6
- package/dist/data-structures/binary-tree/binary-tree.js +19 -15
- package/dist/data-structures/binary-tree/bst.d.ts +8 -1
- package/dist/data-structures/binary-tree/bst.js +9 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +10 -16
- package/dist/data-structures/binary-tree/rb-tree.js +16 -29
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +8 -1
- package/dist/data-structures/binary-tree/tree-multimap.js +9 -0
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +11 -1
- package/src/data-structures/binary-tree/binary-tree.ts +22 -23
- package/src/data-structures/binary-tree/bst.ts +11 -1
- package/src/data-structures/binary-tree/rb-tree.ts +18 -32
- package/src/data-structures/binary-tree/tree-multimap.ts +17 -1
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
9
|
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BSTNodeKeyOrNode, BTNodeExemplar } from '../../types';
|
|
10
|
-
import { BTNCallback } from '../../types';
|
|
10
|
+
import { BTNCallback, BTNodeKeyOrNode } from '../../types';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
export declare class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
|
|
13
13
|
height: number;
|
|
@@ -58,6 +58,13 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
|
|
|
58
58
|
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
|
|
59
59
|
*/
|
|
60
60
|
isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
|
|
61
|
+
/**
|
|
62
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
63
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
64
|
+
* data type.
|
|
65
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
66
|
+
*/
|
|
67
|
+
isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K;
|
|
61
68
|
/**
|
|
62
69
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
63
70
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -71,6 +71,15 @@ class AVLTree extends bst_1.BST {
|
|
|
71
71
|
isNode(exemplar) {
|
|
72
72
|
return exemplar instanceof AVLTreeNode;
|
|
73
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
76
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
77
|
+
* data type.
|
|
78
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
79
|
+
*/
|
|
80
|
+
isNotNodeInstance(potentialKey) {
|
|
81
|
+
return !(potentialKey instanceof AVLTreeNode);
|
|
82
|
+
}
|
|
74
83
|
/**
|
|
75
84
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
76
85
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -385,21 +385,21 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
|
|
|
385
385
|
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
386
386
|
* @returns a boolean value.
|
|
387
387
|
*/
|
|
388
|
-
isRealNode(node:
|
|
388
|
+
isRealNode(node: BTNodeExemplar<K, V, N>): node is N;
|
|
389
389
|
/**
|
|
390
390
|
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
391
391
|
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
392
392
|
* @returns a boolean value.
|
|
393
393
|
*/
|
|
394
|
-
isNIL(node:
|
|
394
|
+
isNIL(node: BTNodeExemplar<K, V, N>): boolean;
|
|
395
395
|
/**
|
|
396
396
|
* The function checks if a given node is a real node or null.
|
|
397
397
|
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
398
398
|
* @returns a boolean value.
|
|
399
399
|
*/
|
|
400
|
-
isNodeOrNull(node:
|
|
400
|
+
isNodeOrNull(node: BTNodeExemplar<K, V, N>): node is N | null;
|
|
401
401
|
/**
|
|
402
|
-
* The function "isNotNodeInstance" checks if a potential key is a
|
|
402
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
403
403
|
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
404
404
|
* data type.
|
|
405
405
|
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
@@ -423,8 +423,17 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
|
|
|
423
423
|
listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNodeKeyOrNode<K, N>, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[][];
|
|
424
424
|
listLevels<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: BTNodeKeyOrNode<K, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][];
|
|
425
425
|
/**
|
|
426
|
-
* Time
|
|
427
|
-
* Space
|
|
426
|
+
* Time Complexity: O(log n)
|
|
427
|
+
* Space Complexity: O(1)
|
|
428
|
+
*/
|
|
429
|
+
/**
|
|
430
|
+
* Time Complexity: O(log n)
|
|
431
|
+
* Space Complexity: O(1)
|
|
432
|
+
*
|
|
433
|
+
* The function returns the predecessor of a given node in a tree.
|
|
434
|
+
* @param {N} node - The parameter `node` is of type `RedBlackTreeNode`, which represents a node in a
|
|
435
|
+
* tree.
|
|
436
|
+
* @returns the predecessor of the given 'node'.
|
|
428
437
|
*/
|
|
429
438
|
getPredecessor(node: N): N;
|
|
430
439
|
/**
|
|
@@ -980,7 +980,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
980
980
|
* @returns a boolean value.
|
|
981
981
|
*/
|
|
982
982
|
isRealNode(node) {
|
|
983
|
-
return node instanceof BinaryTreeNode && node.key
|
|
983
|
+
return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
|
|
984
984
|
}
|
|
985
985
|
/**
|
|
986
986
|
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
@@ -988,7 +988,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
988
988
|
* @returns a boolean value.
|
|
989
989
|
*/
|
|
990
990
|
isNIL(node) {
|
|
991
|
-
return node instanceof BinaryTreeNode && node.key
|
|
991
|
+
return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
|
|
992
992
|
}
|
|
993
993
|
/**
|
|
994
994
|
* The function checks if a given node is a real node or null.
|
|
@@ -999,7 +999,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
999
999
|
return this.isRealNode(node) || node === null;
|
|
1000
1000
|
}
|
|
1001
1001
|
/**
|
|
1002
|
-
* The function "isNotNodeInstance" checks if a potential key is a
|
|
1002
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
1003
1003
|
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
1004
1004
|
* data type.
|
|
1005
1005
|
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
@@ -1280,19 +1280,23 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1280
1280
|
return levelsNodes;
|
|
1281
1281
|
}
|
|
1282
1282
|
/**
|
|
1283
|
-
*
|
|
1284
|
-
*
|
|
1285
|
-
|
|
1286
|
-
|
|
1283
|
+
* Time Complexity: O(log n)
|
|
1284
|
+
* Space Complexity: O(1)
|
|
1285
|
+
*/
|
|
1286
|
+
/**
|
|
1287
|
+
* Time Complexity: O(log n)
|
|
1288
|
+
* Space Complexity: O(1)
|
|
1289
|
+
*
|
|
1290
|
+
* The function returns the predecessor of a given node in a tree.
|
|
1291
|
+
* @param {N} node - The parameter `node` is of type `RedBlackTreeNode`, which represents a node in a
|
|
1292
|
+
* tree.
|
|
1293
|
+
* @returns the predecessor of the given 'node'.
|
|
1287
1294
|
*/
|
|
1288
1295
|
getPredecessor(node) {
|
|
1289
|
-
|
|
1290
|
-
if (!this.isRealNode(node))
|
|
1291
|
-
return undefined;
|
|
1292
|
-
if (node.left) {
|
|
1296
|
+
if (this.isRealNode(node.left)) {
|
|
1293
1297
|
let predecessor = node.left;
|
|
1294
1298
|
while (!this.isRealNode(predecessor) || (this.isRealNode(predecessor.right) && predecessor.right !== node)) {
|
|
1295
|
-
if (predecessor) {
|
|
1299
|
+
if (this.isRealNode(predecessor)) {
|
|
1296
1300
|
predecessor = predecessor.right;
|
|
1297
1301
|
}
|
|
1298
1302
|
}
|
|
@@ -1310,13 +1314,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1310
1314
|
*/
|
|
1311
1315
|
getSuccessor(x) {
|
|
1312
1316
|
x = this.ensureNode(x);
|
|
1313
|
-
if (!x)
|
|
1317
|
+
if (!this.isRealNode(x))
|
|
1314
1318
|
return undefined;
|
|
1315
|
-
if (x.right) {
|
|
1319
|
+
if (this.isRealNode(x.right)) {
|
|
1316
1320
|
return this.getLeftMost(x.right);
|
|
1317
1321
|
}
|
|
1318
1322
|
let y = x.parent;
|
|
1319
|
-
while (y &&
|
|
1323
|
+
while (this.isRealNode(y) && x === y.right) {
|
|
1320
1324
|
x = y;
|
|
1321
1325
|
y = y.parent;
|
|
1322
1326
|
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BSTNested, BSTNodeKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, BTNodeExemplar } from '../../types';
|
|
9
|
-
import { BSTVariant, CP, IterationType } from '../../types';
|
|
9
|
+
import { BSTVariant, BTNodeKeyOrNode, CP, IterationType } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
export declare class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, N> {
|
|
@@ -169,6 +169,13 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
169
169
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
170
170
|
*/
|
|
171
171
|
getNodeByKey(key: K, iterationType?: IterationType): N | undefined;
|
|
172
|
+
/**
|
|
173
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
174
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
175
|
+
* data type.
|
|
176
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
177
|
+
*/
|
|
178
|
+
isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K;
|
|
172
179
|
/**
|
|
173
180
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
174
181
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -387,6 +387,15 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
387
387
|
}
|
|
388
388
|
}
|
|
389
389
|
}
|
|
390
|
+
/**
|
|
391
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
392
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
393
|
+
* data type.
|
|
394
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
395
|
+
*/
|
|
396
|
+
isNotNodeInstance(potentialKey) {
|
|
397
|
+
return !(potentialKey instanceof BSTNode);
|
|
398
|
+
}
|
|
390
399
|
/**
|
|
391
400
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
392
401
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { BiTreeDeleteResult, BTNCallback, BTNodeExemplar, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
8
|
+
import { BiTreeDeleteResult, BTNCallback, BTNodeExemplar, BTNodeKeyOrNode, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
9
9
|
import { BST, BSTNode } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class RedBlackTreeNode<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
|
|
@@ -65,6 +65,13 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
65
65
|
* class.
|
|
66
66
|
*/
|
|
67
67
|
isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
|
|
68
|
+
/**
|
|
69
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
70
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
71
|
+
* data type.
|
|
72
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
73
|
+
*/
|
|
74
|
+
isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K;
|
|
68
75
|
/**
|
|
69
76
|
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
70
77
|
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
|
|
@@ -120,24 +127,11 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
120
127
|
getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
121
128
|
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
122
129
|
/**
|
|
123
|
-
* Time Complexity: O(log n)
|
|
130
|
+
* Time Complexity: O(log n)
|
|
124
131
|
* Space Complexity: O(1)
|
|
125
132
|
*/
|
|
126
133
|
/**
|
|
127
|
-
* Time Complexity: O(log n)
|
|
128
|
-
* Space Complexity: O(1)
|
|
129
|
-
*
|
|
130
|
-
* The function returns the successor of a given node in a red-black tree.
|
|
131
|
-
* @param {RedBlackTreeNode} x - RedBlackTreeNode - The node for which we want to find the successor.
|
|
132
|
-
* @returns the successor of the given RedBlackTreeNode.
|
|
133
|
-
*/
|
|
134
|
-
getSuccessor(x: N): N | undefined;
|
|
135
|
-
/**
|
|
136
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
137
|
-
* Space Complexity: O(1)
|
|
138
|
-
*/
|
|
139
|
-
/**
|
|
140
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
134
|
+
* Time Complexity: O(log n)
|
|
141
135
|
* Space Complexity: O(1)
|
|
142
136
|
*
|
|
143
137
|
* The function returns the predecessor of a given node in a red-black tree.
|
|
@@ -85,6 +85,15 @@ class RedBlackTree extends bst_1.BST {
|
|
|
85
85
|
isNode(exemplar) {
|
|
86
86
|
return exemplar instanceof RedBlackTreeNode;
|
|
87
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
90
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
91
|
+
* data type.
|
|
92
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
93
|
+
*/
|
|
94
|
+
isNotNodeInstance(potentialKey) {
|
|
95
|
+
return !(potentialKey instanceof RedBlackTreeNode);
|
|
96
|
+
}
|
|
88
97
|
/**
|
|
89
98
|
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
90
99
|
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
|
|
@@ -263,7 +272,9 @@ class RedBlackTree extends bst_1.BST {
|
|
|
263
272
|
* Space Complexity: O(1)
|
|
264
273
|
*/
|
|
265
274
|
isRealNode(node) {
|
|
266
|
-
|
|
275
|
+
if (node === this.Sentinel || node === undefined)
|
|
276
|
+
return false;
|
|
277
|
+
return node instanceof RedBlackTreeNode;
|
|
267
278
|
}
|
|
268
279
|
/**
|
|
269
280
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
@@ -298,35 +309,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
298
309
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
299
310
|
}
|
|
300
311
|
/**
|
|
301
|
-
* Time Complexity: O(log n)
|
|
302
|
-
* Space Complexity: O(1)
|
|
303
|
-
*/
|
|
304
|
-
/**
|
|
305
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
306
|
-
* Space Complexity: O(1)
|
|
307
|
-
*
|
|
308
|
-
* The function returns the successor of a given node in a red-black tree.
|
|
309
|
-
* @param {RedBlackTreeNode} x - RedBlackTreeNode - The node for which we want to find the successor.
|
|
310
|
-
* @returns the successor of the given RedBlackTreeNode.
|
|
311
|
-
*/
|
|
312
|
-
getSuccessor(x) {
|
|
313
|
-
var _a;
|
|
314
|
-
if (x.right !== this.Sentinel) {
|
|
315
|
-
return (_a = this.getLeftMost(x.right)) !== null && _a !== void 0 ? _a : undefined;
|
|
316
|
-
}
|
|
317
|
-
let y = x.parent;
|
|
318
|
-
while (y !== this.Sentinel && y !== undefined && x === y.right) {
|
|
319
|
-
x = y;
|
|
320
|
-
y = y.parent;
|
|
321
|
-
}
|
|
322
|
-
return y;
|
|
323
|
-
}
|
|
324
|
-
/**
|
|
325
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
312
|
+
* Time Complexity: O(log n)
|
|
326
313
|
* Space Complexity: O(1)
|
|
327
314
|
*/
|
|
328
315
|
/**
|
|
329
|
-
* Time Complexity: O(log n)
|
|
316
|
+
* Time Complexity: O(log n)
|
|
330
317
|
* Space Complexity: O(1)
|
|
331
318
|
*
|
|
332
319
|
* The function returns the predecessor of a given node in a red-black tree.
|
|
@@ -335,11 +322,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
335
322
|
* @returns the predecessor of the given RedBlackTreeNode 'x'.
|
|
336
323
|
*/
|
|
337
324
|
getPredecessor(x) {
|
|
338
|
-
if (x.left
|
|
325
|
+
if (this.isRealNode(x.left)) {
|
|
339
326
|
return this.getRightMost(x.left);
|
|
340
327
|
}
|
|
341
328
|
let y = x.parent;
|
|
342
|
-
while (
|
|
329
|
+
while (this.isRealNode(y) && x === y.left) {
|
|
343
330
|
x = y;
|
|
344
331
|
y = y.parent;
|
|
345
332
|
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BSTNodeKeyOrNode, BTNodeExemplar, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
|
|
9
|
-
import { BiTreeDeleteResult, BTNCallback, IterationType, TreeMultimapNested } from '../../types';
|
|
9
|
+
import { BiTreeDeleteResult, BTNCallback, BTNodeKeyOrNode, IterationType, TreeMultimapNested } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultimapNode<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNodeNested<K, V>> extends AVLTreeNode<K, V, N> {
|
|
@@ -48,6 +48,13 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
48
48
|
* class.
|
|
49
49
|
*/
|
|
50
50
|
isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
|
|
51
|
+
/**
|
|
52
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
53
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
54
|
+
* data type.
|
|
55
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
56
|
+
*/
|
|
57
|
+
isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K;
|
|
51
58
|
/**
|
|
52
59
|
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
53
60
|
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, which means it
|
|
@@ -60,6 +60,15 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
60
60
|
isNode(exemplar) {
|
|
61
61
|
return exemplar instanceof TreeMultimapNode;
|
|
62
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
65
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
66
|
+
* data type.
|
|
67
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
68
|
+
*/
|
|
69
|
+
isNotNodeInstance(potentialKey) {
|
|
70
|
+
return !(potentialKey instanceof TreeMultimapNode);
|
|
71
|
+
}
|
|
63
72
|
/**
|
|
64
73
|
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
65
74
|
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, which means it
|
package/package.json
CHANGED
|
@@ -14,7 +14,7 @@ import type {
|
|
|
14
14
|
BSTNodeKeyOrNode,
|
|
15
15
|
BTNodeExemplar
|
|
16
16
|
} from '../../types';
|
|
17
|
-
import { BTNCallback } from '../../types';
|
|
17
|
+
import { BTNCallback, BTNodeKeyOrNode } from '../../types';
|
|
18
18
|
import { IBinaryTree } from '../../interfaces';
|
|
19
19
|
|
|
20
20
|
export class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
|
|
@@ -90,6 +90,16 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
|
|
|
90
90
|
return exemplar instanceof AVLTreeNode;
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
+
/**
|
|
94
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
95
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
96
|
+
* data type.
|
|
97
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
98
|
+
*/
|
|
99
|
+
override isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K {
|
|
100
|
+
return !(potentialKey instanceof AVLTreeNode)
|
|
101
|
+
}
|
|
102
|
+
|
|
93
103
|
/**
|
|
94
104
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
95
105
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -1236,8 +1236,8 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1236
1236
|
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
1237
1237
|
* @returns a boolean value.
|
|
1238
1238
|
*/
|
|
1239
|
-
isRealNode(node:
|
|
1240
|
-
return node instanceof BinaryTreeNode && node.key
|
|
1239
|
+
isRealNode(node: BTNodeExemplar<K, V, N>): node is N {
|
|
1240
|
+
return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
|
|
1241
1241
|
}
|
|
1242
1242
|
|
|
1243
1243
|
/**
|
|
@@ -1245,8 +1245,8 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1245
1245
|
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
1246
1246
|
* @returns a boolean value.
|
|
1247
1247
|
*/
|
|
1248
|
-
isNIL(node:
|
|
1249
|
-
return node instanceof BinaryTreeNode && node.key
|
|
1248
|
+
isNIL(node: BTNodeExemplar<K, V, N>) {
|
|
1249
|
+
return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
|
|
1250
1250
|
}
|
|
1251
1251
|
|
|
1252
1252
|
/**
|
|
@@ -1254,12 +1254,12 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1254
1254
|
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
1255
1255
|
* @returns a boolean value.
|
|
1256
1256
|
*/
|
|
1257
|
-
isNodeOrNull(node:
|
|
1257
|
+
isNodeOrNull(node: BTNodeExemplar<K, V, N>): node is N | null {
|
|
1258
1258
|
return this.isRealNode(node) || node === null;
|
|
1259
1259
|
}
|
|
1260
1260
|
|
|
1261
1261
|
/**
|
|
1262
|
-
* The function "isNotNodeInstance" checks if a potential key is a
|
|
1262
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
1263
1263
|
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
1264
1264
|
* data type.
|
|
1265
1265
|
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
@@ -1606,26 +1606,24 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1606
1606
|
}
|
|
1607
1607
|
|
|
1608
1608
|
/**
|
|
1609
|
-
* Time
|
|
1610
|
-
* Space
|
|
1609
|
+
* Time Complexity: O(log n)
|
|
1610
|
+
* Space Complexity: O(1)
|
|
1611
1611
|
*/
|
|
1612
1612
|
|
|
1613
|
-
getPredecessor(node: N): N;
|
|
1614
|
-
|
|
1615
1613
|
/**
|
|
1616
|
-
*
|
|
1617
|
-
*
|
|
1618
|
-
*
|
|
1619
|
-
*
|
|
1614
|
+
* Time Complexity: O(log n)
|
|
1615
|
+
* Space Complexity: O(1)
|
|
1616
|
+
*
|
|
1617
|
+
* The function returns the predecessor of a given node in a tree.
|
|
1618
|
+
* @param {N} node - The parameter `node` is of type `RedBlackTreeNode`, which represents a node in a
|
|
1619
|
+
* tree.
|
|
1620
|
+
* @returns the predecessor of the given 'node'.
|
|
1620
1621
|
*/
|
|
1621
|
-
getPredecessor(node:
|
|
1622
|
-
|
|
1623
|
-
if (!this.isRealNode(node)) return undefined;
|
|
1624
|
-
|
|
1625
|
-
if (node.left) {
|
|
1622
|
+
getPredecessor(node: N): N {
|
|
1623
|
+
if (this.isRealNode(node.left)) {
|
|
1626
1624
|
let predecessor: N | null | undefined = node.left;
|
|
1627
1625
|
while (!this.isRealNode(predecessor) || (this.isRealNode(predecessor.right) && predecessor.right !== node)) {
|
|
1628
|
-
if (predecessor) {
|
|
1626
|
+
if (this.isRealNode(predecessor)) {
|
|
1629
1627
|
predecessor = predecessor.right;
|
|
1630
1628
|
}
|
|
1631
1629
|
}
|
|
@@ -1642,15 +1640,16 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1642
1640
|
* after the given node in the inorder traversal of the binary tree.
|
|
1643
1641
|
*/
|
|
1644
1642
|
getSuccessor(x?: K | N | null): N | null | undefined {
|
|
1643
|
+
|
|
1645
1644
|
x = this.ensureNode(x);
|
|
1646
|
-
if (!x) return undefined;
|
|
1645
|
+
if (!this.isRealNode(x)) return undefined;
|
|
1647
1646
|
|
|
1648
|
-
if (x.right) {
|
|
1647
|
+
if (this.isRealNode(x.right)) {
|
|
1649
1648
|
return this.getLeftMost(x.right);
|
|
1650
1649
|
}
|
|
1651
1650
|
|
|
1652
1651
|
let y: N | null | undefined = x.parent;
|
|
1653
|
-
while (y &&
|
|
1652
|
+
while (this.isRealNode(y) && x === y.right) {
|
|
1654
1653
|
x = y;
|
|
1655
1654
|
y = y.parent;
|
|
1656
1655
|
}
|
|
@@ -14,7 +14,7 @@ import type {
|
|
|
14
14
|
BTNodeExemplar,
|
|
15
15
|
BTNodePureExemplar
|
|
16
16
|
} from '../../types';
|
|
17
|
-
import { BSTVariant, CP, IterationType } from '../../types';
|
|
17
|
+
import { BSTVariant, BTNodeKeyOrNode, CP, IterationType } from '../../types';
|
|
18
18
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
19
19
|
import { IBinaryTree } from '../../interfaces';
|
|
20
20
|
import { Queue } from '../queue';
|
|
@@ -442,6 +442,16 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
442
442
|
}
|
|
443
443
|
}
|
|
444
444
|
|
|
445
|
+
/**
|
|
446
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
447
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
448
|
+
* data type.
|
|
449
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
450
|
+
*/
|
|
451
|
+
override isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K {
|
|
452
|
+
return !(potentialKey instanceof BSTNode)
|
|
453
|
+
}
|
|
454
|
+
|
|
445
455
|
/**
|
|
446
456
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
447
457
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
BSTNodeKeyOrNode,
|
|
12
12
|
BTNCallback,
|
|
13
13
|
BTNodeExemplar,
|
|
14
|
+
BTNodeKeyOrNode,
|
|
14
15
|
IterationType,
|
|
15
16
|
RBTNColor,
|
|
16
17
|
RBTreeOptions,
|
|
@@ -115,6 +116,16 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
115
116
|
return exemplar instanceof RedBlackTreeNode;
|
|
116
117
|
}
|
|
117
118
|
|
|
119
|
+
/**
|
|
120
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
121
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
122
|
+
* data type.
|
|
123
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
124
|
+
*/
|
|
125
|
+
override isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K {
|
|
126
|
+
return !(potentialKey instanceof RedBlackTreeNode)
|
|
127
|
+
}
|
|
128
|
+
|
|
118
129
|
/**
|
|
119
130
|
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
120
131
|
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
|
|
@@ -300,7 +311,8 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
300
311
|
*/
|
|
301
312
|
|
|
302
313
|
override isRealNode(node: N | undefined): node is N {
|
|
303
|
-
|
|
314
|
+
if (node === this.Sentinel || node === undefined) return false;
|
|
315
|
+
return node instanceof RedBlackTreeNode;
|
|
304
316
|
}
|
|
305
317
|
|
|
306
318
|
getNode<C extends BTNCallback<N, K>>(
|
|
@@ -362,38 +374,12 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
362
374
|
}
|
|
363
375
|
|
|
364
376
|
/**
|
|
365
|
-
* Time Complexity: O(log n)
|
|
366
|
-
* Space Complexity: O(1)
|
|
367
|
-
*/
|
|
368
|
-
|
|
369
|
-
/**
|
|
370
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
371
|
-
* Space Complexity: O(1)
|
|
372
|
-
*
|
|
373
|
-
* The function returns the successor of a given node in a red-black tree.
|
|
374
|
-
* @param {RedBlackTreeNode} x - RedBlackTreeNode - The node for which we want to find the successor.
|
|
375
|
-
* @returns the successor of the given RedBlackTreeNode.
|
|
376
|
-
*/
|
|
377
|
-
override getSuccessor(x: N): N | undefined {
|
|
378
|
-
if (x.right !== this.Sentinel) {
|
|
379
|
-
return this.getLeftMost(x.right) ?? undefined;
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
let y: N | undefined = x.parent;
|
|
383
|
-
while (y !== this.Sentinel && y !== undefined && x === y.right) {
|
|
384
|
-
x = y;
|
|
385
|
-
y = y.parent;
|
|
386
|
-
}
|
|
387
|
-
return y;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
/**
|
|
391
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
377
|
+
* Time Complexity: O(log n)
|
|
392
378
|
* Space Complexity: O(1)
|
|
393
379
|
*/
|
|
394
380
|
|
|
395
381
|
/**
|
|
396
|
-
* Time Complexity: O(log n)
|
|
382
|
+
* Time Complexity: O(log n)
|
|
397
383
|
* Space Complexity: O(1)
|
|
398
384
|
*
|
|
399
385
|
* The function returns the predecessor of a given node in a red-black tree.
|
|
@@ -402,12 +388,12 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
402
388
|
* @returns the predecessor of the given RedBlackTreeNode 'x'.
|
|
403
389
|
*/
|
|
404
390
|
override getPredecessor(x: N): N {
|
|
405
|
-
if (x.left
|
|
406
|
-
return this.getRightMost(x.left
|
|
391
|
+
if (this.isRealNode(x.left)) {
|
|
392
|
+
return this.getRightMost(x.left)!;
|
|
407
393
|
}
|
|
408
394
|
|
|
409
395
|
let y: N | undefined = x.parent;
|
|
410
|
-
while (
|
|
396
|
+
while (this.isRealNode(y) && x === y.left) {
|
|
411
397
|
x = y!;
|
|
412
398
|
y = y!.parent;
|
|
413
399
|
}
|
|
@@ -6,7 +6,14 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BSTNodeKeyOrNode, BTNodeExemplar, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
BiTreeDeleteResult,
|
|
11
|
+
BTNCallback,
|
|
12
|
+
BTNodeKeyOrNode,
|
|
13
|
+
FamilyPosition,
|
|
14
|
+
IterationType,
|
|
15
|
+
TreeMultimapNested
|
|
16
|
+
} from '../../types';
|
|
10
17
|
import { IBinaryTree } from '../../interfaces';
|
|
11
18
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
19
|
|
|
@@ -85,6 +92,15 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
85
92
|
return exemplar instanceof TreeMultimapNode;
|
|
86
93
|
}
|
|
87
94
|
|
|
95
|
+
/**
|
|
96
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
97
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
98
|
+
* data type.
|
|
99
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
100
|
+
*/
|
|
101
|
+
override isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K {
|
|
102
|
+
return !(potentialKey instanceof TreeMultimapNode)
|
|
103
|
+
}
|
|
88
104
|
|
|
89
105
|
/**
|
|
90
106
|
* The function `exemplarToNode` converts an exemplar object into a node object.
|