heap-typed 1.52.5 → 1.52.8
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/constants/index.d.ts +4 -0
- package/dist/constants/index.js +8 -0
- package/dist/data-structures/base/iterable-element-base.d.ts +8 -1
- package/dist/data-structures/base/iterable-element-base.js +10 -1
- package/dist/data-structures/base/iterable-entry-base.d.ts +8 -1
- package/dist/data-structures/base/iterable-entry-base.js +10 -10
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
- package/dist/data-structures/binary-tree/avl-tree.d.ts +23 -24
- package/dist/data-structures/binary-tree/avl-tree.js +71 -64
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +534 -402
- package/dist/data-structures/binary-tree/binary-tree.js +669 -598
- package/dist/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/data-structures/binary-tree/bst.js +115 -113
- package/dist/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/data-structures/binary-tree/rb-tree.js +40 -39
- package/dist/data-structures/binary-tree/segment-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/segment-tree.js +2 -2
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
- package/dist/data-structures/binary-tree/tree-multi-map.js +44 -43
- package/dist/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/data-structures/graph/abstract-graph.js +7 -4
- package/dist/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/data-structures/graph/directed-graph.js +4 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +2 -2
- package/dist/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/data-structures/hash/hash-map.js +1 -1
- package/dist/data-structures/heap/heap.js +3 -3
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.js +7 -7
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/singly-linked-list.js +6 -6
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/data-structures/matrix/matrix.d.ts +2 -2
- package/dist/data-structures/matrix/navigator.d.ts +2 -2
- package/dist/data-structures/matrix/navigator.js +4 -2
- package/dist/data-structures/queue/deque.d.ts +3 -3
- package/dist/data-structures/queue/deque.js +29 -29
- package/dist/data-structures/queue/queue.d.ts +1 -1
- package/dist/data-structures/stack/stack.d.ts +2 -2
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -6
- package/package.json +2 -2
- package/src/constants/index.ts +4 -0
- package/src/data-structures/base/iterable-element-base.ts +11 -1
- package/src/data-structures/base/iterable-entry-base.ts +11 -19
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +47 -50
- package/src/data-structures/binary-tree/avl-tree.ts +69 -71
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +698 -726
- package/src/data-structures/binary-tree/bst.ts +123 -129
- package/src/data-structures/binary-tree/rb-tree.ts +44 -46
- package/src/data-structures/binary-tree/segment-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +48 -49
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +4 -4
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +3 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
- package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/queue/deque.ts +31 -31
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/stack/stack.ts +2 -2
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/index.ts +2 -1
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
|
@@ -4,8 +4,8 @@ exports.AVLTree = exports.AVLTreeNode = void 0;
|
|
|
4
4
|
/**
|
|
5
5
|
* data-structure-typed
|
|
6
6
|
*
|
|
7
|
-
* @author
|
|
8
|
-
* @copyright Copyright (c) 2022
|
|
7
|
+
* @author Pablo Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
9
9
|
* @license MIT License
|
|
10
10
|
*/
|
|
11
11
|
const bst_1 = require("./bst");
|
|
@@ -52,7 +52,7 @@ class AVLTree extends bst_1.BST {
|
|
|
52
52
|
/**
|
|
53
53
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
54
54
|
* entries, or raw elements.
|
|
55
|
-
* @param
|
|
55
|
+
* @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
|
|
56
56
|
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
|
|
57
57
|
* be used to initialize the AVLTree.
|
|
58
58
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
@@ -60,10 +60,10 @@ class AVLTree extends bst_1.BST {
|
|
|
60
60
|
* keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
|
|
61
61
|
* `nodeBuilder` (
|
|
62
62
|
*/
|
|
63
|
-
constructor(
|
|
63
|
+
constructor(keysOrNodesOrEntriesOrRaws = [], options) {
|
|
64
64
|
super([], options);
|
|
65
|
-
if (
|
|
66
|
-
super.addMany(
|
|
65
|
+
if (keysOrNodesOrEntriesOrRaws)
|
|
66
|
+
super.addMany(keysOrNodesOrEntriesOrRaws);
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
69
69
|
* The function creates a new AVL tree node with the given key and value.
|
|
@@ -85,17 +85,17 @@ class AVLTree extends bst_1.BST {
|
|
|
85
85
|
* @returns a new AVLTree object.
|
|
86
86
|
*/
|
|
87
87
|
createTree(options) {
|
|
88
|
-
return new AVLTree([], Object.assign({ iterationType: this.iterationType, comparator: this.
|
|
88
|
+
return new AVLTree([], Object.assign({ iterationType: this.iterationType, comparator: this._comparator, toEntryFn: this._toEntryFn }, options));
|
|
89
89
|
}
|
|
90
90
|
/**
|
|
91
91
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
92
|
-
* @param {
|
|
93
|
-
* `
|
|
94
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
92
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
93
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
94
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
|
|
95
95
|
* an instance of the `AVLTreeNode` class.
|
|
96
96
|
*/
|
|
97
|
-
isNode(
|
|
98
|
-
return
|
|
97
|
+
isNode(keyOrNodeOrEntryOrRaw) {
|
|
98
|
+
return keyOrNodeOrEntryOrRaw instanceof AVLTreeNode;
|
|
99
99
|
}
|
|
100
100
|
/**
|
|
101
101
|
* Time Complexity: O(log n)
|
|
@@ -103,36 +103,36 @@ class AVLTree extends bst_1.BST {
|
|
|
103
103
|
*
|
|
104
104
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
105
105
|
* structure, then balances the path.
|
|
106
|
-
* @param {
|
|
107
|
-
* `
|
|
106
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
107
|
+
* `keyOrNodeOrEntryOrRaw` can accept values of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
|
|
108
108
|
* `RawElement`.
|
|
109
109
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
110
110
|
* the key or node being added to the data structure.
|
|
111
111
|
* @returns The method is returning a boolean value.
|
|
112
112
|
*/
|
|
113
|
-
add(
|
|
114
|
-
if (
|
|
113
|
+
add(keyOrNodeOrEntryOrRaw, value) {
|
|
114
|
+
if (keyOrNodeOrEntryOrRaw === null)
|
|
115
115
|
return false;
|
|
116
|
-
const inserted = super.add(
|
|
116
|
+
const inserted = super.add(keyOrNodeOrEntryOrRaw, value);
|
|
117
117
|
if (inserted)
|
|
118
|
-
this._balancePath(
|
|
118
|
+
this._balancePath(keyOrNodeOrEntryOrRaw);
|
|
119
119
|
return inserted;
|
|
120
120
|
}
|
|
121
121
|
/**
|
|
122
122
|
* Time Complexity: O(log n)
|
|
123
123
|
* Space Complexity: O(1)
|
|
124
124
|
*
|
|
125
|
-
* The function overrides the delete method
|
|
126
|
-
*
|
|
127
|
-
* @param
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
125
|
+
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
126
|
+
* balances the tree if necessary.
|
|
127
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
|
|
128
|
+
* parameter in the `override delete` method can be one of the following types:
|
|
129
|
+
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
130
|
+
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
131
|
+
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
132
|
+
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
133
133
|
*/
|
|
134
|
-
delete(
|
|
135
|
-
const deletedResults = super.delete(
|
|
134
|
+
delete(predicate) {
|
|
135
|
+
const deletedResults = super.delete(predicate);
|
|
136
136
|
for (const { needBalanced } of deletedResults) {
|
|
137
137
|
if (needBalanced) {
|
|
138
138
|
this._balancePath(needBalanced);
|
|
@@ -298,8 +298,10 @@ class AVLTree extends bst_1.BST {
|
|
|
298
298
|
C.right = A;
|
|
299
299
|
}
|
|
300
300
|
this._updateHeight(A);
|
|
301
|
-
|
|
302
|
-
|
|
301
|
+
if (B)
|
|
302
|
+
this._updateHeight(B);
|
|
303
|
+
if (C)
|
|
304
|
+
this._updateHeight(C);
|
|
303
305
|
}
|
|
304
306
|
/**
|
|
305
307
|
* Time Complexity: O(1)
|
|
@@ -337,7 +339,8 @@ class AVLTree extends bst_1.BST {
|
|
|
337
339
|
B.left = A;
|
|
338
340
|
}
|
|
339
341
|
this._updateHeight(A);
|
|
340
|
-
|
|
342
|
+
if (B)
|
|
343
|
+
this._updateHeight(B);
|
|
341
344
|
}
|
|
342
345
|
/**
|
|
343
346
|
* Time Complexity: O(1)
|
|
@@ -388,8 +391,10 @@ class AVLTree extends bst_1.BST {
|
|
|
388
391
|
if (C)
|
|
389
392
|
C.right = B;
|
|
390
393
|
this._updateHeight(A);
|
|
391
|
-
|
|
392
|
-
|
|
394
|
+
if (B)
|
|
395
|
+
this._updateHeight(B);
|
|
396
|
+
if (C)
|
|
397
|
+
this._updateHeight(C);
|
|
393
398
|
}
|
|
394
399
|
/**
|
|
395
400
|
* Time Complexity: O(log n)
|
|
@@ -397,47 +402,49 @@ class AVLTree extends bst_1.BST {
|
|
|
397
402
|
*
|
|
398
403
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
399
404
|
* to restore balance in an AVL tree after inserting a node.
|
|
400
|
-
* @param {
|
|
405
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} node - The `node` parameter can be of type `R` or
|
|
401
406
|
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
402
407
|
*/
|
|
403
408
|
_balancePath(node) {
|
|
404
409
|
node = this.ensureNode(node);
|
|
405
|
-
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
|
|
410
|
+
const path = this.getPathToRoot(node => node, node, false); // first O(log n) + O(log n)
|
|
406
411
|
for (let i = 0; i < path.length; i++) {
|
|
407
412
|
// second O(log n)
|
|
408
413
|
const A = path[i];
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
if (
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
414
|
+
if (A) {
|
|
415
|
+
// Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
|
|
416
|
+
this._updateHeight(A); // first O(1)
|
|
417
|
+
// Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
|
|
418
|
+
// 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:
|
|
419
|
+
switch (this._balanceFactor(A) // second O(1)
|
|
420
|
+
) {
|
|
421
|
+
case -2:
|
|
422
|
+
if (A && A.left) {
|
|
423
|
+
if (this._balanceFactor(A.left) <= 0) {
|
|
424
|
+
// second O(1)
|
|
425
|
+
// Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
|
|
426
|
+
this._balanceLL(A);
|
|
427
|
+
}
|
|
428
|
+
else {
|
|
429
|
+
// Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
|
|
430
|
+
this._balanceLR(A);
|
|
431
|
+
}
|
|
425
432
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
+
break;
|
|
434
|
+
case +2:
|
|
435
|
+
if (A && A.right) {
|
|
436
|
+
if (this._balanceFactor(A.right) >= 0) {
|
|
437
|
+
// Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
|
|
438
|
+
this._balanceRR(A);
|
|
439
|
+
}
|
|
440
|
+
else {
|
|
441
|
+
// Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
|
|
442
|
+
this._balanceRL(A);
|
|
443
|
+
}
|
|
433
444
|
}
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
this._balanceRL(A);
|
|
437
|
-
}
|
|
438
|
-
}
|
|
445
|
+
}
|
|
446
|
+
// 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.
|
|
439
447
|
}
|
|
440
|
-
// 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.
|
|
441
448
|
}
|
|
442
449
|
}
|
|
443
450
|
/**
|
|
@@ -4,8 +4,8 @@ exports.BinaryIndexedTree = void 0;
|
|
|
4
4
|
/**
|
|
5
5
|
* data-structure-typed
|
|
6
6
|
*
|
|
7
|
-
* @author
|
|
8
|
-
* @copyright Copyright (c) 2022
|
|
7
|
+
* @author Pablo Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
9
9
|
* @license MIT License
|
|
10
10
|
*/
|
|
11
11
|
const utils_1 = require("../../utils");
|