heap-typed 1.50.7 → 1.50.9
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-multi-map.d.ts +14 -3
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +17 -6
- package/dist/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +33 -34
- package/dist/data-structures/binary-tree/bst.d.ts +22 -3
- package/dist/data-structures/binary-tree/bst.js +78 -39
- package/dist/data-structures/binary-tree/rb-tree.d.ts +5 -5
- package/dist/data-structures/binary-tree/rb-tree.js +47 -50
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +40 -23
- package/dist/data-structures/binary-tree/tree-multi-map.js +44 -28
- package/dist/data-structures/heap/heap.d.ts +1 -1
- package/dist/data-structures/heap/heap.js +5 -5
- package/dist/types/common.d.ts +6 -29
- package/dist/types/common.js +0 -40
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/rb-tree.js +0 -6
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +20 -7
- package/src/data-structures/binary-tree/binary-tree.ts +54 -45
- package/src/data-structures/binary-tree/bst.ts +86 -42
- package/src/data-structures/binary-tree/rb-tree.ts +49 -49
- package/src/data-structures/binary-tree/tree-multi-map.ts +48 -29
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/types/common.ts +6 -30
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
1
|
+
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, IterationType, KeyOrNodeOrEntry, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
2
2
|
import { CRUD, RBTNColor } from '../../types';
|
|
3
3
|
import { BST, BSTNode } from './bst';
|
|
4
4
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -12,7 +12,7 @@ export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTre
|
|
|
12
12
|
* associated with the key in the Red-Black Tree Node. It is not required and can be omitted when
|
|
13
13
|
* creating a new instance of the Red-Black Tree Node.
|
|
14
14
|
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black
|
|
15
|
-
* Tree Node. It is an optional parameter with a default value of `
|
|
15
|
+
* Tree Node. It is an optional parameter with a default value of `'BLACK'`.
|
|
16
16
|
*/
|
|
17
17
|
constructor(key: K, value?: V, color?: RBTNColor);
|
|
18
18
|
protected _color: RBTNColor;
|
|
@@ -58,8 +58,8 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
58
58
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
59
59
|
* associated with the key in the node. It is not required and can be omitted if not needed.
|
|
60
60
|
* @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
|
|
61
|
-
* Red-Black Tree. It is an optional parameter with a default value of "
|
|
62
|
-
* can be either "
|
|
61
|
+
* Red-Black Tree. It is an optional parameter with a default value of "'BLACK'". The color
|
|
62
|
+
* can be either "'RED'" or "'BLACK'".
|
|
63
63
|
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
64
64
|
* value, and color.
|
|
65
65
|
*/
|
|
@@ -140,7 +140,7 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
140
140
|
* its default value is taken from the `iterationType` property of the class.
|
|
141
141
|
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
142
142
|
*/
|
|
143
|
-
getNode<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, beginRoot?: BSTNKeyOrNode<K, NODE>, iterationType?:
|
|
143
|
+
getNode<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, beginRoot?: BSTNKeyOrNode<K, NODE>, iterationType?: IterationType): NODE | null | undefined;
|
|
144
144
|
/**
|
|
145
145
|
* Time Complexity: O(1)
|
|
146
146
|
* Space Complexity: O(1)
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RedBlackTree = exports.RedBlackTreeNode = void 0;
|
|
4
|
-
const types_1 = require("../../types");
|
|
5
4
|
const bst_1 = require("./bst");
|
|
6
5
|
class RedBlackTreeNode extends bst_1.BSTNode {
|
|
7
6
|
/**
|
|
@@ -13,9 +12,9 @@ class RedBlackTreeNode extends bst_1.BSTNode {
|
|
|
13
12
|
* associated with the key in the Red-Black Tree Node. It is not required and can be omitted when
|
|
14
13
|
* creating a new instance of the Red-Black Tree Node.
|
|
15
14
|
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black
|
|
16
|
-
* Tree Node. It is an optional parameter with a default value of `
|
|
15
|
+
* Tree Node. It is an optional parameter with a default value of `'BLACK'`.
|
|
17
16
|
*/
|
|
18
|
-
constructor(key, value, color =
|
|
17
|
+
constructor(key, value, color = 'BLACK') {
|
|
19
18
|
super(key, value);
|
|
20
19
|
this._color = color;
|
|
21
20
|
}
|
|
@@ -75,12 +74,12 @@ class RedBlackTree extends bst_1.BST {
|
|
|
75
74
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
76
75
|
* associated with the key in the node. It is not required and can be omitted if not needed.
|
|
77
76
|
* @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
|
|
78
|
-
* Red-Black Tree. It is an optional parameter with a default value of "
|
|
79
|
-
* can be either "
|
|
77
|
+
* Red-Black Tree. It is an optional parameter with a default value of "'BLACK'". The color
|
|
78
|
+
* can be either "'RED'" or "'BLACK'".
|
|
80
79
|
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
81
80
|
* value, and color.
|
|
82
81
|
*/
|
|
83
|
-
createNode(key, value, color =
|
|
82
|
+
createNode(key, value, color = 'BLACK') {
|
|
84
83
|
return new RedBlackTreeNode(key, value, color);
|
|
85
84
|
}
|
|
86
85
|
/**
|
|
@@ -121,11 +120,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
121
120
|
return;
|
|
122
121
|
}
|
|
123
122
|
else {
|
|
124
|
-
node = this.createNode(key, value,
|
|
123
|
+
node = this.createNode(key, value, 'RED');
|
|
125
124
|
}
|
|
126
125
|
}
|
|
127
126
|
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
128
|
-
node = this.createNode(keyOrNodeOrEntry, value,
|
|
127
|
+
node = this.createNode(keyOrNodeOrEntry, value, 'RED');
|
|
129
128
|
}
|
|
130
129
|
else {
|
|
131
130
|
return;
|
|
@@ -193,9 +192,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
193
192
|
*/
|
|
194
193
|
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
195
194
|
var _a;
|
|
196
|
-
|
|
197
|
-
callback = (node => node);
|
|
198
|
-
return (_a = super.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
195
|
+
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
199
196
|
}
|
|
200
197
|
/**
|
|
201
198
|
* Time Complexity: O(1)
|
|
@@ -234,10 +231,10 @@ class RedBlackTree extends bst_1.BST {
|
|
|
234
231
|
if (!this.isRealNode(newNode))
|
|
235
232
|
return false;
|
|
236
233
|
const insertStatus = this._insert(newNode);
|
|
237
|
-
if (insertStatus ===
|
|
234
|
+
if (insertStatus === 'CREATED') {
|
|
238
235
|
// Ensure the root is black
|
|
239
236
|
if (this.isRealNode(this._root)) {
|
|
240
|
-
this._root.color =
|
|
237
|
+
this._root.color = 'BLACK';
|
|
241
238
|
}
|
|
242
239
|
else {
|
|
243
240
|
return false;
|
|
@@ -246,7 +243,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
246
243
|
return true;
|
|
247
244
|
}
|
|
248
245
|
else
|
|
249
|
-
return insertStatus ===
|
|
246
|
+
return insertStatus === 'UPDATED';
|
|
250
247
|
}
|
|
251
248
|
/**
|
|
252
249
|
* Time Complexity: O(log n)
|
|
@@ -313,7 +310,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
313
310
|
}
|
|
314
311
|
this._size--;
|
|
315
312
|
// If the original color was black, fix the tree
|
|
316
|
-
if (originalColor ===
|
|
313
|
+
if (originalColor === 'BLACK') {
|
|
317
314
|
this._deleteFixup(replacementNode);
|
|
318
315
|
}
|
|
319
316
|
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
@@ -379,7 +376,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
379
376
|
}
|
|
380
377
|
else {
|
|
381
378
|
this._replaceNode(current, node);
|
|
382
|
-
return
|
|
379
|
+
return 'UPDATED';
|
|
383
380
|
}
|
|
384
381
|
}
|
|
385
382
|
node.parent = parent;
|
|
@@ -394,9 +391,9 @@ class RedBlackTree extends bst_1.BST {
|
|
|
394
391
|
}
|
|
395
392
|
node.left = this.SENTINEL;
|
|
396
393
|
node.right = this.SENTINEL;
|
|
397
|
-
node.color =
|
|
394
|
+
node.color = 'RED';
|
|
398
395
|
this._insertFixup(node);
|
|
399
|
-
return
|
|
396
|
+
return 'CREATED';
|
|
400
397
|
}
|
|
401
398
|
/**
|
|
402
399
|
* Time Complexity: O(1)
|
|
@@ -440,16 +437,16 @@ class RedBlackTree extends bst_1.BST {
|
|
|
440
437
|
_insertFixup(z) {
|
|
441
438
|
var _a, _b, _c, _d;
|
|
442
439
|
// Continue fixing the tree as long as the parent of z is red
|
|
443
|
-
while (((_a = z === null || z === void 0 ? void 0 : z.parent) === null || _a === void 0 ? void 0 : _a.color) ===
|
|
440
|
+
while (((_a = z === null || z === void 0 ? void 0 : z.parent) === null || _a === void 0 ? void 0 : _a.color) === 'RED') {
|
|
444
441
|
// Check if the parent of z is the left child of its parent
|
|
445
442
|
if (z.parent === ((_b = z.parent.parent) === null || _b === void 0 ? void 0 : _b.left)) {
|
|
446
443
|
// Case 1: The uncle (y) of z is red
|
|
447
444
|
const y = z.parent.parent.right;
|
|
448
|
-
if ((y === null || y === void 0 ? void 0 : y.color) ===
|
|
445
|
+
if ((y === null || y === void 0 ? void 0 : y.color) === 'RED') {
|
|
449
446
|
// Set colors to restore properties of Red-Black Tree
|
|
450
|
-
z.parent.color =
|
|
451
|
-
y.color =
|
|
452
|
-
z.parent.parent.color =
|
|
447
|
+
z.parent.color = 'BLACK';
|
|
448
|
+
y.color = 'BLACK';
|
|
449
|
+
z.parent.parent.color = 'RED';
|
|
453
450
|
// Move up the tree to continue fixing
|
|
454
451
|
z = z.parent.parent;
|
|
455
452
|
}
|
|
@@ -463,8 +460,8 @@ class RedBlackTree extends bst_1.BST {
|
|
|
463
460
|
// Case 3: The uncle (y) of z is black, and z is a left child
|
|
464
461
|
// Adjust colors and perform a right rotation
|
|
465
462
|
if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
|
|
466
|
-
z.parent.color =
|
|
467
|
-
z.parent.parent.color =
|
|
463
|
+
z.parent.color = 'BLACK';
|
|
464
|
+
z.parent.parent.color = 'RED';
|
|
468
465
|
this._rightRotate(z.parent.parent);
|
|
469
466
|
}
|
|
470
467
|
}
|
|
@@ -473,10 +470,10 @@ class RedBlackTree extends bst_1.BST {
|
|
|
473
470
|
// Symmetric case for the right child (left and right exchanged)
|
|
474
471
|
// Follow the same logic as above with left and right exchanged
|
|
475
472
|
const y = (_d = (_c = z === null || z === void 0 ? void 0 : z.parent) === null || _c === void 0 ? void 0 : _c.parent) === null || _d === void 0 ? void 0 : _d.left;
|
|
476
|
-
if ((y === null || y === void 0 ? void 0 : y.color) ===
|
|
477
|
-
z.parent.color =
|
|
478
|
-
y.color =
|
|
479
|
-
z.parent.parent.color =
|
|
473
|
+
if ((y === null || y === void 0 ? void 0 : y.color) === 'RED') {
|
|
474
|
+
z.parent.color = 'BLACK';
|
|
475
|
+
y.color = 'BLACK';
|
|
476
|
+
z.parent.parent.color = 'RED';
|
|
480
477
|
z = z.parent.parent;
|
|
481
478
|
}
|
|
482
479
|
else {
|
|
@@ -485,8 +482,8 @@ class RedBlackTree extends bst_1.BST {
|
|
|
485
482
|
this._rightRotate(z);
|
|
486
483
|
}
|
|
487
484
|
if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
|
|
488
|
-
z.parent.color =
|
|
489
|
-
z.parent.parent.color =
|
|
485
|
+
z.parent.color = 'BLACK';
|
|
486
|
+
z.parent.parent.color = 'RED';
|
|
490
487
|
this._leftRotate(z.parent.parent);
|
|
491
488
|
}
|
|
492
489
|
}
|
|
@@ -494,7 +491,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
494
491
|
}
|
|
495
492
|
// Ensure that the root is black after fixing
|
|
496
493
|
if (this.isRealNode(this._root))
|
|
497
|
-
this._root.color =
|
|
494
|
+
this._root.color = 'BLACK';
|
|
498
495
|
}
|
|
499
496
|
/**
|
|
500
497
|
* Time Complexity: O(log n)
|
|
@@ -513,13 +510,13 @@ class RedBlackTree extends bst_1.BST {
|
|
|
513
510
|
_deleteFixup(node) {
|
|
514
511
|
var _a, _b, _c, _d;
|
|
515
512
|
// Early exit condition
|
|
516
|
-
if (!node || node === this.root || node.color ===
|
|
513
|
+
if (!node || node === this.root || node.color === 'BLACK') {
|
|
517
514
|
if (node) {
|
|
518
|
-
node.color =
|
|
515
|
+
node.color = 'BLACK'; // Ensure the final node is black
|
|
519
516
|
}
|
|
520
517
|
return;
|
|
521
518
|
}
|
|
522
|
-
while (node && node !== this.root && node.color ===
|
|
519
|
+
while (node && node !== this.root && node.color === 'BLACK') {
|
|
523
520
|
const parent = node.parent;
|
|
524
521
|
if (!parent) {
|
|
525
522
|
break; // Ensure the loop terminates if there's an issue with the tree structure
|
|
@@ -527,25 +524,25 @@ class RedBlackTree extends bst_1.BST {
|
|
|
527
524
|
if (node === parent.left) {
|
|
528
525
|
let sibling = parent.right;
|
|
529
526
|
// Cases 1 and 2: Sibling is red or both children of sibling are black
|
|
530
|
-
if ((sibling === null || sibling === void 0 ? void 0 : sibling.color) ===
|
|
531
|
-
sibling.color =
|
|
532
|
-
parent.color =
|
|
527
|
+
if ((sibling === null || sibling === void 0 ? void 0 : sibling.color) === 'RED') {
|
|
528
|
+
sibling.color = 'BLACK';
|
|
529
|
+
parent.color = 'RED';
|
|
533
530
|
this._leftRotate(parent);
|
|
534
531
|
sibling = parent.right;
|
|
535
532
|
}
|
|
536
533
|
// Case 3: Sibling's left child is black
|
|
537
|
-
if (((_b = (_a = sibling === null || sibling === void 0 ? void 0 : sibling.left) === null || _a === void 0 ? void 0 : _a.color) !== null && _b !== void 0 ? _b :
|
|
534
|
+
if (((_b = (_a = sibling === null || sibling === void 0 ? void 0 : sibling.left) === null || _a === void 0 ? void 0 : _a.color) !== null && _b !== void 0 ? _b : 'BLACK') === 'BLACK') {
|
|
538
535
|
if (sibling)
|
|
539
|
-
sibling.color =
|
|
536
|
+
sibling.color = 'RED';
|
|
540
537
|
node = parent;
|
|
541
538
|
}
|
|
542
539
|
else {
|
|
543
540
|
// Case 4: Adjust colors and perform a right rotation
|
|
544
541
|
if (sibling === null || sibling === void 0 ? void 0 : sibling.left)
|
|
545
|
-
sibling.left.color =
|
|
542
|
+
sibling.left.color = 'BLACK';
|
|
546
543
|
if (sibling)
|
|
547
544
|
sibling.color = parent.color;
|
|
548
|
-
parent.color =
|
|
545
|
+
parent.color = 'BLACK';
|
|
549
546
|
this._rightRotate(parent);
|
|
550
547
|
node = this.root;
|
|
551
548
|
}
|
|
@@ -554,28 +551,28 @@ class RedBlackTree extends bst_1.BST {
|
|
|
554
551
|
// Symmetric case for the right child (left and right exchanged)
|
|
555
552
|
let sibling = parent.left;
|
|
556
553
|
// Cases 1 and 2: Sibling is red or both children of sibling are black
|
|
557
|
-
if ((sibling === null || sibling === void 0 ? void 0 : sibling.color) ===
|
|
558
|
-
sibling.color =
|
|
554
|
+
if ((sibling === null || sibling === void 0 ? void 0 : sibling.color) === 'RED') {
|
|
555
|
+
sibling.color = 'BLACK';
|
|
559
556
|
if (parent)
|
|
560
|
-
parent.color =
|
|
557
|
+
parent.color = 'RED';
|
|
561
558
|
this._rightRotate(parent);
|
|
562
559
|
if (parent)
|
|
563
560
|
sibling = parent.left;
|
|
564
561
|
}
|
|
565
562
|
// Case 3: Sibling's left child is black
|
|
566
|
-
if (((_d = (_c = sibling === null || sibling === void 0 ? void 0 : sibling.right) === null || _c === void 0 ? void 0 : _c.color) !== null && _d !== void 0 ? _d :
|
|
563
|
+
if (((_d = (_c = sibling === null || sibling === void 0 ? void 0 : sibling.right) === null || _c === void 0 ? void 0 : _c.color) !== null && _d !== void 0 ? _d : 'BLACK') === 'BLACK') {
|
|
567
564
|
if (sibling)
|
|
568
|
-
sibling.color =
|
|
565
|
+
sibling.color = 'RED';
|
|
569
566
|
node = parent;
|
|
570
567
|
}
|
|
571
568
|
else {
|
|
572
569
|
// Case 4: Adjust colors and perform a left rotation
|
|
573
570
|
if (sibling === null || sibling === void 0 ? void 0 : sibling.right)
|
|
574
|
-
sibling.right.color =
|
|
571
|
+
sibling.right.color = 'BLACK';
|
|
575
572
|
if (sibling)
|
|
576
573
|
sibling.color = parent.color;
|
|
577
574
|
if (parent)
|
|
578
|
-
parent.color =
|
|
575
|
+
parent.color = 'BLACK';
|
|
579
576
|
this._leftRotate(parent);
|
|
580
577
|
node = this.root;
|
|
581
578
|
}
|
|
@@ -583,7 +580,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
583
580
|
}
|
|
584
581
|
// Ensure that the final node (possibly the root) is black
|
|
585
582
|
if (node) {
|
|
586
|
-
node.color =
|
|
583
|
+
node.color = 'BLACK';
|
|
587
584
|
}
|
|
588
585
|
}
|
|
589
586
|
/**
|
|
@@ -5,22 +5,24 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types';
|
|
9
|
-
import {
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, IterationType, KeyOrNodeOrEntry, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types';
|
|
9
|
+
import { RBTNColor } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
|
|
12
12
|
export declare class TreeMultiMapNode<K = any, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>> extends RedBlackTreeNode<K, V, NODE> {
|
|
13
13
|
/**
|
|
14
|
-
* The constructor function initializes
|
|
15
|
-
* @param {K} key - The key parameter
|
|
16
|
-
*
|
|
17
|
-
* @param {V} [value] - The `value` parameter is an optional parameter
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* 1.
|
|
22
|
-
|
|
23
|
-
|
|
14
|
+
* The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
|
|
15
|
+
* @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
|
|
16
|
+
* used to identify and locate the node within the tree.
|
|
17
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
18
|
+
* associated with the key in the Red-Black Tree node. It is not required and can be omitted when
|
|
19
|
+
* creating a new node.
|
|
20
|
+
* @param [count=1] - The `count` parameter represents the number of occurrences of a particular key
|
|
21
|
+
* in the Red-Black Tree. It is an optional parameter with a default value of 1.
|
|
22
|
+
* @param {RBTNColor} [color=BLACK] - The `color` parameter is used to specify the color of the node
|
|
23
|
+
* in a Red-Black Tree. It is optional and has a default value of `'BLACK'`.
|
|
24
|
+
*/
|
|
25
|
+
constructor(key: K, value?: V, count?: number, color?: RBTNColor);
|
|
24
26
|
protected _count: number;
|
|
25
27
|
/**
|
|
26
28
|
* The function returns the value of the private variable _count.
|
|
@@ -51,19 +53,34 @@ export declare class TreeMultiMap<K = any, V = any, NODE extends TreeMultiMapNod
|
|
|
51
53
|
* @returns the sum of the count property of all nodes in the tree.
|
|
52
54
|
*/
|
|
53
55
|
get count(): number;
|
|
54
|
-
getMutableCount(): number;
|
|
55
56
|
/**
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
* which is a generic type that can be replaced with any specific type when using the function.
|
|
59
|
-
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
60
|
-
* associated with the key in the node. It is of type `V`, which can be any data type.
|
|
61
|
-
* @param {number} [count] - The `count` parameter represents the number of occurrences of a
|
|
62
|
-
* key-value pair in the TreeMultiMap. It is an optional parameter, so if it is not provided, it will
|
|
63
|
-
* default to 1.
|
|
64
|
-
* @returns a new instance of the TreeMultiMapNode class, casted as NODE.
|
|
57
|
+
* Time Complexity: O(n)
|
|
58
|
+
* Space Complexity: O(1)
|
|
65
59
|
*/
|
|
66
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Time Complexity: O(n)
|
|
62
|
+
* Space Complexity: O(1)
|
|
63
|
+
*
|
|
64
|
+
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
65
|
+
* search.
|
|
66
|
+
* @returns the sum of the count property of all nodes in the tree.
|
|
67
|
+
*/
|
|
68
|
+
getComputedCount(): number;
|
|
69
|
+
/**
|
|
70
|
+
* The function creates a new TreeMultiMapNode with the specified key, value, color, and count.
|
|
71
|
+
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
72
|
+
* which is a generic type representing the key type of the node.
|
|
73
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
74
|
+
* node. It is an optional parameter, which means it can be omitted when calling the `createNode`
|
|
75
|
+
* function. If provided, it should be of type `V`.
|
|
76
|
+
* @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
|
|
77
|
+
* a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
|
|
78
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
|
|
79
|
+
* the tree. It is an optional parameter and is used to keep track of the number of values associated
|
|
80
|
+
* with a key in the tree.
|
|
81
|
+
* @returns A new instance of the TreeMultiMapNode class is being returned.
|
|
82
|
+
*/
|
|
83
|
+
createNode(key: K, value?: V, color?: RBTNColor, count?: number): NODE;
|
|
67
84
|
/**
|
|
68
85
|
* The function creates a new instance of a TreeMultiMap with the specified options and returns it.
|
|
69
86
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TreeMultiMap = exports.TreeMultiMapNode = void 0;
|
|
4
|
-
const types_1 = require("../../types");
|
|
5
4
|
const rb_tree_1 = require("./rb-tree");
|
|
6
5
|
class TreeMultiMapNode extends rb_tree_1.RedBlackTreeNode {
|
|
7
6
|
/**
|
|
8
|
-
* The constructor function initializes
|
|
9
|
-
* @param {K} key - The key parameter
|
|
10
|
-
*
|
|
11
|
-
* @param {V} [value] - The `value` parameter is an optional parameter
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* 1.
|
|
7
|
+
* The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
|
|
8
|
+
* @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
|
|
9
|
+
* used to identify and locate the node within the tree.
|
|
10
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
11
|
+
* associated with the key in the Red-Black Tree node. It is not required and can be omitted when
|
|
12
|
+
* creating a new node.
|
|
13
|
+
* @param [count=1] - The `count` parameter represents the number of occurrences of a particular key
|
|
14
|
+
* in the Red-Black Tree. It is an optional parameter with a default value of 1.
|
|
15
|
+
* @param {RBTNColor} [color=BLACK] - The `color` parameter is used to specify the color of the node
|
|
16
|
+
* in a Red-Black Tree. It is optional and has a default value of `'BLACK'`.
|
|
16
17
|
*/
|
|
17
|
-
constructor(key, value, count = 1) {
|
|
18
|
-
super(key, value);
|
|
18
|
+
constructor(key, value, count = 1, color = 'BLACK') {
|
|
19
|
+
super(key, value, color);
|
|
19
20
|
this._count = 1;
|
|
20
21
|
this.count = count;
|
|
21
22
|
}
|
|
@@ -60,24 +61,39 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
60
61
|
get count() {
|
|
61
62
|
return this._count;
|
|
62
63
|
}
|
|
63
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Time Complexity: O(n)
|
|
66
|
+
* Space Complexity: O(1)
|
|
67
|
+
*/
|
|
68
|
+
/**
|
|
69
|
+
* Time Complexity: O(n)
|
|
70
|
+
* Space Complexity: O(1)
|
|
71
|
+
*
|
|
72
|
+
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
73
|
+
* search.
|
|
74
|
+
* @returns the sum of the count property of all nodes in the tree.
|
|
75
|
+
*/
|
|
76
|
+
getComputedCount() {
|
|
64
77
|
let sum = 0;
|
|
65
78
|
this.dfs(node => (sum += node.count));
|
|
66
79
|
return sum;
|
|
67
80
|
}
|
|
68
81
|
/**
|
|
69
|
-
* The function creates a new TreeMultiMapNode
|
|
82
|
+
* The function creates a new TreeMultiMapNode with the specified key, value, color, and count.
|
|
70
83
|
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
71
|
-
* which is a generic type
|
|
72
|
-
* @param {V} [value] - The `value` parameter
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
* default
|
|
77
|
-
* @
|
|
84
|
+
* which is a generic type representing the key type of the node.
|
|
85
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
86
|
+
* node. It is an optional parameter, which means it can be omitted when calling the `createNode`
|
|
87
|
+
* function. If provided, it should be of type `V`.
|
|
88
|
+
* @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
|
|
89
|
+
* a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
|
|
90
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
|
|
91
|
+
* the tree. It is an optional parameter and is used to keep track of the number of values associated
|
|
92
|
+
* with a key in the tree.
|
|
93
|
+
* @returns A new instance of the TreeMultiMapNode class is being returned.
|
|
78
94
|
*/
|
|
79
|
-
createNode(key, value, count) {
|
|
80
|
-
return new TreeMultiMapNode(key, value, count);
|
|
95
|
+
createNode(key, value, color = 'BLACK', count) {
|
|
96
|
+
return new TreeMultiMapNode(key, value, count, color);
|
|
81
97
|
}
|
|
82
98
|
/**
|
|
83
99
|
* The function creates a new instance of a TreeMultiMap with the specified options and returns it.
|
|
@@ -115,11 +131,11 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
115
131
|
return;
|
|
116
132
|
}
|
|
117
133
|
else {
|
|
118
|
-
node = this.createNode(key, value, count);
|
|
134
|
+
node = this.createNode(key, value, 'BLACK', count);
|
|
119
135
|
}
|
|
120
136
|
}
|
|
121
137
|
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
122
|
-
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
138
|
+
node = this.createNode(keyOrNodeOrEntry, value, 'BLACK', count);
|
|
123
139
|
}
|
|
124
140
|
else {
|
|
125
141
|
return;
|
|
@@ -271,7 +287,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
271
287
|
}
|
|
272
288
|
this._size--;
|
|
273
289
|
// If the original color was black, fix the tree
|
|
274
|
-
if (originalColor ===
|
|
290
|
+
if (originalColor === 'BLACK') {
|
|
275
291
|
this._deleteFixup(replacementNode);
|
|
276
292
|
}
|
|
277
293
|
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
@@ -308,11 +324,11 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
308
324
|
* @returns a boolean value.
|
|
309
325
|
*/
|
|
310
326
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
311
|
-
const sorted = this.dfs(node => node, '
|
|
327
|
+
const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
|
|
312
328
|
if (sorted.length < 1)
|
|
313
329
|
return false;
|
|
314
330
|
this.clear();
|
|
315
|
-
if (iterationType ===
|
|
331
|
+
if (iterationType === 'RECURSIVE') {
|
|
316
332
|
const buildBalanceBST = (l, r) => {
|
|
317
333
|
if (l > r)
|
|
318
334
|
return;
|
|
@@ -375,7 +391,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
375
391
|
destNode = this.ensureNode(destNode);
|
|
376
392
|
if (srcNode && destNode) {
|
|
377
393
|
const { key, value, count, color } = destNode;
|
|
378
|
-
const tempNode = this.createNode(key, value, count);
|
|
394
|
+
const tempNode = this.createNode(key, value, color, count);
|
|
379
395
|
if (tempNode) {
|
|
380
396
|
tempNode.color = color;
|
|
381
397
|
destNode.key = srcNode.key;
|
|
@@ -159,7 +159,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
159
159
|
* Space Complexity: O(log n)
|
|
160
160
|
*
|
|
161
161
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
162
|
-
* @param order - Traverse order parameter: '
|
|
162
|
+
* @param order - Traverse order parameter: 'IN' (in-order), 'PRE' (pre-order) or 'POST' (post-order).
|
|
163
163
|
* @returns An array containing elements traversed in the specified order.
|
|
164
164
|
*/
|
|
165
165
|
dfs(order?: DFSOrderPattern): E[];
|
|
@@ -229,26 +229,26 @@ class Heap extends base_1.IterableElementBase {
|
|
|
229
229
|
* Space Complexity: O(log n)
|
|
230
230
|
*
|
|
231
231
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
232
|
-
* @param order - Traverse order parameter: '
|
|
232
|
+
* @param order - Traverse order parameter: 'IN' (in-order), 'PRE' (pre-order) or 'POST' (post-order).
|
|
233
233
|
* @returns An array containing elements traversed in the specified order.
|
|
234
234
|
*/
|
|
235
|
-
dfs(order = '
|
|
235
|
+
dfs(order = 'PRE') {
|
|
236
236
|
const result = [];
|
|
237
237
|
// Auxiliary recursive function, traverses the binary heap according to the traversal order
|
|
238
238
|
const _dfs = (index) => {
|
|
239
239
|
const left = 2 * index + 1, right = left + 1;
|
|
240
240
|
if (index < this.size) {
|
|
241
|
-
if (order === '
|
|
241
|
+
if (order === 'IN') {
|
|
242
242
|
_dfs(left);
|
|
243
243
|
result.push(this.elements[index]);
|
|
244
244
|
_dfs(right);
|
|
245
245
|
}
|
|
246
|
-
else if (order === '
|
|
246
|
+
else if (order === 'PRE') {
|
|
247
247
|
result.push(this.elements[index]);
|
|
248
248
|
_dfs(left);
|
|
249
249
|
_dfs(right);
|
|
250
250
|
}
|
|
251
|
-
else if (order === '
|
|
251
|
+
else if (order === 'POST') {
|
|
252
252
|
_dfs(left);
|
|
253
253
|
_dfs(right);
|
|
254
254
|
result.push(this.elements[index]);
|
package/dist/types/common.d.ts
CHANGED
|
@@ -1,33 +1,15 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
INVERSE = "INVERSE"
|
|
4
|
-
}
|
|
5
|
-
export declare enum CP {
|
|
6
|
-
lt = "lt",
|
|
7
|
-
eq = "eq",
|
|
8
|
-
gt = "gt"
|
|
9
|
-
}
|
|
1
|
+
export type BSTVariant = 'STANDARD' | 'INVERSE';
|
|
2
|
+
export type CP = 'LT' | 'EQ' | 'GT';
|
|
10
3
|
/**
|
|
11
4
|
* Enum representing different loop types.
|
|
12
5
|
*
|
|
13
6
|
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
|
|
14
7
|
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
|
|
15
8
|
*/
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
RECURSIVE = "RECURSIVE"
|
|
19
|
-
}
|
|
20
|
-
export declare enum FamilyPosition {
|
|
21
|
-
ROOT = "ROOT",
|
|
22
|
-
LEFT = "LEFT",
|
|
23
|
-
RIGHT = "RIGHT",
|
|
24
|
-
ROOT_LEFT = "ROOT_LEFT",
|
|
25
|
-
ROOT_RIGHT = "ROOT_RIGHT",
|
|
26
|
-
ISOLATED = "ISOLATED",
|
|
27
|
-
MAL_NODE = "MAL_NODE"
|
|
28
|
-
}
|
|
9
|
+
export type IterationType = 'ITERATIVE' | 'RECURSIVE';
|
|
10
|
+
export type FamilyPosition = 'ROOT' | 'LEFT' | 'RIGHT' | 'ROOT_LEFT' | 'ROOT_RIGHT' | 'ISOLATED' | 'MAL_NODE';
|
|
29
11
|
export type Comparator<K> = (a: K, b: K) => number;
|
|
30
|
-
export type DFSOrderPattern = '
|
|
12
|
+
export type DFSOrderPattern = 'PRE' | 'IN' | 'POST';
|
|
31
13
|
export type NodeDisplayLayout = [string[], number, number, number];
|
|
32
14
|
export type BTNCallback<N, D = any> = (node: N) => D;
|
|
33
15
|
export interface IterableWithSize<T> extends Iterable<T> {
|
|
@@ -52,9 +34,4 @@ export type BinaryTreeDeleteResult<N> = {
|
|
|
52
34
|
deleted: N | null | undefined;
|
|
53
35
|
needBalanced: N | null | undefined;
|
|
54
36
|
};
|
|
55
|
-
export
|
|
56
|
-
CREATED = "CREATED",
|
|
57
|
-
READ = "READ",
|
|
58
|
-
UPDATED = "UPDATED",
|
|
59
|
-
DELETED = "DELETED"
|
|
60
|
-
}
|
|
37
|
+
export type CRUD = 'CREATED' | 'READ' | 'UPDATED' | 'DELETED';
|