heap-typed 1.38.0 → 1.38.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/avl-tree.d.ts +9 -9
- package/dist/data-structures/binary-tree/avl-tree.js +22 -22
- package/dist/data-structures/binary-tree/binary-tree.d.ts +31 -31
- package/dist/data-structures/binary-tree/binary-tree.js +32 -32
- package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +9 -9
- package/dist/data-structures/binary-tree/tree-multiset.js +23 -23
- package/dist/data-structures/hash/hash-map.d.ts +25 -25
- package/dist/data-structures/hash/hash-map.js +59 -59
- package/dist/data-structures/hash/hash-table.d.ts +34 -34
- package/dist/data-structures/hash/hash-table.js +99 -99
- package/dist/data-structures/heap/heap.d.ts +66 -66
- package/dist/data-structures/heap/heap.js +167 -167
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +3 -3
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +17 -17
- package/dist/data-structures/linked-list/skip-linked-list.js +34 -34
- package/dist/data-structures/matrix/matrix2d.d.ts +7 -7
- package/dist/data-structures/matrix/matrix2d.js +9 -9
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +6 -6
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/package.json +1 -4
- package/src/data-structures/binary-tree/avl-tree.ts +28 -28
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +57 -57
- package/src/data-structures/binary-tree/bst.ts +4 -0
- package/src/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multiset.ts +30 -31
- package/src/data-structures/graph/abstract-graph.ts +10 -11
- package/src/data-structures/graph/directed-graph.ts +1 -2
- package/src/data-structures/graph/undirected-graph.ts +4 -5
- package/src/data-structures/hash/hash-map.ts +82 -76
- package/src/data-structures/hash/hash-table.ts +112 -109
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +182 -181
- package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/skip-linked-list.ts +45 -38
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/matrix2d.ts +10 -10
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/queue/deque.ts +5 -4
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +9 -9
- package/src/index.ts +3 -3
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/helpers.ts +5 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
package/dist/index.js
CHANGED
|
@@ -22,6 +22,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
22
22
|
* @license MIT License
|
|
23
23
|
*/
|
|
24
24
|
// export { Heap, MaxHeap, MinHeap } from 'data-structure-typed';
|
|
25
|
-
__exportStar(require("data-
|
|
26
|
-
__exportStar(require("
|
|
27
|
-
__exportStar(require("
|
|
25
|
+
__exportStar(require("./data-structures/heap"), exports);
|
|
26
|
+
__exportStar(require("./types/data-structures/heap"), exports);
|
|
27
|
+
__exportStar(require("./types/helpers"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "heap-typed",
|
|
3
|
-
"version": "1.38.
|
|
3
|
+
"version": "1.38.2",
|
|
4
4
|
"description": "Heap. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -128,8 +128,5 @@
|
|
|
128
128
|
"ts-jest": "^29.1.1",
|
|
129
129
|
"typedoc": "^0.25.1",
|
|
130
130
|
"typescript": "^4.9.5"
|
|
131
|
-
},
|
|
132
|
-
"dependencies": {
|
|
133
|
-
"data-structure-typed": "^1.38.0"
|
|
134
131
|
}
|
|
135
132
|
}
|
|
@@ -32,33 +32,6 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
|
|
|
32
32
|
super(options);
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
/**
|
|
36
|
-
* The function swaps the key, value, and height properties between two nodes in a binary tree.
|
|
37
|
-
* @param {N} srcNode - The `srcNode` parameter represents the source node that needs to be swapped
|
|
38
|
-
* with the `destNode`.
|
|
39
|
-
* @param {N} destNode - The `destNode` parameter represents the destination node where the values
|
|
40
|
-
* from the source node (`srcNode`) will be swapped to.
|
|
41
|
-
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
42
|
-
*/
|
|
43
|
-
protected override _swap(srcNode: N, destNode: N): N {
|
|
44
|
-
const {key, val, height} = destNode;
|
|
45
|
-
const tempNode = this.createNode(key, val);
|
|
46
|
-
|
|
47
|
-
if (tempNode) {
|
|
48
|
-
tempNode.height = height;
|
|
49
|
-
|
|
50
|
-
destNode.key = srcNode.key;
|
|
51
|
-
destNode.val = srcNode.val;
|
|
52
|
-
destNode.height = srcNode.height;
|
|
53
|
-
|
|
54
|
-
srcNode.key = tempNode.key;
|
|
55
|
-
srcNode.val = tempNode.val;
|
|
56
|
-
srcNode.height = tempNode.height;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return destNode;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
35
|
/**
|
|
63
36
|
* The function creates a new AVL tree node with the specified key and value.
|
|
64
37
|
* @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
|
|
@@ -105,6 +78,33 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
|
|
|
105
78
|
return deletedResults;
|
|
106
79
|
}
|
|
107
80
|
|
|
81
|
+
/**
|
|
82
|
+
* The function swaps the key, value, and height properties between two nodes in a binary tree.
|
|
83
|
+
* @param {N} srcNode - The `srcNode` parameter represents the source node that needs to be swapped
|
|
84
|
+
* with the `destNode`.
|
|
85
|
+
* @param {N} destNode - The `destNode` parameter represents the destination node where the values
|
|
86
|
+
* from the source node (`srcNode`) will be swapped to.
|
|
87
|
+
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
88
|
+
*/
|
|
89
|
+
protected override _swap(srcNode: N, destNode: N): N {
|
|
90
|
+
const {key, val, height} = destNode;
|
|
91
|
+
const tempNode = this.createNode(key, val);
|
|
92
|
+
|
|
93
|
+
if (tempNode) {
|
|
94
|
+
tempNode.height = height;
|
|
95
|
+
|
|
96
|
+
destNode.key = srcNode.key;
|
|
97
|
+
destNode.val = srcNode.val;
|
|
98
|
+
destNode.height = srcNode.height;
|
|
99
|
+
|
|
100
|
+
srcNode.key = tempNode.key;
|
|
101
|
+
srcNode.val = tempNode.val;
|
|
102
|
+
srcNode.height = tempNode.height;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return destNode;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
108
|
/**
|
|
109
109
|
* The function calculates the balance factor of a node in a binary tree.
|
|
110
110
|
* @param {N} node - The parameter "node" represents a node in a binary tree data structure.
|
|
@@ -152,7 +152,7 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
|
|
|
152
152
|
// 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:
|
|
153
153
|
switch (
|
|
154
154
|
this._balanceFactor(A) // second O(1)
|
|
155
|
-
|
|
155
|
+
) {
|
|
156
156
|
case -2:
|
|
157
157
|
if (A && A.left) {
|
|
158
158
|
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}: {frequency?: number; max: number}) {
|
|
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};
|
|
@@ -26,16 +26,6 @@ import {Queue} from '../queue';
|
|
|
26
26
|
* @template FAMILY - The type of the family relationship in the binary tree.
|
|
27
27
|
*/
|
|
28
28
|
export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> = BinaryTreeNodeNested<V>> {
|
|
29
|
-
/**
|
|
30
|
-
* Creates a new instance of BinaryTreeNode.
|
|
31
|
-
* @param {BinaryTreeNodeKey} key - The key associated with the node.
|
|
32
|
-
* @param {V} val - The value stored in the node.
|
|
33
|
-
*/
|
|
34
|
-
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
35
|
-
this.key = key;
|
|
36
|
-
this.val = val;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
29
|
/**
|
|
40
30
|
* The key associated with the node.
|
|
41
31
|
*/
|
|
@@ -46,6 +36,21 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
|
|
|
46
36
|
*/
|
|
47
37
|
val: V | undefined;
|
|
48
38
|
|
|
39
|
+
/**
|
|
40
|
+
* The parent node of the current node.
|
|
41
|
+
*/
|
|
42
|
+
parent: FAMILY | null | undefined;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Creates a new instance of BinaryTreeNode.
|
|
46
|
+
* @param {BinaryTreeNodeKey} key - The key associated with the node.
|
|
47
|
+
* @param {V} val - The value stored in the node.
|
|
48
|
+
*/
|
|
49
|
+
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
50
|
+
this.key = key;
|
|
51
|
+
this.val = val;
|
|
52
|
+
}
|
|
53
|
+
|
|
49
54
|
private _left: FAMILY | null | undefined;
|
|
50
55
|
|
|
51
56
|
/**
|
|
@@ -86,11 +91,6 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
|
|
|
86
91
|
this._right = v;
|
|
87
92
|
}
|
|
88
93
|
|
|
89
|
-
/**
|
|
90
|
-
* The parent node of the current node.
|
|
91
|
-
*/
|
|
92
|
-
parent: FAMILY | null | undefined;
|
|
93
|
-
|
|
94
94
|
/**
|
|
95
95
|
* Get the position of the node within its family.
|
|
96
96
|
* @returns {FamilyPosition} - The family position of the node.
|
|
@@ -128,6 +128,8 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
|
|
|
128
128
|
* @template N - The type of the binary tree's nodes.
|
|
129
129
|
*/
|
|
130
130
|
export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode> implements IBinaryTree<N> {
|
|
131
|
+
private _loopType: IterationType = IterationType.ITERATIVE;
|
|
132
|
+
|
|
131
133
|
/**
|
|
132
134
|
* Creates a new instance of BinaryTree.
|
|
133
135
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
@@ -139,16 +141,6 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
139
141
|
}
|
|
140
142
|
}
|
|
141
143
|
|
|
142
|
-
/**
|
|
143
|
-
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
144
|
-
* @param {BinaryTreeNodeKey} key - The key for the new node.
|
|
145
|
-
* @param {N['val']} val - The value for the new node.
|
|
146
|
-
* @returns {N} - The newly created BinaryTreeNode.
|
|
147
|
-
*/
|
|
148
|
-
createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
|
|
149
|
-
return new BinaryTreeNode<N['val'], N>(key, val) as N;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
144
|
private _root: N | null = null;
|
|
153
145
|
|
|
154
146
|
/**
|
|
@@ -167,8 +159,6 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
167
159
|
return this._size;
|
|
168
160
|
}
|
|
169
161
|
|
|
170
|
-
private _loopType: IterationType = IterationType.ITERATIVE;
|
|
171
|
-
|
|
172
162
|
/**
|
|
173
163
|
* Get the iteration type used in the binary tree.
|
|
174
164
|
*/
|
|
@@ -185,24 +175,13 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
185
175
|
}
|
|
186
176
|
|
|
187
177
|
/**
|
|
188
|
-
*
|
|
189
|
-
* @param {
|
|
190
|
-
* @param {N}
|
|
191
|
-
* @returns {N} - The
|
|
178
|
+
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
179
|
+
* @param {BinaryTreeNodeKey} key - The key for the new node.
|
|
180
|
+
* @param {N['val']} val - The value for the new node.
|
|
181
|
+
* @returns {N} - The newly created BinaryTreeNode.
|
|
192
182
|
*/
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
const tempNode = this.createNode(key, val);
|
|
196
|
-
|
|
197
|
-
if (tempNode) {
|
|
198
|
-
destNode.key = srcNode.key;
|
|
199
|
-
destNode.val = srcNode.val;
|
|
200
|
-
|
|
201
|
-
srcNode.key = tempNode.key;
|
|
202
|
-
srcNode.val = tempNode.val;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
return destNode;
|
|
183
|
+
createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
|
|
184
|
+
return new BinaryTreeNode<N['val'], N>(key, val) as N;
|
|
206
185
|
}
|
|
207
186
|
|
|
208
187
|
/**
|
|
@@ -428,7 +407,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
428
407
|
return -1;
|
|
429
408
|
}
|
|
430
409
|
|
|
431
|
-
const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
|
|
410
|
+
const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
|
|
432
411
|
let maxHeight = 0;
|
|
433
412
|
|
|
434
413
|
while (stack.length > 0) {
|
|
@@ -449,8 +428,6 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
449
428
|
}
|
|
450
429
|
}
|
|
451
430
|
|
|
452
|
-
protected _defaultCallbackByKey: MapCallback<N> = node => node.key;
|
|
453
|
-
|
|
454
431
|
/**
|
|
455
432
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
456
433
|
* recursive or iterative approach.
|
|
@@ -865,7 +842,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
865
842
|
_traverse(beginRoot);
|
|
866
843
|
} else {
|
|
867
844
|
// 0: visit, 1: print
|
|
868
|
-
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
|
|
845
|
+
const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}];
|
|
869
846
|
|
|
870
847
|
while (stack.length > 0) {
|
|
871
848
|
const cur = stack.pop();
|
|
@@ -902,8 +879,6 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
902
879
|
return ans;
|
|
903
880
|
}
|
|
904
881
|
|
|
905
|
-
// --- start additional methods ---
|
|
906
|
-
|
|
907
882
|
/**
|
|
908
883
|
* The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
|
|
909
884
|
* function on each node.
|
|
@@ -973,13 +948,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
973
948
|
}
|
|
974
949
|
}
|
|
975
950
|
|
|
976
|
-
|
|
977
|
-
* Time complexity is O(n)
|
|
978
|
-
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
|
979
|
-
* The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
|
|
980
|
-
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
981
|
-
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
982
|
-
*/
|
|
951
|
+
// --- start additional methods ---
|
|
983
952
|
|
|
984
953
|
/**
|
|
985
954
|
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
@@ -1080,6 +1049,37 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
1080
1049
|
return ans;
|
|
1081
1050
|
}
|
|
1082
1051
|
|
|
1052
|
+
/**
|
|
1053
|
+
* Swap the data of two nodes in the binary tree.
|
|
1054
|
+
* @param {N} srcNode - The source node to swap.
|
|
1055
|
+
* @param {N} destNode - The destination node to swap.
|
|
1056
|
+
* @returns {N} - The destination node after the swap.
|
|
1057
|
+
*/
|
|
1058
|
+
protected _swap(srcNode: N, destNode: N): N {
|
|
1059
|
+
const {key, val} = destNode;
|
|
1060
|
+
const tempNode = this.createNode(key, val);
|
|
1061
|
+
|
|
1062
|
+
if (tempNode) {
|
|
1063
|
+
destNode.key = srcNode.key;
|
|
1064
|
+
destNode.val = srcNode.val;
|
|
1065
|
+
|
|
1066
|
+
srcNode.key = tempNode.key;
|
|
1067
|
+
srcNode.val = tempNode.val;
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
return destNode;
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
/**
|
|
1074
|
+
* Time complexity is O(n)
|
|
1075
|
+
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
|
1076
|
+
* The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
|
|
1077
|
+
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
1078
|
+
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
1079
|
+
*/
|
|
1080
|
+
|
|
1081
|
+
protected _defaultCallbackByKey: MapCallback<N> = node => node.key;
|
|
1082
|
+
|
|
1083
1083
|
/**
|
|
1084
1084
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
1085
1085
|
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
|
|
@@ -153,22 +153,26 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
153
153
|
function hasNoNull(arr: (BinaryTreeNodeKey | null)[] | (N | null)[]): arr is BinaryTreeNodeKey[] | N[] {
|
|
154
154
|
return arr.indexOf(null) === -1;
|
|
155
155
|
}
|
|
156
|
+
|
|
156
157
|
if (!isBalanceAdd || !hasNoNull(keysOrNodes)) {
|
|
157
158
|
return super.addMany(keysOrNodes, data);
|
|
158
159
|
}
|
|
159
160
|
const inserted: (N | null | undefined)[] = [];
|
|
160
161
|
const combinedArr: [BinaryTreeNodeKey | N, N['val']][] = keysOrNodes.map((value, index) => [value, data?.[index]]);
|
|
161
162
|
let sorted = [];
|
|
163
|
+
|
|
162
164
|
function isNodeOrNullTuple(arr: [BinaryTreeNodeKey | N, N['val']][]): arr is [N, N['val']][] {
|
|
163
165
|
for (const [keyOrNode] of arr) if (keyOrNode instanceof BSTNode) return true;
|
|
164
166
|
return false;
|
|
165
167
|
}
|
|
168
|
+
|
|
166
169
|
function isBinaryTreeKeyOrNullTuple(
|
|
167
170
|
arr: [BinaryTreeNodeKey | N, N['val']][]
|
|
168
171
|
): arr is [BinaryTreeNodeKey, N['val']][] {
|
|
169
172
|
for (const [keyOrNode] of arr) if (typeof keyOrNode === 'number') return true;
|
|
170
173
|
return false;
|
|
171
174
|
}
|
|
175
|
+
|
|
172
176
|
let sortedKeysOrNodes: (number | N | null)[] = [],
|
|
173
177
|
sortedData: (N['val'] | undefined)[] | undefined = [];
|
|
174
178
|
|
|
@@ -6,13 +6,13 @@ export class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNo
|
|
|
6
6
|
V,
|
|
7
7
|
FAMILY
|
|
8
8
|
> {
|
|
9
|
-
private _color: RBColor;
|
|
10
|
-
|
|
11
9
|
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
12
10
|
super(key, val);
|
|
13
11
|
this._color = RBColor.RED;
|
|
14
12
|
}
|
|
15
13
|
|
|
14
|
+
private _color: RBColor;
|
|
15
|
+
|
|
16
16
|
get color(): RBColor {
|
|
17
17
|
return this._color;
|
|
18
18
|
}
|
|
@@ -14,6 +14,8 @@ export class TreeMultisetNode<
|
|
|
14
14
|
V = any,
|
|
15
15
|
FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>
|
|
16
16
|
> extends AVLTreeNode<V, FAMILY> {
|
|
17
|
+
count: number;
|
|
18
|
+
|
|
17
19
|
/**
|
|
18
20
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
19
21
|
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
|
|
@@ -28,8 +30,6 @@ export class TreeMultisetNode<
|
|
|
28
30
|
super(key, val);
|
|
29
31
|
this.count = count;
|
|
30
32
|
}
|
|
31
|
-
|
|
32
|
-
count: number;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
/**
|
|
@@ -37,8 +37,7 @@ export class TreeMultisetNode<
|
|
|
37
37
|
*/
|
|
38
38
|
export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultisetNode>
|
|
39
39
|
extends AVLTree<N>
|
|
40
|
-
implements IBinaryTree<N>
|
|
41
|
-
{
|
|
40
|
+
implements IBinaryTree<N> {
|
|
42
41
|
/**
|
|
43
42
|
* The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
|
|
44
43
|
* merge duplicated values.
|
|
@@ -68,33 +67,6 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
68
67
|
return new TreeMultisetNode(key, val, count) as N;
|
|
69
68
|
}
|
|
70
69
|
|
|
71
|
-
/**
|
|
72
|
-
* The function swaps the values of two nodes in a binary tree.
|
|
73
|
-
* @param {N} srcNode - The source node that needs to be swapped with the destination node.
|
|
74
|
-
* @param {N} destNode - The `destNode` parameter represents the destination node where the values
|
|
75
|
-
* from `srcNode` will be swapped into.
|
|
76
|
-
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
77
|
-
*/
|
|
78
|
-
protected override _swap(srcNode: N, destNode: N): N {
|
|
79
|
-
const {key, val, count, height} = destNode;
|
|
80
|
-
const tempNode = this.createNode(key, val, count);
|
|
81
|
-
if (tempNode) {
|
|
82
|
-
tempNode.height = height;
|
|
83
|
-
|
|
84
|
-
destNode.key = srcNode.key;
|
|
85
|
-
destNode.val = srcNode.val;
|
|
86
|
-
destNode.count = srcNode.count;
|
|
87
|
-
destNode.height = srcNode.height;
|
|
88
|
-
|
|
89
|
-
srcNode.key = tempNode.key;
|
|
90
|
-
srcNode.val = tempNode.val;
|
|
91
|
-
srcNode.count = tempNode.count;
|
|
92
|
-
srcNode.height = tempNode.height;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return destNode;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
70
|
/**
|
|
99
71
|
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
100
72
|
* exists, and balancing the tree if necessary.
|
|
@@ -365,6 +337,33 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
365
337
|
this._setCount(0);
|
|
366
338
|
}
|
|
367
339
|
|
|
340
|
+
/**
|
|
341
|
+
* The function swaps the values of two nodes in a binary tree.
|
|
342
|
+
* @param {N} srcNode - The source node that needs to be swapped with the destination node.
|
|
343
|
+
* @param {N} destNode - The `destNode` parameter represents the destination node where the values
|
|
344
|
+
* from `srcNode` will be swapped into.
|
|
345
|
+
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
346
|
+
*/
|
|
347
|
+
protected override _swap(srcNode: N, destNode: N): N {
|
|
348
|
+
const {key, val, count, height} = destNode;
|
|
349
|
+
const tempNode = this.createNode(key, val, count);
|
|
350
|
+
if (tempNode) {
|
|
351
|
+
tempNode.height = height;
|
|
352
|
+
|
|
353
|
+
destNode.key = srcNode.key;
|
|
354
|
+
destNode.val = srcNode.val;
|
|
355
|
+
destNode.count = srcNode.count;
|
|
356
|
+
destNode.height = srcNode.height;
|
|
357
|
+
|
|
358
|
+
srcNode.key = tempNode.key;
|
|
359
|
+
srcNode.val = tempNode.val;
|
|
360
|
+
srcNode.count = tempNode.count;
|
|
361
|
+
srcNode.height = tempNode.height;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
return destNode;
|
|
365
|
+
}
|
|
366
|
+
|
|
368
367
|
/**
|
|
369
368
|
* The function sets the value of the "_count" property.
|
|
370
369
|
* @param {number} v - number
|
|
@@ -105,8 +105,7 @@ export abstract class AbstractEdge<V = any> {
|
|
|
105
105
|
export abstract class AbstractGraph<
|
|
106
106
|
V extends AbstractVertex<any> = AbstractVertex<any>,
|
|
107
107
|
E extends AbstractEdge<any> = AbstractEdge<any>
|
|
108
|
-
> implements IGraph<V, E>
|
|
109
|
-
{
|
|
108
|
+
> implements IGraph<V, E> {
|
|
110
109
|
private _vertices: Map<VertexKey, V> = new Map<VertexKey, V>();
|
|
111
110
|
|
|
112
111
|
get vertices(): Map<VertexKey, V> {
|
|
@@ -554,14 +553,14 @@ export abstract class AbstractGraph<
|
|
|
554
553
|
}
|
|
555
554
|
|
|
556
555
|
getMinDist &&
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
}
|
|
556
|
+
distMap.forEach((d, v) => {
|
|
557
|
+
if (v !== srcVertex) {
|
|
558
|
+
if (d < minDist) {
|
|
559
|
+
minDist = d;
|
|
560
|
+
if (genPaths) minDest = v;
|
|
563
561
|
}
|
|
564
|
-
}
|
|
562
|
+
}
|
|
563
|
+
});
|
|
565
564
|
|
|
566
565
|
genPaths && getPaths(minDest);
|
|
567
566
|
|
|
@@ -623,7 +622,7 @@ export abstract class AbstractGraph<
|
|
|
623
622
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
624
623
|
}
|
|
625
624
|
|
|
626
|
-
const heap = new PriorityQueue<{key: number; val: V}>((a, b) => a.key - b.key);
|
|
625
|
+
const heap = new PriorityQueue<{ key: number; val: V }>((a, b) => a.key - b.key);
|
|
627
626
|
heap.add({key: 0, val: srcVertex});
|
|
628
627
|
|
|
629
628
|
distMap.set(srcVertex, 0);
|
|
@@ -852,7 +851,7 @@ export abstract class AbstractGraph<
|
|
|
852
851
|
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
|
853
852
|
* path between vertices in the
|
|
854
853
|
*/
|
|
855
|
-
floyd(): {costs: number[][]; predecessor: (V | null)[][]} {
|
|
854
|
+
floyd(): { costs: number[][]; predecessor: (V | null)[][] } {
|
|
856
855
|
const idAndVertices = [...this._vertices];
|
|
857
856
|
const n = idAndVertices.length;
|
|
858
857
|
|
|
@@ -64,8 +64,7 @@ export class DirectedEdge<V = any> extends AbstractEdge<V> {
|
|
|
64
64
|
|
|
65
65
|
export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E extends DirectedEdge<any> = DirectedEdge>
|
|
66
66
|
extends AbstractGraph<V, E>
|
|
67
|
-
implements IGraph<V, E>
|
|
68
|
-
{
|
|
67
|
+
implements IGraph<V, E> {
|
|
69
68
|
/**
|
|
70
69
|
* The constructor function initializes an instance of a class.
|
|
71
70
|
*/
|
|
@@ -51,12 +51,11 @@ export class UndirectedEdge<V = number> extends AbstractEdge<V> {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
export class UndirectedGraph<
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
V extends UndirectedVertex<any> = UndirectedVertex,
|
|
55
|
+
E extends UndirectedEdge<any> = UndirectedEdge
|
|
56
|
+
>
|
|
57
57
|
extends AbstractGraph<V, E>
|
|
58
|
-
implements IGraph<V, E>
|
|
59
|
-
{
|
|
58
|
+
implements IGraph<V, E> {
|
|
60
59
|
/**
|
|
61
60
|
* The constructor initializes a new Map object to store edges.
|
|
62
61
|
*/
|