min-heap-typed 1.48.8 → 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 -10
- package/dist/data-structures/binary-tree/binary-tree.js +55 -49
- 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/dist/data-structures/queue/deque.d.ts +0 -110
- package/dist/data-structures/queue/deque.js +1 -172
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +11 -1
- package/src/data-structures/binary-tree/binary-tree.ts +65 -56
- 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
- package/src/data-structures/queue/deque.ts +1 -191
|
@@ -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.
|
|
@@ -37,10 +37,6 @@ export declare class BinaryTreeNode<K = any, V = any, N extends BinaryTreeNode<K
|
|
|
37
37
|
* 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
|
|
38
38
|
* 4. Subtrees: Each child of a node forms the root of a subtree.
|
|
39
39
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
40
|
-
* 6. Internal Nodes: Nodes with at least one child are internal.
|
|
41
|
-
* 7. Balanced Trees: The heights of the left and right subtrees of any node differ by no more than one.
|
|
42
|
-
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
43
|
-
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
44
40
|
*/
|
|
45
41
|
export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTree<K, V, N, BinaryTreeNested<K, V, N>>> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, N, TREE> {
|
|
46
42
|
iterationType: IterationType;
|
|
@@ -389,21 +385,21 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
|
|
|
389
385
|
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
390
386
|
* @returns a boolean value.
|
|
391
387
|
*/
|
|
392
|
-
isRealNode(node:
|
|
388
|
+
isRealNode(node: BTNodeExemplar<K, V, N>): node is N;
|
|
393
389
|
/**
|
|
394
390
|
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
395
391
|
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
396
392
|
* @returns a boolean value.
|
|
397
393
|
*/
|
|
398
|
-
isNIL(node:
|
|
394
|
+
isNIL(node: BTNodeExemplar<K, V, N>): boolean;
|
|
399
395
|
/**
|
|
400
396
|
* The function checks if a given node is a real node or null.
|
|
401
397
|
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
402
398
|
* @returns a boolean value.
|
|
403
399
|
*/
|
|
404
|
-
isNodeOrNull(node:
|
|
400
|
+
isNodeOrNull(node: BTNodeExemplar<K, V, N>): node is N | null;
|
|
405
401
|
/**
|
|
406
|
-
* The function "isNotNodeInstance" checks if a potential key is a
|
|
402
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
407
403
|
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
408
404
|
* data type.
|
|
409
405
|
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
@@ -427,8 +423,17 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
|
|
|
427
423
|
listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNodeKeyOrNode<K, N>, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[][];
|
|
428
424
|
listLevels<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: BTNodeKeyOrNode<K, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][];
|
|
429
425
|
/**
|
|
430
|
-
* Time
|
|
431
|
-
* 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'.
|
|
432
437
|
*/
|
|
433
438
|
getPredecessor(node: N): N;
|
|
434
439
|
/**
|
|
@@ -65,10 +65,6 @@ exports.BinaryTreeNode = BinaryTreeNode;
|
|
|
65
65
|
* 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
|
|
66
66
|
* 4. Subtrees: Each child of a node forms the root of a subtree.
|
|
67
67
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
68
|
-
* 6. Internal Nodes: Nodes with at least one child are internal.
|
|
69
|
-
* 7. Balanced Trees: The heights of the left and right subtrees of any node differ by no more than one.
|
|
70
|
-
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
71
|
-
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
72
68
|
*/
|
|
73
69
|
class BinaryTree extends base_1.IterableEntryBase {
|
|
74
70
|
/**
|
|
@@ -196,44 +192,50 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
196
192
|
* @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
|
|
197
193
|
*/
|
|
198
194
|
add(keyOrNodeOrEntry, value) {
|
|
199
|
-
let inserted;
|
|
200
195
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
201
196
|
if (newNode === undefined)
|
|
202
197
|
return;
|
|
203
|
-
//
|
|
204
|
-
if (
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
198
|
+
// If the tree is empty, directly set the new node as the root node
|
|
199
|
+
if (!this.root) {
|
|
200
|
+
this._root = newNode;
|
|
201
|
+
this._size = 1;
|
|
202
|
+
return newNode;
|
|
203
|
+
}
|
|
204
|
+
const queue = new queue_1.Queue([this.root]);
|
|
205
|
+
let potentialParent; // Record the parent node of the potential insertion location
|
|
206
|
+
while (queue.size > 0) {
|
|
207
|
+
const cur = queue.shift();
|
|
208
|
+
if (!cur)
|
|
209
|
+
continue;
|
|
210
|
+
// Check for duplicate keys when newNode is not null
|
|
211
|
+
if (newNode !== null && cur.key === newNode.key) {
|
|
212
|
+
this._replaceNode(cur, newNode);
|
|
213
|
+
return newNode; // If duplicate keys are found, no insertion is performed
|
|
214
|
+
}
|
|
215
|
+
// Record the first possible insertion location found
|
|
216
|
+
if (potentialParent === undefined && (cur.left === undefined || cur.right === undefined)) {
|
|
217
|
+
potentialParent = cur;
|
|
218
|
+
}
|
|
219
|
+
// Continue traversing the left and right subtrees
|
|
220
|
+
if (cur.left !== null) {
|
|
221
|
+
cur.left && queue.push(cur.left);
|
|
222
|
+
}
|
|
223
|
+
if (cur.right !== null) {
|
|
224
|
+
cur.right && queue.push(cur.right);
|
|
221
225
|
}
|
|
222
|
-
};
|
|
223
|
-
if (this.root) {
|
|
224
|
-
inserted = _bfs(this.root, newNode);
|
|
225
226
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
if (
|
|
229
|
-
|
|
227
|
+
// At the end of the traversal, if the insertion position is found, insert
|
|
228
|
+
if (potentialParent) {
|
|
229
|
+
if (potentialParent.left === undefined) {
|
|
230
|
+
potentialParent.left = newNode;
|
|
230
231
|
}
|
|
231
|
-
else {
|
|
232
|
-
|
|
232
|
+
else if (potentialParent.right === undefined) {
|
|
233
|
+
potentialParent.right = newNode;
|
|
233
234
|
}
|
|
234
|
-
|
|
235
|
+
this._size++;
|
|
236
|
+
return newNode;
|
|
235
237
|
}
|
|
236
|
-
return
|
|
238
|
+
return undefined; // If the insertion position cannot be found, return undefined
|
|
237
239
|
}
|
|
238
240
|
/**
|
|
239
241
|
* Time Complexity: O(k log n) - O(k * n)
|
|
@@ -978,7 +980,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
978
980
|
* @returns a boolean value.
|
|
979
981
|
*/
|
|
980
982
|
isRealNode(node) {
|
|
981
|
-
return node instanceof BinaryTreeNode && node.key
|
|
983
|
+
return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
|
|
982
984
|
}
|
|
983
985
|
/**
|
|
984
986
|
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
@@ -986,7 +988,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
986
988
|
* @returns a boolean value.
|
|
987
989
|
*/
|
|
988
990
|
isNIL(node) {
|
|
989
|
-
return node instanceof BinaryTreeNode && node.key
|
|
991
|
+
return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
|
|
990
992
|
}
|
|
991
993
|
/**
|
|
992
994
|
* The function checks if a given node is a real node or null.
|
|
@@ -997,7 +999,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
997
999
|
return this.isRealNode(node) || node === null;
|
|
998
1000
|
}
|
|
999
1001
|
/**
|
|
1000
|
-
* The function "isNotNodeInstance" checks if a potential key is a
|
|
1002
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
1001
1003
|
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
1002
1004
|
* data type.
|
|
1003
1005
|
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
@@ -1278,19 +1280,23 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1278
1280
|
return levelsNodes;
|
|
1279
1281
|
}
|
|
1280
1282
|
/**
|
|
1281
|
-
*
|
|
1282
|
-
*
|
|
1283
|
-
|
|
1284
|
-
|
|
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'.
|
|
1285
1294
|
*/
|
|
1286
1295
|
getPredecessor(node) {
|
|
1287
|
-
|
|
1288
|
-
if (!this.isRealNode(node))
|
|
1289
|
-
return undefined;
|
|
1290
|
-
if (node.left) {
|
|
1296
|
+
if (this.isRealNode(node.left)) {
|
|
1291
1297
|
let predecessor = node.left;
|
|
1292
1298
|
while (!this.isRealNode(predecessor) || (this.isRealNode(predecessor.right) && predecessor.right !== node)) {
|
|
1293
|
-
if (predecessor) {
|
|
1299
|
+
if (this.isRealNode(predecessor)) {
|
|
1294
1300
|
predecessor = predecessor.right;
|
|
1295
1301
|
}
|
|
1296
1302
|
}
|
|
@@ -1308,13 +1314,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1308
1314
|
*/
|
|
1309
1315
|
getSuccessor(x) {
|
|
1310
1316
|
x = this.ensureNode(x);
|
|
1311
|
-
if (!x)
|
|
1317
|
+
if (!this.isRealNode(x))
|
|
1312
1318
|
return undefined;
|
|
1313
|
-
if (x.right) {
|
|
1319
|
+
if (this.isRealNode(x.right)) {
|
|
1314
1320
|
return this.getLeftMost(x.right);
|
|
1315
1321
|
}
|
|
1316
1322
|
let y = x.parent;
|
|
1317
|
-
while (y &&
|
|
1323
|
+
while (this.isRealNode(y) && x === y.right) {
|
|
1318
1324
|
x = y;
|
|
1319
1325
|
y = y.parent;
|
|
1320
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
|
|
@@ -439,113 +439,3 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
439
439
|
indexInBucket: number;
|
|
440
440
|
};
|
|
441
441
|
}
|
|
442
|
-
export declare class ObjectDeque<E = number> {
|
|
443
|
-
constructor(capacity?: number);
|
|
444
|
-
protected _nodes: {
|
|
445
|
-
[key: number]: E;
|
|
446
|
-
};
|
|
447
|
-
get nodes(): {
|
|
448
|
-
[p: number]: E;
|
|
449
|
-
};
|
|
450
|
-
protected _capacity: number;
|
|
451
|
-
get capacity(): number;
|
|
452
|
-
protected _first: number;
|
|
453
|
-
get first(): number;
|
|
454
|
-
protected _last: number;
|
|
455
|
-
get last(): number;
|
|
456
|
-
protected _size: number;
|
|
457
|
-
get size(): number;
|
|
458
|
-
/**
|
|
459
|
-
* Time Complexity: O(1)
|
|
460
|
-
* Space Complexity: O(1)
|
|
461
|
-
*/
|
|
462
|
-
/**
|
|
463
|
-
* Time Complexity: O(1)
|
|
464
|
-
* Space Complexity: O(1)
|
|
465
|
-
*
|
|
466
|
-
* The "addFirst" function adds an element to the beginning of an array-like data structure.
|
|
467
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the beginning of the data
|
|
468
|
-
* structure.
|
|
469
|
-
*/
|
|
470
|
-
addFirst(element: E): void;
|
|
471
|
-
/**
|
|
472
|
-
* Time Complexity: O(1)
|
|
473
|
-
* Space Complexity: O(1)
|
|
474
|
-
*/
|
|
475
|
-
/**
|
|
476
|
-
* Time Complexity: O(1)
|
|
477
|
-
* Space Complexity: O(1)
|
|
478
|
-
*
|
|
479
|
-
* The addLast function adds an element to the end of an array-like data structure.
|
|
480
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the end of the data structure.
|
|
481
|
-
*/
|
|
482
|
-
addLast(element: E): void;
|
|
483
|
-
/**
|
|
484
|
-
* Time Complexity: O(1)
|
|
485
|
-
* Space Complexity: O(1)
|
|
486
|
-
*/
|
|
487
|
-
/**
|
|
488
|
-
* Time Complexity: O(1)
|
|
489
|
-
* Space Complexity: O(1)
|
|
490
|
-
*
|
|
491
|
-
* The function `pollFirst()` removes and returns the first element in a data structure.
|
|
492
|
-
* @returns The element of the first element in the data structure.
|
|
493
|
-
*/
|
|
494
|
-
pollFirst(): E | undefined;
|
|
495
|
-
/**
|
|
496
|
-
* Time Complexity: O(1)
|
|
497
|
-
* Space Complexity: O(1)
|
|
498
|
-
*/
|
|
499
|
-
/**
|
|
500
|
-
* Time Complexity: O(1)
|
|
501
|
-
* Space Complexity: O(1)
|
|
502
|
-
*
|
|
503
|
-
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
504
|
-
* @returns The element at the first position of the `_nodes` array.
|
|
505
|
-
*/
|
|
506
|
-
getFirst(): E | undefined;
|
|
507
|
-
/**
|
|
508
|
-
* Time Complexity: O(1)
|
|
509
|
-
* Space Complexity: O(1)
|
|
510
|
-
*/
|
|
511
|
-
/**
|
|
512
|
-
* Time Complexity: O(1)
|
|
513
|
-
* Space Complexity: O(1)
|
|
514
|
-
*
|
|
515
|
-
* The `pollLast()` function removes and returns the last element in a data structure.
|
|
516
|
-
* @returns The element that was removed from the data structure.
|
|
517
|
-
*/
|
|
518
|
-
pollLast(): E | undefined;
|
|
519
|
-
/**
|
|
520
|
-
* Time Complexity: O(1)
|
|
521
|
-
* Space Complexity: O(1)
|
|
522
|
-
*/
|
|
523
|
-
/**
|
|
524
|
-
* Time Complexity: O(1)
|
|
525
|
-
* Space Complexity: O(1)
|
|
526
|
-
*
|
|
527
|
-
* The `getLast()` function returns the last element in an array-like data structure.
|
|
528
|
-
* @returns The last element in the array "_nodes" is being returned.
|
|
529
|
-
*/
|
|
530
|
-
getLast(): E | undefined;
|
|
531
|
-
/**
|
|
532
|
-
* Time Complexity: O(1)
|
|
533
|
-
* Space Complexity: O(1)
|
|
534
|
-
*/
|
|
535
|
-
/**
|
|
536
|
-
* Time Complexity: O(1)
|
|
537
|
-
* Space Complexity: O(1)
|
|
538
|
-
*
|
|
539
|
-
* The get function returns the element at the specified index in an array-like data structure.
|
|
540
|
-
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
541
|
-
* retrieve from the array.
|
|
542
|
-
* @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
|
|
543
|
-
* index, `undefined` is returned.
|
|
544
|
-
*/
|
|
545
|
-
get(index: number): NonNullable<E> | undefined;
|
|
546
|
-
/**
|
|
547
|
-
* The function checks if the size of a data structure is less than or equal to zero.
|
|
548
|
-
* @returns The method is returning a boolean element indicating whether the size of the object is less than or equal to 0.
|
|
549
|
-
*/
|
|
550
|
-
isEmpty(): boolean;
|
|
551
|
-
}
|