data-structure-typed 1.47.4 → 1.47.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/benchmark/report.html +2 -2
- package/benchmark/report.json +12 -18
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +23 -15
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +66 -82
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/cjs/data-structures/binary-tree/bst.js +56 -40
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +26 -17
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +31 -22
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/cjs/data-structures/hash/hash-map.js +10 -4
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/cjs/data-structures/hash/hash-table.js +50 -5
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +25 -22
- package/dist/cjs/data-structures/heap/heap.js +101 -41
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/max-heap.js +2 -2
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/min-heap.js +2 -2
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +125 -119
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +38 -37
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +65 -60
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +50 -49
- package/dist/cjs/data-structures/queue/deque.js +81 -71
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +46 -0
- package/dist/cjs/data-structures/queue/queue.js +80 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +20 -6
- package/dist/cjs/data-structures/stack/stack.js +65 -8
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +5 -0
- package/dist/cjs/data-structures/trie/trie.js +47 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -1
- package/dist/cjs/types/common.d.ts +2 -0
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +26 -16
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +66 -83
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/mjs/data-structures/binary-tree/bst.js +59 -41
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +30 -19
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +34 -23
- package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/mjs/data-structures/hash/hash-map.js +10 -4
- package/dist/mjs/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/mjs/data-structures/hash/hash-table.js +50 -5
- package/dist/mjs/data-structures/heap/heap.d.ts +25 -22
- package/dist/mjs/data-structures/heap/heap.js +102 -42
- package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/max-heap.js +2 -2
- package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/min-heap.js +2 -2
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +125 -119
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +38 -37
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +65 -60
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/mjs/data-structures/queue/deque.d.ts +50 -49
- package/dist/mjs/data-structures/queue/deque.js +81 -71
- package/dist/mjs/data-structures/queue/queue.d.ts +46 -0
- package/dist/mjs/data-structures/queue/queue.js +80 -0
- package/dist/mjs/data-structures/stack/stack.d.ts +20 -6
- package/dist/mjs/data-structures/stack/stack.js +65 -8
- package/dist/mjs/data-structures/trie/trie.d.ts +5 -0
- package/dist/mjs/data-structures/trie/trie.js +47 -0
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -1
- package/dist/mjs/types/common.d.ts +2 -0
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/umd/data-structure-typed.js +856 -509
- 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 +27 -17
- package/src/data-structures/binary-tree/binary-tree.ts +114 -97
- package/src/data-structures/binary-tree/bst.ts +67 -47
- package/src/data-structures/binary-tree/rb-tree.ts +34 -20
- package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +13 -7
- package/src/data-structures/hash/hash-table.ts +59 -9
- package/src/data-structures/heap/heap.ts +115 -46
- package/src/data-structures/heap/max-heap.ts +5 -5
- package/src/data-structures/heap/min-heap.ts +5 -5
- package/src/data-structures/linked-list/doubly-linked-list.ts +137 -128
- package/src/data-structures/linked-list/singly-linked-list.ts +72 -64
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +86 -75
- package/src/data-structures/queue/queue.ts +88 -0
- package/src/data-structures/stack/stack.ts +75 -10
- package/src/data-structures/trie/trie.ts +53 -0
- package/src/interfaces/binary-tree.ts +13 -1
- package/src/types/common.ts +5 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
- package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
- package/test/performance/data-structures/heap/heap.test.ts +14 -5
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +1 -1
- package/test/unit/data-structures/hash/hash-table.test.ts +58 -2
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +49 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
- package/test/unit/data-structures/queue/queue.test.ts +37 -0
- package/test/unit/data-structures/stack/stack.test.ts +55 -5
- package/test/unit/data-structures/trie/trie.test.ts +33 -0
- package/test/unit/unrestricted-interconversion.test.ts +100 -0
|
@@ -45,21 +45,22 @@ export class BSTNode extends BinaryTreeNode {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
export class BST extends BinaryTree {
|
|
48
|
-
options;
|
|
49
48
|
/**
|
|
50
49
|
* The constructor function initializes a binary search tree with an optional comparator function.
|
|
51
50
|
* @param {BSTOptions} [options] - An optional object that contains additional configuration options
|
|
52
51
|
* for the binary search tree.
|
|
53
52
|
*/
|
|
54
|
-
constructor(options) {
|
|
55
|
-
super(options);
|
|
53
|
+
constructor(elements, options) {
|
|
54
|
+
super([], options);
|
|
56
55
|
if (options) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
56
|
+
const { comparator } = options;
|
|
57
|
+
if (comparator) {
|
|
58
|
+
this.comparator = comparator;
|
|
59
|
+
}
|
|
61
60
|
}
|
|
62
61
|
this._root = undefined;
|
|
62
|
+
if (elements)
|
|
63
|
+
this.init(elements);
|
|
63
64
|
}
|
|
64
65
|
_root;
|
|
65
66
|
/**
|
|
@@ -68,6 +69,7 @@ export class BST extends BinaryTree {
|
|
|
68
69
|
get root() {
|
|
69
70
|
return this._root;
|
|
70
71
|
}
|
|
72
|
+
comparator = (a, b) => a - b;
|
|
71
73
|
/**
|
|
72
74
|
* The function creates a new binary search tree node with the given key and value.
|
|
73
75
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
@@ -80,12 +82,11 @@ export class BST extends BinaryTree {
|
|
|
80
82
|
return new BSTNode(key, value);
|
|
81
83
|
}
|
|
82
84
|
createTree(options) {
|
|
83
|
-
return new BST(
|
|
85
|
+
return new BST([], {
|
|
86
|
+
iterationType: this.iterationType,
|
|
87
|
+
comparator: this.comparator, ...options
|
|
88
|
+
});
|
|
84
89
|
}
|
|
85
|
-
/**
|
|
86
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
87
|
-
* Space Complexity: O(1) - Constant space is used.
|
|
88
|
-
*/
|
|
89
90
|
/**
|
|
90
91
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
91
92
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -176,8 +177,8 @@ export class BST extends BinaryTree {
|
|
|
176
177
|
return inserted;
|
|
177
178
|
}
|
|
178
179
|
/**
|
|
179
|
-
* Time Complexity: O(
|
|
180
|
-
* Space Complexity: O(
|
|
180
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
181
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
181
182
|
*/
|
|
182
183
|
/**
|
|
183
184
|
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
@@ -199,7 +200,7 @@ export class BST extends BinaryTree {
|
|
|
199
200
|
* current instance of the binary search tree
|
|
200
201
|
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
201
202
|
*/
|
|
202
|
-
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.
|
|
203
|
+
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
203
204
|
// TODO this addMany function is inefficient, it should be optimized
|
|
204
205
|
function hasNoUndefined(arr) {
|
|
205
206
|
return arr.indexOf(undefined) === -1;
|
|
@@ -269,8 +270,8 @@ export class BST extends BinaryTree {
|
|
|
269
270
|
return inserted;
|
|
270
271
|
}
|
|
271
272
|
/**
|
|
272
|
-
* Time Complexity: O(log n) -
|
|
273
|
-
* Space Complexity: O(
|
|
273
|
+
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
274
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
274
275
|
*/
|
|
275
276
|
/**
|
|
276
277
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
@@ -287,7 +288,7 @@ export class BST extends BinaryTree {
|
|
|
287
288
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
288
289
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
289
290
|
*/
|
|
290
|
-
lastKey(beginRoot = this.root, iterationType = this.
|
|
291
|
+
lastKey(beginRoot = this.root, iterationType = this.iterationType) {
|
|
291
292
|
if (this._compare(0, 1) === CP.lt)
|
|
292
293
|
return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
|
|
293
294
|
else if (this._compare(0, 1) === CP.gt)
|
|
@@ -297,7 +298,7 @@ export class BST extends BinaryTree {
|
|
|
297
298
|
}
|
|
298
299
|
/**
|
|
299
300
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
300
|
-
* Space Complexity: O(
|
|
301
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
301
302
|
*/
|
|
302
303
|
/**
|
|
303
304
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
@@ -344,6 +345,10 @@ export class BST extends BinaryTree {
|
|
|
344
345
|
}
|
|
345
346
|
}
|
|
346
347
|
}
|
|
348
|
+
/**
|
|
349
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
350
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
351
|
+
*/
|
|
347
352
|
/**
|
|
348
353
|
* The function `ensureNotKey` returns the node corresponding to the given key if it is a node key,
|
|
349
354
|
* otherwise it returns the key itself.
|
|
@@ -356,10 +361,6 @@ export class BST extends BinaryTree {
|
|
|
356
361
|
ensureNotKey(key, iterationType = IterationType.ITERATIVE) {
|
|
357
362
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
358
363
|
}
|
|
359
|
-
/**
|
|
360
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
361
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
362
|
-
*/
|
|
363
364
|
/**
|
|
364
365
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
365
366
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -383,7 +384,7 @@ export class BST extends BinaryTree {
|
|
|
383
384
|
* performed on the binary tree. It can have two possible values:
|
|
384
385
|
* @returns The method returns an array of nodes (`N[]`).
|
|
385
386
|
*/
|
|
386
|
-
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.
|
|
387
|
+
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
387
388
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
388
389
|
if (!beginRoot)
|
|
389
390
|
return [];
|
|
@@ -464,7 +465,7 @@ export class BST extends BinaryTree {
|
|
|
464
465
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
465
466
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
466
467
|
*/
|
|
467
|
-
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = CP.lt, targetNode = this.root, iterationType = this.
|
|
468
|
+
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
468
469
|
targetNode = this.ensureNotKey(targetNode);
|
|
469
470
|
const ans = [];
|
|
470
471
|
if (!targetNode)
|
|
@@ -505,17 +506,8 @@ export class BST extends BinaryTree {
|
|
|
505
506
|
}
|
|
506
507
|
}
|
|
507
508
|
/**
|
|
508
|
-
*
|
|
509
|
-
*
|
|
510
|
-
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
|
|
511
|
-
*
|
|
512
|
-
* Use Cases and Efficiency:
|
|
513
|
-
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
|
|
514
|
-
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
515
|
-
*/
|
|
516
|
-
/**
|
|
517
|
-
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
518
|
-
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
509
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
510
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
519
511
|
*/
|
|
520
512
|
/**
|
|
521
513
|
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
@@ -528,7 +520,7 @@ export class BST extends BinaryTree {
|
|
|
528
520
|
* values:
|
|
529
521
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
530
522
|
*/
|
|
531
|
-
perfectlyBalance(iterationType = this.
|
|
523
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
532
524
|
const sorted = this.dfs(node => node, 'in'), n = sorted.length;
|
|
533
525
|
this.clear();
|
|
534
526
|
if (sorted.length < 1)
|
|
@@ -566,8 +558,17 @@ export class BST extends BinaryTree {
|
|
|
566
558
|
}
|
|
567
559
|
}
|
|
568
560
|
/**
|
|
569
|
-
*
|
|
570
|
-
*
|
|
561
|
+
* Balancing Adjustment:
|
|
562
|
+
* Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
|
|
563
|
+
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
|
|
564
|
+
*
|
|
565
|
+
* Use Cases and Efficiency:
|
|
566
|
+
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
|
|
567
|
+
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
568
|
+
*/
|
|
569
|
+
/**
|
|
570
|
+
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
571
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
571
572
|
*/
|
|
572
573
|
/**
|
|
573
574
|
* Time Complexity: O(n) - Visiting each node once.
|
|
@@ -578,7 +579,7 @@ export class BST extends BinaryTree {
|
|
|
578
579
|
* to check if the AVL tree is balanced. It can have two possible values:
|
|
579
580
|
* @returns a boolean value.
|
|
580
581
|
*/
|
|
581
|
-
isAVLBalanced(iterationType = this.
|
|
582
|
+
isAVLBalanced(iterationType = this.iterationType) {
|
|
582
583
|
if (!this.root)
|
|
583
584
|
return true;
|
|
584
585
|
let balanced = true;
|
|
@@ -623,6 +624,23 @@ export class BST extends BinaryTree {
|
|
|
623
624
|
}
|
|
624
625
|
return balanced;
|
|
625
626
|
}
|
|
627
|
+
/**
|
|
628
|
+
* Time Complexity: O(n) - Visiting each node once.
|
|
629
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
630
|
+
*/
|
|
631
|
+
init(elements) {
|
|
632
|
+
if (elements) {
|
|
633
|
+
for (const entryOrKey of elements) {
|
|
634
|
+
if (Array.isArray(entryOrKey)) {
|
|
635
|
+
const [key, value] = entryOrKey;
|
|
636
|
+
this.add(key, value);
|
|
637
|
+
}
|
|
638
|
+
else {
|
|
639
|
+
this.add(entryOrKey);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
}
|
|
626
644
|
_setRoot(v) {
|
|
627
645
|
if (v) {
|
|
628
646
|
v.parent = undefined;
|
|
@@ -638,7 +656,7 @@ export class BST extends BinaryTree {
|
|
|
638
656
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
639
657
|
*/
|
|
640
658
|
_compare(a, b) {
|
|
641
|
-
const compared = this.
|
|
659
|
+
const compared = this.comparator(a, b);
|
|
642
660
|
if (compared > 0)
|
|
643
661
|
return CP.gt;
|
|
644
662
|
else if (compared < 0)
|
|
@@ -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, BTNKey, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
8
|
+
import { BiTreeDeleteResult, BTNCallback, BTNKey, IterableEntriesOrKeys, 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<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
@@ -21,23 +21,18 @@ export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N>
|
|
|
21
21
|
*/
|
|
22
22
|
export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNode<V, RedBlackTreeNodeNested<V>>, TREE extends RedBlackTree<V, N, TREE> = RedBlackTree<V, N, RedBlackTreeNested<V, N>>> extends BST<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
23
23
|
Sentinel: N;
|
|
24
|
-
options: RBTreeOptions;
|
|
25
24
|
/**
|
|
26
25
|
* The constructor function initializes a Red-Black Tree with an optional set of options.
|
|
27
26
|
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
28
27
|
* passed to the constructor. It is used to configure the RBTree object with specific options.
|
|
29
28
|
*/
|
|
30
|
-
constructor(options?: RBTreeOptions);
|
|
29
|
+
constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<RBTreeOptions>);
|
|
31
30
|
protected _root: N;
|
|
32
31
|
get root(): N;
|
|
33
32
|
protected _size: number;
|
|
34
33
|
get size(): number;
|
|
35
34
|
createNode(key: BTNKey, value?: V, color?: RBTNColor): N;
|
|
36
35
|
createTree(options?: RBTreeOptions): TREE;
|
|
37
|
-
/**
|
|
38
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
39
|
-
* Space Complexity: O(1)
|
|
40
|
-
*/
|
|
41
36
|
/**
|
|
42
37
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
43
38
|
* Space Complexity: O(1)
|
|
@@ -70,6 +65,10 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
70
65
|
* @returns an array of `BiTreeDeleteResult<N>`.
|
|
71
66
|
*/
|
|
72
67
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback?: C): BiTreeDeleteResult<N>[];
|
|
68
|
+
/**
|
|
69
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
70
|
+
* Space Complexity: O(1)
|
|
71
|
+
*/
|
|
73
72
|
isRealNode(node: N | undefined): node is N;
|
|
74
73
|
getNode<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
75
74
|
getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
@@ -101,7 +100,12 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
101
100
|
* @returns the predecessor of the given RedBlackTreeNode 'x'.
|
|
102
101
|
*/
|
|
103
102
|
getPredecessor(x: N): N;
|
|
103
|
+
/**
|
|
104
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
105
|
+
* Space Complexity: O(1)
|
|
106
|
+
*/
|
|
104
107
|
clear(): void;
|
|
108
|
+
init(elements: IterableEntriesOrKeys<V>): void;
|
|
105
109
|
protected _setRoot(v: N): void;
|
|
106
110
|
/**
|
|
107
111
|
* Time Complexity: O(1)
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
8
|
+
import { RBTNColor } from '../../types';
|
|
9
9
|
import { BST, BSTNode } from './bst';
|
|
10
10
|
import { BinaryTreeNode } from './binary-tree';
|
|
11
11
|
export class RedBlackTreeNode extends BSTNode {
|
|
@@ -24,21 +24,16 @@ export class RedBlackTreeNode extends BSTNode {
|
|
|
24
24
|
*/
|
|
25
25
|
export class RedBlackTree extends BST {
|
|
26
26
|
Sentinel = new RedBlackTreeNode(NaN);
|
|
27
|
-
options;
|
|
28
27
|
/**
|
|
29
28
|
* The constructor function initializes a Red-Black Tree with an optional set of options.
|
|
30
29
|
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
31
30
|
* passed to the constructor. It is used to configure the RBTree object with specific options.
|
|
32
31
|
*/
|
|
33
|
-
constructor(options) {
|
|
34
|
-
super(options);
|
|
35
|
-
if (options) {
|
|
36
|
-
this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b, ...options };
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
|
|
40
|
-
}
|
|
32
|
+
constructor(elements, options) {
|
|
33
|
+
super([], options);
|
|
41
34
|
this._root = this.Sentinel;
|
|
35
|
+
if (elements)
|
|
36
|
+
this.init(elements);
|
|
42
37
|
}
|
|
43
38
|
_root;
|
|
44
39
|
get root() {
|
|
@@ -52,12 +47,11 @@ export class RedBlackTree extends BST {
|
|
|
52
47
|
return new RedBlackTreeNode(key, value, color);
|
|
53
48
|
}
|
|
54
49
|
createTree(options) {
|
|
55
|
-
return new RedBlackTree(
|
|
50
|
+
return new RedBlackTree([], {
|
|
51
|
+
iterationType: this.iterationType,
|
|
52
|
+
comparator: this.comparator, ...options
|
|
53
|
+
});
|
|
56
54
|
}
|
|
57
|
-
/**
|
|
58
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
59
|
-
* Space Complexity: O(1)
|
|
60
|
-
*/
|
|
61
55
|
/**
|
|
62
56
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
63
57
|
* Space Complexity: O(1)
|
|
@@ -203,13 +197,13 @@ export class RedBlackTree extends BST {
|
|
|
203
197
|
// TODO
|
|
204
198
|
return ans;
|
|
205
199
|
}
|
|
206
|
-
isRealNode(node) {
|
|
207
|
-
return node !== this.Sentinel && node !== undefined;
|
|
208
|
-
}
|
|
209
200
|
/**
|
|
210
201
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
211
202
|
* Space Complexity: O(1)
|
|
212
203
|
*/
|
|
204
|
+
isRealNode(node) {
|
|
205
|
+
return node !== this.Sentinel && node !== undefined;
|
|
206
|
+
}
|
|
213
207
|
/**
|
|
214
208
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
215
209
|
* Space Complexity: O(1)
|
|
@@ -231,7 +225,7 @@ export class RedBlackTree extends BST {
|
|
|
231
225
|
* `getNodes` method, which is called within the `getNode` method.
|
|
232
226
|
* @returns a value of type `N`, `null`, or `undefined`.
|
|
233
227
|
*/
|
|
234
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
228
|
+
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
235
229
|
if (identifier instanceof BinaryTreeNode)
|
|
236
230
|
callback = (node => node);
|
|
237
231
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -284,10 +278,27 @@ export class RedBlackTree extends BST {
|
|
|
284
278
|
}
|
|
285
279
|
return y;
|
|
286
280
|
}
|
|
281
|
+
/**
|
|
282
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
283
|
+
* Space Complexity: O(1)
|
|
284
|
+
*/
|
|
287
285
|
clear() {
|
|
288
286
|
this._root = this.Sentinel;
|
|
289
287
|
this._size = 0;
|
|
290
288
|
}
|
|
289
|
+
init(elements) {
|
|
290
|
+
if (elements) {
|
|
291
|
+
for (const entryOrKey of elements) {
|
|
292
|
+
if (Array.isArray(entryOrKey)) {
|
|
293
|
+
const [key, value] = entryOrKey;
|
|
294
|
+
this.add(key, value);
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
this.add(entryOrKey);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
291
302
|
_setRoot(v) {
|
|
292
303
|
if (v) {
|
|
293
304
|
v.parent = undefined;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BTNKey, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
|
|
9
|
-
import { BiTreeDeleteResult, BTNCallback, IterationType, TreeMultimapNested } from '../../types';
|
|
9
|
+
import { BiTreeDeleteResult, BTNCallback, IterableEntriesOrKeys, IterationType, TreeMultimapNested } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>> extends AVLTreeNode<V, N> {
|
|
@@ -27,14 +27,13 @@ export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N>
|
|
|
27
27
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
28
28
|
*/
|
|
29
29
|
export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNode<V, TreeMultimapNodeNested<V>>, TREE extends TreeMultimap<V, N, TREE> = TreeMultimap<V, N, TreeMultimapNested<V, N>>> extends AVLTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
30
|
-
options: TreeMultimapOptions;
|
|
31
30
|
/**
|
|
32
31
|
* The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
|
|
33
32
|
* merge duplicated values.
|
|
34
33
|
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
35
34
|
* TreeMultimap.
|
|
36
35
|
*/
|
|
37
|
-
constructor(options?: TreeMultimapOptions);
|
|
36
|
+
constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<TreeMultimapOptions>);
|
|
38
37
|
private _count;
|
|
39
38
|
get count(): number;
|
|
40
39
|
/**
|
|
@@ -48,10 +47,6 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
48
47
|
*/
|
|
49
48
|
createNode(key: BTNKey, value?: V, count?: number): N;
|
|
50
49
|
createTree(options?: TreeMultimapOptions): TREE;
|
|
51
|
-
/**
|
|
52
|
-
* 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.
|
|
53
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
54
|
-
*/
|
|
55
50
|
/**
|
|
56
51
|
* 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.
|
|
57
52
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -68,8 +63,8 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
68
63
|
*/
|
|
69
64
|
add(keyOrNode: BTNKey | N | null | undefined, value?: V, count?: number): N | undefined;
|
|
70
65
|
/**
|
|
71
|
-
* Time Complexity: O(
|
|
72
|
-
* Space Complexity: O(1) - constant space, as it
|
|
66
|
+
* 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.
|
|
67
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
73
68
|
*/
|
|
74
69
|
/**
|
|
75
70
|
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
|
@@ -86,8 +81,8 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
86
81
|
*/
|
|
87
82
|
addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: V[]): (N | undefined)[];
|
|
88
83
|
/**
|
|
89
|
-
* Time Complexity: O(
|
|
90
|
-
* Space Complexity: O(1) - constant space, as it
|
|
84
|
+
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
85
|
+
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
91
86
|
*/
|
|
92
87
|
/**
|
|
93
88
|
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
@@ -100,10 +95,10 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
100
95
|
* values:
|
|
101
96
|
* @returns a boolean value.
|
|
102
97
|
*/
|
|
103
|
-
perfectlyBalance(iterationType?: IterationType
|
|
98
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
104
99
|
/**
|
|
105
|
-
* Time Complexity: O(
|
|
106
|
-
* Space Complexity: O(
|
|
100
|
+
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
|
101
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
107
102
|
*/
|
|
108
103
|
/**
|
|
109
104
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
|
@@ -126,13 +121,18 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
126
121
|
*/
|
|
127
122
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BiTreeDeleteResult<N>[];
|
|
128
123
|
/**
|
|
129
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree.
|
|
130
|
-
* Space Complexity: O(
|
|
124
|
+
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
125
|
+
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
|
131
126
|
*/
|
|
132
127
|
/**
|
|
133
128
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
134
129
|
*/
|
|
135
130
|
clear(): void;
|
|
131
|
+
/**
|
|
132
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
|
133
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
134
|
+
*/
|
|
135
|
+
init(elements: IterableEntriesOrKeys<V>): void;
|
|
136
136
|
/**
|
|
137
137
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
138
138
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -21,21 +21,16 @@ export class TreeMultimapNode extends AVLTreeNode {
|
|
|
21
21
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
22
22
|
*/
|
|
23
23
|
export class TreeMultimap extends AVLTree {
|
|
24
|
-
options;
|
|
25
24
|
/**
|
|
26
25
|
* The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
|
|
27
26
|
* merge duplicated values.
|
|
28
27
|
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
29
28
|
* TreeMultimap.
|
|
30
29
|
*/
|
|
31
|
-
constructor(options
|
|
32
|
-
super(options);
|
|
33
|
-
if (
|
|
34
|
-
this.
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
|
|
38
|
-
}
|
|
30
|
+
constructor(elements, options) {
|
|
31
|
+
super([], options);
|
|
32
|
+
if (elements)
|
|
33
|
+
this.init(elements);
|
|
39
34
|
}
|
|
40
35
|
_count = 0;
|
|
41
36
|
get count() {
|
|
@@ -54,12 +49,11 @@ export class TreeMultimap extends AVLTree {
|
|
|
54
49
|
return new TreeMultimapNode(key, value, count);
|
|
55
50
|
}
|
|
56
51
|
createTree(options) {
|
|
57
|
-
return new TreeMultimap(
|
|
52
|
+
return new TreeMultimap([], {
|
|
53
|
+
iterationType: this.iterationType,
|
|
54
|
+
comparator: this.comparator, ...options
|
|
55
|
+
});
|
|
58
56
|
}
|
|
59
|
-
/**
|
|
60
|
-
* 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.
|
|
61
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
62
|
-
*/
|
|
63
57
|
/**
|
|
64
58
|
* 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.
|
|
65
59
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -154,8 +148,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
154
148
|
return inserted;
|
|
155
149
|
}
|
|
156
150
|
/**
|
|
157
|
-
* Time Complexity: O(
|
|
158
|
-
* Space Complexity: O(1) - constant space, as it
|
|
151
|
+
* 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.
|
|
152
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
159
153
|
*/
|
|
160
154
|
/**
|
|
161
155
|
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
|
@@ -187,8 +181,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
187
181
|
return inserted;
|
|
188
182
|
}
|
|
189
183
|
/**
|
|
190
|
-
* Time Complexity: O(
|
|
191
|
-
* Space Complexity: O(1) - constant space, as it
|
|
184
|
+
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
185
|
+
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
192
186
|
*/
|
|
193
187
|
/**
|
|
194
188
|
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
@@ -201,7 +195,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
201
195
|
* values:
|
|
202
196
|
* @returns a boolean value.
|
|
203
197
|
*/
|
|
204
|
-
perfectlyBalance(iterationType = this.
|
|
198
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
205
199
|
const sorted = this.dfs(node => node, 'in'), n = sorted.length;
|
|
206
200
|
if (sorted.length < 1)
|
|
207
201
|
return false;
|
|
@@ -238,8 +232,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
238
232
|
}
|
|
239
233
|
}
|
|
240
234
|
/**
|
|
241
|
-
* Time Complexity: O(
|
|
242
|
-
* Space Complexity: O(
|
|
235
|
+
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
|
236
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
243
237
|
*/
|
|
244
238
|
/**
|
|
245
239
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
|
@@ -318,8 +312,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
318
312
|
return deletedResult;
|
|
319
313
|
}
|
|
320
314
|
/**
|
|
321
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree.
|
|
322
|
-
* Space Complexity: O(
|
|
315
|
+
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
316
|
+
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
|
323
317
|
*/
|
|
324
318
|
/**
|
|
325
319
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
@@ -328,6 +322,23 @@ export class TreeMultimap extends AVLTree {
|
|
|
328
322
|
super.clear();
|
|
329
323
|
this._count = 0;
|
|
330
324
|
}
|
|
325
|
+
/**
|
|
326
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
|
327
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
328
|
+
*/
|
|
329
|
+
init(elements) {
|
|
330
|
+
if (elements) {
|
|
331
|
+
for (const entryOrKey of elements) {
|
|
332
|
+
if (Array.isArray(entryOrKey)) {
|
|
333
|
+
const [key, value] = entryOrKey;
|
|
334
|
+
this.add(key, value);
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
this.add(entryOrKey);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
331
342
|
/**
|
|
332
343
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
333
344
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -589,7 +589,7 @@ export class AbstractGraph {
|
|
|
589
589
|
if (vertexOrKey instanceof AbstractVertex)
|
|
590
590
|
distMap.set(vertexOrKey, Infinity);
|
|
591
591
|
}
|
|
592
|
-
const heap = new PriorityQueue({ comparator: (a, b) => a.key - b.key });
|
|
592
|
+
const heap = new PriorityQueue([], { comparator: (a, b) => a.key - b.key });
|
|
593
593
|
heap.add({ key: 0, value: srcVertex });
|
|
594
594
|
distMap.set(srcVertex, 0);
|
|
595
595
|
preMap.set(srcVertex, null);
|
|
@@ -143,7 +143,7 @@ export declare class HashMap<K = any, V = any> {
|
|
|
143
143
|
* @returns a new HashMap object that contains the key-value pairs from the original HashMap that
|
|
144
144
|
* satisfy the given predicate function.
|
|
145
145
|
*/
|
|
146
|
-
filter(predicate: (element: [K, V], map: HashMap<K, V>) => boolean): HashMap<K, V>;
|
|
146
|
+
filter(predicate: (element: [K, V], index: number, map: HashMap<K, V>) => boolean): HashMap<K, V>;
|
|
147
147
|
/**
|
|
148
148
|
* The `map` function takes a callback function and returns a new HashMap with the values transformed
|
|
149
149
|
* by the callback.
|
|
@@ -151,7 +151,7 @@ export declare class HashMap<K = any, V = any> {
|
|
|
151
151
|
* `map`.
|
|
152
152
|
* @returns a new HashMap object with the values mapped according to the provided callback function.
|
|
153
153
|
*/
|
|
154
|
-
map<NV>(callback: (element: [K, V], map: HashMap<K, V>) => NV): HashMap<K, NV>;
|
|
154
|
+
map<NV>(callback: (element: [K, V], index: number, map: HashMap<K, V>) => NV): HashMap<K, NV>;
|
|
155
155
|
/**
|
|
156
156
|
* The `reduce` function iterates over the elements of a HashMap and applies a callback function to
|
|
157
157
|
* each element, accumulating a single value.
|
|
@@ -164,7 +164,7 @@ export declare class HashMap<K = any, V = any> {
|
|
|
164
164
|
* @returns The `reduce` function is returning the final value of the accumulator after iterating
|
|
165
165
|
* over all the elements in the HashMap and applying the callback function to each element.
|
|
166
166
|
*/
|
|
167
|
-
reduce<A>(callback: (accumulator: A, element: [K, V], map: HashMap<K, V>) => A, initialValue: A): A;
|
|
167
|
+
reduce<A>(callback: (accumulator: A, element: [K, V], index: number, map: HashMap<K, V>) => A, initialValue: A): A;
|
|
168
168
|
/**
|
|
169
169
|
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
170
170
|
* Space Complexity: O(1)
|