data-structure-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/CHANGELOG.md +1 -1
- package/README.md +58 -55
- package/README_zh-CN.md +58 -56
- package/benchmark/report.html +1 -46
- package/benchmark/report.json +23 -458
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +8 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +9 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +15 -10
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +55 -49
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +8 -1
- package/dist/cjs/data-structures/binary-tree/bst.js +9 -0
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +10 -16
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +16 -29
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +8 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +9 -0
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +0 -110
- package/dist/cjs/data-structures/queue/deque.js +1 -172
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +8 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +9 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +15 -10
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +55 -49
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +8 -1
- package/dist/mjs/data-structures/binary-tree/bst.js +9 -0
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +10 -16
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +16 -28
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +8 -1
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +9 -0
- package/dist/mjs/data-structures/queue/deque.d.ts +0 -110
- package/dist/mjs/data-structures/queue/deque.js +0 -170
- package/dist/umd/data-structure-typed.js +94 -239
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +11 -1
- package/src/data-structures/binary-tree/binary-tree.ts +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
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +15 -8
- package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +5 -5
- package/test/unit/data-structures/queue/deque.test.ts +265 -367
- package/test/unit/data-structures/queue/queue.test.ts +158 -158
|
@@ -66,10 +66,6 @@ export class BinaryTreeNode {
|
|
|
66
66
|
* 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
|
|
67
67
|
* 4. Subtrees: Each child of a node forms the root of a subtree.
|
|
68
68
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
69
|
-
* 6. Internal Nodes: Nodes with at least one child are internal.
|
|
70
|
-
* 7. Balanced Trees: The heights of the left and right subtrees of any node differ by no more than one.
|
|
71
|
-
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
72
|
-
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
73
69
|
*/
|
|
74
70
|
export class BinaryTree extends IterableEntryBase {
|
|
75
71
|
iterationType = IterationType.ITERATIVE;
|
|
@@ -198,44 +194,50 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
198
194
|
* @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
|
|
199
195
|
*/
|
|
200
196
|
add(keyOrNodeOrEntry, value) {
|
|
201
|
-
let inserted;
|
|
202
197
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
203
198
|
if (newNode === undefined)
|
|
204
199
|
return;
|
|
205
|
-
//
|
|
206
|
-
if (
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
200
|
+
// If the tree is empty, directly set the new node as the root node
|
|
201
|
+
if (!this.root) {
|
|
202
|
+
this._root = newNode;
|
|
203
|
+
this._size = 1;
|
|
204
|
+
return newNode;
|
|
205
|
+
}
|
|
206
|
+
const queue = new Queue([this.root]);
|
|
207
|
+
let potentialParent; // Record the parent node of the potential insertion location
|
|
208
|
+
while (queue.size > 0) {
|
|
209
|
+
const cur = queue.shift();
|
|
210
|
+
if (!cur)
|
|
211
|
+
continue;
|
|
212
|
+
// Check for duplicate keys when newNode is not null
|
|
213
|
+
if (newNode !== null && cur.key === newNode.key) {
|
|
214
|
+
this._replaceNode(cur, newNode);
|
|
215
|
+
return newNode; // If duplicate keys are found, no insertion is performed
|
|
216
|
+
}
|
|
217
|
+
// Record the first possible insertion location found
|
|
218
|
+
if (potentialParent === undefined && (cur.left === undefined || cur.right === undefined)) {
|
|
219
|
+
potentialParent = cur;
|
|
220
|
+
}
|
|
221
|
+
// Continue traversing the left and right subtrees
|
|
222
|
+
if (cur.left !== null) {
|
|
223
|
+
cur.left && queue.push(cur.left);
|
|
224
|
+
}
|
|
225
|
+
if (cur.right !== null) {
|
|
226
|
+
cur.right && queue.push(cur.right);
|
|
223
227
|
}
|
|
224
|
-
};
|
|
225
|
-
if (this.root) {
|
|
226
|
-
inserted = _bfs(this.root, newNode);
|
|
227
228
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
if (
|
|
231
|
-
|
|
229
|
+
// At the end of the traversal, if the insertion position is found, insert
|
|
230
|
+
if (potentialParent) {
|
|
231
|
+
if (potentialParent.left === undefined) {
|
|
232
|
+
potentialParent.left = newNode;
|
|
232
233
|
}
|
|
233
|
-
else {
|
|
234
|
-
|
|
234
|
+
else if (potentialParent.right === undefined) {
|
|
235
|
+
potentialParent.right = newNode;
|
|
235
236
|
}
|
|
236
|
-
|
|
237
|
+
this._size++;
|
|
238
|
+
return newNode;
|
|
237
239
|
}
|
|
238
|
-
return
|
|
240
|
+
return undefined; // If the insertion position cannot be found, return undefined
|
|
239
241
|
}
|
|
240
242
|
/**
|
|
241
243
|
* Time Complexity: O(k log n) - O(k * n)
|
|
@@ -977,7 +979,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
977
979
|
* @returns a boolean value.
|
|
978
980
|
*/
|
|
979
981
|
isRealNode(node) {
|
|
980
|
-
return node instanceof BinaryTreeNode && node.key
|
|
982
|
+
return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
|
|
981
983
|
}
|
|
982
984
|
/**
|
|
983
985
|
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
@@ -985,7 +987,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
985
987
|
* @returns a boolean value.
|
|
986
988
|
*/
|
|
987
989
|
isNIL(node) {
|
|
988
|
-
return node instanceof BinaryTreeNode && node.key
|
|
990
|
+
return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
|
|
989
991
|
}
|
|
990
992
|
/**
|
|
991
993
|
* The function checks if a given node is a real node or null.
|
|
@@ -996,7 +998,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
996
998
|
return this.isRealNode(node) || node === null;
|
|
997
999
|
}
|
|
998
1000
|
/**
|
|
999
|
-
* The function "isNotNodeInstance" checks if a potential key is a
|
|
1001
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
1000
1002
|
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
1001
1003
|
* data type.
|
|
1002
1004
|
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
@@ -1277,19 +1279,23 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1277
1279
|
return levelsNodes;
|
|
1278
1280
|
}
|
|
1279
1281
|
/**
|
|
1280
|
-
*
|
|
1281
|
-
*
|
|
1282
|
-
|
|
1283
|
-
|
|
1282
|
+
* Time Complexity: O(log n)
|
|
1283
|
+
* Space Complexity: O(1)
|
|
1284
|
+
*/
|
|
1285
|
+
/**
|
|
1286
|
+
* Time Complexity: O(log n)
|
|
1287
|
+
* Space Complexity: O(1)
|
|
1288
|
+
*
|
|
1289
|
+
* The function returns the predecessor of a given node in a tree.
|
|
1290
|
+
* @param {N} node - The parameter `node` is of type `RedBlackTreeNode`, which represents a node in a
|
|
1291
|
+
* tree.
|
|
1292
|
+
* @returns the predecessor of the given 'node'.
|
|
1284
1293
|
*/
|
|
1285
1294
|
getPredecessor(node) {
|
|
1286
|
-
|
|
1287
|
-
if (!this.isRealNode(node))
|
|
1288
|
-
return undefined;
|
|
1289
|
-
if (node.left) {
|
|
1295
|
+
if (this.isRealNode(node.left)) {
|
|
1290
1296
|
let predecessor = node.left;
|
|
1291
1297
|
while (!this.isRealNode(predecessor) || (this.isRealNode(predecessor.right) && predecessor.right !== node)) {
|
|
1292
|
-
if (predecessor) {
|
|
1298
|
+
if (this.isRealNode(predecessor)) {
|
|
1293
1299
|
predecessor = predecessor.right;
|
|
1294
1300
|
}
|
|
1295
1301
|
}
|
|
@@ -1307,13 +1313,13 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1307
1313
|
*/
|
|
1308
1314
|
getSuccessor(x) {
|
|
1309
1315
|
x = this.ensureNode(x);
|
|
1310
|
-
if (!x)
|
|
1316
|
+
if (!this.isRealNode(x))
|
|
1311
1317
|
return undefined;
|
|
1312
|
-
if (x.right) {
|
|
1318
|
+
if (this.isRealNode(x.right)) {
|
|
1313
1319
|
return this.getLeftMost(x.right);
|
|
1314
1320
|
}
|
|
1315
1321
|
let y = x.parent;
|
|
1316
|
-
while (y &&
|
|
1322
|
+
while (this.isRealNode(y) && x === y.right) {
|
|
1317
1323
|
x = y;
|
|
1318
1324
|
y = y.parent;
|
|
1319
1325
|
}
|
|
@@ -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.
|
|
@@ -390,6 +390,15 @@ export class BST extends BinaryTree {
|
|
|
390
390
|
}
|
|
391
391
|
}
|
|
392
392
|
}
|
|
393
|
+
/**
|
|
394
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
395
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
396
|
+
* data type.
|
|
397
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
398
|
+
*/
|
|
399
|
+
isNotNodeInstance(potentialKey) {
|
|
400
|
+
return !(potentialKey instanceof BSTNode);
|
|
401
|
+
}
|
|
393
402
|
/**
|
|
394
403
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
395
404
|
* 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.
|
|
@@ -86,6 +86,15 @@ export class RedBlackTree extends BST {
|
|
|
86
86
|
isNode(exemplar) {
|
|
87
87
|
return exemplar instanceof RedBlackTreeNode;
|
|
88
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
91
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
92
|
+
* data type.
|
|
93
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
94
|
+
*/
|
|
95
|
+
isNotNodeInstance(potentialKey) {
|
|
96
|
+
return !(potentialKey instanceof RedBlackTreeNode);
|
|
97
|
+
}
|
|
89
98
|
/**
|
|
90
99
|
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
91
100
|
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
|
|
@@ -264,7 +273,9 @@ export class RedBlackTree extends BST {
|
|
|
264
273
|
* Space Complexity: O(1)
|
|
265
274
|
*/
|
|
266
275
|
isRealNode(node) {
|
|
267
|
-
|
|
276
|
+
if (node === this.Sentinel || node === undefined)
|
|
277
|
+
return false;
|
|
278
|
+
return node instanceof RedBlackTreeNode;
|
|
268
279
|
}
|
|
269
280
|
/**
|
|
270
281
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
@@ -298,34 +309,11 @@ export class RedBlackTree extends BST {
|
|
|
298
309
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
299
310
|
}
|
|
300
311
|
/**
|
|
301
|
-
* Time Complexity: O(log n)
|
|
312
|
+
* Time Complexity: O(log n)
|
|
302
313
|
* Space Complexity: O(1)
|
|
303
314
|
*/
|
|
304
315
|
/**
|
|
305
|
-
* Time Complexity: O(log n)
|
|
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
|
-
if (x.right !== this.Sentinel) {
|
|
314
|
-
return this.getLeftMost(x.right) ?? undefined;
|
|
315
|
-
}
|
|
316
|
-
let y = x.parent;
|
|
317
|
-
while (y !== this.Sentinel && y !== undefined && x === y.right) {
|
|
318
|
-
x = y;
|
|
319
|
-
y = y.parent;
|
|
320
|
-
}
|
|
321
|
-
return y;
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
325
|
-
* Space Complexity: O(1)
|
|
326
|
-
*/
|
|
327
|
-
/**
|
|
328
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
316
|
+
* Time Complexity: O(log n)
|
|
329
317
|
* Space Complexity: O(1)
|
|
330
318
|
*
|
|
331
319
|
* The function returns the predecessor of a given node in a red-black tree.
|
|
@@ -334,11 +322,11 @@ export class RedBlackTree extends BST {
|
|
|
334
322
|
* @returns the predecessor of the given RedBlackTreeNode 'x'.
|
|
335
323
|
*/
|
|
336
324
|
getPredecessor(x) {
|
|
337
|
-
if (x.left
|
|
325
|
+
if (this.isRealNode(x.left)) {
|
|
338
326
|
return this.getRightMost(x.left);
|
|
339
327
|
}
|
|
340
328
|
let y = x.parent;
|
|
341
|
-
while (
|
|
329
|
+
while (this.isRealNode(y) && x === y.left) {
|
|
342
330
|
x = y;
|
|
343
331
|
y = y.parent;
|
|
344
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 @@ export class TreeMultimap extends 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
|
-
}
|
|
@@ -789,173 +789,3 @@ export class Deque extends IterableElementBase {
|
|
|
789
789
|
return { bucketIndex, indexInBucket };
|
|
790
790
|
}
|
|
791
791
|
}
|
|
792
|
-
// O(1) time complexity of obtaining the element
|
|
793
|
-
// O(n) time complexity of adding at the beginning and the end
|
|
794
|
-
// todo tested slowest one
|
|
795
|
-
export class ObjectDeque {
|
|
796
|
-
constructor(capacity) {
|
|
797
|
-
if (capacity !== undefined)
|
|
798
|
-
this._capacity = capacity;
|
|
799
|
-
}
|
|
800
|
-
_nodes = {};
|
|
801
|
-
get nodes() {
|
|
802
|
-
return this._nodes;
|
|
803
|
-
}
|
|
804
|
-
_capacity = Number.MAX_SAFE_INTEGER;
|
|
805
|
-
get capacity() {
|
|
806
|
-
return this._capacity;
|
|
807
|
-
}
|
|
808
|
-
_first = -1;
|
|
809
|
-
get first() {
|
|
810
|
-
return this._first;
|
|
811
|
-
}
|
|
812
|
-
_last = -1;
|
|
813
|
-
get last() {
|
|
814
|
-
return this._last;
|
|
815
|
-
}
|
|
816
|
-
_size = 0;
|
|
817
|
-
get size() {
|
|
818
|
-
return this._size;
|
|
819
|
-
}
|
|
820
|
-
/**
|
|
821
|
-
* Time Complexity: O(1)
|
|
822
|
-
* Space Complexity: O(1)
|
|
823
|
-
*/
|
|
824
|
-
/**
|
|
825
|
-
* Time Complexity: O(1)
|
|
826
|
-
* Space Complexity: O(1)
|
|
827
|
-
*
|
|
828
|
-
* The "addFirst" function adds an element to the beginning of an array-like data structure.
|
|
829
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the beginning of the data
|
|
830
|
-
* structure.
|
|
831
|
-
*/
|
|
832
|
-
addFirst(element) {
|
|
833
|
-
if (this.size === 0) {
|
|
834
|
-
const mid = Math.floor(this.capacity / 2);
|
|
835
|
-
this._first = mid;
|
|
836
|
-
this._last = mid;
|
|
837
|
-
}
|
|
838
|
-
else {
|
|
839
|
-
this._first--;
|
|
840
|
-
}
|
|
841
|
-
this.nodes[this.first] = element;
|
|
842
|
-
this._size++;
|
|
843
|
-
}
|
|
844
|
-
/**
|
|
845
|
-
* Time Complexity: O(1)
|
|
846
|
-
* Space Complexity: O(1)
|
|
847
|
-
*/
|
|
848
|
-
/**
|
|
849
|
-
* Time Complexity: O(1)
|
|
850
|
-
* Space Complexity: O(1)
|
|
851
|
-
*
|
|
852
|
-
* The addLast function adds an element to the end of an array-like data structure.
|
|
853
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the end of the data structure.
|
|
854
|
-
*/
|
|
855
|
-
addLast(element) {
|
|
856
|
-
if (this.size === 0) {
|
|
857
|
-
const mid = Math.floor(this.capacity / 2);
|
|
858
|
-
this._first = mid;
|
|
859
|
-
this._last = mid;
|
|
860
|
-
}
|
|
861
|
-
else {
|
|
862
|
-
this._last++;
|
|
863
|
-
}
|
|
864
|
-
this.nodes[this.last] = element;
|
|
865
|
-
this._size++;
|
|
866
|
-
}
|
|
867
|
-
/**
|
|
868
|
-
* Time Complexity: O(1)
|
|
869
|
-
* Space Complexity: O(1)
|
|
870
|
-
*/
|
|
871
|
-
/**
|
|
872
|
-
* Time Complexity: O(1)
|
|
873
|
-
* Space Complexity: O(1)
|
|
874
|
-
*
|
|
875
|
-
* The function `pollFirst()` removes and returns the first element in a data structure.
|
|
876
|
-
* @returns The element of the first element in the data structure.
|
|
877
|
-
*/
|
|
878
|
-
pollFirst() {
|
|
879
|
-
if (!this.size)
|
|
880
|
-
return;
|
|
881
|
-
const element = this.getFirst();
|
|
882
|
-
delete this.nodes[this.first];
|
|
883
|
-
this._first++;
|
|
884
|
-
this._size--;
|
|
885
|
-
return element;
|
|
886
|
-
}
|
|
887
|
-
/**
|
|
888
|
-
* Time Complexity: O(1)
|
|
889
|
-
* Space Complexity: O(1)
|
|
890
|
-
*/
|
|
891
|
-
/**
|
|
892
|
-
* Time Complexity: O(1)
|
|
893
|
-
* Space Complexity: O(1)
|
|
894
|
-
*
|
|
895
|
-
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
896
|
-
* @returns The element at the first position of the `_nodes` array.
|
|
897
|
-
*/
|
|
898
|
-
getFirst() {
|
|
899
|
-
if (this.size)
|
|
900
|
-
return this.nodes[this.first];
|
|
901
|
-
}
|
|
902
|
-
/**
|
|
903
|
-
* Time Complexity: O(1)
|
|
904
|
-
* Space Complexity: O(1)
|
|
905
|
-
*/
|
|
906
|
-
/**
|
|
907
|
-
* Time Complexity: O(1)
|
|
908
|
-
* Space Complexity: O(1)
|
|
909
|
-
*
|
|
910
|
-
* The `pollLast()` function removes and returns the last element in a data structure.
|
|
911
|
-
* @returns The element that was removed from the data structure.
|
|
912
|
-
*/
|
|
913
|
-
pollLast() {
|
|
914
|
-
if (!this.size)
|
|
915
|
-
return;
|
|
916
|
-
const element = this.getLast();
|
|
917
|
-
delete this.nodes[this.last];
|
|
918
|
-
this._last--;
|
|
919
|
-
this._size--;
|
|
920
|
-
return element;
|
|
921
|
-
}
|
|
922
|
-
/**
|
|
923
|
-
* Time Complexity: O(1)
|
|
924
|
-
* Space Complexity: O(1)
|
|
925
|
-
*/
|
|
926
|
-
/**
|
|
927
|
-
* Time Complexity: O(1)
|
|
928
|
-
* Space Complexity: O(1)
|
|
929
|
-
*
|
|
930
|
-
* The `getLast()` function returns the last element in an array-like data structure.
|
|
931
|
-
* @returns The last element in the array "_nodes" is being returned.
|
|
932
|
-
*/
|
|
933
|
-
getLast() {
|
|
934
|
-
if (this.size)
|
|
935
|
-
return this.nodes[this.last];
|
|
936
|
-
}
|
|
937
|
-
/**
|
|
938
|
-
* Time Complexity: O(1)
|
|
939
|
-
* Space Complexity: O(1)
|
|
940
|
-
*/
|
|
941
|
-
/**
|
|
942
|
-
* Time Complexity: O(1)
|
|
943
|
-
* Space Complexity: O(1)
|
|
944
|
-
*
|
|
945
|
-
* The get function returns the element at the specified index in an array-like data structure.
|
|
946
|
-
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
947
|
-
* retrieve from the array.
|
|
948
|
-
* @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
|
|
949
|
-
* index, `undefined` is returned.
|
|
950
|
-
*/
|
|
951
|
-
get(index) {
|
|
952
|
-
return this.nodes[this.first + index] || undefined;
|
|
953
|
-
}
|
|
954
|
-
/**
|
|
955
|
-
* The function checks if the size of a data structure is less than or equal to zero.
|
|
956
|
-
* @returns The method is returning a boolean element indicating whether the size of the object is less than or equal to 0.
|
|
957
|
-
*/
|
|
958
|
-
isEmpty() {
|
|
959
|
-
return this.size <= 0;
|
|
960
|
-
}
|
|
961
|
-
}
|