min-heap-typed 1.41.0 → 1.41.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/binary-tree.d.ts +9 -6
- package/dist/data-structures/binary-tree/binary-tree.js +29 -10
- package/dist/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/data-structures/binary-tree/bst.js +2 -2
- package/dist/data-structures/binary-tree/rb-tree.d.ts +20 -4
- package/dist/data-structures/binary-tree/rb-tree.js +109 -44
- package/dist/data-structures/binary-tree/tree-multiset.js +2 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +73 -25
- package/src/data-structures/binary-tree/bst.ts +7 -4
- package/src/data-structures/binary-tree/rb-tree.ts +121 -68
- package/src/data-structures/binary-tree/tree-multiset.ts +4 -3
- package/src/data-structures/graph/abstract-graph.ts +11 -12
- package/src/data-structures/graph/directed-graph.ts +7 -6
- package/src/data-structures/graph/undirected-graph.ts +7 -6
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +1 -2
- package/src/data-structures/hash/tree-set.ts +1 -2
- package/src/data-structures/heap/heap.ts +2 -2
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/vector2d.ts +1 -2
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +3 -4
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -174,12 +174,15 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
174
174
|
getNodes<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
175
175
|
getNodes<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
176
176
|
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
177
|
-
has<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N, iterationType?: IterationType): boolean;
|
|
178
|
-
has<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N, iterationType?: IterationType): boolean;
|
|
179
|
-
has<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C, beginRoot?: N, iterationType?: IterationType): boolean;
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
177
|
+
has<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N | null, iterationType?: IterationType): boolean;
|
|
178
|
+
has<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): boolean;
|
|
179
|
+
has<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C, beginRoot?: N | null, iterationType?: IterationType): boolean;
|
|
180
|
+
getNode<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
181
|
+
getNode<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
182
|
+
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
183
|
+
get<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N | null, iterationType?: IterationType): V | undefined;
|
|
184
|
+
get<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): V | undefined;
|
|
185
|
+
get<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | null, iterationType?: IterationType): V | undefined;
|
|
183
186
|
/**
|
|
184
187
|
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
185
188
|
* up to the root node, with the option to reverse the order of the nodes.
|
|
@@ -171,7 +171,7 @@ class BinaryTree {
|
|
|
171
171
|
return;
|
|
172
172
|
}
|
|
173
173
|
const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined;
|
|
174
|
-
const existNode = key !== undefined ? this.
|
|
174
|
+
const existNode = key !== undefined ? this.getNode(key, (node) => node.key) : undefined;
|
|
175
175
|
if (this.root) {
|
|
176
176
|
if (existNode) {
|
|
177
177
|
existNode.value = value;
|
|
@@ -249,7 +249,7 @@ class BinaryTree {
|
|
|
249
249
|
return bstDeletedResult;
|
|
250
250
|
if (identifier instanceof BinaryTreeNode)
|
|
251
251
|
callback = (node => node);
|
|
252
|
-
const curr = this.
|
|
252
|
+
const curr = this.getNode(identifier, callback);
|
|
253
253
|
if (!curr)
|
|
254
254
|
return bstDeletedResult;
|
|
255
255
|
const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
|
|
@@ -302,9 +302,9 @@ class BinaryTree {
|
|
|
302
302
|
*/
|
|
303
303
|
getDepth(distNode, beginRoot = this.root) {
|
|
304
304
|
if (typeof distNode === 'number')
|
|
305
|
-
distNode = this.
|
|
305
|
+
distNode = this.getNode(distNode);
|
|
306
306
|
if (typeof beginRoot === 'number')
|
|
307
|
-
beginRoot = this.
|
|
307
|
+
beginRoot = this.getNode(beginRoot);
|
|
308
308
|
let depth = 0;
|
|
309
309
|
while (distNode === null || distNode === void 0 ? void 0 : distNode.parent) {
|
|
310
310
|
if (distNode === beginRoot) {
|
|
@@ -329,7 +329,7 @@ class BinaryTree {
|
|
|
329
329
|
*/
|
|
330
330
|
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
331
331
|
if (typeof beginRoot === 'number')
|
|
332
|
-
beginRoot = this.
|
|
332
|
+
beginRoot = this.getNode(beginRoot);
|
|
333
333
|
if (!beginRoot)
|
|
334
334
|
return -1;
|
|
335
335
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
@@ -503,7 +503,6 @@ class BinaryTree {
|
|
|
503
503
|
has(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
504
504
|
if (identifier instanceof BinaryTreeNode)
|
|
505
505
|
callback = (node => node);
|
|
506
|
-
// TODO may support finding node by value equal
|
|
507
506
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
508
507
|
}
|
|
509
508
|
/**
|
|
@@ -521,13 +520,33 @@ class BinaryTree {
|
|
|
521
520
|
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
522
521
|
* @returns either the found node (of type N) or null if no node is found.
|
|
523
522
|
*/
|
|
524
|
-
|
|
523
|
+
getNode(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
525
524
|
var _a;
|
|
526
525
|
if (identifier instanceof BinaryTreeNode)
|
|
527
526
|
callback = (node => node);
|
|
528
|
-
// TODO may support finding node by value equal
|
|
529
527
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
530
528
|
}
|
|
529
|
+
/**
|
|
530
|
+
* The function `get` returns the first node value in a binary tree that matches the given property or key.
|
|
531
|
+
* @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
|
|
532
|
+
* the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
|
|
533
|
+
* type.
|
|
534
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
535
|
+
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
536
|
+
* whether the node matches the criteria or not. The default callback function
|
|
537
|
+
* (`((node: N) => node.key)`) is used if no callback function is
|
|
538
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
539
|
+
* the root node from which the search should begin.
|
|
540
|
+
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
541
|
+
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
542
|
+
* @returns either the found value (of type V) or undefined if no node value is found.
|
|
543
|
+
*/
|
|
544
|
+
get(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
545
|
+
var _a, _b;
|
|
546
|
+
if (identifier instanceof BinaryTreeNode)
|
|
547
|
+
callback = (node => node);
|
|
548
|
+
return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : undefined;
|
|
549
|
+
}
|
|
531
550
|
/**
|
|
532
551
|
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
533
552
|
* up to the root node, with the option to reverse the order of the nodes.
|
|
@@ -563,7 +582,7 @@ class BinaryTree {
|
|
|
563
582
|
*/
|
|
564
583
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
565
584
|
if (typeof beginRoot === 'number')
|
|
566
|
-
beginRoot = this.
|
|
585
|
+
beginRoot = this.getNode(beginRoot);
|
|
567
586
|
if (!beginRoot)
|
|
568
587
|
return beginRoot;
|
|
569
588
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
@@ -686,7 +705,7 @@ class BinaryTree {
|
|
|
686
705
|
*/
|
|
687
706
|
subTreeTraverse(callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
688
707
|
if (typeof beginRoot === 'number')
|
|
689
|
-
beginRoot = this.
|
|
708
|
+
beginRoot = this.getNode(beginRoot);
|
|
690
709
|
const ans = [];
|
|
691
710
|
if (!beginRoot)
|
|
692
711
|
return ans;
|
|
@@ -72,7 +72,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
72
72
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
73
73
|
* matching node is found.
|
|
74
74
|
*/
|
|
75
|
-
|
|
75
|
+
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
76
76
|
/**
|
|
77
77
|
* The function `lastKey` returns the key of the rightmost node if the comparison result is less
|
|
78
78
|
* than, the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
@@ -223,7 +223,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
223
223
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
224
224
|
* matching node is found.
|
|
225
225
|
*/
|
|
226
|
-
|
|
226
|
+
getNode(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
227
227
|
var _a;
|
|
228
228
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
229
229
|
}
|
|
@@ -347,7 +347,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
347
347
|
*/
|
|
348
348
|
lesserOrGreaterTraverse(callback = ((node) => node.key), lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
349
349
|
if (typeof targetNode === 'number')
|
|
350
|
-
targetNode = this.
|
|
350
|
+
targetNode = this.getNode(targetNode);
|
|
351
351
|
const ans = [];
|
|
352
352
|
if (!targetNode)
|
|
353
353
|
return ans;
|
|
@@ -1,17 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import { RBTNColor } from '../../types';
|
|
1
9
|
export declare class RBTreeNode {
|
|
2
10
|
key: number;
|
|
3
11
|
parent: RBTreeNode;
|
|
4
12
|
left: RBTreeNode;
|
|
5
13
|
right: RBTreeNode;
|
|
6
14
|
color: number;
|
|
7
|
-
constructor();
|
|
15
|
+
constructor(key: number, color?: RBTNColor);
|
|
8
16
|
}
|
|
17
|
+
export declare const NIL: RBTreeNode;
|
|
18
|
+
/**
|
|
19
|
+
* 1. Each node is either red or black.
|
|
20
|
+
* 2. The root node is always black.
|
|
21
|
+
* 3. Leaf nodes are typically NIL nodes and are considered black.
|
|
22
|
+
* 4. Red nodes must have black children.
|
|
23
|
+
* 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
|
|
24
|
+
*/
|
|
9
25
|
export declare class RedBlackTree {
|
|
10
26
|
constructor();
|
|
11
27
|
protected _root: RBTreeNode;
|
|
12
28
|
get root(): RBTreeNode;
|
|
13
|
-
protected _NIL: RBTreeNode;
|
|
14
|
-
get NIL(): RBTreeNode;
|
|
15
29
|
/**
|
|
16
30
|
* The `insert` function inserts a new node with a given key into a red-black tree and fixes any
|
|
17
31
|
* violations of the red-black tree properties.
|
|
@@ -28,6 +42,7 @@ export declare class RedBlackTree {
|
|
|
28
42
|
* @returns The `delete` function does not return anything. It has a return type of `void`.
|
|
29
43
|
*/
|
|
30
44
|
delete(key: number): void;
|
|
45
|
+
isRealNode(node: RBTreeNode | null | undefined): node is RBTreeNode;
|
|
31
46
|
/**
|
|
32
47
|
* The function `getNode` is a recursive depth-first search algorithm that searches for a node with a
|
|
33
48
|
* given key in a red-black tree.
|
|
@@ -45,7 +60,7 @@ export declare class RedBlackTree {
|
|
|
45
60
|
* a Red-Black Tree.
|
|
46
61
|
* @returns The leftmost node in the given RBTreeNode.
|
|
47
62
|
*/
|
|
48
|
-
getLeftMost(node
|
|
63
|
+
getLeftMost(node?: RBTreeNode): RBTreeNode;
|
|
49
64
|
/**
|
|
50
65
|
* The function returns the rightmost node in a red-black tree.
|
|
51
66
|
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
|
|
@@ -65,6 +80,7 @@ export declare class RedBlackTree {
|
|
|
65
80
|
* @returns the predecessor of the given RBTreeNode 'x'.
|
|
66
81
|
*/
|
|
67
82
|
getPredecessor(x: RBTreeNode): RBTreeNode;
|
|
83
|
+
print(beginRoot?: RBTreeNode): void;
|
|
68
84
|
/**
|
|
69
85
|
* The function performs a left rotation on a red-black tree node.
|
|
70
86
|
* @param {RBTreeNode} x - The parameter `x` is a RBTreeNode object.
|
|
@@ -1,31 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Tyler Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
8
|
+
*/
|
|
2
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RedBlackTree = exports.RBTreeNode = void 0;
|
|
10
|
+
exports.RedBlackTree = exports.NIL = exports.RBTreeNode = void 0;
|
|
4
11
|
const types_1 = require("../../types");
|
|
5
12
|
class RBTreeNode {
|
|
6
|
-
constructor() {
|
|
7
|
-
this.key = 0;
|
|
13
|
+
constructor(key, color = types_1.RBTNColor.BLACK) {
|
|
8
14
|
this.color = types_1.RBTNColor.BLACK;
|
|
15
|
+
this.key = key;
|
|
16
|
+
this.color = color;
|
|
9
17
|
this.parent = null;
|
|
10
18
|
this.left = null;
|
|
11
19
|
this.right = null;
|
|
12
20
|
}
|
|
13
21
|
}
|
|
14
22
|
exports.RBTreeNode = RBTreeNode;
|
|
23
|
+
exports.NIL = new RBTreeNode(0);
|
|
24
|
+
/**
|
|
25
|
+
* 1. Each node is either red or black.
|
|
26
|
+
* 2. The root node is always black.
|
|
27
|
+
* 3. Leaf nodes are typically NIL nodes and are considered black.
|
|
28
|
+
* 4. Red nodes must have black children.
|
|
29
|
+
* 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
|
|
30
|
+
*/
|
|
15
31
|
class RedBlackTree {
|
|
16
32
|
constructor() {
|
|
17
|
-
this.
|
|
18
|
-
this.NIL.color = types_1.RBTNColor.BLACK;
|
|
19
|
-
this.NIL.left = null;
|
|
20
|
-
this.NIL.right = null;
|
|
21
|
-
this._root = this.NIL;
|
|
33
|
+
this._root = exports.NIL;
|
|
22
34
|
}
|
|
23
35
|
get root() {
|
|
24
36
|
return this._root;
|
|
25
37
|
}
|
|
26
|
-
get NIL() {
|
|
27
|
-
return this._NIL;
|
|
28
|
-
}
|
|
29
38
|
/**
|
|
30
39
|
* The `insert` function inserts a new node with a given key into a red-black tree and fixes any
|
|
31
40
|
* violations of the red-black tree properties.
|
|
@@ -34,15 +43,12 @@ class RedBlackTree {
|
|
|
34
43
|
* @returns The function does not explicitly return anything.
|
|
35
44
|
*/
|
|
36
45
|
insert(key) {
|
|
37
|
-
const node = new RBTreeNode();
|
|
38
|
-
node.
|
|
39
|
-
node.
|
|
40
|
-
node.left = this.NIL;
|
|
41
|
-
node.right = this.NIL;
|
|
42
|
-
node.color = types_1.RBTNColor.RED;
|
|
46
|
+
const node = new RBTreeNode(key, types_1.RBTNColor.RED);
|
|
47
|
+
node.left = exports.NIL;
|
|
48
|
+
node.right = exports.NIL;
|
|
43
49
|
let y = null;
|
|
44
50
|
let x = this.root;
|
|
45
|
-
while (x !==
|
|
51
|
+
while (x !== exports.NIL) {
|
|
46
52
|
y = x;
|
|
47
53
|
if (node.key < x.key) {
|
|
48
54
|
x = x.left;
|
|
@@ -79,9 +85,9 @@ class RedBlackTree {
|
|
|
79
85
|
*/
|
|
80
86
|
delete(key) {
|
|
81
87
|
const helper = (node) => {
|
|
82
|
-
let z =
|
|
88
|
+
let z = exports.NIL;
|
|
83
89
|
let x, y;
|
|
84
|
-
while (node !==
|
|
90
|
+
while (node !== exports.NIL) {
|
|
85
91
|
if (node.key === key) {
|
|
86
92
|
z = node;
|
|
87
93
|
}
|
|
@@ -92,17 +98,16 @@ class RedBlackTree {
|
|
|
92
98
|
node = node.left;
|
|
93
99
|
}
|
|
94
100
|
}
|
|
95
|
-
if (z ===
|
|
96
|
-
console.log("Couldn't find key in the tree");
|
|
101
|
+
if (z === exports.NIL) {
|
|
97
102
|
return;
|
|
98
103
|
}
|
|
99
104
|
y = z;
|
|
100
105
|
let yOriginalColor = y.color;
|
|
101
|
-
if (z.left ===
|
|
106
|
+
if (z.left === exports.NIL) {
|
|
102
107
|
x = z.right;
|
|
103
108
|
this._rbTransplant(z, z.right);
|
|
104
109
|
}
|
|
105
|
-
else if (z.right ===
|
|
110
|
+
else if (z.right === exports.NIL) {
|
|
106
111
|
x = z.left;
|
|
107
112
|
this._rbTransplant(z, z.left);
|
|
108
113
|
}
|
|
@@ -123,12 +128,15 @@ class RedBlackTree {
|
|
|
123
128
|
y.left.parent = y;
|
|
124
129
|
y.color = z.color;
|
|
125
130
|
}
|
|
126
|
-
if (yOriginalColor ===
|
|
131
|
+
if (yOriginalColor === types_1.RBTNColor.BLACK) {
|
|
127
132
|
this._fixDelete(x);
|
|
128
133
|
}
|
|
129
134
|
};
|
|
130
135
|
helper(this.root);
|
|
131
136
|
}
|
|
137
|
+
isRealNode(node) {
|
|
138
|
+
return node !== exports.NIL && node !== null;
|
|
139
|
+
}
|
|
132
140
|
/**
|
|
133
141
|
* The function `getNode` is a recursive depth-first search algorithm that searches for a node with a
|
|
134
142
|
* given key in a red-black tree.
|
|
@@ -141,13 +149,17 @@ class RedBlackTree {
|
|
|
141
149
|
*/
|
|
142
150
|
getNode(key, beginRoot = this.root) {
|
|
143
151
|
const dfs = (node) => {
|
|
144
|
-
if (
|
|
145
|
-
|
|
152
|
+
if (this.isRealNode(node)) {
|
|
153
|
+
if (key === node.key) {
|
|
154
|
+
return node;
|
|
155
|
+
}
|
|
156
|
+
if (key < node.key)
|
|
157
|
+
return dfs(node.left);
|
|
158
|
+
return dfs(node.right);
|
|
146
159
|
}
|
|
147
|
-
|
|
148
|
-
return
|
|
160
|
+
else {
|
|
161
|
+
return null;
|
|
149
162
|
}
|
|
150
|
-
return dfs(node.right);
|
|
151
163
|
};
|
|
152
164
|
return dfs(beginRoot);
|
|
153
165
|
}
|
|
@@ -157,8 +169,8 @@ class RedBlackTree {
|
|
|
157
169
|
* a Red-Black Tree.
|
|
158
170
|
* @returns The leftmost node in the given RBTreeNode.
|
|
159
171
|
*/
|
|
160
|
-
getLeftMost(node) {
|
|
161
|
-
while (node.left !== null && node.left !==
|
|
172
|
+
getLeftMost(node = this.root) {
|
|
173
|
+
while (node.left !== null && node.left !== exports.NIL) {
|
|
162
174
|
node = node.left;
|
|
163
175
|
}
|
|
164
176
|
return node;
|
|
@@ -169,7 +181,7 @@ class RedBlackTree {
|
|
|
169
181
|
* @returns the rightmost node in a red-black tree.
|
|
170
182
|
*/
|
|
171
183
|
getRightMost(node) {
|
|
172
|
-
while (node.right !== null && node.right !==
|
|
184
|
+
while (node.right !== null && node.right !== exports.NIL) {
|
|
173
185
|
node = node.right;
|
|
174
186
|
}
|
|
175
187
|
return node;
|
|
@@ -180,11 +192,11 @@ class RedBlackTree {
|
|
|
180
192
|
* @returns the successor of the given RBTreeNode.
|
|
181
193
|
*/
|
|
182
194
|
getSuccessor(x) {
|
|
183
|
-
if (x.right !==
|
|
195
|
+
if (x.right !== exports.NIL) {
|
|
184
196
|
return this.getLeftMost(x.right);
|
|
185
197
|
}
|
|
186
198
|
let y = x.parent;
|
|
187
|
-
while (y !==
|
|
199
|
+
while (y !== exports.NIL && y !== null && x === y.right) {
|
|
188
200
|
x = y;
|
|
189
201
|
y = y.parent;
|
|
190
202
|
}
|
|
@@ -197,16 +209,69 @@ class RedBlackTree {
|
|
|
197
209
|
* @returns the predecessor of the given RBTreeNode 'x'.
|
|
198
210
|
*/
|
|
199
211
|
getPredecessor(x) {
|
|
200
|
-
if (x.left !==
|
|
212
|
+
if (x.left !== exports.NIL) {
|
|
201
213
|
return this.getRightMost(x.left);
|
|
202
214
|
}
|
|
203
215
|
let y = x.parent;
|
|
204
|
-
while (y !==
|
|
216
|
+
while (y !== exports.NIL && x === y.left) {
|
|
205
217
|
x = y;
|
|
206
218
|
y = y.parent;
|
|
207
219
|
}
|
|
208
220
|
return y;
|
|
209
221
|
}
|
|
222
|
+
print(beginRoot = this.root) {
|
|
223
|
+
const display = (root) => {
|
|
224
|
+
const [lines, , ,] = _displayAux(root);
|
|
225
|
+
for (const line of lines) {
|
|
226
|
+
console.log(line);
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
const _displayAux = (node) => {
|
|
230
|
+
if (node === null) {
|
|
231
|
+
return [[], 0, 0, 0];
|
|
232
|
+
}
|
|
233
|
+
if (node.right === null && node.left === null) {
|
|
234
|
+
const line = `${node.key}`;
|
|
235
|
+
const width = line.length;
|
|
236
|
+
const height = 1;
|
|
237
|
+
const middle = Math.floor(width / 2);
|
|
238
|
+
return [[line], width, height, middle];
|
|
239
|
+
}
|
|
240
|
+
if (node.right === null) {
|
|
241
|
+
const [lines, n, p, x] = _displayAux(node.left);
|
|
242
|
+
const s = `${node.key}`;
|
|
243
|
+
const u = s.length;
|
|
244
|
+
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s;
|
|
245
|
+
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u);
|
|
246
|
+
const shifted_lines = lines.map(line => line + ' '.repeat(u));
|
|
247
|
+
return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
|
|
248
|
+
}
|
|
249
|
+
if (node.left === null) {
|
|
250
|
+
const [lines, n, p, u] = _displayAux(node.right);
|
|
251
|
+
const s = `${node.key}`;
|
|
252
|
+
const x = s.length;
|
|
253
|
+
const first_line = s + '_'.repeat(x) + ' '.repeat(n - x);
|
|
254
|
+
const second_line = ' '.repeat(u + x) + '\\' + ' '.repeat(n - x - 1);
|
|
255
|
+
const shifted_lines = lines.map(line => ' '.repeat(u) + line);
|
|
256
|
+
return [[first_line, second_line, ...shifted_lines], n + x, p + 2, Math.floor(u / 2)];
|
|
257
|
+
}
|
|
258
|
+
const [left, n, p, x] = _displayAux(node.left);
|
|
259
|
+
const [right, m, q, y] = _displayAux(node.right);
|
|
260
|
+
const s = `${node.key}`;
|
|
261
|
+
const u = s.length;
|
|
262
|
+
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s + '_'.repeat(y) + ' '.repeat(m - y);
|
|
263
|
+
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u + y) + '\\' + ' '.repeat(m - y - 1);
|
|
264
|
+
if (p < q) {
|
|
265
|
+
left.push(...new Array(q - p).fill(' '.repeat(n)));
|
|
266
|
+
}
|
|
267
|
+
else if (q < p) {
|
|
268
|
+
right.push(...new Array(p - q).fill(' '.repeat(m)));
|
|
269
|
+
}
|
|
270
|
+
const zipped_lines = left.map((a, i) => a + ' '.repeat(u) + right[i]);
|
|
271
|
+
return [[first_line, second_line, ...zipped_lines], n + m + u, Math.max(p, q) + 2, n + Math.floor(u / 2)];
|
|
272
|
+
};
|
|
273
|
+
display(beginRoot);
|
|
274
|
+
}
|
|
210
275
|
/**
|
|
211
276
|
* The function performs a left rotation on a red-black tree node.
|
|
212
277
|
* @param {RBTreeNode} x - The parameter `x` is a RBTreeNode object.
|
|
@@ -214,7 +279,7 @@ class RedBlackTree {
|
|
|
214
279
|
_leftRotate(x) {
|
|
215
280
|
const y = x.right;
|
|
216
281
|
x.right = y.left;
|
|
217
|
-
if (y.left !==
|
|
282
|
+
if (y.left !== exports.NIL) {
|
|
218
283
|
y.left.parent = x;
|
|
219
284
|
}
|
|
220
285
|
y.parent = x.parent;
|
|
@@ -238,7 +303,7 @@ class RedBlackTree {
|
|
|
238
303
|
_rightRotate(x) {
|
|
239
304
|
const y = x.left;
|
|
240
305
|
x.left = y.right;
|
|
241
|
-
if (y.right !==
|
|
306
|
+
if (y.right !== exports.NIL) {
|
|
242
307
|
y.right.parent = x;
|
|
243
308
|
}
|
|
244
309
|
y.parent = x.parent;
|
|
@@ -261,7 +326,7 @@ class RedBlackTree {
|
|
|
261
326
|
*/
|
|
262
327
|
_fixDelete(x) {
|
|
263
328
|
let s;
|
|
264
|
-
while (x !== this.root && x.color ===
|
|
329
|
+
while (x !== this.root && x.color === types_1.RBTNColor.BLACK) {
|
|
265
330
|
if (x === x.parent.left) {
|
|
266
331
|
s = x.parent.right;
|
|
267
332
|
if (s.color === 1) {
|
|
@@ -270,12 +335,12 @@ class RedBlackTree {
|
|
|
270
335
|
this._leftRotate(x.parent);
|
|
271
336
|
s = x.parent.right;
|
|
272
337
|
}
|
|
273
|
-
if (s.left.color ===
|
|
338
|
+
if (s.left !== null && s.left.color === types_1.RBTNColor.BLACK && s.right.color === types_1.RBTNColor.BLACK) {
|
|
274
339
|
s.color = types_1.RBTNColor.RED;
|
|
275
340
|
x = x.parent;
|
|
276
341
|
}
|
|
277
342
|
else {
|
|
278
|
-
if (s.right.color ===
|
|
343
|
+
if (s.right.color === types_1.RBTNColor.BLACK) {
|
|
279
344
|
s.left.color = types_1.RBTNColor.BLACK;
|
|
280
345
|
s.color = types_1.RBTNColor.RED;
|
|
281
346
|
this._rightRotate(s);
|
|
@@ -296,12 +361,12 @@ class RedBlackTree {
|
|
|
296
361
|
this._rightRotate(x.parent);
|
|
297
362
|
s = x.parent.left;
|
|
298
363
|
}
|
|
299
|
-
if (s.right.color ===
|
|
364
|
+
if (s.right.color === types_1.RBTNColor.BLACK && s.right.color === types_1.RBTNColor.BLACK) {
|
|
300
365
|
s.color = types_1.RBTNColor.RED;
|
|
301
366
|
x = x.parent;
|
|
302
367
|
}
|
|
303
368
|
else {
|
|
304
|
-
if (s.left.color ===
|
|
369
|
+
if (s.left.color === types_1.RBTNColor.BLACK) {
|
|
305
370
|
s.right.color = types_1.RBTNColor.BLACK;
|
|
306
371
|
s.color = types_1.RBTNColor.RED;
|
|
307
372
|
this._leftRotate(s);
|
|
@@ -159,7 +159,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
159
159
|
else if (parent.right === undefined) {
|
|
160
160
|
parent.right = newNode;
|
|
161
161
|
if (newNode !== null) {
|
|
162
|
-
this._size =
|
|
162
|
+
this._size = this.size + 1;
|
|
163
163
|
this._setCount(this.count + newNode.count);
|
|
164
164
|
}
|
|
165
165
|
return parent.right;
|
|
@@ -262,7 +262,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
262
262
|
const bstDeletedResult = [];
|
|
263
263
|
if (!this.root)
|
|
264
264
|
return bstDeletedResult;
|
|
265
|
-
const curr = this.
|
|
265
|
+
const curr = this.getNode(identifier, callback);
|
|
266
266
|
if (!curr)
|
|
267
267
|
return bstDeletedResult;
|
|
268
268
|
const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "1.41.
|
|
3
|
+
"version": "1.41.2",
|
|
4
4
|
"description": "Min Heap. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -131,6 +131,6 @@
|
|
|
131
131
|
"typescript": "^4.9.5"
|
|
132
132
|
},
|
|
133
133
|
"dependencies": {
|
|
134
|
-
"data-structure-typed": "^1.41.
|
|
134
|
+
"data-structure-typed": "^1.41.2"
|
|
135
135
|
}
|
|
136
136
|
}
|
|
@@ -21,7 +21,8 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
|
|
|
21
21
|
|
|
22
22
|
export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>>
|
|
23
23
|
extends BST<V, N>
|
|
24
|
-
implements IBinaryTree<V, N>
|
|
24
|
+
implements IBinaryTree<V, N>
|
|
25
|
+
{
|
|
25
26
|
/**
|
|
26
27
|
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
27
28
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -160,7 +161,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
160
161
|
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
161
162
|
switch (
|
|
162
163
|
this._balanceFactor(A) // second O(1)
|
|
163
|
-
|
|
164
|
+
) {
|
|
164
165
|
case -2:
|
|
165
166
|
if (A && A.left) {
|
|
166
167
|
if (this._balanceFactor(A.left) <= 0) {
|
|
@@ -17,7 +17,7 @@ export class BinaryIndexedTree {
|
|
|
17
17
|
* @param - - `frequency`: The default frequency value. It is optional and has a default
|
|
18
18
|
* value of 0.
|
|
19
19
|
*/
|
|
20
|
-
constructor({frequency = 0, max}: {
|
|
20
|
+
constructor({frequency = 0, max}: {frequency?: number; max: number}) {
|
|
21
21
|
this._freq = frequency;
|
|
22
22
|
this._max = max;
|
|
23
23
|
this._freqMap = {0: 0};
|