min-heap-typed 1.41.0 → 1.41.1
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 +6 -3
- package/dist/data-structures/binary-tree/binary-tree.js +30 -8
- 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 +5 -4
- package/dist/data-structures/binary-tree/rb-tree.js +42 -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 +65 -17
- package/src/data-structures/binary-tree/bst.ts +7 -4
- package/src/data-structures/binary-tree/rb-tree.ts +47 -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
|
@@ -177,9 +177,12 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
177
177
|
has<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N, iterationType?: IterationType): boolean;
|
|
178
178
|
has<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N, iterationType?: IterationType): boolean;
|
|
179
179
|
has<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C, beginRoot?: N, iterationType?: IterationType): boolean;
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
180
|
+
getNode<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N, iterationType?: IterationType): N | null;
|
|
181
|
+
getNode<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N, iterationType?: IterationType): N | null;
|
|
182
|
+
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N, iterationType?: IterationType): N | null;
|
|
183
|
+
get<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N, iterationType?: IterationType): V | undefined;
|
|
184
|
+
get<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N, iterationType?: IterationType): V | undefined;
|
|
185
|
+
get<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N, 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) {
|
|
@@ -521,13 +521,35 @@ class BinaryTree {
|
|
|
521
521
|
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
522
522
|
* @returns either the found node (of type N) or null if no node is found.
|
|
523
523
|
*/
|
|
524
|
-
|
|
524
|
+
getNode(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
525
525
|
var _a;
|
|
526
526
|
if (identifier instanceof BinaryTreeNode)
|
|
527
527
|
callback = (node => node);
|
|
528
528
|
// TODO may support finding node by value equal
|
|
529
529
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
530
530
|
}
|
|
531
|
+
/**
|
|
532
|
+
* The function `get` returns the first node value in a binary tree that matches the given property or key.
|
|
533
|
+
* @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
|
|
534
|
+
* the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
|
|
535
|
+
* type.
|
|
536
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
537
|
+
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
538
|
+
* whether the node matches the criteria or not. The default callback function
|
|
539
|
+
* (`((node: N) => node.key)`) is used if no callback function is
|
|
540
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
541
|
+
* the root node from which the search should begin.
|
|
542
|
+
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
543
|
+
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
544
|
+
* @returns either the found value (of type V) or undefined if no node value is found.
|
|
545
|
+
*/
|
|
546
|
+
get(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
547
|
+
var _a;
|
|
548
|
+
if (identifier instanceof BinaryTreeNode)
|
|
549
|
+
callback = (node => node);
|
|
550
|
+
// TODO may support finding node by value equal
|
|
551
|
+
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0].value) !== null && _a !== void 0 ? _a : undefined;
|
|
552
|
+
}
|
|
531
553
|
/**
|
|
532
554
|
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
533
555
|
* up to the root node, with the option to reverse the order of the nodes.
|
|
@@ -563,7 +585,7 @@ class BinaryTree {
|
|
|
563
585
|
*/
|
|
564
586
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
565
587
|
if (typeof beginRoot === 'number')
|
|
566
|
-
beginRoot = this.
|
|
588
|
+
beginRoot = this.getNode(beginRoot);
|
|
567
589
|
if (!beginRoot)
|
|
568
590
|
return beginRoot;
|
|
569
591
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
@@ -686,7 +708,7 @@ class BinaryTree {
|
|
|
686
708
|
*/
|
|
687
709
|
subTreeTraverse(callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
688
710
|
if (typeof beginRoot === 'number')
|
|
689
|
-
beginRoot = this.
|
|
711
|
+
beginRoot = this.getNode(beginRoot);
|
|
690
712
|
const ans = [];
|
|
691
713
|
if (!beginRoot)
|
|
692
714
|
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,17 @@
|
|
|
1
|
+
import { RBTNColor } from '../../types';
|
|
1
2
|
export declare class RBTreeNode {
|
|
2
3
|
key: number;
|
|
3
4
|
parent: RBTreeNode;
|
|
4
5
|
left: RBTreeNode;
|
|
5
6
|
right: RBTreeNode;
|
|
6
7
|
color: number;
|
|
7
|
-
constructor();
|
|
8
|
+
constructor(key: number, color?: RBTNColor);
|
|
8
9
|
}
|
|
10
|
+
export declare const SN: RBTreeNode;
|
|
9
11
|
export declare class RedBlackTree {
|
|
10
12
|
constructor();
|
|
11
13
|
protected _root: RBTreeNode;
|
|
12
14
|
get root(): RBTreeNode;
|
|
13
|
-
protected _NIL: RBTreeNode;
|
|
14
|
-
get NIL(): RBTreeNode;
|
|
15
15
|
/**
|
|
16
16
|
* The `insert` function inserts a new node with a given key into a red-black tree and fixes any
|
|
17
17
|
* violations of the red-black tree properties.
|
|
@@ -28,6 +28,7 @@ export declare class RedBlackTree {
|
|
|
28
28
|
* @returns The `delete` function does not return anything. It has a return type of `void`.
|
|
29
29
|
*/
|
|
30
30
|
delete(key: number): void;
|
|
31
|
+
isRealNode(node: RBTreeNode): node is RBTreeNode;
|
|
31
32
|
/**
|
|
32
33
|
* The function `getNode` is a recursive depth-first search algorithm that searches for a node with a
|
|
33
34
|
* given key in a red-black tree.
|
|
@@ -45,7 +46,7 @@ export declare class RedBlackTree {
|
|
|
45
46
|
* a Red-Black Tree.
|
|
46
47
|
* @returns The leftmost node in the given RBTreeNode.
|
|
47
48
|
*/
|
|
48
|
-
getLeftMost(node
|
|
49
|
+
getLeftMost(node?: RBTreeNode): RBTreeNode;
|
|
49
50
|
/**
|
|
50
51
|
* The function returns the rightmost node in a red-black tree.
|
|
51
52
|
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
|
|
@@ -1,31 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RedBlackTree = exports.RBTreeNode = void 0;
|
|
3
|
+
exports.RedBlackTree = exports.SN = exports.RBTreeNode = void 0;
|
|
4
4
|
const types_1 = require("../../types");
|
|
5
5
|
class RBTreeNode {
|
|
6
|
-
constructor() {
|
|
7
|
-
this.key = 0;
|
|
6
|
+
constructor(key, color = types_1.RBTNColor.BLACK) {
|
|
8
7
|
this.color = types_1.RBTNColor.BLACK;
|
|
8
|
+
this.key = key;
|
|
9
|
+
this.color = color;
|
|
9
10
|
this.parent = null;
|
|
10
11
|
this.left = null;
|
|
11
12
|
this.right = null;
|
|
12
13
|
}
|
|
13
14
|
}
|
|
14
15
|
exports.RBTreeNode = RBTreeNode;
|
|
16
|
+
exports.SN = new RBTreeNode(0);
|
|
15
17
|
class RedBlackTree {
|
|
16
18
|
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;
|
|
19
|
+
this._root = exports.SN;
|
|
22
20
|
}
|
|
23
21
|
get root() {
|
|
24
22
|
return this._root;
|
|
25
23
|
}
|
|
26
|
-
get NIL() {
|
|
27
|
-
return this._NIL;
|
|
28
|
-
}
|
|
29
24
|
/**
|
|
30
25
|
* The `insert` function inserts a new node with a given key into a red-black tree and fixes any
|
|
31
26
|
* violations of the red-black tree properties.
|
|
@@ -34,15 +29,12 @@ class RedBlackTree {
|
|
|
34
29
|
* @returns The function does not explicitly return anything.
|
|
35
30
|
*/
|
|
36
31
|
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;
|
|
32
|
+
const node = new RBTreeNode(key, types_1.RBTNColor.RED);
|
|
33
|
+
node.left = exports.SN;
|
|
34
|
+
node.right = exports.SN;
|
|
43
35
|
let y = null;
|
|
44
36
|
let x = this.root;
|
|
45
|
-
while (x !==
|
|
37
|
+
while (x !== exports.SN) {
|
|
46
38
|
y = x;
|
|
47
39
|
if (node.key < x.key) {
|
|
48
40
|
x = x.left;
|
|
@@ -79,9 +71,9 @@ class RedBlackTree {
|
|
|
79
71
|
*/
|
|
80
72
|
delete(key) {
|
|
81
73
|
const helper = (node) => {
|
|
82
|
-
let z =
|
|
74
|
+
let z = exports.SN;
|
|
83
75
|
let x, y;
|
|
84
|
-
while (node !==
|
|
76
|
+
while (node !== exports.SN) {
|
|
85
77
|
if (node.key === key) {
|
|
86
78
|
z = node;
|
|
87
79
|
}
|
|
@@ -92,17 +84,16 @@ class RedBlackTree {
|
|
|
92
84
|
node = node.left;
|
|
93
85
|
}
|
|
94
86
|
}
|
|
95
|
-
if (z ===
|
|
96
|
-
console.log("Couldn't find key in the tree");
|
|
87
|
+
if (z === exports.SN) {
|
|
97
88
|
return;
|
|
98
89
|
}
|
|
99
90
|
y = z;
|
|
100
91
|
let yOriginalColor = y.color;
|
|
101
|
-
if (z.left ===
|
|
92
|
+
if (z.left === exports.SN) {
|
|
102
93
|
x = z.right;
|
|
103
94
|
this._rbTransplant(z, z.right);
|
|
104
95
|
}
|
|
105
|
-
else if (z.right ===
|
|
96
|
+
else if (z.right === exports.SN) {
|
|
106
97
|
x = z.left;
|
|
107
98
|
this._rbTransplant(z, z.left);
|
|
108
99
|
}
|
|
@@ -123,12 +114,15 @@ class RedBlackTree {
|
|
|
123
114
|
y.left.parent = y;
|
|
124
115
|
y.color = z.color;
|
|
125
116
|
}
|
|
126
|
-
if (yOriginalColor ===
|
|
117
|
+
if (yOriginalColor === types_1.RBTNColor.BLACK) {
|
|
127
118
|
this._fixDelete(x);
|
|
128
119
|
}
|
|
129
120
|
};
|
|
130
121
|
helper(this.root);
|
|
131
122
|
}
|
|
123
|
+
isRealNode(node) {
|
|
124
|
+
return node !== exports.SN && node !== null;
|
|
125
|
+
}
|
|
132
126
|
/**
|
|
133
127
|
* The function `getNode` is a recursive depth-first search algorithm that searches for a node with a
|
|
134
128
|
* given key in a red-black tree.
|
|
@@ -141,13 +135,17 @@ class RedBlackTree {
|
|
|
141
135
|
*/
|
|
142
136
|
getNode(key, beginRoot = this.root) {
|
|
143
137
|
const dfs = (node) => {
|
|
144
|
-
if (
|
|
145
|
-
|
|
138
|
+
if (this.isRealNode(node)) {
|
|
139
|
+
if (key === node.key) {
|
|
140
|
+
return node;
|
|
141
|
+
}
|
|
142
|
+
if (key < node.key)
|
|
143
|
+
return dfs(node.left);
|
|
144
|
+
return dfs(node.right);
|
|
146
145
|
}
|
|
147
|
-
|
|
148
|
-
return
|
|
146
|
+
else {
|
|
147
|
+
return null;
|
|
149
148
|
}
|
|
150
|
-
return dfs(node.right);
|
|
151
149
|
};
|
|
152
150
|
return dfs(beginRoot);
|
|
153
151
|
}
|
|
@@ -157,8 +155,8 @@ class RedBlackTree {
|
|
|
157
155
|
* a Red-Black Tree.
|
|
158
156
|
* @returns The leftmost node in the given RBTreeNode.
|
|
159
157
|
*/
|
|
160
|
-
getLeftMost(node) {
|
|
161
|
-
while (node.left !== null && node.left !==
|
|
158
|
+
getLeftMost(node = this.root) {
|
|
159
|
+
while (node.left !== null && node.left !== exports.SN) {
|
|
162
160
|
node = node.left;
|
|
163
161
|
}
|
|
164
162
|
return node;
|
|
@@ -169,7 +167,7 @@ class RedBlackTree {
|
|
|
169
167
|
* @returns the rightmost node in a red-black tree.
|
|
170
168
|
*/
|
|
171
169
|
getRightMost(node) {
|
|
172
|
-
while (node.right !== null && node.right !==
|
|
170
|
+
while (node.right !== null && node.right !== exports.SN) {
|
|
173
171
|
node = node.right;
|
|
174
172
|
}
|
|
175
173
|
return node;
|
|
@@ -180,11 +178,11 @@ class RedBlackTree {
|
|
|
180
178
|
* @returns the successor of the given RBTreeNode.
|
|
181
179
|
*/
|
|
182
180
|
getSuccessor(x) {
|
|
183
|
-
if (x.right !==
|
|
181
|
+
if (x.right !== exports.SN) {
|
|
184
182
|
return this.getLeftMost(x.right);
|
|
185
183
|
}
|
|
186
184
|
let y = x.parent;
|
|
187
|
-
while (y !==
|
|
185
|
+
while (y !== exports.SN && y !== null && x === y.right) {
|
|
188
186
|
x = y;
|
|
189
187
|
y = y.parent;
|
|
190
188
|
}
|
|
@@ -197,11 +195,11 @@ class RedBlackTree {
|
|
|
197
195
|
* @returns the predecessor of the given RBTreeNode 'x'.
|
|
198
196
|
*/
|
|
199
197
|
getPredecessor(x) {
|
|
200
|
-
if (x.left !==
|
|
198
|
+
if (x.left !== exports.SN) {
|
|
201
199
|
return this.getRightMost(x.left);
|
|
202
200
|
}
|
|
203
201
|
let y = x.parent;
|
|
204
|
-
while (y !==
|
|
202
|
+
while (y !== exports.SN && x === y.left) {
|
|
205
203
|
x = y;
|
|
206
204
|
y = y.parent;
|
|
207
205
|
}
|
|
@@ -214,7 +212,7 @@ class RedBlackTree {
|
|
|
214
212
|
_leftRotate(x) {
|
|
215
213
|
const y = x.right;
|
|
216
214
|
x.right = y.left;
|
|
217
|
-
if (y.left !==
|
|
215
|
+
if (y.left !== exports.SN) {
|
|
218
216
|
y.left.parent = x;
|
|
219
217
|
}
|
|
220
218
|
y.parent = x.parent;
|
|
@@ -238,7 +236,7 @@ class RedBlackTree {
|
|
|
238
236
|
_rightRotate(x) {
|
|
239
237
|
const y = x.left;
|
|
240
238
|
x.left = y.right;
|
|
241
|
-
if (y.right !==
|
|
239
|
+
if (y.right !== exports.SN) {
|
|
242
240
|
y.right.parent = x;
|
|
243
241
|
}
|
|
244
242
|
y.parent = x.parent;
|
|
@@ -261,7 +259,7 @@ class RedBlackTree {
|
|
|
261
259
|
*/
|
|
262
260
|
_fixDelete(x) {
|
|
263
261
|
let s;
|
|
264
|
-
while (x !== this.root && x.color ===
|
|
262
|
+
while (x !== this.root && x.color === types_1.RBTNColor.BLACK) {
|
|
265
263
|
if (x === x.parent.left) {
|
|
266
264
|
s = x.parent.right;
|
|
267
265
|
if (s.color === 1) {
|
|
@@ -270,12 +268,12 @@ class RedBlackTree {
|
|
|
270
268
|
this._leftRotate(x.parent);
|
|
271
269
|
s = x.parent.right;
|
|
272
270
|
}
|
|
273
|
-
if (s.left.color ===
|
|
271
|
+
if (s.left !== null && s.left.color === types_1.RBTNColor.BLACK && s.right.color === types_1.RBTNColor.BLACK) {
|
|
274
272
|
s.color = types_1.RBTNColor.RED;
|
|
275
273
|
x = x.parent;
|
|
276
274
|
}
|
|
277
275
|
else {
|
|
278
|
-
if (s.right.color ===
|
|
276
|
+
if (s.right.color === types_1.RBTNColor.BLACK) {
|
|
279
277
|
s.left.color = types_1.RBTNColor.BLACK;
|
|
280
278
|
s.color = types_1.RBTNColor.RED;
|
|
281
279
|
this._rightRotate(s);
|
|
@@ -296,12 +294,12 @@ class RedBlackTree {
|
|
|
296
294
|
this._rightRotate(x.parent);
|
|
297
295
|
s = x.parent.left;
|
|
298
296
|
}
|
|
299
|
-
if (s.right.color ===
|
|
297
|
+
if (s.right.color === types_1.RBTNColor.BLACK && s.right.color === types_1.RBTNColor.BLACK) {
|
|
300
298
|
s.color = types_1.RBTNColor.RED;
|
|
301
299
|
x = x.parent;
|
|
302
300
|
}
|
|
303
301
|
else {
|
|
304
|
-
if (s.left.color ===
|
|
302
|
+
if (s.left.color === types_1.RBTNColor.BLACK) {
|
|
305
303
|
s.right.color = types_1.RBTNColor.BLACK;
|
|
306
304
|
s.color = types_1.RBTNColor.RED;
|
|
307
305
|
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.1",
|
|
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.1"
|
|
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};
|
|
@@ -108,7 +108,8 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
108
108
|
* @template N - The type of the binary tree's nodes.
|
|
109
109
|
*/
|
|
110
110
|
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
111
|
-
implements IBinaryTree<V, N>
|
|
111
|
+
implements IBinaryTree<V, N>
|
|
112
|
+
{
|
|
112
113
|
iterationType: IterationType = IterationType.ITERATIVE;
|
|
113
114
|
|
|
114
115
|
/**
|
|
@@ -201,7 +202,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
201
202
|
}
|
|
202
203
|
|
|
203
204
|
const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined;
|
|
204
|
-
const existNode = key !== undefined ? this.
|
|
205
|
+
const existNode = key !== undefined ? this.getNode(key, (node: N) => node.key) : undefined;
|
|
205
206
|
|
|
206
207
|
if (this.root) {
|
|
207
208
|
if (existNode) {
|
|
@@ -290,7 +291,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
290
291
|
if (!this.root) return bstDeletedResult;
|
|
291
292
|
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
292
293
|
|
|
293
|
-
const curr = this.
|
|
294
|
+
const curr = this.getNode(identifier, callback);
|
|
294
295
|
if (!curr) return bstDeletedResult;
|
|
295
296
|
|
|
296
297
|
const parent: N | null = curr?.parent ? curr.parent : null;
|
|
@@ -342,8 +343,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
342
343
|
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
343
344
|
*/
|
|
344
345
|
getDepth(distNode: BTNKey | N | null, beginRoot: BTNKey | N | null = this.root): number {
|
|
345
|
-
if (typeof distNode === 'number') distNode = this.
|
|
346
|
-
if (typeof beginRoot === 'number') beginRoot = this.
|
|
346
|
+
if (typeof distNode === 'number') distNode = this.getNode(distNode);
|
|
347
|
+
if (typeof beginRoot === 'number') beginRoot = this.getNode(beginRoot);
|
|
347
348
|
let depth = 0;
|
|
348
349
|
while (distNode?.parent) {
|
|
349
350
|
if (distNode === beginRoot) {
|
|
@@ -368,7 +369,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
368
369
|
* @returns the height of the binary tree.
|
|
369
370
|
*/
|
|
370
371
|
getHeight(beginRoot: BTNKey | N | null = this.root, iterationType = this.iterationType): number {
|
|
371
|
-
if (typeof beginRoot === 'number') beginRoot = this.
|
|
372
|
+
if (typeof beginRoot === 'number') beginRoot = this.getNode(beginRoot);
|
|
372
373
|
if (!beginRoot) return -1;
|
|
373
374
|
|
|
374
375
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -385,7 +386,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
385
386
|
return -1;
|
|
386
387
|
}
|
|
387
388
|
|
|
388
|
-
const stack: {
|
|
389
|
+
const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
|
|
389
390
|
let maxHeight = 0;
|
|
390
391
|
|
|
391
392
|
while (stack.length > 0) {
|
|
@@ -604,21 +605,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
604
605
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
605
606
|
}
|
|
606
607
|
|
|
607
|
-
|
|
608
|
+
getNode<C extends BTNCallback<N, BTNKey>>(
|
|
608
609
|
identifier: BTNKey,
|
|
609
610
|
callback?: C,
|
|
610
611
|
beginRoot?: N,
|
|
611
612
|
iterationType?: IterationType
|
|
612
613
|
): N | null;
|
|
613
614
|
|
|
614
|
-
|
|
615
|
+
getNode<C extends BTNCallback<N, N>>(
|
|
615
616
|
identifier: N | null,
|
|
616
617
|
callback?: C,
|
|
617
618
|
beginRoot?: N,
|
|
618
619
|
iterationType?: IterationType
|
|
619
620
|
): N | null;
|
|
620
621
|
|
|
621
|
-
|
|
622
|
+
getNode<C extends BTNCallback<N>>(
|
|
622
623
|
identifier: ReturnType<C>,
|
|
623
624
|
callback: C,
|
|
624
625
|
beginRoot?: N,
|
|
@@ -640,7 +641,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
640
641
|
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
641
642
|
* @returns either the found node (of type N) or null if no node is found.
|
|
642
643
|
*/
|
|
643
|
-
|
|
644
|
+
getNode<C extends BTNCallback<N>>(
|
|
644
645
|
identifier: ReturnType<C> | null,
|
|
645
646
|
callback: C = ((node: N) => node.key) as C,
|
|
646
647
|
beginRoot = this.root,
|
|
@@ -651,6 +652,53 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
651
652
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
652
653
|
}
|
|
653
654
|
|
|
655
|
+
get<C extends BTNCallback<N, BTNKey>>(
|
|
656
|
+
identifier: BTNKey,
|
|
657
|
+
callback?: C,
|
|
658
|
+
beginRoot?: N,
|
|
659
|
+
iterationType?: IterationType
|
|
660
|
+
): V | undefined;
|
|
661
|
+
|
|
662
|
+
get<C extends BTNCallback<N, N>>(
|
|
663
|
+
identifier: N | null,
|
|
664
|
+
callback?: C,
|
|
665
|
+
beginRoot?: N,
|
|
666
|
+
iterationType?: IterationType
|
|
667
|
+
): V | undefined;
|
|
668
|
+
|
|
669
|
+
get<C extends BTNCallback<N>>(
|
|
670
|
+
identifier: ReturnType<C>,
|
|
671
|
+
callback: C,
|
|
672
|
+
beginRoot?: N,
|
|
673
|
+
iterationType?: IterationType
|
|
674
|
+
): V | undefined;
|
|
675
|
+
|
|
676
|
+
/**
|
|
677
|
+
* The function `get` returns the first node value in a binary tree that matches the given property or key.
|
|
678
|
+
* @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
|
|
679
|
+
* the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
|
|
680
|
+
* type.
|
|
681
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
682
|
+
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
683
|
+
* whether the node matches the criteria or not. The default callback function
|
|
684
|
+
* (`((node: N) => node.key)`) is used if no callback function is
|
|
685
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
686
|
+
* the root node from which the search should begin.
|
|
687
|
+
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
688
|
+
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
689
|
+
* @returns either the found value (of type V) or undefined if no node value is found.
|
|
690
|
+
*/
|
|
691
|
+
get<C extends BTNCallback<N>>(
|
|
692
|
+
identifier: ReturnType<C> | null,
|
|
693
|
+
callback: C = ((node: N) => node.key) as C,
|
|
694
|
+
beginRoot = this.root,
|
|
695
|
+
iterationType = this.iterationType
|
|
696
|
+
): V | undefined {
|
|
697
|
+
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
698
|
+
// TODO may support finding node by value equal
|
|
699
|
+
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0].value ?? undefined;
|
|
700
|
+
}
|
|
701
|
+
|
|
654
702
|
/**
|
|
655
703
|
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
656
704
|
* up to the root node, with the option to reverse the order of the nodes.
|
|
@@ -686,7 +734,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
686
734
|
* no leftmost node, it returns `null`.
|
|
687
735
|
*/
|
|
688
736
|
getLeftMost(beginRoot: BTNKey | N | null = this.root, iterationType = this.iterationType): N | null {
|
|
689
|
-
if (typeof beginRoot === 'number') beginRoot = this.
|
|
737
|
+
if (typeof beginRoot === 'number') beginRoot = this.getNode(beginRoot);
|
|
690
738
|
|
|
691
739
|
if (!beginRoot) return beginRoot;
|
|
692
740
|
|
|
@@ -812,7 +860,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
812
860
|
beginRoot: BTNKey | N | null = this.root,
|
|
813
861
|
iterationType = this.iterationType
|
|
814
862
|
): ReturnType<C>[] {
|
|
815
|
-
if (typeof beginRoot === 'number') beginRoot = this.
|
|
863
|
+
if (typeof beginRoot === 'number') beginRoot = this.getNode(beginRoot);
|
|
816
864
|
|
|
817
865
|
const ans: ReturnType<BTNCallback<N>>[] = [];
|
|
818
866
|
if (!beginRoot) return ans;
|
|
@@ -888,7 +936,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
888
936
|
_traverse(beginRoot);
|
|
889
937
|
} else {
|
|
890
938
|
// 0: visit, 1: print
|
|
891
|
-
const stack: {
|
|
939
|
+
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
|
|
892
940
|
|
|
893
941
|
while (stack.length > 0) {
|
|
894
942
|
const cur = stack.pop();
|
|
@@ -960,7 +1008,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
960
1008
|
if (current.right) queue.push(current.right);
|
|
961
1009
|
|
|
962
1010
|
traverse(level + 1);
|
|
963
|
-
}
|
|
1011
|
+
};
|
|
964
1012
|
|
|
965
1013
|
traverse(0);
|
|
966
1014
|
} else {
|
|
@@ -1055,7 +1103,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1055
1103
|
* @returns The function `getSuccessor` returns a value of type `N` (the successor node), or `null`
|
|
1056
1104
|
* if there is no successor, or `undefined` if the input `x` is `undefined`.
|
|
1057
1105
|
*/
|
|
1058
|
-
getSuccessor(x: N): N | null | undefined{
|
|
1106
|
+
getSuccessor(x: N): N | null | undefined {
|
|
1059
1107
|
if (x.right) {
|
|
1060
1108
|
return this.getLeftMost(x.right);
|
|
1061
1109
|
}
|
|
@@ -1178,7 +1226,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1178
1226
|
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1179
1227
|
* binary tree nodes in a specific order.
|
|
1180
1228
|
*/
|
|
1181
|
-
*
|
|
1229
|
+
*[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
|
|
1182
1230
|
if (!node) {
|
|
1183
1231
|
return;
|
|
1184
1232
|
}
|