min-heap-typed 1.50.1 → 1.50.2
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/base/iterable-base.d.ts +114 -9
- package/dist/data-structures/base/iterable-base.js +143 -7
- package/dist/data-structures/binary-tree/avl-tree.d.ts +43 -46
- package/dist/data-structures/binary-tree/avl-tree.js +68 -71
- package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/data-structures/binary-tree/bst.d.ts +54 -74
- package/dist/data-structures/binary-tree/bst.js +30 -71
- package/dist/data-structures/binary-tree/rb-tree.d.ts +78 -60
- package/dist/data-structures/binary-tree/rb-tree.js +84 -89
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +37 -56
- package/dist/data-structures/binary-tree/tree-multimap.js +64 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/data-structures/graph/abstract-graph.js +3 -0
- package/dist/data-structures/graph/directed-graph.d.ts +14 -0
- package/dist/data-structures/graph/directed-graph.js +26 -0
- package/dist/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/data-structures/graph/map-graph.js +14 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +16 -0
- package/dist/data-structures/graph/undirected-graph.js +25 -0
- package/dist/data-structures/hash/hash-map.d.ts +121 -15
- package/dist/data-structures/hash/hash-map.js +160 -25
- package/dist/data-structures/heap/heap.d.ts +66 -6
- package/dist/data-structures/heap/heap.js +66 -6
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
- package/dist/data-structures/linked-list/doubly-linked-list.js +70 -64
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +128 -103
- package/dist/data-structures/linked-list/singly-linked-list.js +130 -112
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/data-structures/matrix/matrix.js +50 -11
- package/dist/data-structures/queue/deque.d.ts +49 -19
- package/dist/data-structures/queue/deque.js +101 -47
- package/dist/data-structures/queue/queue.d.ts +39 -5
- package/dist/data-structures/queue/queue.js +47 -5
- package/dist/data-structures/stack/stack.d.ts +16 -0
- package/dist/data-structures/stack/stack.js +22 -0
- package/dist/data-structures/trie/trie.d.ts +38 -1
- package/dist/data-structures/trie/trie.js +41 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +172 -19
- package/src/data-structures/binary-tree/avl-tree.ts +97 -97
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +89 -131
- package/src/data-structures/binary-tree/rb-tree.ts +127 -155
- package/src/data-structures/binary-tree/tree-multimap.ts +96 -112
- package/src/data-structures/graph/abstract-graph.ts +4 -0
- package/src/data-structures/graph/directed-graph.ts +30 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +28 -0
- package/src/data-structures/hash/hash-map.ts +175 -34
- package/src/data-structures/heap/heap.ts +66 -6
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -66
- package/src/data-structures/linked-list/singly-linked-list.ts +132 -114
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/queue/deque.ts +108 -49
- package/src/data-structures/queue/queue.ts +51 -5
- package/src/data-structures/stack/stack.ts +24 -0
- package/src/data-structures/trie/trie.ts +45 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
|
@@ -28,7 +28,7 @@ exports.AVLTreeNode = AVLTreeNode;
|
|
|
28
28
|
class AVLTree extends bst_1.BST {
|
|
29
29
|
/**
|
|
30
30
|
* The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options.
|
|
31
|
-
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V,
|
|
31
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
|
|
32
32
|
* objects. It represents a collection of nodes that will be added to the AVL tree during
|
|
33
33
|
* initialization.
|
|
34
34
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
@@ -46,7 +46,7 @@ class AVLTree extends bst_1.BST {
|
|
|
46
46
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
47
47
|
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
|
48
48
|
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
|
49
|
-
* node type `
|
|
49
|
+
* node type `NODE`.
|
|
50
50
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
51
51
|
*/
|
|
52
52
|
createNode(key, value) {
|
|
@@ -64,7 +64,7 @@ class AVLTree extends bst_1.BST {
|
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
66
66
|
* The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
|
|
67
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
67
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
68
68
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
|
|
69
69
|
*/
|
|
70
70
|
isNode(keyOrNodeOrEntry) {
|
|
@@ -111,8 +111,8 @@ class AVLTree extends bst_1.BST {
|
|
|
111
111
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
112
112
|
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will
|
|
113
113
|
* default to the `_defaultOneParamCallback` function. The `callback` function should have a single
|
|
114
|
-
* parameter of type `
|
|
115
|
-
* @returns The method is returning an array of `BinaryTreeDeleteResult<
|
|
114
|
+
* parameter of type `NODE
|
|
115
|
+
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
116
116
|
*/
|
|
117
117
|
delete(identifier, callback = this._defaultOneParamCallback) {
|
|
118
118
|
if (identifier instanceof AVLTreeNode)
|
|
@@ -128,9 +128,9 @@ class AVLTree extends bst_1.BST {
|
|
|
128
128
|
/**
|
|
129
129
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
|
|
130
130
|
* tree.
|
|
131
|
-
* @param {K |
|
|
132
|
-
* needs to be swapped with the destination node. It can be of type `K`, `
|
|
133
|
-
* @param {K |
|
|
131
|
+
* @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
132
|
+
* needs to be swapped with the destination node. It can be of type `K`, `NODE`, or `undefined`.
|
|
133
|
+
* @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
|
|
134
134
|
* node where the values from the source node will be swapped to.
|
|
135
135
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
136
136
|
* if either `srcNode` or `destNode` is undefined.
|
|
@@ -157,14 +157,13 @@ class AVLTree extends bst_1.BST {
|
|
|
157
157
|
/**
|
|
158
158
|
* Time Complexity: O(1)
|
|
159
159
|
* Space Complexity: O(1)
|
|
160
|
-
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
161
160
|
*/
|
|
162
161
|
/**
|
|
163
162
|
* Time Complexity: O(1)
|
|
164
163
|
* Space Complexity: O(1)
|
|
165
164
|
*
|
|
166
165
|
* The function calculates the balance factor of a node in a binary tree.
|
|
167
|
-
* @param {
|
|
166
|
+
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
|
|
168
167
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
169
168
|
* height of the left subtree from the height of the right subtree.
|
|
170
169
|
*/
|
|
@@ -181,7 +180,6 @@ class AVLTree extends bst_1.BST {
|
|
|
181
180
|
/**
|
|
182
181
|
* Time Complexity: O(1)
|
|
183
182
|
* Space Complexity: O(1)
|
|
184
|
-
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
185
183
|
*/
|
|
186
184
|
/**
|
|
187
185
|
* Time Complexity: O(1)
|
|
@@ -189,7 +187,7 @@ class AVLTree extends bst_1.BST {
|
|
|
189
187
|
*
|
|
190
188
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
191
189
|
* right children.
|
|
192
|
-
* @param {
|
|
190
|
+
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
|
|
193
191
|
*/
|
|
194
192
|
_updateHeight(node) {
|
|
195
193
|
if (!node.left && !node.right)
|
|
@@ -203,71 +201,16 @@ class AVLTree extends bst_1.BST {
|
|
|
203
201
|
else
|
|
204
202
|
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
205
203
|
}
|
|
206
|
-
/**
|
|
207
|
-
* Time Complexity: O(log n)
|
|
208
|
-
* Space Complexity: O(1)
|
|
209
|
-
* 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.
|
|
210
|
-
*/
|
|
211
|
-
/**
|
|
212
|
-
* Time Complexity: O(log n)
|
|
213
|
-
* Space Complexity: O(1)
|
|
214
|
-
*
|
|
215
|
-
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
216
|
-
* to restore balance in an AVL tree after inserting a node.
|
|
217
|
-
* @param {N} node - The `node` parameter in the `_balancePath` function represents the node in the
|
|
218
|
-
* AVL tree that needs to be balanced.
|
|
219
|
-
*/
|
|
220
|
-
_balancePath(node) {
|
|
221
|
-
node = this.ensureNode(node);
|
|
222
|
-
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
|
|
223
|
-
for (let i = 0; i < path.length; i++) {
|
|
224
|
-
// second O(log n)
|
|
225
|
-
const A = path[i];
|
|
226
|
-
// Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
|
|
227
|
-
this._updateHeight(A); // first O(1)
|
|
228
|
-
// Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
|
|
229
|
-
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
230
|
-
switch (this._balanceFactor(A) // second O(1)
|
|
231
|
-
) {
|
|
232
|
-
case -2:
|
|
233
|
-
if (A && A.left) {
|
|
234
|
-
if (this._balanceFactor(A.left) <= 0) {
|
|
235
|
-
// second O(1)
|
|
236
|
-
// Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
|
|
237
|
-
this._balanceLL(A);
|
|
238
|
-
}
|
|
239
|
-
else {
|
|
240
|
-
// Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
|
|
241
|
-
this._balanceLR(A);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
break;
|
|
245
|
-
case +2:
|
|
246
|
-
if (A && A.right) {
|
|
247
|
-
if (this._balanceFactor(A.right) >= 0) {
|
|
248
|
-
// Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
|
|
249
|
-
this._balanceRR(A);
|
|
250
|
-
}
|
|
251
|
-
else {
|
|
252
|
-
// Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
|
|
253
|
-
this._balanceRL(A);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
// TODO So far, no sure if this is necessary that Recursive Repair: Once rotation operations are executed, it may cause imbalance issues at higher levels of the tree. Therefore, you need to recursively check and repair imbalance problems upwards until you reach the root node.
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
204
|
/**
|
|
261
205
|
* Time Complexity: O(1)
|
|
262
206
|
* Space Complexity: O(1)
|
|
263
|
-
* constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory.
|
|
264
207
|
*/
|
|
265
208
|
/**
|
|
266
209
|
* Time Complexity: O(1)
|
|
267
210
|
* Space Complexity: O(1)
|
|
268
211
|
*
|
|
269
212
|
* The function `_balanceLL` performs a left-left rotation to balance a binary tree.
|
|
270
|
-
* @param {
|
|
213
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
271
214
|
*/
|
|
272
215
|
_balanceLL(A) {
|
|
273
216
|
const parentOfA = A.parent;
|
|
@@ -308,7 +251,7 @@ class AVLTree extends bst_1.BST {
|
|
|
308
251
|
* Space Complexity: O(1)
|
|
309
252
|
*
|
|
310
253
|
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
311
|
-
* @param {
|
|
254
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
312
255
|
*/
|
|
313
256
|
_balanceLR(A) {
|
|
314
257
|
const parentOfA = A.parent;
|
|
@@ -364,7 +307,7 @@ class AVLTree extends bst_1.BST {
|
|
|
364
307
|
* Space Complexity: O(1)
|
|
365
308
|
*
|
|
366
309
|
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
367
|
-
* @param {
|
|
310
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
368
311
|
*/
|
|
369
312
|
_balanceRR(A) {
|
|
370
313
|
const parentOfA = A.parent;
|
|
@@ -406,7 +349,7 @@ class AVLTree extends bst_1.BST {
|
|
|
406
349
|
* Space Complexity: O(1)
|
|
407
350
|
*
|
|
408
351
|
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
409
|
-
* @param {
|
|
352
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
410
353
|
*/
|
|
411
354
|
_balanceRL(A) {
|
|
412
355
|
const parentOfA = A.parent;
|
|
@@ -453,6 +396,60 @@ class AVLTree extends bst_1.BST {
|
|
|
453
396
|
B && this._updateHeight(B);
|
|
454
397
|
C && this._updateHeight(C);
|
|
455
398
|
}
|
|
399
|
+
/**
|
|
400
|
+
* Time Complexity: O(log n)
|
|
401
|
+
* Space Complexity: O(1)
|
|
402
|
+
* 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.
|
|
403
|
+
*/
|
|
404
|
+
/**
|
|
405
|
+
* Time Complexity: O(log n)
|
|
406
|
+
* Space Complexity: O(1)
|
|
407
|
+
*
|
|
408
|
+
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
409
|
+
* to restore balance in an AVL tree after inserting a node.
|
|
410
|
+
* @param {NODE} node - The `node` parameter in the `_balancePath` function represents the node in the
|
|
411
|
+
* AVL tree that needs to be balanced.
|
|
412
|
+
*/
|
|
413
|
+
_balancePath(node) {
|
|
414
|
+
node = this.ensureNode(node);
|
|
415
|
+
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
|
|
416
|
+
for (let i = 0; i < path.length; i++) {
|
|
417
|
+
// second O(log n)
|
|
418
|
+
const A = path[i];
|
|
419
|
+
// Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
|
|
420
|
+
this._updateHeight(A); // first O(1)
|
|
421
|
+
// Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
|
|
422
|
+
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
423
|
+
switch (this._balanceFactor(A) // second O(1)
|
|
424
|
+
) {
|
|
425
|
+
case -2:
|
|
426
|
+
if (A && A.left) {
|
|
427
|
+
if (this._balanceFactor(A.left) <= 0) {
|
|
428
|
+
// second O(1)
|
|
429
|
+
// Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
|
|
430
|
+
this._balanceLL(A);
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
// Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
|
|
434
|
+
this._balanceLR(A);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
break;
|
|
438
|
+
case +2:
|
|
439
|
+
if (A && A.right) {
|
|
440
|
+
if (this._balanceFactor(A.right) >= 0) {
|
|
441
|
+
// Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
|
|
442
|
+
this._balanceRR(A);
|
|
443
|
+
}
|
|
444
|
+
else {
|
|
445
|
+
// Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
|
|
446
|
+
this._balanceRL(A);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
// TODO So far, no sure if this is necessary that Recursive Repair: Once rotation operations are executed, it may cause imbalance issues at higher levels of the tree. Therefore, you need to recursively check and repair imbalance problems upwards until you reach the root node.
|
|
451
|
+
}
|
|
452
|
+
}
|
|
456
453
|
_replaceNode(oldNode, newNode) {
|
|
457
454
|
newNode.height = oldNode.height;
|
|
458
455
|
return super._replaceNode(oldNode, newNode);
|