avl-tree-typed 1.47.9 → 1.48.1
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 +6 -0
- package/dist/data-structures/binary-tree/avl-tree.js +8 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +52 -0
- package/dist/data-structures/binary-tree/binary-tree.js +106 -28
- package/dist/data-structures/binary-tree/bst.d.ts +13 -0
- package/dist/data-structures/binary-tree/bst.js +41 -21
- package/dist/data-structures/binary-tree/rb-tree.d.ts +16 -0
- package/dist/data-structures/binary-tree/rb-tree.js +54 -31
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +28 -0
- package/dist/data-structures/binary-tree/tree-multimap.js +60 -21
- package/dist/data-structures/hash/hash-map.d.ts +173 -16
- package/dist/data-structures/hash/hash-map.js +373 -37
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -0
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +9 -0
- package/src/data-structures/binary-tree/binary-tree.ts +109 -24
- package/src/data-structures/binary-tree/bst.ts +38 -19
- package/src/data-structures/binary-tree/rb-tree.ts +55 -31
- package/src/data-structures/binary-tree/tree-multimap.ts +60 -17
- package/src/data-structures/hash/hash-map.ts +400 -40
- package/src/types/data-structures/hash/hash-map.ts +2 -0
|
@@ -52,6 +52,12 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
52
52
|
* @returns a new AVLTree object.
|
|
53
53
|
*/
|
|
54
54
|
createTree(options?: AVLTreeOptions): TREE;
|
|
55
|
+
/**
|
|
56
|
+
* The function checks if an exemplar is an instance of AVLTreeNode.
|
|
57
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
58
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
|
|
59
|
+
*/
|
|
60
|
+
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
|
|
55
61
|
/**
|
|
56
62
|
* 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.
|
|
57
63
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -63,6 +63,14 @@ class AVLTree extends bst_1.BST {
|
|
|
63
63
|
createTree(options) {
|
|
64
64
|
return new AVLTree([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
65
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* The function checks if an exemplar is an instance of AVLTreeNode.
|
|
68
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
69
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
|
|
70
|
+
*/
|
|
71
|
+
isNode(exemplar) {
|
|
72
|
+
return exemplar instanceof AVLTreeNode;
|
|
73
|
+
}
|
|
66
74
|
/**
|
|
67
75
|
* 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.
|
|
68
76
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -72,6 +72,20 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
72
72
|
* @returns a new instance of a binary tree.
|
|
73
73
|
*/
|
|
74
74
|
createTree(options?: Partial<BinaryTreeOptions>): TREE;
|
|
75
|
+
/**
|
|
76
|
+
* The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
|
|
77
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
78
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
|
79
|
+
*/
|
|
80
|
+
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
|
|
81
|
+
/**
|
|
82
|
+
* The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node
|
|
83
|
+
* object.
|
|
84
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing the exemplar parameter of the
|
|
85
|
+
* function. It can be any type.
|
|
86
|
+
* @returns a value of type `N` (which represents a node), or `null`, or `undefined`.
|
|
87
|
+
*/
|
|
88
|
+
exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | null | undefined;
|
|
75
89
|
/**
|
|
76
90
|
* The function checks if a given value is an entry in a binary tree node.
|
|
77
91
|
* @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
|
|
@@ -446,6 +460,44 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
446
460
|
* by the return type of the `callback` function.
|
|
447
461
|
*/
|
|
448
462
|
morris<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: BTNodeKeyOrNode<N>): ReturnType<C>[];
|
|
463
|
+
/**
|
|
464
|
+
* Time complexity: O(n)
|
|
465
|
+
* Space complexity: O(n)
|
|
466
|
+
*/
|
|
467
|
+
/**
|
|
468
|
+
* Time complexity: O(n)
|
|
469
|
+
* Space complexity: O(n)
|
|
470
|
+
*
|
|
471
|
+
* The function "keys" returns an array of keys from a given object.
|
|
472
|
+
* @returns an array of BTNKey objects.
|
|
473
|
+
*/
|
|
474
|
+
keys(): BTNKey[];
|
|
475
|
+
/**
|
|
476
|
+
* Time complexity: O(n)
|
|
477
|
+
* Space complexity: O(n)
|
|
478
|
+
*/
|
|
479
|
+
/**
|
|
480
|
+
* Time complexity: O(n)
|
|
481
|
+
* Space complexity: O(n)
|
|
482
|
+
*
|
|
483
|
+
* The function "values" returns an array of values from a map-like object.
|
|
484
|
+
* @returns The `values()` method is returning an array of values (`V`) from the entries in the
|
|
485
|
+
* object.
|
|
486
|
+
*/
|
|
487
|
+
values(): (V | undefined)[];
|
|
488
|
+
/**
|
|
489
|
+
* Time complexity: O(n)
|
|
490
|
+
* Space complexity: O(n)
|
|
491
|
+
*/
|
|
492
|
+
/**
|
|
493
|
+
* Time complexity: O(n)
|
|
494
|
+
* Space complexity: O(n)
|
|
495
|
+
*
|
|
496
|
+
* The `clone` function creates a new tree object and copies all the nodes from the original tree to
|
|
497
|
+
* the new tree.
|
|
498
|
+
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
499
|
+
*/
|
|
500
|
+
clone(): TREE;
|
|
449
501
|
/**
|
|
450
502
|
* Time complexity: O(n)
|
|
451
503
|
* Space complexity: O(1)
|
|
@@ -117,6 +117,51 @@ class BinaryTree {
|
|
|
117
117
|
createTree(options) {
|
|
118
118
|
return new BinaryTree([], Object.assign({ iterationType: this.iterationType }, options));
|
|
119
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
|
|
122
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
123
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
|
124
|
+
*/
|
|
125
|
+
isNode(exemplar) {
|
|
126
|
+
return exemplar instanceof BinaryTreeNode;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node
|
|
130
|
+
* object.
|
|
131
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing the exemplar parameter of the
|
|
132
|
+
* function. It can be any type.
|
|
133
|
+
* @returns a value of type `N` (which represents a node), or `null`, or `undefined`.
|
|
134
|
+
*/
|
|
135
|
+
exemplarToNode(exemplar) {
|
|
136
|
+
if (exemplar === undefined)
|
|
137
|
+
return;
|
|
138
|
+
let node;
|
|
139
|
+
if (exemplar === null) {
|
|
140
|
+
node = null;
|
|
141
|
+
}
|
|
142
|
+
else if (this.isEntry(exemplar)) {
|
|
143
|
+
const [key, value] = exemplar;
|
|
144
|
+
if (key === undefined) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
else if (key === null) {
|
|
148
|
+
node = null;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
node = this.createNode(key, value);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else if (this.isNode(exemplar)) {
|
|
155
|
+
node = exemplar;
|
|
156
|
+
}
|
|
157
|
+
else if (this.isNodeKey(exemplar)) {
|
|
158
|
+
node = this.createNode(exemplar);
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
return node;
|
|
164
|
+
}
|
|
120
165
|
/**
|
|
121
166
|
* The function checks if a given value is an entry in a binary tree node.
|
|
122
167
|
* @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
|
|
@@ -139,7 +184,10 @@ class BinaryTree {
|
|
|
139
184
|
* @returns The function `add` returns the inserted node (`N`), `null`, or `undefined`.
|
|
140
185
|
*/
|
|
141
186
|
add(keyOrNodeOrEntry) {
|
|
142
|
-
let inserted
|
|
187
|
+
let inserted;
|
|
188
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
189
|
+
if (newNode === undefined)
|
|
190
|
+
return;
|
|
143
191
|
const _bfs = (root, newNode) => {
|
|
144
192
|
const queue = new queue_1.Queue([root]);
|
|
145
193
|
while (queue.size > 0) {
|
|
@@ -157,36 +205,12 @@ class BinaryTree {
|
|
|
157
205
|
queue.push(cur.right);
|
|
158
206
|
}
|
|
159
207
|
};
|
|
160
|
-
if (keyOrNodeOrEntry === null) {
|
|
161
|
-
needInsert = null;
|
|
162
|
-
}
|
|
163
|
-
else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
164
|
-
needInsert = this.createNode(keyOrNodeOrEntry);
|
|
165
|
-
}
|
|
166
|
-
else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
|
|
167
|
-
needInsert = keyOrNodeOrEntry;
|
|
168
|
-
}
|
|
169
|
-
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
170
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
171
|
-
if (key === undefined) {
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
else if (key === null) {
|
|
175
|
-
needInsert = null;
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
needInsert = this.createNode(key, value);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
208
|
if (this.root) {
|
|
185
|
-
inserted = _bfs(this.root,
|
|
209
|
+
inserted = _bfs(this.root, newNode);
|
|
186
210
|
}
|
|
187
211
|
else {
|
|
188
|
-
this._setRoot(
|
|
189
|
-
if (
|
|
212
|
+
this._setRoot(newNode);
|
|
213
|
+
if (newNode) {
|
|
190
214
|
this._size = 1;
|
|
191
215
|
}
|
|
192
216
|
else {
|
|
@@ -1380,6 +1404,60 @@ class BinaryTree {
|
|
|
1380
1404
|
}
|
|
1381
1405
|
return ans;
|
|
1382
1406
|
}
|
|
1407
|
+
/**
|
|
1408
|
+
* Time complexity: O(n)
|
|
1409
|
+
* Space complexity: O(n)
|
|
1410
|
+
*/
|
|
1411
|
+
/**
|
|
1412
|
+
* Time complexity: O(n)
|
|
1413
|
+
* Space complexity: O(n)
|
|
1414
|
+
*
|
|
1415
|
+
* The function "keys" returns an array of keys from a given object.
|
|
1416
|
+
* @returns an array of BTNKey objects.
|
|
1417
|
+
*/
|
|
1418
|
+
keys() {
|
|
1419
|
+
const keys = [];
|
|
1420
|
+
for (const entry of this) {
|
|
1421
|
+
keys.push(entry[0]);
|
|
1422
|
+
}
|
|
1423
|
+
return keys;
|
|
1424
|
+
}
|
|
1425
|
+
/**
|
|
1426
|
+
* Time complexity: O(n)
|
|
1427
|
+
* Space complexity: O(n)
|
|
1428
|
+
*/
|
|
1429
|
+
/**
|
|
1430
|
+
* Time complexity: O(n)
|
|
1431
|
+
* Space complexity: O(n)
|
|
1432
|
+
*
|
|
1433
|
+
* The function "values" returns an array of values from a map-like object.
|
|
1434
|
+
* @returns The `values()` method is returning an array of values (`V`) from the entries in the
|
|
1435
|
+
* object.
|
|
1436
|
+
*/
|
|
1437
|
+
values() {
|
|
1438
|
+
const values = [];
|
|
1439
|
+
for (const entry of this) {
|
|
1440
|
+
values.push(entry[1]);
|
|
1441
|
+
}
|
|
1442
|
+
return values;
|
|
1443
|
+
}
|
|
1444
|
+
/**
|
|
1445
|
+
* Time complexity: O(n)
|
|
1446
|
+
* Space complexity: O(n)
|
|
1447
|
+
*/
|
|
1448
|
+
/**
|
|
1449
|
+
* Time complexity: O(n)
|
|
1450
|
+
* Space complexity: O(n)
|
|
1451
|
+
*
|
|
1452
|
+
* The `clone` function creates a new tree object and copies all the nodes from the original tree to
|
|
1453
|
+
* the new tree.
|
|
1454
|
+
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
1455
|
+
*/
|
|
1456
|
+
clone() {
|
|
1457
|
+
const cloned = this.createTree();
|
|
1458
|
+
this.bfs(node => cloned.add([node.key, node.value]));
|
|
1459
|
+
return cloned;
|
|
1460
|
+
}
|
|
1383
1461
|
/**
|
|
1384
1462
|
* Time complexity: O(n)
|
|
1385
1463
|
* Space complexity: O(1)
|
|
@@ -72,6 +72,19 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
72
72
|
* @returns a new instance of the BST class with the specified options.
|
|
73
73
|
*/
|
|
74
74
|
createTree(options?: Partial<BSTOptions>): TREE;
|
|
75
|
+
/**
|
|
76
|
+
* The function checks if an exemplar is an instance of BSTNode.
|
|
77
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
78
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
79
|
+
*/
|
|
80
|
+
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
|
|
81
|
+
/**
|
|
82
|
+
* The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
|
|
83
|
+
* is valid, otherwise it returns undefined.
|
|
84
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
85
|
+
* @returns a variable `node` which is of type `N` or `undefined`.
|
|
86
|
+
*/
|
|
87
|
+
exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined;
|
|
75
88
|
/**
|
|
76
89
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
77
90
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -100,6 +100,45 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
100
100
|
createTree(options) {
|
|
101
101
|
return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
102
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* 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>`.
|
|
106
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
107
|
+
*/
|
|
108
|
+
isNode(exemplar) {
|
|
109
|
+
return exemplar instanceof BSTNode;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
|
|
113
|
+
* is valid, otherwise it returns undefined.
|
|
114
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
115
|
+
* @returns a variable `node` which is of type `N` or `undefined`.
|
|
116
|
+
*/
|
|
117
|
+
exemplarToNode(exemplar) {
|
|
118
|
+
let node;
|
|
119
|
+
if (exemplar === null || exemplar === undefined) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
else if (this.isNode(exemplar)) {
|
|
123
|
+
node = exemplar;
|
|
124
|
+
}
|
|
125
|
+
else if (this.isEntry(exemplar)) {
|
|
126
|
+
const [key, value] = exemplar;
|
|
127
|
+
if (key === undefined || key === null) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
node = this.createNode(key, value);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
else if (this.isNodeKey(exemplar)) {
|
|
135
|
+
node = this.createNode(exemplar);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
return node;
|
|
141
|
+
}
|
|
103
142
|
/**
|
|
104
143
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
105
144
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -115,28 +154,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
115
154
|
* (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
|
|
116
155
|
*/
|
|
117
156
|
add(keyOrNodeOrEntry) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
let newNode;
|
|
122
|
-
if (keyOrNodeOrEntry instanceof BSTNode) {
|
|
123
|
-
newNode = keyOrNodeOrEntry;
|
|
124
|
-
}
|
|
125
|
-
else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
126
|
-
newNode = this.createNode(keyOrNodeOrEntry);
|
|
127
|
-
}
|
|
128
|
-
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
129
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
130
|
-
if (key === undefined || key === null) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
newNode = this.createNode(key, value);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
157
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
158
|
+
if (newNode === undefined)
|
|
138
159
|
return;
|
|
139
|
-
}
|
|
140
160
|
if (this.root === undefined) {
|
|
141
161
|
this._setRoot(newNode);
|
|
142
162
|
this._size++;
|
|
@@ -58,6 +58,22 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
58
58
|
* @returns a new instance of a RedBlackTree object.
|
|
59
59
|
*/
|
|
60
60
|
createTree(options?: RBTreeOptions): TREE;
|
|
61
|
+
/**
|
|
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>`.
|
|
64
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
65
|
+
* class.
|
|
66
|
+
*/
|
|
67
|
+
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
|
|
68
|
+
/**
|
|
69
|
+
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
70
|
+
* otherwise it returns undefined.
|
|
71
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
|
|
72
|
+
* node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
|
|
73
|
+
* that is not a valid exemplar.
|
|
74
|
+
* @returns a variable `node` which is of type `N | undefined`.
|
|
75
|
+
*/
|
|
76
|
+
exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined;
|
|
61
77
|
/**
|
|
62
78
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
63
79
|
* Space Complexity: O(1)
|
|
@@ -77,28 +77,32 @@ class RedBlackTree extends bst_1.BST {
|
|
|
77
77
|
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
78
78
|
}
|
|
79
79
|
/**
|
|
80
|
-
*
|
|
81
|
-
*
|
|
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>`.
|
|
82
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
83
|
+
* class.
|
|
82
84
|
*/
|
|
85
|
+
isNode(exemplar) {
|
|
86
|
+
return exemplar instanceof RedBlackTreeNode;
|
|
87
|
+
}
|
|
83
88
|
/**
|
|
84
|
-
* The function
|
|
85
|
-
*
|
|
86
|
-
* @
|
|
87
|
-
*
|
|
89
|
+
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
90
|
+
* otherwise it returns undefined.
|
|
91
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
|
|
92
|
+
* node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
|
|
93
|
+
* that is not a valid exemplar.
|
|
94
|
+
* @returns a variable `node` which is of type `N | undefined`.
|
|
88
95
|
*/
|
|
89
|
-
|
|
96
|
+
exemplarToNode(exemplar) {
|
|
90
97
|
let node;
|
|
91
|
-
if (
|
|
92
|
-
node = this.createNode(keyOrNodeOrEntry, undefined, types_1.RBTNColor.RED);
|
|
93
|
-
}
|
|
94
|
-
else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) {
|
|
95
|
-
node = keyOrNodeOrEntry;
|
|
96
|
-
}
|
|
97
|
-
else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
98
|
+
if (exemplar === null || exemplar === undefined) {
|
|
98
99
|
return;
|
|
99
100
|
}
|
|
100
|
-
else if (this.
|
|
101
|
-
|
|
101
|
+
else if (this.isNode(exemplar)) {
|
|
102
|
+
node = exemplar;
|
|
103
|
+
}
|
|
104
|
+
else if (this.isEntry(exemplar)) {
|
|
105
|
+
const [key, value] = exemplar;
|
|
102
106
|
if (key === undefined || key === null) {
|
|
103
107
|
return;
|
|
104
108
|
}
|
|
@@ -106,50 +110,69 @@ class RedBlackTree extends bst_1.BST {
|
|
|
106
110
|
node = this.createNode(key, value, types_1.RBTNColor.RED);
|
|
107
111
|
}
|
|
108
112
|
}
|
|
113
|
+
else if (this.isNodeKey(exemplar)) {
|
|
114
|
+
node = this.createNode(exemplar, undefined, types_1.RBTNColor.RED);
|
|
115
|
+
}
|
|
109
116
|
else {
|
|
110
117
|
return;
|
|
111
118
|
}
|
|
112
|
-
node
|
|
113
|
-
|
|
119
|
+
return node;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
123
|
+
* Space Complexity: O(1)
|
|
124
|
+
*/
|
|
125
|
+
/**
|
|
126
|
+
* The function adds a node to a Red-Black Tree data structure.
|
|
127
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
128
|
+
* @returns The method `add` returns either an instance of `N` (the node that was added) or
|
|
129
|
+
* `undefined`.
|
|
130
|
+
*/
|
|
131
|
+
add(keyOrNodeOrEntry) {
|
|
132
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
133
|
+
if (newNode === undefined)
|
|
134
|
+
return;
|
|
135
|
+
newNode.left = this.Sentinel;
|
|
136
|
+
newNode.right = this.Sentinel;
|
|
114
137
|
let y = undefined;
|
|
115
138
|
let x = this.root;
|
|
116
139
|
while (x !== this.Sentinel) {
|
|
117
140
|
y = x;
|
|
118
141
|
if (x) {
|
|
119
|
-
if (
|
|
142
|
+
if (newNode.key < x.key) {
|
|
120
143
|
x = x.left;
|
|
121
144
|
}
|
|
122
|
-
else if (
|
|
145
|
+
else if (newNode.key > x.key) {
|
|
123
146
|
x = x === null || x === void 0 ? void 0 : x.right;
|
|
124
147
|
}
|
|
125
148
|
else {
|
|
126
|
-
if (
|
|
127
|
-
this._replaceNode(x,
|
|
149
|
+
if (newNode !== x) {
|
|
150
|
+
this._replaceNode(x, newNode);
|
|
128
151
|
}
|
|
129
152
|
return;
|
|
130
153
|
}
|
|
131
154
|
}
|
|
132
155
|
}
|
|
133
|
-
|
|
156
|
+
newNode.parent = y;
|
|
134
157
|
if (y === undefined) {
|
|
135
|
-
this._setRoot(
|
|
158
|
+
this._setRoot(newNode);
|
|
136
159
|
}
|
|
137
|
-
else if (
|
|
138
|
-
y.left =
|
|
160
|
+
else if (newNode.key < y.key) {
|
|
161
|
+
y.left = newNode;
|
|
139
162
|
}
|
|
140
163
|
else {
|
|
141
|
-
y.right =
|
|
164
|
+
y.right = newNode;
|
|
142
165
|
}
|
|
143
|
-
if (
|
|
144
|
-
|
|
166
|
+
if (newNode.parent === undefined) {
|
|
167
|
+
newNode.color = types_1.RBTNColor.BLACK;
|
|
145
168
|
this._size++;
|
|
146
169
|
return;
|
|
147
170
|
}
|
|
148
|
-
if (
|
|
171
|
+
if (newNode.parent.parent === undefined) {
|
|
149
172
|
this._size++;
|
|
150
173
|
return;
|
|
151
174
|
}
|
|
152
|
-
this._fixInsert(
|
|
175
|
+
this._fixInsert(newNode);
|
|
153
176
|
this._size++;
|
|
154
177
|
}
|
|
155
178
|
/**
|
|
@@ -41,6 +41,22 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
41
41
|
*/
|
|
42
42
|
createNode(key: BTNKey, value?: V, count?: number): N;
|
|
43
43
|
createTree(options?: TreeMultimapOptions): TREE;
|
|
44
|
+
/**
|
|
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>`.
|
|
47
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
48
|
+
* class.
|
|
49
|
+
*/
|
|
50
|
+
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
|
|
51
|
+
/**
|
|
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
|
|
54
|
+
* the value type and `N` represents the node type.
|
|
55
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
56
|
+
* times the node should be created. If not provided, it defaults to 1.
|
|
57
|
+
* @returns a value of type `N` (the generic type parameter) or `undefined`.
|
|
58
|
+
*/
|
|
59
|
+
exemplarToNode(exemplar: BTNodeExemplar<V, N>, count?: number): N | undefined;
|
|
44
60
|
/**
|
|
45
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.
|
|
46
62
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -121,6 +137,18 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
121
137
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
122
138
|
*/
|
|
123
139
|
clear(): void;
|
|
140
|
+
/**
|
|
141
|
+
* Time complexity: O(n)
|
|
142
|
+
* Space complexity: O(n)
|
|
143
|
+
*/
|
|
144
|
+
/**
|
|
145
|
+
* Time complexity: O(n)
|
|
146
|
+
* Space complexity: O(n)
|
|
147
|
+
*
|
|
148
|
+
* The `clone` function creates a deep copy of a tree object.
|
|
149
|
+
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
150
|
+
*/
|
|
151
|
+
clone(): TREE;
|
|
124
152
|
/**
|
|
125
153
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
126
154
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -51,6 +51,48 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
51
51
|
createTree(options) {
|
|
52
52
|
return new TreeMultimap([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
53
53
|
}
|
|
54
|
+
/**
|
|
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>`.
|
|
57
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
58
|
+
* class.
|
|
59
|
+
*/
|
|
60
|
+
isNode(exemplar) {
|
|
61
|
+
return exemplar instanceof TreeMultimapNode;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
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
|
|
66
|
+
* the value type and `N` represents the node type.
|
|
67
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
68
|
+
* times the node should be created. If not provided, it defaults to 1.
|
|
69
|
+
* @returns a value of type `N` (the generic type parameter) or `undefined`.
|
|
70
|
+
*/
|
|
71
|
+
exemplarToNode(exemplar, count = 1) {
|
|
72
|
+
let node;
|
|
73
|
+
if (exemplar === undefined || exemplar === null) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
else if (this.isNode(exemplar)) {
|
|
77
|
+
node = exemplar;
|
|
78
|
+
}
|
|
79
|
+
else if (this.isEntry(exemplar)) {
|
|
80
|
+
const [key, value] = exemplar;
|
|
81
|
+
if (key === undefined || key === null) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
node = this.createNode(key, value, count);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else if (this.isNodeKey(exemplar)) {
|
|
89
|
+
node = this.createNode(exemplar, undefined, count);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
return node;
|
|
95
|
+
}
|
|
54
96
|
/**
|
|
55
97
|
* 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.
|
|
56
98
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -68,28 +110,9 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
68
110
|
* @returns either a node (`N`) or `undefined`.
|
|
69
111
|
*/
|
|
70
112
|
add(keyOrNodeOrEntry, count = 1) {
|
|
71
|
-
|
|
72
|
-
if (
|
|
113
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry, count);
|
|
114
|
+
if (newNode === undefined)
|
|
73
115
|
return;
|
|
74
|
-
}
|
|
75
|
-
else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
|
|
76
|
-
newNode = keyOrNodeOrEntry;
|
|
77
|
-
}
|
|
78
|
-
else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
79
|
-
newNode = this.createNode(keyOrNodeOrEntry, undefined, count);
|
|
80
|
-
}
|
|
81
|
-
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
82
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
83
|
-
if (key === undefined || key === null) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
newNode = this.createNode(key, value, count);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
116
|
const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
|
|
94
117
|
const inserted = super.add(newNode);
|
|
95
118
|
if (inserted) {
|
|
@@ -257,6 +280,22 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
257
280
|
super.clear();
|
|
258
281
|
this._count = 0;
|
|
259
282
|
}
|
|
283
|
+
/**
|
|
284
|
+
* Time complexity: O(n)
|
|
285
|
+
* Space complexity: O(n)
|
|
286
|
+
*/
|
|
287
|
+
/**
|
|
288
|
+
* Time complexity: O(n)
|
|
289
|
+
* Space complexity: O(n)
|
|
290
|
+
*
|
|
291
|
+
* The `clone` function creates a deep copy of a tree object.
|
|
292
|
+
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
293
|
+
*/
|
|
294
|
+
clone() {
|
|
295
|
+
const cloned = this.createTree();
|
|
296
|
+
this.bfs(node => cloned.add([node.key, node.value], node.count));
|
|
297
|
+
return cloned;
|
|
298
|
+
}
|
|
260
299
|
/**
|
|
261
300
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
262
301
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|