data-structure-typed 1.49.5 → 1.49.6
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 +14 -23
- package/benchmark/report.html +14 -23
- package/benchmark/report.json +163 -256
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +192 -149
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/cjs/data-structures/binary-tree/bst.js +113 -89
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +3 -3
- package/dist/cjs/types/common.js +2 -2
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +192 -149
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/mjs/data-structures/binary-tree/bst.js +113 -89
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +3 -3
- package/dist/mjs/types/common.js +2 -2
- package/dist/umd/data-structure-typed.js +497 -419
- 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 +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +253 -205
- package/src/data-structures/binary-tree/bst.ts +125 -104
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -12
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +37 -0
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -16
- package/test/performance/data-structures/binary-tree/bst.test.ts +5 -13
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +5 -15
- package/test/performance/data-structures/comparison/comparison.test.ts +13 -36
- package/test/performance/data-structures/graph/directed-graph.test.ts +3 -14
- package/test/performance/data-structures/hash/hash-map.test.ts +11 -34
- package/test/performance/data-structures/heap/heap.test.ts +5 -18
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +0 -2
- package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +2 -4
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +4 -14
- package/test/performance/data-structures/queue/queue.test.ts +8 -25
- package/test/performance/data-structures/stack/stack.test.ts +6 -18
- package/test/performance/data-structures/trie/trie.test.ts +2 -6
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -5
- package/test/unit/data-structures/binary-tree/bst.test.ts +17 -1
- package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
- package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
- package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
|
@@ -24,18 +24,18 @@ export class AVLTreeNode extends BSTNode {
|
|
|
24
24
|
*/
|
|
25
25
|
export class AVLTree extends BST {
|
|
26
26
|
/**
|
|
27
|
-
* The constructor function initializes an AVLTree object with optional
|
|
28
|
-
* @param [
|
|
29
|
-
* objects. It represents a collection of
|
|
27
|
+
* The constructor function initializes an AVLTree object with optional nodes and options.
|
|
28
|
+
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
|
|
29
|
+
* objects. It represents a collection of nodes that will be added to the AVL tree during
|
|
30
30
|
* initialization.
|
|
31
31
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
32
32
|
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
33
33
|
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
34
34
|
*/
|
|
35
|
-
constructor(
|
|
35
|
+
constructor(nodes, options) {
|
|
36
36
|
super([], options);
|
|
37
|
-
if (
|
|
38
|
-
super.addMany(
|
|
37
|
+
if (nodes)
|
|
38
|
+
super.addMany(nodes);
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
41
|
* The function creates a new AVL tree node with the specified key and value.
|
|
@@ -64,12 +64,12 @@ export class AVLTree extends BST {
|
|
|
64
64
|
});
|
|
65
65
|
}
|
|
66
66
|
/**
|
|
67
|
-
* The function checks if an
|
|
68
|
-
* @param
|
|
69
|
-
* @returns a boolean value indicating whether the
|
|
67
|
+
* The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
|
|
68
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
69
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
|
|
70
70
|
*/
|
|
71
|
-
isNode(
|
|
72
|
-
return
|
|
71
|
+
isNode(keyOrNodeOrEntry) {
|
|
72
|
+
return keyOrNodeOrEntry instanceof AVLTreeNode;
|
|
73
73
|
}
|
|
74
74
|
/**
|
|
75
75
|
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
@@ -81,12 +81,13 @@ export class AVLTree extends BST {
|
|
|
81
81
|
return !(potentialKey instanceof AVLTreeNode);
|
|
82
82
|
}
|
|
83
83
|
/**
|
|
84
|
-
* Time Complexity: O(log n)
|
|
85
|
-
* Space Complexity: O(1)
|
|
84
|
+
* Time Complexity: O(log n)
|
|
85
|
+
* Space Complexity: O(1)
|
|
86
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
86
87
|
*/
|
|
87
88
|
/**
|
|
88
|
-
* Time Complexity: O(log n)
|
|
89
|
-
* Space Complexity: O(1)
|
|
89
|
+
* Time Complexity: O(log n)
|
|
90
|
+
* Space Complexity: O(1)
|
|
90
91
|
*
|
|
91
92
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
92
93
|
* a new node.
|
|
@@ -98,19 +99,19 @@ export class AVLTree extends BST {
|
|
|
98
99
|
*/
|
|
99
100
|
add(keyOrNodeOrEntry, value) {
|
|
100
101
|
if (keyOrNodeOrEntry === null)
|
|
101
|
-
return
|
|
102
|
+
return false;
|
|
102
103
|
const inserted = super.add(keyOrNodeOrEntry, value);
|
|
103
104
|
if (inserted)
|
|
104
|
-
this._balancePath(
|
|
105
|
+
this._balancePath(keyOrNodeOrEntry);
|
|
105
106
|
return inserted;
|
|
106
107
|
}
|
|
107
108
|
/**
|
|
108
|
-
* Time Complexity: O(log n)
|
|
109
|
-
* Space Complexity: O(1)
|
|
109
|
+
* Time Complexity: O(log n)
|
|
110
|
+
* Space Complexity: O(1)
|
|
110
111
|
*/
|
|
111
112
|
/**
|
|
112
|
-
* Time Complexity: O(log n)
|
|
113
|
-
* Space Complexity: O(1)
|
|
113
|
+
* Time Complexity: O(log n)
|
|
114
|
+
* Space Complexity: O(1)
|
|
114
115
|
*
|
|
115
116
|
* The function overrides the delete method of a binary tree, performs the deletion, and then
|
|
116
117
|
* balances the tree if necessary.
|
|
@@ -164,12 +165,13 @@ export class AVLTree extends BST {
|
|
|
164
165
|
return undefined;
|
|
165
166
|
}
|
|
166
167
|
/**
|
|
167
|
-
* Time Complexity: O(1)
|
|
168
|
-
* Space Complexity: O(1)
|
|
168
|
+
* Time Complexity: O(1)
|
|
169
|
+
* Space Complexity: O(1)
|
|
170
|
+
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
169
171
|
*/
|
|
170
172
|
/**
|
|
171
|
-
* Time Complexity: O(1)
|
|
172
|
-
* Space Complexity: O(1)
|
|
173
|
+
* Time Complexity: O(1)
|
|
174
|
+
* Space Complexity: O(1)
|
|
173
175
|
*
|
|
174
176
|
* The function calculates the balance factor of a node in a binary tree.
|
|
175
177
|
* @param {N} node - The parameter "node" represents a node in a binary tree data structure.
|
|
@@ -187,12 +189,13 @@ export class AVLTree extends BST {
|
|
|
187
189
|
return node.right.height - node.left.height;
|
|
188
190
|
}
|
|
189
191
|
/**
|
|
190
|
-
* Time Complexity: O(1)
|
|
191
|
-
* Space Complexity: O(1)
|
|
192
|
+
* Time Complexity: O(1)
|
|
193
|
+
* Space Complexity: O(1)
|
|
194
|
+
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
192
195
|
*/
|
|
193
196
|
/**
|
|
194
|
-
* Time Complexity: O(1)
|
|
195
|
-
* Space Complexity: O(1)
|
|
197
|
+
* Time Complexity: O(1)
|
|
198
|
+
* Space Complexity: O(1)
|
|
196
199
|
*
|
|
197
200
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
198
201
|
* right children.
|
|
@@ -211,12 +214,13 @@ export class AVLTree extends BST {
|
|
|
211
214
|
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
212
215
|
}
|
|
213
216
|
/**
|
|
214
|
-
* Time Complexity: O(log n)
|
|
215
|
-
* Space Complexity: O(1)
|
|
217
|
+
* Time Complexity: O(log n)
|
|
218
|
+
* Space Complexity: O(1)
|
|
219
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size.
|
|
216
220
|
*/
|
|
217
221
|
/**
|
|
218
|
-
* Time Complexity: O(log n)
|
|
219
|
-
* Space Complexity: O(1)
|
|
222
|
+
* Time Complexity: O(log n)
|
|
223
|
+
* Space Complexity: O(1)
|
|
220
224
|
*
|
|
221
225
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
222
226
|
* to restore balance in an AVL tree after inserting a node.
|
|
@@ -224,6 +228,7 @@ export class AVLTree extends BST {
|
|
|
224
228
|
* AVL tree that needs to be balanced.
|
|
225
229
|
*/
|
|
226
230
|
_balancePath(node) {
|
|
231
|
+
node = this.ensureNode(node);
|
|
227
232
|
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
|
|
228
233
|
for (let i = 0; i < path.length; i++) {
|
|
229
234
|
// second O(log n)
|
|
@@ -263,12 +268,13 @@ export class AVLTree extends BST {
|
|
|
263
268
|
}
|
|
264
269
|
}
|
|
265
270
|
/**
|
|
266
|
-
* Time Complexity: O(1)
|
|
267
|
-
* Space Complexity: O(1)
|
|
271
|
+
* Time Complexity: O(1)
|
|
272
|
+
* Space Complexity: O(1)
|
|
273
|
+
* constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory.
|
|
268
274
|
*/
|
|
269
275
|
/**
|
|
270
|
-
* Time Complexity: O(1)
|
|
271
|
-
* Space Complexity: O(1)
|
|
276
|
+
* Time Complexity: O(1)
|
|
277
|
+
* Space Complexity: O(1)
|
|
272
278
|
*
|
|
273
279
|
* The function `_balanceLL` performs a left-left rotation to balance a binary tree.
|
|
274
280
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -304,12 +310,12 @@ export class AVLTree extends BST {
|
|
|
304
310
|
this._updateHeight(B);
|
|
305
311
|
}
|
|
306
312
|
/**
|
|
307
|
-
* Time Complexity: O(1)
|
|
308
|
-
* Space Complexity: O(1)
|
|
313
|
+
* Time Complexity: O(1)
|
|
314
|
+
* Space Complexity: O(1)
|
|
309
315
|
*/
|
|
310
316
|
/**
|
|
311
|
-
* Time Complexity: O(1)
|
|
312
|
-
* Space Complexity: O(1)
|
|
317
|
+
* Time Complexity: O(1)
|
|
318
|
+
* Space Complexity: O(1)
|
|
313
319
|
*
|
|
314
320
|
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
315
321
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -360,12 +366,12 @@ export class AVLTree extends BST {
|
|
|
360
366
|
C && this._updateHeight(C);
|
|
361
367
|
}
|
|
362
368
|
/**
|
|
363
|
-
* Time Complexity: O(1)
|
|
364
|
-
* Space Complexity: O(1)
|
|
369
|
+
* Time Complexity: O(1)
|
|
370
|
+
* Space Complexity: O(1)
|
|
365
371
|
*/
|
|
366
372
|
/**
|
|
367
|
-
* Time Complexity: O(1)
|
|
368
|
-
* Space Complexity: O(1)
|
|
373
|
+
* Time Complexity: O(1)
|
|
374
|
+
* Space Complexity: O(1)
|
|
369
375
|
*
|
|
370
376
|
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
371
377
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -402,12 +408,12 @@ export class AVLTree extends BST {
|
|
|
402
408
|
B && this._updateHeight(B);
|
|
403
409
|
}
|
|
404
410
|
/**
|
|
405
|
-
* Time Complexity: O(1)
|
|
406
|
-
* Space Complexity: O(1)
|
|
411
|
+
* Time Complexity: O(1)
|
|
412
|
+
* Space Complexity: O(1)
|
|
407
413
|
*/
|
|
408
414
|
/**
|
|
409
|
-
* Time Complexity: O(1)
|
|
410
|
-
* Space Complexity: O(1)
|
|
415
|
+
* Time Complexity: O(1)
|
|
416
|
+
* Space Complexity: O(1)
|
|
411
417
|
*
|
|
412
418
|
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
413
419
|
* @param {N} A - A is a node in a binary tree.
|