min-heap-typed 1.47.5 → 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/dist/data-structures/binary-tree/avl-tree.d.ts +8 -8
- package/dist/data-structures/binary-tree/avl-tree.js +23 -15
- package/dist/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/data-structures/binary-tree/binary-tree.js +66 -82
- package/dist/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/data-structures/binary-tree/bst.js +56 -40
- package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/data-structures/binary-tree/rb-tree.js +26 -17
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/data-structures/binary-tree/tree-multimap.js +31 -22
- package/dist/data-structures/graph/abstract-graph.js +1 -1
- package/dist/data-structures/heap/heap.d.ts +19 -21
- package/dist/data-structures/heap/heap.js +52 -34
- package/dist/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/data-structures/heap/max-heap.js +2 -2
- package/dist/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/data-structures/heap/min-heap.js +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +9 -1
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +8 -1
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/data-structures/queue/deque.d.ts +1 -0
- package/dist/data-structures/queue/deque.js +3 -0
- package/dist/data-structures/queue/queue.d.ts +1 -0
- package/dist/data-structures/queue/queue.js +3 -0
- package/dist/data-structures/stack/stack.d.ts +2 -1
- package/dist/data-structures/stack/stack.js +10 -2
- package/dist/interfaces/binary-tree.d.ts +3 -1
- package/dist/types/common.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/package.json +2 -2
- 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/heap/heap.ts +57 -39
- 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 +10 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
- 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 +4 -0
- package/src/data-structures/queue/queue.ts +4 -0
- package/src/data-structures/stack/stack.ts +12 -3
- 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
|
@@ -51,15 +51,18 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
51
51
|
* @param {BSTOptions} [options] - An optional object that contains additional configuration options
|
|
52
52
|
* for the binary search tree.
|
|
53
53
|
*/
|
|
54
|
-
constructor(options) {
|
|
55
|
-
super(options);
|
|
54
|
+
constructor(elements, options) {
|
|
55
|
+
super([], options);
|
|
56
|
+
this.comparator = (a, b) => a - b;
|
|
56
57
|
if (options) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
const { comparator } = options;
|
|
59
|
+
if (comparator) {
|
|
60
|
+
this.comparator = comparator;
|
|
61
|
+
}
|
|
61
62
|
}
|
|
62
63
|
this._root = undefined;
|
|
64
|
+
if (elements)
|
|
65
|
+
this.init(elements);
|
|
63
66
|
}
|
|
64
67
|
/**
|
|
65
68
|
* Get the root node of the binary tree.
|
|
@@ -79,12 +82,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
79
82
|
return new BSTNode(key, value);
|
|
80
83
|
}
|
|
81
84
|
createTree(options) {
|
|
82
|
-
return new BST(Object.assign(
|
|
85
|
+
return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
83
86
|
}
|
|
84
|
-
/**
|
|
85
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
86
|
-
* Space Complexity: O(1) - Constant space is used.
|
|
87
|
-
*/
|
|
88
87
|
/**
|
|
89
88
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
90
89
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -175,8 +174,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
175
174
|
return inserted;
|
|
176
175
|
}
|
|
177
176
|
/**
|
|
178
|
-
* Time Complexity: O(
|
|
179
|
-
* Space Complexity: O(
|
|
177
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
178
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
180
179
|
*/
|
|
181
180
|
/**
|
|
182
181
|
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
@@ -198,7 +197,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
198
197
|
* current instance of the binary search tree
|
|
199
198
|
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
200
199
|
*/
|
|
201
|
-
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.
|
|
200
|
+
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
202
201
|
// TODO this addMany function is inefficient, it should be optimized
|
|
203
202
|
function hasNoUndefined(arr) {
|
|
204
203
|
return arr.indexOf(undefined) === -1;
|
|
@@ -268,8 +267,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
268
267
|
return inserted;
|
|
269
268
|
}
|
|
270
269
|
/**
|
|
271
|
-
* Time Complexity: O(log n) -
|
|
272
|
-
* Space Complexity: O(
|
|
270
|
+
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
271
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
273
272
|
*/
|
|
274
273
|
/**
|
|
275
274
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
@@ -286,7 +285,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
286
285
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
287
286
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
288
287
|
*/
|
|
289
|
-
lastKey(beginRoot = this.root, iterationType = this.
|
|
288
|
+
lastKey(beginRoot = this.root, iterationType = this.iterationType) {
|
|
290
289
|
var _a, _b, _c, _d, _e, _f;
|
|
291
290
|
if (this._compare(0, 1) === types_1.CP.lt)
|
|
292
291
|
return (_b = (_a = this.getRightMost(beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.key) !== null && _b !== void 0 ? _b : 0;
|
|
@@ -297,7 +296,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
297
296
|
}
|
|
298
297
|
/**
|
|
299
298
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
300
|
-
* Space Complexity: O(
|
|
299
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
301
300
|
*/
|
|
302
301
|
/**
|
|
303
302
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
@@ -344,6 +343,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
344
343
|
}
|
|
345
344
|
}
|
|
346
345
|
}
|
|
346
|
+
/**
|
|
347
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
348
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
349
|
+
*/
|
|
347
350
|
/**
|
|
348
351
|
* The function `ensureNotKey` returns the node corresponding to the given key if it is a node key,
|
|
349
352
|
* otherwise it returns the key itself.
|
|
@@ -356,10 +359,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
356
359
|
ensureNotKey(key, iterationType = types_1.IterationType.ITERATIVE) {
|
|
357
360
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
358
361
|
}
|
|
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
362
|
/**
|
|
364
363
|
* 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
364
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -383,7 +382,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
383
382
|
* performed on the binary tree. It can have two possible values:
|
|
384
383
|
* @returns The method returns an array of nodes (`N[]`).
|
|
385
384
|
*/
|
|
386
|
-
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.
|
|
385
|
+
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
387
386
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
388
387
|
if (!beginRoot)
|
|
389
388
|
return [];
|
|
@@ -464,7 +463,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
464
463
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
465
464
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
466
465
|
*/
|
|
467
|
-
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.
|
|
466
|
+
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
468
467
|
targetNode = this.ensureNotKey(targetNode);
|
|
469
468
|
const ans = [];
|
|
470
469
|
if (!targetNode)
|
|
@@ -505,17 +504,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
505
504
|
}
|
|
506
505
|
}
|
|
507
506
|
/**
|
|
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.
|
|
507
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
508
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
519
509
|
*/
|
|
520
510
|
/**
|
|
521
511
|
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
@@ -528,7 +518,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
528
518
|
* values:
|
|
529
519
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
530
520
|
*/
|
|
531
|
-
perfectlyBalance(iterationType = this.
|
|
521
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
532
522
|
const sorted = this.dfs(node => node, 'in'), n = sorted.length;
|
|
533
523
|
this.clear();
|
|
534
524
|
if (sorted.length < 1)
|
|
@@ -566,8 +556,17 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
566
556
|
}
|
|
567
557
|
}
|
|
568
558
|
/**
|
|
569
|
-
*
|
|
570
|
-
*
|
|
559
|
+
* Balancing Adjustment:
|
|
560
|
+
* 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.
|
|
561
|
+
* 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.
|
|
562
|
+
*
|
|
563
|
+
* Use Cases and Efficiency:
|
|
564
|
+
* 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.
|
|
565
|
+
* 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).
|
|
566
|
+
*/
|
|
567
|
+
/**
|
|
568
|
+
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
569
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
571
570
|
*/
|
|
572
571
|
/**
|
|
573
572
|
* Time Complexity: O(n) - Visiting each node once.
|
|
@@ -578,7 +577,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
578
577
|
* to check if the AVL tree is balanced. It can have two possible values:
|
|
579
578
|
* @returns a boolean value.
|
|
580
579
|
*/
|
|
581
|
-
isAVLBalanced(iterationType = this.
|
|
580
|
+
isAVLBalanced(iterationType = this.iterationType) {
|
|
582
581
|
var _a, _b;
|
|
583
582
|
if (!this.root)
|
|
584
583
|
return true;
|
|
@@ -624,6 +623,23 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
624
623
|
}
|
|
625
624
|
return balanced;
|
|
626
625
|
}
|
|
626
|
+
/**
|
|
627
|
+
* Time Complexity: O(n) - Visiting each node once.
|
|
628
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
629
|
+
*/
|
|
630
|
+
init(elements) {
|
|
631
|
+
if (elements) {
|
|
632
|
+
for (const entryOrKey of elements) {
|
|
633
|
+
if (Array.isArray(entryOrKey)) {
|
|
634
|
+
const [key, value] = entryOrKey;
|
|
635
|
+
this.add(key, value);
|
|
636
|
+
}
|
|
637
|
+
else {
|
|
638
|
+
this.add(entryOrKey);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
627
643
|
_setRoot(v) {
|
|
628
644
|
if (v) {
|
|
629
645
|
v.parent = undefined;
|
|
@@ -639,7 +655,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
639
655
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
640
656
|
*/
|
|
641
657
|
_compare(a, b) {
|
|
642
|
-
const compared = this.
|
|
658
|
+
const compared = this.comparator(a, b);
|
|
643
659
|
if (compared > 0)
|
|
644
660
|
return types_1.CP.gt;
|
|
645
661
|
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)
|
|
@@ -31,17 +31,13 @@ class RedBlackTree extends bst_1.BST {
|
|
|
31
31
|
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
32
32
|
* passed to the constructor. It is used to configure the RBTree object with specific options.
|
|
33
33
|
*/
|
|
34
|
-
constructor(options) {
|
|
35
|
-
super(options);
|
|
34
|
+
constructor(elements, options) {
|
|
35
|
+
super([], options);
|
|
36
36
|
this.Sentinel = new RedBlackTreeNode(NaN);
|
|
37
37
|
this._size = 0;
|
|
38
|
-
if (options) {
|
|
39
|
-
this.options = Object.assign({ iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b }, options);
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
this.options = { iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b };
|
|
43
|
-
}
|
|
44
38
|
this._root = this.Sentinel;
|
|
39
|
+
if (elements)
|
|
40
|
+
this.init(elements);
|
|
45
41
|
}
|
|
46
42
|
get root() {
|
|
47
43
|
return this._root;
|
|
@@ -53,12 +49,8 @@ class RedBlackTree extends bst_1.BST {
|
|
|
53
49
|
return new RedBlackTreeNode(key, value, color);
|
|
54
50
|
}
|
|
55
51
|
createTree(options) {
|
|
56
|
-
return new RedBlackTree(Object.assign(
|
|
52
|
+
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
57
53
|
}
|
|
58
|
-
/**
|
|
59
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
60
|
-
* Space Complexity: O(1)
|
|
61
|
-
*/
|
|
62
54
|
/**
|
|
63
55
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
64
56
|
* Space Complexity: O(1)
|
|
@@ -204,13 +196,13 @@ class RedBlackTree extends bst_1.BST {
|
|
|
204
196
|
// TODO
|
|
205
197
|
return ans;
|
|
206
198
|
}
|
|
207
|
-
isRealNode(node) {
|
|
208
|
-
return node !== this.Sentinel && node !== undefined;
|
|
209
|
-
}
|
|
210
199
|
/**
|
|
211
200
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
212
201
|
* Space Complexity: O(1)
|
|
213
202
|
*/
|
|
203
|
+
isRealNode(node) {
|
|
204
|
+
return node !== this.Sentinel && node !== undefined;
|
|
205
|
+
}
|
|
214
206
|
/**
|
|
215
207
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
216
208
|
* Space Complexity: O(1)
|
|
@@ -232,7 +224,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
232
224
|
* `getNodes` method, which is called within the `getNode` method.
|
|
233
225
|
* @returns a value of type `N`, `null`, or `undefined`.
|
|
234
226
|
*/
|
|
235
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
227
|
+
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
236
228
|
var _a;
|
|
237
229
|
if (identifier instanceof binary_tree_1.BinaryTreeNode)
|
|
238
230
|
callback = (node => node);
|
|
@@ -287,10 +279,27 @@ class RedBlackTree extends bst_1.BST {
|
|
|
287
279
|
}
|
|
288
280
|
return y;
|
|
289
281
|
}
|
|
282
|
+
/**
|
|
283
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
284
|
+
* Space Complexity: O(1)
|
|
285
|
+
*/
|
|
290
286
|
clear() {
|
|
291
287
|
this._root = this.Sentinel;
|
|
292
288
|
this._size = 0;
|
|
293
289
|
}
|
|
290
|
+
init(elements) {
|
|
291
|
+
if (elements) {
|
|
292
|
+
for (const entryOrKey of elements) {
|
|
293
|
+
if (Array.isArray(entryOrKey)) {
|
|
294
|
+
const [key, value] = entryOrKey;
|
|
295
|
+
this.add(key, value);
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
this.add(entryOrKey);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
294
303
|
_setRoot(v) {
|
|
295
304
|
if (v) {
|
|
296
305
|
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.
|
|
@@ -30,15 +30,11 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
30
30
|
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
31
31
|
* TreeMultimap.
|
|
32
32
|
*/
|
|
33
|
-
constructor(options
|
|
34
|
-
super(options);
|
|
33
|
+
constructor(elements, options) {
|
|
34
|
+
super([], options);
|
|
35
35
|
this._count = 0;
|
|
36
|
-
if (
|
|
37
|
-
this.
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
this.options = { iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b };
|
|
41
|
-
}
|
|
36
|
+
if (elements)
|
|
37
|
+
this.init(elements);
|
|
42
38
|
}
|
|
43
39
|
get count() {
|
|
44
40
|
return this._count;
|
|
@@ -56,12 +52,8 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
56
52
|
return new TreeMultimapNode(key, value, count);
|
|
57
53
|
}
|
|
58
54
|
createTree(options) {
|
|
59
|
-
return new TreeMultimap(Object.assign(
|
|
55
|
+
return new TreeMultimap([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
60
56
|
}
|
|
61
|
-
/**
|
|
62
|
-
* 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.
|
|
63
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
64
|
-
*/
|
|
65
57
|
/**
|
|
66
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.
|
|
67
59
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -156,8 +148,8 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
156
148
|
return inserted;
|
|
157
149
|
}
|
|
158
150
|
/**
|
|
159
|
-
* Time Complexity: O(
|
|
160
|
-
* 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.
|
|
161
153
|
*/
|
|
162
154
|
/**
|
|
163
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.
|
|
@@ -189,8 +181,8 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
189
181
|
return inserted;
|
|
190
182
|
}
|
|
191
183
|
/**
|
|
192
|
-
* Time Complexity: O(
|
|
193
|
-
* 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.
|
|
194
186
|
*/
|
|
195
187
|
/**
|
|
196
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.
|
|
@@ -203,7 +195,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
203
195
|
* values:
|
|
204
196
|
* @returns a boolean value.
|
|
205
197
|
*/
|
|
206
|
-
perfectlyBalance(iterationType = this.
|
|
198
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
207
199
|
const sorted = this.dfs(node => node, 'in'), n = sorted.length;
|
|
208
200
|
if (sorted.length < 1)
|
|
209
201
|
return false;
|
|
@@ -240,8 +232,8 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
240
232
|
}
|
|
241
233
|
}
|
|
242
234
|
/**
|
|
243
|
-
* Time Complexity: O(
|
|
244
|
-
* 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.
|
|
245
237
|
*/
|
|
246
238
|
/**
|
|
247
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.
|
|
@@ -321,8 +313,8 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
321
313
|
return deletedResult;
|
|
322
314
|
}
|
|
323
315
|
/**
|
|
324
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree.
|
|
325
|
-
* Space Complexity: O(
|
|
316
|
+
* 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.
|
|
317
|
+
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
|
326
318
|
*/
|
|
327
319
|
/**
|
|
328
320
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
@@ -331,6 +323,23 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
331
323
|
super.clear();
|
|
332
324
|
this._count = 0;
|
|
333
325
|
}
|
|
326
|
+
/**
|
|
327
|
+
* 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.
|
|
328
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
329
|
+
*/
|
|
330
|
+
init(elements) {
|
|
331
|
+
if (elements) {
|
|
332
|
+
for (const entryOrKey of elements) {
|
|
333
|
+
if (Array.isArray(entryOrKey)) {
|
|
334
|
+
const [key, value] = entryOrKey;
|
|
335
|
+
this.add(key, value);
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
this.add(entryOrKey);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
334
343
|
/**
|
|
335
344
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
336
345
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -594,7 +594,7 @@ class AbstractGraph {
|
|
|
594
594
|
if (vertexOrKey instanceof AbstractVertex)
|
|
595
595
|
distMap.set(vertexOrKey, Infinity);
|
|
596
596
|
}
|
|
597
|
-
const heap = new priority_queue_1.PriorityQueue({ comparator: (a, b) => a.key - b.key });
|
|
597
|
+
const heap = new priority_queue_1.PriorityQueue([], { comparator: (a, b) => a.key - b.key });
|
|
598
598
|
heap.add({ key: 0, value: srcVertex });
|
|
599
599
|
distMap.set(srcVertex, 0);
|
|
600
600
|
preMap.set(srcVertex, null);
|