data-structure-typed 1.20.0 → 1.21.0
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/LICENSE +21 -0
- package/README.md +6 -4
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +11 -18
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +52 -74
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +20 -28
- package/dist/data-structures/graph/abstract-graph.d.ts +3 -3
- package/dist/data-structures/graph/abstract-graph.js +6 -2
- package/dist/data-structures/graph/index.d.ts +1 -0
- package/dist/data-structures/graph/index.js +1 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +111 -0
- package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +0 -2
- package/dist/data-structures/tree/tree.d.ts +1 -1
- package/dist/data-structures/types/abstract-binary-tree.d.ts +0 -1
- package/dist/data-structures/types/abstract-graph.d.ts +1 -0
- package/dist/data-structures/types/index.d.ts +1 -0
- package/dist/data-structures/types/index.js +1 -0
- package/dist/data-structures/types/map-graph.d.ts +1 -0
- package/dist/data-structures/types/map-graph.js +2 -0
- package/dist/data-structures/types/tree-multiset.d.ts +1 -3
- package/package.json +1 -1
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +0 -1479
- package/src/data-structures/binary-tree/avl-tree.ts +0 -301
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -78
- package/src/data-structures/binary-tree/binary-tree.ts +0 -42
- package/src/data-structures/binary-tree/bst.ts +0 -438
- package/src/data-structures/binary-tree/index.ts +0 -12
- package/src/data-structures/binary-tree/rb-tree.ts +0 -102
- package/src/data-structures/binary-tree/segment-tree.ts +0 -243
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -712
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/diagrams/README.md +0 -5
- package/src/data-structures/graph/abstract-graph.ts +0 -1033
- package/src/data-structures/graph/directed-graph.ts +0 -472
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -270
- package/src/data-structures/hash/coordinate-map.ts +0 -67
- package/src/data-structures/hash/coordinate-set.ts +0 -56
- package/src/data-structures/hash/hash-table.ts +0 -3
- package/src/data-structures/hash/index.ts +0 -6
- package/src/data-structures/hash/pair.ts +0 -3
- package/src/data-structures/hash/tree-map.ts +0 -3
- package/src/data-structures/hash/tree-set.ts +0 -3
- package/src/data-structures/heap/heap.ts +0 -200
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -31
- package/src/data-structures/heap/min-heap.ts +0 -34
- package/src/data-structures/index.ts +0 -15
- package/src/data-structures/interfaces/abstract-binary-tree.ts +0 -194
- package/src/data-structures/interfaces/abstract-graph.ts +0 -40
- package/src/data-structures/interfaces/avl-tree.ts +0 -28
- package/src/data-structures/interfaces/binary-tree.ts +0 -8
- package/src/data-structures/interfaces/bst.ts +0 -32
- package/src/data-structures/interfaces/directed-graph.ts +0 -20
- package/src/data-structures/interfaces/doubly-linked-list.ts +0 -1
- package/src/data-structures/interfaces/heap.ts +0 -1
- package/src/data-structures/interfaces/index.ts +0 -15
- package/src/data-structures/interfaces/navigator.ts +0 -1
- package/src/data-structures/interfaces/priority-queue.ts +0 -1
- package/src/data-structures/interfaces/rb-tree.ts +0 -11
- package/src/data-structures/interfaces/segment-tree.ts +0 -1
- package/src/data-structures/interfaces/singly-linked-list.ts +0 -1
- package/src/data-structures/interfaces/tree-multiset.ts +0 -12
- package/src/data-structures/interfaces/undirected-graph.ts +0 -6
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -573
- package/src/data-structures/linked-list/index.ts +0 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -490
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -3
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -27
- package/src/data-structures/matrix/matrix2d.ts +0 -208
- package/src/data-structures/matrix/navigator.ts +0 -122
- package/src/data-structures/matrix/vector2d.ts +0 -316
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -49
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -50
- package/src/data-structures/priority-queue/priority-queue.ts +0 -354
- package/src/data-structures/queue/deque.ts +0 -251
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -120
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -98
- package/src/data-structures/tree/index.ts +0 -1
- package/src/data-structures/tree/tree.ts +0 -69
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -227
- package/src/data-structures/types/abstract-binary-tree.ts +0 -41
- package/src/data-structures/types/abstract-graph.ts +0 -5
- package/src/data-structures/types/avl-tree.ts +0 -5
- package/src/data-structures/types/binary-tree.ts +0 -9
- package/src/data-structures/types/bst.ts +0 -12
- package/src/data-structures/types/directed-graph.ts +0 -8
- package/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/src/data-structures/types/heap.ts +0 -5
- package/src/data-structures/types/helpers.ts +0 -1
- package/src/data-structures/types/index.ts +0 -15
- package/src/data-structures/types/navigator.ts +0 -13
- package/src/data-structures/types/priority-queue.ts +0 -9
- package/src/data-structures/types/rb-tree.ts +0 -8
- package/src/data-structures/types/segment-tree.ts +0 -1
- package/src/data-structures/types/singly-linked-list.ts +0 -1
- package/src/data-structures/types/tree-multiset.ts +0 -8
- package/src/index.ts +0 -2
- package/src/utils/index.ts +0 -3
- package/src/utils/types/index.ts +0 -2
- package/src/utils/types/utils.ts +0 -6
- package/src/utils/types/validate-type.ts +0 -25
- package/src/utils/utils.ts +0 -78
- package/src/utils/validate-type.ts +0 -69
|
@@ -1,301 +0,0 @@
|
|
|
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 {BST, BSTNode} from './bst';
|
|
9
|
-
import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeId} from '../types';
|
|
10
|
-
import {IAVLTree, IAVLTreeNode} from '../interfaces';
|
|
11
|
-
|
|
12
|
-
export class AVLTreeNode<T = any, NEIGHBOR extends AVLTreeNode<T, NEIGHBOR> = AVLTreeNodeNested<T>> extends BSTNode<T, NEIGHBOR> implements IAVLTreeNode<T, NEIGHBOR> {
|
|
13
|
-
constructor(id: BinaryTreeNodeId, val?: T) {
|
|
14
|
-
super(id, val);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IAVLTree<N> {
|
|
19
|
-
/**
|
|
20
|
-
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
21
|
-
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
22
|
-
* constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
|
|
23
|
-
* options.
|
|
24
|
-
*/
|
|
25
|
-
constructor(options?: AVLTreeOptions) {
|
|
26
|
-
super(options);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* The function creates a new AVL tree node with the given id and value.
|
|
31
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
32
|
-
* identify each node in the tree.
|
|
33
|
-
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
34
|
-
* that will be stored in the node.
|
|
35
|
-
* @returns a new AVLTreeNode object with the specified id and value.
|
|
36
|
-
*/
|
|
37
|
-
override createNode(id: BinaryTreeNodeId, val?: N['val']): N {
|
|
38
|
-
return new AVLTreeNode<N['val'], N>(id, val) as N;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
|
|
43
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add.
|
|
44
|
-
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
|
|
45
|
-
* `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
|
|
46
|
-
* @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
|
|
47
|
-
*/
|
|
48
|
-
override add(id: BinaryTreeNodeId, val?: N['val']): N | null | undefined {
|
|
49
|
-
// TODO support node as a param
|
|
50
|
-
const inserted = super.add(id, val);
|
|
51
|
-
if (inserted) this.balancePath(inserted);
|
|
52
|
-
return inserted;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
|
|
57
|
-
* then balances the tree if necessary.
|
|
58
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
|
|
59
|
-
* removed from the AVL tree.
|
|
60
|
-
* @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
|
|
61
|
-
* determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
|
|
62
|
-
* `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
|
|
63
|
-
* @returns The method is returning an array of `AVLTreeDeleted<N>` objects.
|
|
64
|
-
*/
|
|
65
|
-
override remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): BinaryTreeDeletedResult<N>[] {
|
|
66
|
-
const deletedResults = super.remove(id, isUpdateAllLeftSum);
|
|
67
|
-
for (const {needBalanced} of deletedResults) {
|
|
68
|
-
if (needBalanced) {
|
|
69
|
-
this.balancePath(needBalanced);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return deletedResults;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
|
|
77
|
-
* height of its right subtree.
|
|
78
|
-
* @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
|
|
79
|
-
* @returns The balance factor of the given AVL tree node.
|
|
80
|
-
*/
|
|
81
|
-
balanceFactor(node: N): number {
|
|
82
|
-
if (!node.right) // node has no right subtree
|
|
83
|
-
return -node.height;
|
|
84
|
-
else if (!node.left) // node has no left subtree
|
|
85
|
-
return +node.height;
|
|
86
|
-
else
|
|
87
|
-
return node.right.height - node.left.height;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
|
|
92
|
-
* @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
93
|
-
*/
|
|
94
|
-
updateHeight(node: N): void {
|
|
95
|
-
if (!node.left && !node.right) // node is a leaf
|
|
96
|
-
node.height = 0;
|
|
97
|
-
else if (!node.left) {
|
|
98
|
-
// node has no left subtree
|
|
99
|
-
const rightHeight = node.right ? node.right.height : 0;
|
|
100
|
-
node.height = 1 + rightHeight;
|
|
101
|
-
} else if (!node.right) // node has no right subtree
|
|
102
|
-
node.height = 1 + node.left.height;
|
|
103
|
-
else
|
|
104
|
-
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* The `balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
|
|
109
|
-
* each node in the path from the given node to the root.
|
|
110
|
-
* @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
111
|
-
*/
|
|
112
|
-
balancePath(node: N): void {
|
|
113
|
-
const path = this.getPathToRoot(node);
|
|
114
|
-
for (let i = path.length - 1; i >= 0; i--) {
|
|
115
|
-
const A = path[i];
|
|
116
|
-
this.updateHeight(A);
|
|
117
|
-
switch (this.balanceFactor(A)) {
|
|
118
|
-
case -2:
|
|
119
|
-
if (A && A.left) {
|
|
120
|
-
if (this.balanceFactor(A.left) <= 0) {
|
|
121
|
-
this.balanceLL(A); // Perform LL rotation
|
|
122
|
-
} else {
|
|
123
|
-
this.balanceLR(A); // Perform LR rotation
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
break;
|
|
127
|
-
case +2:
|
|
128
|
-
if (A && A.right) {
|
|
129
|
-
if (this.balanceFactor(A.right) >= 0) {
|
|
130
|
-
this.balanceRR(A); // Perform RR rotation
|
|
131
|
-
} else {
|
|
132
|
-
this.balanceRL(A); // Perform RL rotation
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* The `balanceLL` function performs a left-left rotation on an AVL tree to balance it.
|
|
141
|
-
* @param A - The parameter A is an AVLTreeNode object.
|
|
142
|
-
*/
|
|
143
|
-
balanceLL(A: N): void {
|
|
144
|
-
const parentOfA = A.parent;
|
|
145
|
-
const B = A.left; // A is left-heavy and B is left-heavy
|
|
146
|
-
A.parent = B;
|
|
147
|
-
if (B && B.right) {
|
|
148
|
-
B.right.parent = A;
|
|
149
|
-
}
|
|
150
|
-
if (B) B.parent = parentOfA;
|
|
151
|
-
if (A === this.root) {
|
|
152
|
-
if (B) this._setRoot(B);
|
|
153
|
-
} else {
|
|
154
|
-
if (parentOfA?.left === A) {
|
|
155
|
-
parentOfA.left = B;
|
|
156
|
-
} else {
|
|
157
|
-
if (parentOfA) parentOfA.right = B;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
if (B) {
|
|
162
|
-
A.left = B.right; // Make T2 the left subtree of A
|
|
163
|
-
B.right = A; // Make A the left child of B
|
|
164
|
-
}
|
|
165
|
-
this.updateHeight(A);
|
|
166
|
-
if (B) this.updateHeight(B);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* The `balanceLR` function performs a left-right rotation to balance an AVL tree.
|
|
171
|
-
* @param A - A is an AVLTreeNode object.
|
|
172
|
-
*/
|
|
173
|
-
balanceLR(A: N): void {
|
|
174
|
-
const parentOfA = A.parent;
|
|
175
|
-
const B = A.left; // A is left-heavy
|
|
176
|
-
let C = null;
|
|
177
|
-
if (B) {
|
|
178
|
-
C = B.right;// B is right-heavy
|
|
179
|
-
}
|
|
180
|
-
if (A) A.parent = C;
|
|
181
|
-
if (B) B.parent = C;
|
|
182
|
-
|
|
183
|
-
if (C) {
|
|
184
|
-
if (C.left) {
|
|
185
|
-
C.left.parent = B;
|
|
186
|
-
}
|
|
187
|
-
if (C.right) {
|
|
188
|
-
C.right.parent = A;
|
|
189
|
-
}
|
|
190
|
-
C.parent = parentOfA;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
if (A === this.root) {
|
|
194
|
-
if (C) this._setRoot(C);
|
|
195
|
-
} else {
|
|
196
|
-
if (parentOfA) {
|
|
197
|
-
if (parentOfA.left === A) {
|
|
198
|
-
parentOfA.left = C;
|
|
199
|
-
} else {
|
|
200
|
-
parentOfA.right = C;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
if (C) {
|
|
206
|
-
A.left = C.right; // Make T3 the left subtree of A
|
|
207
|
-
if (B) B.right = C.left; // Make T2 the right subtree of B
|
|
208
|
-
C.left = B;
|
|
209
|
-
C.right = A;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
this.updateHeight(A); // Adjust heights
|
|
213
|
-
B && this.updateHeight(B);
|
|
214
|
-
C && this.updateHeight(C);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* The `balanceRR` function performs a right-right rotation on an AVL tree to balance it.
|
|
219
|
-
* @param A - The parameter A is an AVLTreeNode object.
|
|
220
|
-
*/
|
|
221
|
-
balanceRR(A: N): void {
|
|
222
|
-
const parentOfA = A.parent;
|
|
223
|
-
const B = A.right; // A is right-heavy and B is right-heavy
|
|
224
|
-
A.parent = B;
|
|
225
|
-
if (B) {
|
|
226
|
-
if (B.left) {
|
|
227
|
-
B.left.parent = A;
|
|
228
|
-
}
|
|
229
|
-
B.parent = parentOfA;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
if (A === this.root) {
|
|
233
|
-
if (B) this._setRoot(B);
|
|
234
|
-
} else {
|
|
235
|
-
if (parentOfA) {
|
|
236
|
-
if (parentOfA.left === A) {
|
|
237
|
-
parentOfA.left = B;
|
|
238
|
-
} else {
|
|
239
|
-
parentOfA.right = B;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
if (B) {
|
|
245
|
-
A.right = B.left; // Make T2 the right subtree of A
|
|
246
|
-
B.left = A;
|
|
247
|
-
}
|
|
248
|
-
this.updateHeight(A);
|
|
249
|
-
B && this.updateHeight(B);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* The `balanceRL` function performs a right-left rotation to balance an AVL tree.
|
|
254
|
-
* @param A - A is an AVLTreeNode object.
|
|
255
|
-
*/
|
|
256
|
-
balanceRL(A: N): void {
|
|
257
|
-
const parentOfA = A.parent;
|
|
258
|
-
const B = A.right; // A is right-heavy
|
|
259
|
-
let C = null;
|
|
260
|
-
if (B) {
|
|
261
|
-
C = B.left; // B is left-heavy
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
A.parent = C;
|
|
265
|
-
if (B) B.parent = C;
|
|
266
|
-
|
|
267
|
-
if (C) {
|
|
268
|
-
if (C.left) {
|
|
269
|
-
C.left.parent = A;
|
|
270
|
-
}
|
|
271
|
-
if (C.right) {
|
|
272
|
-
C.right.parent = B;
|
|
273
|
-
}
|
|
274
|
-
C.parent = parentOfA;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
if (A === this.root) {
|
|
279
|
-
if (C) this._setRoot(C);
|
|
280
|
-
} else {
|
|
281
|
-
if (parentOfA) {
|
|
282
|
-
if (parentOfA.left === A) {
|
|
283
|
-
parentOfA.left = C;
|
|
284
|
-
} else {
|
|
285
|
-
parentOfA.right = C;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
if (C) A.right = C.left; // Make T2 the right subtree of A
|
|
291
|
-
if (B && C) B.left = C.right; // Make T3 the left subtree of B
|
|
292
|
-
if (C) C.left = A;
|
|
293
|
-
if (C) C.right = B;
|
|
294
|
-
|
|
295
|
-
this.updateHeight(A); // Adjust heights
|
|
296
|
-
B && this.updateHeight(B);
|
|
297
|
-
C && this.updateHeight(C);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
|
|
@@ -1,78 +0,0 @@
|
|
|
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
|
-
export class BinaryIndexedTree {
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* The constructor initializes an array with a specified length and fills it with zeros.
|
|
12
|
-
* @param {number} n - The parameter `n` represents the size of the array that will be used to store the sum tree. The
|
|
13
|
-
* sum tree is a binary tree data structure used to efficiently calculate the sum of a range of elements in an array.
|
|
14
|
-
* The size of the sum tree array is `n + 1` because
|
|
15
|
-
*/
|
|
16
|
-
constructor(n: number) {
|
|
17
|
-
this._sumTree = new Array<number>(n + 1).fill(0);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
private _sumTree: number[];
|
|
21
|
-
|
|
22
|
-
get sumTree(): number[] {
|
|
23
|
-
return this._sumTree;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
static lowBit(x: number) {
|
|
27
|
-
return x & (-x);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The update function updates the values in a binary indexed tree by adding a delta value to the specified index and
|
|
32
|
-
* its ancestors.
|
|
33
|
-
* @param {number} i - The parameter `i` represents the index of the element in the `_sumTree` array that needs to be
|
|
34
|
-
* updated.
|
|
35
|
-
* @param {number} delta - The "delta" parameter represents the change in value that needs to be added to the element
|
|
36
|
-
* at index "i" in the "_sumTree" array.
|
|
37
|
-
*/
|
|
38
|
-
update(i: number, delta: number) {
|
|
39
|
-
while (i < this._sumTree.length) {
|
|
40
|
-
this._sumTree[i] += delta;
|
|
41
|
-
i += BinaryIndexedTree.lowBit(i);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* The function calculates the prefix sum of an array using a binary indexed tree.
|
|
47
|
-
* @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
|
|
48
|
-
* array for which we want to calculate the prefix sum.
|
|
49
|
-
* @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
|
|
50
|
-
* `i`.
|
|
51
|
-
*/
|
|
52
|
-
getPrefixSum(i: number) {
|
|
53
|
-
let sum = 0;
|
|
54
|
-
while (i > 0) {
|
|
55
|
-
sum += this._sumTree[i];
|
|
56
|
-
i -= BinaryIndexedTree.lowBit(i);
|
|
57
|
-
}
|
|
58
|
-
return sum;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* The function `getRangeSum` calculates the sum of a range of numbers in an array.
|
|
63
|
-
* @param {number} start - The start parameter is the starting index of the range for which we want to calculate the
|
|
64
|
-
* sum.
|
|
65
|
-
* @param {number} end - The "end" parameter represents the ending index of the range for which we want to calculate
|
|
66
|
-
* the sum.
|
|
67
|
-
* @returns the sum of the elements in the range specified by the start and end indices.
|
|
68
|
-
*/
|
|
69
|
-
getRangeSum(start: number, end: number): number {
|
|
70
|
-
if (!(0 <= start && start <= end && end <= this._sumTree.length))
|
|
71
|
-
throw 'Index out of bounds';
|
|
72
|
-
return this.getPrefixSum(end) - this.getPrefixSum(start);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
protected _setSumTree(value: number[]) {
|
|
76
|
-
this._sumTree = value;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
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
|
-
|
|
9
|
-
import type {BinaryTreeNodeId, BinaryTreeNodeNested, BinaryTreeOptions} from '../types';
|
|
10
|
-
import {AbstractBinaryTree, AbstractBinaryTreeNode} from './abstract-binary-tree';
|
|
11
|
-
import {IBinaryTree, IBinaryTreeNode} from '../interfaces';
|
|
12
|
-
|
|
13
|
-
export class BinaryTreeNode<T = any, NEIGHBOR extends BinaryTreeNode<T, NEIGHBOR> = BinaryTreeNodeNested<T>> extends AbstractBinaryTreeNode<T, NEIGHBOR> implements IBinaryTreeNode<T, NEIGHBOR> {
|
|
14
|
-
constructor(id: BinaryTreeNodeId, val?: T) {
|
|
15
|
-
super(id, val);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode> extends AbstractBinaryTree<N> implements IBinaryTree<N> {
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* This is a constructor function for a binary tree class that takes an optional options parameter.
|
|
23
|
-
* @param {BinaryTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
24
|
-
* constructor of the `BinaryTree` class. It allows you to customize the behavior of the binary tree by providing
|
|
25
|
-
* different configuration options.
|
|
26
|
-
*/
|
|
27
|
-
constructor(options?: BinaryTreeOptions) {
|
|
28
|
-
super(options);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* The function creates a new binary tree node with an optional value.
|
|
33
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
|
|
34
|
-
* `BinaryTreeNodeId`, which represents the unique identifier for each node in the binary tree.
|
|
35
|
-
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
36
|
-
* stored in the node.
|
|
37
|
-
* @returns a new instance of a BinaryTreeNode with the specified id and value.
|
|
38
|
-
*/
|
|
39
|
-
createNode(id: BinaryTreeNodeId, val?: N['val']): N {
|
|
40
|
-
return new BinaryTreeNode<N['val'], N>(id, val) as N;
|
|
41
|
-
}
|
|
42
|
-
}
|