queue-typed 1.48.2 → 1.48.4
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 +16 -16
- package/dist/data-structures/binary-tree/avl-tree.js +7 -7
- package/dist/data-structures/binary-tree/binary-tree.d.ts +89 -87
- package/dist/data-structures/binary-tree/binary-tree.js +67 -58
- package/dist/data-structures/binary-tree/bst.d.ts +28 -47
- package/dist/data-structures/binary-tree/bst.js +54 -57
- package/dist/data-structures/binary-tree/rb-tree.d.ts +15 -15
- package/dist/data-structures/binary-tree/rb-tree.js +7 -7
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +22 -22
- package/dist/data-structures/binary-tree/tree-multimap.js +11 -11
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/data-structures/graph/abstract-graph.js +4 -0
- package/dist/data-structures/graph/directed-graph.d.ts +25 -7
- package/dist/data-structures/graph/directed-graph.js +58 -12
- package/dist/data-structures/graph/undirected-graph.d.ts +25 -6
- package/dist/data-structures/graph/undirected-graph.js +70 -7
- package/dist/interfaces/binary-tree.d.ts +6 -6
- package/dist/types/common.d.ts +11 -8
- package/dist/types/common.js +6 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +20 -21
- package/src/data-structures/binary-tree/binary-tree.ts +147 -136
- package/src/data-structures/binary-tree/bst.ts +86 -82
- package/src/data-structures/binary-tree/rb-tree.ts +25 -26
- package/src/data-structures/binary-tree/tree-multimap.ts +30 -35
- package/src/data-structures/graph/abstract-graph.ts +5 -0
- package/src/data-structures/graph/directed-graph.ts +61 -12
- package/src/data-structures/graph/undirected-graph.ts +75 -7
- package/src/interfaces/binary-tree.ts +5 -6
- package/src/types/common.ts +11 -8
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
|
@@ -65,11 +65,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
65
65
|
*/
|
|
66
66
|
constructor(elements, options) {
|
|
67
67
|
super([], options);
|
|
68
|
-
this.
|
|
68
|
+
this._variant = types_1.BSTVariant.MIN;
|
|
69
69
|
if (options) {
|
|
70
|
-
const {
|
|
71
|
-
if (
|
|
72
|
-
this.
|
|
70
|
+
const { variant } = options;
|
|
71
|
+
if (variant) {
|
|
72
|
+
this._variant = variant;
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
this._root = undefined;
|
|
@@ -79,9 +79,12 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
79
79
|
get root() {
|
|
80
80
|
return this._root;
|
|
81
81
|
}
|
|
82
|
+
get variant() {
|
|
83
|
+
return this._variant;
|
|
84
|
+
}
|
|
82
85
|
/**
|
|
83
86
|
* The function creates a new binary search tree node with the given key and value.
|
|
84
|
-
* @param {
|
|
87
|
+
* @param {K} key - The key parameter is the key value that will be associated with
|
|
85
88
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
86
89
|
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It
|
|
87
90
|
* represents the value associated with the node in a binary search tree.
|
|
@@ -98,11 +101,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
98
101
|
* @returns a new instance of the BST class with the specified options.
|
|
99
102
|
*/
|
|
100
103
|
createTree(options) {
|
|
101
|
-
return new BST([], Object.assign({ iterationType: this.iterationType,
|
|
104
|
+
return new BST([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
|
|
102
105
|
}
|
|
103
106
|
/**
|
|
104
107
|
* The function checks if an exemplar is an instance of BSTNode.
|
|
105
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
108
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<K, V, N>`.
|
|
106
109
|
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
107
110
|
*/
|
|
108
111
|
isNode(exemplar) {
|
|
@@ -111,7 +114,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
111
114
|
/**
|
|
112
115
|
* The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
|
|
113
116
|
* is valid, otherwise it returns undefined.
|
|
114
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
117
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
115
118
|
* @returns a variable `node` which is of type `N` or `undefined`.
|
|
116
119
|
*/
|
|
117
120
|
exemplarToNode(exemplar) {
|
|
@@ -131,7 +134,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
131
134
|
node = this.createNode(key, value);
|
|
132
135
|
}
|
|
133
136
|
}
|
|
134
|
-
else if (this.
|
|
137
|
+
else if (this.isNotNodeInstance(exemplar)) {
|
|
135
138
|
node = this.createNode(exemplar);
|
|
136
139
|
}
|
|
137
140
|
else {
|
|
@@ -239,17 +242,17 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
239
242
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
240
243
|
let aR, bR;
|
|
241
244
|
if (this.isEntry(a))
|
|
242
|
-
aR = a[0];
|
|
245
|
+
aR = this.extractor(a[0]);
|
|
243
246
|
else if (this.isRealNode(a))
|
|
244
|
-
aR = a.key;
|
|
247
|
+
aR = this.extractor(a.key);
|
|
245
248
|
else
|
|
246
|
-
aR = a;
|
|
249
|
+
aR = this.extractor(a);
|
|
247
250
|
if (this.isEntry(b))
|
|
248
|
-
bR = b[0];
|
|
251
|
+
bR = this.extractor(b[0]);
|
|
249
252
|
else if (this.isRealNode(b))
|
|
250
|
-
bR = b.key;
|
|
253
|
+
bR = this.extractor(b.key);
|
|
251
254
|
else
|
|
252
|
-
bR = b;
|
|
255
|
+
bR = this.extractor(b);
|
|
253
256
|
return aR - bR;
|
|
254
257
|
});
|
|
255
258
|
const _dfs = (arr) => {
|
|
@@ -286,34 +289,31 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
286
289
|
}
|
|
287
290
|
return inserted;
|
|
288
291
|
}
|
|
289
|
-
/**
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
else
|
|
315
|
-
return (_f = (_e = this.getRightMost(beginRoot, iterationType)) === null || _e === void 0 ? void 0 : _e.key) !== null && _f !== void 0 ? _f : 0;
|
|
316
|
-
}
|
|
292
|
+
// /**
|
|
293
|
+
// * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
294
|
+
// * Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
295
|
+
// */
|
|
296
|
+
//
|
|
297
|
+
// /**
|
|
298
|
+
// * Time Complexity: O(log n) - Average case for a balanced tree.
|
|
299
|
+
// * Space Complexity: O(1) - Constant space is used.
|
|
300
|
+
// *
|
|
301
|
+
// * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
302
|
+
// * leftmost node if the comparison result is greater than.
|
|
303
|
+
// * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
304
|
+
// * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
305
|
+
// * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
306
|
+
// * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
307
|
+
// * be performed. It can have one of the following values:
|
|
308
|
+
// * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
309
|
+
// * the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
310
|
+
// * rightmost node otherwise. If no node is found, it returns 0.
|
|
311
|
+
// */
|
|
312
|
+
// lastKey(beginRoot: BSTNodeKeyOrNode<K,N> = this.root, iterationType = this.iterationType): K {
|
|
313
|
+
// if (this._compare(0, 1) === CP.lt) return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
|
|
314
|
+
// else if (this._compare(0, 1) === CP.gt) return this.getLeftMost(beginRoot, iterationType)?.key ?? 0;
|
|
315
|
+
// else return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
|
|
316
|
+
// }
|
|
317
317
|
/**
|
|
318
318
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
319
319
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -324,7 +324,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
324
324
|
*
|
|
325
325
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
326
326
|
* either recursive or iterative methods.
|
|
327
|
-
* @param {
|
|
327
|
+
* @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
328
328
|
* It is used to identify the node that we want to retrieve.
|
|
329
329
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
330
330
|
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
@@ -370,14 +370,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
370
370
|
/**
|
|
371
371
|
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
372
372
|
* otherwise it returns the key itself.
|
|
373
|
-
* @param {
|
|
373
|
+
* @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
|
|
374
374
|
* `undefined`.
|
|
375
375
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
376
376
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
377
377
|
* @returns either a node object (N) or undefined.
|
|
378
378
|
*/
|
|
379
379
|
ensureNode(key, iterationType = types_1.IterationType.ITERATIVE) {
|
|
380
|
-
return this.
|
|
380
|
+
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
381
381
|
}
|
|
382
382
|
/**
|
|
383
383
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
@@ -395,7 +395,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
395
395
|
* first node that matches the identifier. If set to true, the function will return an array
|
|
396
396
|
* containing only the first matching node. If set to false (default), the function will continue
|
|
397
397
|
* searching for all nodes that match the identifier and return an array containing
|
|
398
|
-
* @param {
|
|
398
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
399
399
|
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
400
400
|
* traversal will start from the root of the tree.
|
|
401
401
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
@@ -475,7 +475,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
475
475
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
|
|
476
476
|
* `CP`, which is a custom type representing the comparison operator. The possible values for
|
|
477
477
|
* `lesserOrGreater` are
|
|
478
|
-
* @param {
|
|
478
|
+
* @param {K | N | undefined} targetNode - The `targetNode` parameter represents the node in the
|
|
479
479
|
* binary tree that you want to traverse from. It can be specified either by its key, by the node
|
|
480
480
|
* object itself, or it can be left undefined to start the traversal from the root of the tree.
|
|
481
481
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -652,19 +652,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
652
652
|
/**
|
|
653
653
|
* The function compares two values using a comparator function and returns whether the first value
|
|
654
654
|
* is greater than, less than, or equal to the second value.
|
|
655
|
-
* @param {
|
|
656
|
-
* @param {
|
|
655
|
+
* @param {K} a - The parameter "a" is of type K.
|
|
656
|
+
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
657
657
|
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
|
|
658
658
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
659
659
|
*/
|
|
660
660
|
_compare(a, b) {
|
|
661
|
-
const
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
return types_1.CP.lt;
|
|
666
|
-
else
|
|
667
|
-
return types_1.CP.eq;
|
|
661
|
+
const extractedA = this.extractor(a);
|
|
662
|
+
const extractedB = this.extractor(b);
|
|
663
|
+
const compared = this.variant === types_1.BSTVariant.MIN ? extractedA - extractedB : extractedB - extractedA;
|
|
664
|
+
return compared > 0 ? types_1.CP.gt : compared < 0 ? types_1.CP.lt : types_1.CP.eq;
|
|
668
665
|
}
|
|
669
666
|
}
|
|
670
667
|
exports.BST = BST;
|
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { BiTreeDeleteResult, BTNCallback,
|
|
8
|
+
import { BiTreeDeleteResult, BTNCallback, BTNodeExemplar, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
9
9
|
import { BST, BSTNode } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
|
-
export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
11
|
+
export declare class RedBlackTreeNode<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
|
|
12
12
|
color: RBTNColor;
|
|
13
|
-
constructor(key:
|
|
13
|
+
constructor(key: K, value?: V, color?: RBTNColor);
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
16
|
* 1. Each node is either red or black.
|
|
@@ -19,12 +19,12 @@ export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N>
|
|
|
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 RedBlackTreeNode<V, N> = RedBlackTreeNode<V, RedBlackTreeNodeNested<V>>, TREE extends RedBlackTree<V, N, TREE> = RedBlackTree<V, N, RedBlackTreeNested<V, N>>> extends BST<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
22
|
+
export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, N, TREE> = RedBlackTree<K, V, N, RedBlackTreeNested<K, V, N>>> extends BST<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
|
|
23
23
|
Sentinel: N;
|
|
24
24
|
/**
|
|
25
25
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
26
26
|
* initializes the tree with optional elements and options.
|
|
27
|
-
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
27
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<K, V, N>`
|
|
28
28
|
* objects. It represents the initial elements that will be added to the RBTree during its
|
|
29
29
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
30
30
|
* elements to the
|
|
@@ -32,14 +32,14 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
32
32
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
33
33
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
34
34
|
*/
|
|
35
|
-
constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<RBTreeOptions
|
|
35
|
+
constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<RBTreeOptions<K>>);
|
|
36
36
|
protected _root: N;
|
|
37
37
|
get root(): N;
|
|
38
38
|
protected _size: number;
|
|
39
39
|
get size(): number;
|
|
40
40
|
/**
|
|
41
41
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
42
|
-
* @param {
|
|
42
|
+
* @param {K} key - The key parameter is the key value associated with the node. It is used to
|
|
43
43
|
* identify and compare nodes in the Red-Black Tree.
|
|
44
44
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
45
45
|
* associated with the node. It is of type `V`, which is a generic type that can be replaced with any
|
|
@@ -49,7 +49,7 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
49
49
|
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
50
50
|
* value, and color.
|
|
51
51
|
*/
|
|
52
|
-
createNode(key:
|
|
52
|
+
createNode(key: K, value?: V, color?: RBTNColor): N;
|
|
53
53
|
/**
|
|
54
54
|
* The function creates a Red-Black Tree with the specified options and returns it.
|
|
55
55
|
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
@@ -57,23 +57,23 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
57
57
|
* class.
|
|
58
58
|
* @returns a new instance of a RedBlackTree object.
|
|
59
59
|
*/
|
|
60
|
-
createTree(options?: RBTreeOptions): TREE;
|
|
60
|
+
createTree(options?: RBTreeOptions<K>): TREE;
|
|
61
61
|
/**
|
|
62
62
|
* The function checks if an exemplar is an instance of the RedBlackTreeNode class.
|
|
63
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
63
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
64
64
|
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
65
65
|
* class.
|
|
66
66
|
*/
|
|
67
|
-
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
|
|
67
|
+
isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
|
|
68
68
|
/**
|
|
69
69
|
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
70
70
|
* otherwise it returns undefined.
|
|
71
|
-
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
|
|
71
|
+
* @param exemplar - BTNodeExemplar<K, V, N> - A generic type representing an exemplar of a binary tree
|
|
72
72
|
* node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
|
|
73
73
|
* that is not a valid exemplar.
|
|
74
74
|
* @returns a variable `node` which is of type `N | undefined`.
|
|
75
75
|
*/
|
|
76
|
-
exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined;
|
|
76
|
+
exemplarToNode(exemplar: BTNodeExemplar<K, V, N>): N | undefined;
|
|
77
77
|
/**
|
|
78
78
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
79
79
|
* Space Complexity: O(1)
|
|
@@ -84,7 +84,7 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
84
84
|
* @returns The method `add` returns either an instance of `N` (the node that was added) or
|
|
85
85
|
* `undefined`.
|
|
86
86
|
*/
|
|
87
|
-
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined;
|
|
87
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>): N | undefined;
|
|
88
88
|
/**
|
|
89
89
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
90
90
|
* Space Complexity: O(1)
|
|
@@ -110,7 +110,7 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
110
110
|
* Space Complexity: O(1)
|
|
111
111
|
*/
|
|
112
112
|
isRealNode(node: N | undefined): node is N;
|
|
113
|
-
getNode<C extends BTNCallback<N,
|
|
113
|
+
getNode<C extends BTNCallback<N, K>>(identifier: K, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
114
114
|
getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
115
115
|
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
116
116
|
/**
|
|
@@ -29,7 +29,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
29
29
|
/**
|
|
30
30
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
31
31
|
* initializes the tree with optional elements and options.
|
|
32
|
-
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
32
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<K, V, N>`
|
|
33
33
|
* objects. It represents the initial elements that will be added to the RBTree during its
|
|
34
34
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
35
35
|
* elements to the
|
|
@@ -53,7 +53,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
55
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
56
|
-
* @param {
|
|
56
|
+
* @param {K} key - The key parameter is the key value associated with the node. It is used to
|
|
57
57
|
* identify and compare nodes in the Red-Black Tree.
|
|
58
58
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
59
59
|
* associated with the node. It is of type `V`, which is a generic type that can be replaced with any
|
|
@@ -74,11 +74,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
74
74
|
* @returns a new instance of a RedBlackTree object.
|
|
75
75
|
*/
|
|
76
76
|
createTree(options) {
|
|
77
|
-
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType,
|
|
77
|
+
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
|
|
78
78
|
}
|
|
79
79
|
/**
|
|
80
80
|
* The function checks if an exemplar is an instance of the RedBlackTreeNode class.
|
|
81
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
81
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
82
82
|
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
83
83
|
* class.
|
|
84
84
|
*/
|
|
@@ -88,7 +88,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
88
88
|
/**
|
|
89
89
|
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
90
90
|
* otherwise it returns undefined.
|
|
91
|
-
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
|
|
91
|
+
* @param exemplar - BTNodeExemplar<K, V, N> - A generic type representing an exemplar of a binary tree
|
|
92
92
|
* node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
|
|
93
93
|
* that is not a valid exemplar.
|
|
94
94
|
* @returns a variable `node` which is of type `N | undefined`.
|
|
@@ -110,7 +110,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
110
110
|
node = this.createNode(key, value, types_1.RBTNColor.RED);
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
|
-
else if (this.
|
|
113
|
+
else if (this.isNotNodeInstance(exemplar)) {
|
|
114
114
|
node = this.createNode(exemplar, undefined, types_1.RBTNColor.RED);
|
|
115
115
|
}
|
|
116
116
|
else {
|
|
@@ -276,7 +276,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
276
276
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
277
277
|
* the binary tree. It is used to determine if a node matches the given identifier. The `callback`
|
|
278
278
|
* function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
|
|
279
|
-
* @param {
|
|
279
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is the starting point for
|
|
280
280
|
* searching for a node in a binary tree. It can be either a key value or a node object. If it is not
|
|
281
281
|
* provided, the search will start from the root of the binary tree.
|
|
282
282
|
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|
|
@@ -5,15 +5,15 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BSTNodeKeyOrNode,
|
|
8
|
+
import type { BSTNodeKeyOrNode, BTNodeExemplar, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
|
|
9
9
|
import { BiTreeDeleteResult, BTNCallback, IterationType, TreeMultimapNested } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
|
-
export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>> extends AVLTreeNode<V, N> {
|
|
12
|
+
export declare class TreeMultimapNode<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNodeNested<K, V>> extends AVLTreeNode<K, V, N> {
|
|
13
13
|
count: number;
|
|
14
14
|
/**
|
|
15
15
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
16
|
-
* @param {
|
|
16
|
+
* @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
|
|
17
17
|
* of the binary tree node.
|
|
18
18
|
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
19
19
|
* tree node. If no value is provided, it will be `undefined`.
|
|
@@ -21,42 +21,42 @@ export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N>
|
|
|
21
21
|
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
22
22
|
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
23
23
|
*/
|
|
24
|
-
constructor(key:
|
|
24
|
+
constructor(key: K, value?: V, count?: number);
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
27
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
28
28
|
*/
|
|
29
|
-
export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNode<V, TreeMultimapNodeNested<V>>, TREE extends TreeMultimap<V, N, TREE> = TreeMultimap<V, N, TreeMultimapNested<V, N>>> extends AVLTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
30
|
-
constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<TreeMultimapOptions
|
|
29
|
+
export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>, TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>> extends AVLTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
|
|
30
|
+
constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>);
|
|
31
31
|
private _count;
|
|
32
32
|
get count(): number;
|
|
33
33
|
/**
|
|
34
34
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
35
|
-
* @param {
|
|
35
|
+
* @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
36
36
|
* distinguish one node from another in the tree.
|
|
37
37
|
* @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
|
|
38
38
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
39
39
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
40
40
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
41
41
|
*/
|
|
42
|
-
createNode(key:
|
|
43
|
-
createTree(options?: TreeMultimapOptions): TREE;
|
|
42
|
+
createNode(key: K, value?: V, count?: number): N;
|
|
43
|
+
createTree(options?: TreeMultimapOptions<K>): TREE;
|
|
44
44
|
/**
|
|
45
45
|
* The function checks if an exemplar is an instance of the TreeMultimapNode class.
|
|
46
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
46
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
47
47
|
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
48
48
|
* class.
|
|
49
49
|
*/
|
|
50
|
-
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
|
|
50
|
+
isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
|
|
51
51
|
/**
|
|
52
52
|
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
53
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents
|
|
53
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where `V` represents
|
|
54
54
|
* the value type and `N` represents the node type.
|
|
55
55
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
56
56
|
* times the node should be created. If not provided, it defaults to 1.
|
|
57
57
|
* @returns a value of type `N` (the generic type parameter) or `undefined`.
|
|
58
58
|
*/
|
|
59
|
-
exemplarToNode(exemplar: BTNodeExemplar<V, N>, count?: number): N | undefined;
|
|
59
|
+
exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, count?: number): N | undefined;
|
|
60
60
|
/**
|
|
61
61
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
62
62
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -73,7 +73,7 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
73
73
|
* is 1.
|
|
74
74
|
* @returns either a node (`N`) or `undefined`.
|
|
75
75
|
*/
|
|
76
|
-
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count?: number): N | undefined;
|
|
76
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, count?: number): N | undefined;
|
|
77
77
|
/**
|
|
78
78
|
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
79
79
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -88,7 +88,7 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
88
88
|
* either keys, nodes, or entries.
|
|
89
89
|
* @returns The method is returning an array of type `N | undefined`.
|
|
90
90
|
*/
|
|
91
|
-
addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<V, N>>): (N | undefined)[];
|
|
91
|
+
addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>): (N | undefined)[];
|
|
92
92
|
/**
|
|
93
93
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
94
94
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -158,22 +158,22 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
158
158
|
* @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
|
|
159
159
|
* added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
|
|
160
160
|
* `undefined` if there is no node to add.
|
|
161
|
-
* @param {
|
|
161
|
+
* @param {K | N | undefined} parent - The `parent` parameter represents the parent node to
|
|
162
162
|
* which the new node will be added as a child. It can be either a node object (`N`) or a key value
|
|
163
|
-
* (`
|
|
163
|
+
* (`K`).
|
|
164
164
|
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
|
165
165
|
* added, or `undefined` if no node was added.
|
|
166
166
|
*/
|
|
167
|
-
protected _addTo(newNode: N | undefined, parent: BSTNodeKeyOrNode<N>): N | undefined;
|
|
167
|
+
protected _addTo(newNode: N | undefined, parent: BSTNodeKeyOrNode<K, N>): N | undefined;
|
|
168
168
|
/**
|
|
169
169
|
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
170
|
-
* @param {
|
|
171
|
-
* which the values will be swapped. It can be of type `
|
|
172
|
-
* @param {
|
|
170
|
+
* @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
171
|
+
* which the values will be swapped. It can be of type `K`, `N`, or `undefined`.
|
|
172
|
+
* @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
173
173
|
* node where the values from the source node will be swapped to.
|
|
174
174
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
175
175
|
* if either `srcNode` or `destNode` is undefined.
|
|
176
176
|
*/
|
|
177
|
-
protected _swapProperties(srcNode: BSTNodeKeyOrNode<N>, destNode: BSTNodeKeyOrNode<N>): N | undefined;
|
|
177
|
+
protected _swapProperties(srcNode: BSTNodeKeyOrNode<K, N>, destNode: BSTNodeKeyOrNode<K, N>): N | undefined;
|
|
178
178
|
protected _replaceNode(oldNode: N, newNode: N): N;
|
|
179
179
|
}
|
|
@@ -6,7 +6,7 @@ const avl_tree_1 = require("./avl-tree");
|
|
|
6
6
|
class TreeMultimapNode extends avl_tree_1.AVLTreeNode {
|
|
7
7
|
/**
|
|
8
8
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
9
|
-
* @param {
|
|
9
|
+
* @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
|
|
10
10
|
* of the binary tree node.
|
|
11
11
|
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
12
12
|
* tree node. If no value is provided, it will be `undefined`.
|
|
@@ -38,7 +38,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
38
38
|
}
|
|
39
39
|
/**
|
|
40
40
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
41
|
-
* @param {
|
|
41
|
+
* @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
42
42
|
* distinguish one node from another in the tree.
|
|
43
43
|
* @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
|
|
44
44
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
@@ -49,11 +49,11 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
49
49
|
return new TreeMultimapNode(key, value, count);
|
|
50
50
|
}
|
|
51
51
|
createTree(options) {
|
|
52
|
-
return new TreeMultimap([], Object.assign({ iterationType: this.iterationType,
|
|
52
|
+
return new TreeMultimap([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
55
|
* The function checks if an exemplar is an instance of the TreeMultimapNode class.
|
|
56
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
56
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
57
57
|
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
58
58
|
* class.
|
|
59
59
|
*/
|
|
@@ -62,7 +62,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
62
62
|
}
|
|
63
63
|
/**
|
|
64
64
|
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
65
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents
|
|
65
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where `V` represents
|
|
66
66
|
* the value type and `N` represents the node type.
|
|
67
67
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
68
68
|
* times the node should be created. If not provided, it defaults to 1.
|
|
@@ -85,7 +85,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
85
85
|
node = this.createNode(key, value, count);
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
|
-
else if (this.
|
|
88
|
+
else if (this.isNotNodeInstance(exemplar)) {
|
|
89
89
|
node = this.createNode(exemplar, undefined, count);
|
|
90
90
|
}
|
|
91
91
|
else {
|
|
@@ -305,9 +305,9 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
305
305
|
* @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
|
|
306
306
|
* added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
|
|
307
307
|
* `undefined` if there is no node to add.
|
|
308
|
-
* @param {
|
|
308
|
+
* @param {K | N | undefined} parent - The `parent` parameter represents the parent node to
|
|
309
309
|
* which the new node will be added as a child. It can be either a node object (`N`) or a key value
|
|
310
|
-
* (`
|
|
310
|
+
* (`K`).
|
|
311
311
|
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
|
312
312
|
* added, or `undefined` if no node was added.
|
|
313
313
|
*/
|
|
@@ -340,9 +340,9 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
340
340
|
}
|
|
341
341
|
/**
|
|
342
342
|
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
343
|
-
* @param {
|
|
344
|
-
* which the values will be swapped. It can be of type `
|
|
345
|
-
* @param {
|
|
343
|
+
* @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
344
|
+
* which the values will be swapped. It can be of type `K`, `N`, or `undefined`.
|
|
345
|
+
* @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
346
346
|
* node where the values from the source node will be swapped to.
|
|
347
347
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
348
348
|
* if either `srcNode` or `destNode` is undefined.
|
|
@@ -88,6 +88,7 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
88
88
|
hasVertex(vertexOrKey: VO | VertexKey): boolean;
|
|
89
89
|
addVertex(vertex: VO): boolean;
|
|
90
90
|
addVertex(key: VertexKey, value?: V): boolean;
|
|
91
|
+
isVertexKey(potentialKey: any): potentialKey is VertexKey;
|
|
91
92
|
/**
|
|
92
93
|
* Time Complexity: O(1) - Constant time for Map operations.
|
|
93
94
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
@@ -100,6 +100,10 @@ class AbstractGraph extends base_1.IterablePairBase {
|
|
|
100
100
|
return this._addVertexOnly(newVertex);
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
|
+
isVertexKey(potentialKey) {
|
|
104
|
+
const potentialKeyType = typeof potentialKey;
|
|
105
|
+
return potentialKeyType === "string" || potentialKeyType === "number";
|
|
106
|
+
}
|
|
103
107
|
/**
|
|
104
108
|
* Time Complexity: O(1) - Constant time for Map operations.
|
|
105
109
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|