data-structure-typed 1.19.3 → 1.19.5
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/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +361 -488
- package/dist/data-structures/binary-tree/avl-tree.js +46 -90
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
- package/dist/data-structures/binary-tree/binary-tree.js +9 -31
- package/dist/data-structures/binary-tree/bst.js +96 -139
- package/dist/data-structures/binary-tree/rb-tree.js +32 -56
- package/dist/data-structures/binary-tree/segment-tree.js +78 -120
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +176 -253
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/graph/abstract-graph.js +340 -574
- package/dist/data-structures/graph/directed-graph.js +146 -276
- package/dist/data-structures/graph/undirected-graph.js +87 -176
- package/dist/data-structures/hash/coordinate-map.js +23 -45
- package/dist/data-structures/hash/coordinate-set.js +20 -42
- package/dist/data-structures/hash/hash-table.js +2 -5
- package/dist/data-structures/hash/pair.js +2 -5
- package/dist/data-structures/hash/tree-map.js +2 -5
- package/dist/data-structures/hash/tree-set.js +2 -5
- package/dist/data-structures/heap/heap.js +53 -77
- package/dist/data-structures/heap/max-heap.js +8 -26
- package/dist/data-structures/heap/min-heap.js +8 -26
- package/dist/data-structures/linked-list/doubly-linked-list.js +132 -197
- package/dist/data-structures/linked-list/singly-linked-list.js +112 -173
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -5
- package/dist/data-structures/matrix/matrix.js +7 -8
- package/dist/data-structures/matrix/matrix2d.js +76 -93
- package/dist/data-structures/matrix/navigator.js +18 -37
- package/dist/data-structures/matrix/vector2d.js +80 -101
- package/dist/data-structures/priority-queue/max-priority-queue.js +11 -39
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -39
- package/dist/data-structures/priority-queue/priority-queue.js +93 -139
- package/dist/data-structures/queue/deque.js +82 -128
- package/dist/data-structures/queue/queue.js +24 -25
- package/dist/data-structures/stack/stack.js +21 -22
- package/dist/data-structures/tree/tree.js +32 -45
- package/dist/data-structures/trie/trie.js +93 -200
- package/dist/utils/utils.js +22 -107
- package/dist/utils/validate-type.js +2 -2
- package/package.json +3 -2
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/assets/logo.png +0 -0
- package/src/assets/overview-diagram-of-data-structures.png +0 -0
- package/src/data-structures/binary-tree/aa-tree.ts +3 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +1528 -0
- package/src/data-structures/binary-tree/avl-tree.ts +297 -0
- package/src/data-structures/binary-tree/b-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
- package/src/data-structures/binary-tree/binary-tree.ts +40 -0
- package/src/data-structures/binary-tree/bst.ts +435 -0
- package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/src/data-structures/binary-tree/index.ts +12 -0
- package/src/data-structures/binary-tree/rb-tree.ts +102 -0
- package/src/data-structures/binary-tree/segment-tree.ts +243 -0
- package/src/data-structures/binary-tree/splay-tree.ts +3 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +694 -0
- package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
- package/src/data-structures/diagrams/README.md +5 -0
- package/src/data-structures/graph/abstract-graph.ts +1032 -0
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/src/data-structures/graph/directed-graph.ts +472 -0
- package/src/data-structures/graph/index.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +270 -0
- package/src/data-structures/hash/coordinate-map.ts +67 -0
- package/src/data-structures/hash/coordinate-set.ts +56 -0
- package/src/data-structures/hash/hash-table.ts +3 -0
- package/src/data-structures/hash/index.ts +6 -0
- package/src/data-structures/hash/pair.ts +3 -0
- package/src/data-structures/hash/tree-map.ts +3 -0
- package/src/data-structures/hash/tree-set.ts +3 -0
- package/src/data-structures/heap/heap.ts +183 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +31 -0
- package/src/data-structures/heap/min-heap.ts +34 -0
- package/src/data-structures/index.ts +15 -0
- package/src/data-structures/interfaces/abstract-binary-tree.ts +231 -0
- package/src/data-structures/interfaces/abstract-graph.ts +40 -0
- package/src/data-structures/interfaces/avl-tree.ts +28 -0
- package/src/data-structures/interfaces/binary-tree.ts +8 -0
- package/src/data-structures/interfaces/bst.ts +32 -0
- package/src/data-structures/interfaces/directed-graph.ts +20 -0
- package/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
- package/src/data-structures/interfaces/heap.ts +1 -0
- package/src/data-structures/interfaces/index.ts +15 -0
- package/src/data-structures/interfaces/navigator.ts +1 -0
- package/src/data-structures/interfaces/priority-queue.ts +1 -0
- package/src/data-structures/interfaces/rb-tree.ts +11 -0
- package/src/data-structures/interfaces/segment-tree.ts +1 -0
- package/src/data-structures/interfaces/singly-linked-list.ts +1 -0
- package/src/data-structures/interfaces/tree-multiset.ts +12 -0
- package/src/data-structures/interfaces/undirected-graph.ts +6 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +490 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +208 -0
- package/src/data-structures/matrix/navigator.ts +122 -0
- package/src/data-structures/matrix/vector2d.ts +316 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
- package/src/data-structures/priority-queue/priority-queue.ts +354 -0
- package/src/data-structures/queue/deque.ts +251 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +120 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +98 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +69 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +227 -0
- package/src/data-structures/types/abstract-binary-tree.ts +42 -0
- package/src/data-structures/types/abstract-graph.ts +5 -0
- package/src/data-structures/types/avl-tree.ts +5 -0
- package/src/data-structures/types/binary-tree.ts +9 -0
- package/src/data-structures/types/bst.ts +12 -0
- package/src/data-structures/types/directed-graph.ts +8 -0
- package/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/src/data-structures/types/heap.ts +5 -0
- package/src/data-structures/types/helpers.ts +1 -0
- package/src/data-structures/types/index.ts +15 -0
- package/src/data-structures/types/navigator.ts +13 -0
- package/src/data-structures/types/priority-queue.ts +9 -0
- package/src/data-structures/types/rb-tree.ts +8 -0
- package/src/data-structures/types/segment-tree.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +1 -0
- package/src/data-structures/types/tree-multiset.ts +8 -0
- package/src/index.ts +2 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/types/index.ts +2 -0
- package/src/utils/types/utils.ts +6 -0
- package/src/utils/types/validate-type.ts +25 -0
- package/src/utils/utils.ts +78 -0
- package/src/utils/validate-type.ts +69 -0
- package/tsconfig.json +1 -1
|
@@ -1,30 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __values = (this && this.__values) || function(o) {
|
|
18
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
19
|
-
if (m) return m.call(o);
|
|
20
|
-
if (o && typeof o.length === "number") return {
|
|
21
|
-
next: function () {
|
|
22
|
-
if (o && i >= o.length) o = void 0;
|
|
23
|
-
return { value: o && o[i++], done: !o };
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
27
|
-
};
|
|
28
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
3
|
exports.AVLTree = exports.AVLTreeNode = void 0;
|
|
30
4
|
/**
|
|
@@ -34,25 +8,19 @@ exports.AVLTree = exports.AVLTreeNode = void 0;
|
|
|
34
8
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
35
9
|
* @license MIT License
|
|
36
10
|
*/
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
function AVLTreeNode() {
|
|
41
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
42
|
-
}
|
|
43
|
-
return AVLTreeNode;
|
|
44
|
-
}(bst_1.BSTNode));
|
|
11
|
+
const bst_1 = require("./bst");
|
|
12
|
+
class AVLTreeNode extends bst_1.BSTNode {
|
|
13
|
+
}
|
|
45
14
|
exports.AVLTreeNode = AVLTreeNode;
|
|
46
|
-
|
|
47
|
-
__extends(AVLTree, _super);
|
|
15
|
+
class AVLTree extends bst_1.BST {
|
|
48
16
|
/**
|
|
49
17
|
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
50
18
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
51
19
|
* constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
|
|
52
20
|
* options.
|
|
53
21
|
*/
|
|
54
|
-
|
|
55
|
-
|
|
22
|
+
constructor(options) {
|
|
23
|
+
super(options);
|
|
56
24
|
}
|
|
57
25
|
/**
|
|
58
26
|
* The function creates a new AVL tree node with the given id and value.
|
|
@@ -62,9 +30,9 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
62
30
|
* that will be stored in the node.
|
|
63
31
|
* @returns a new AVLTreeNode object with the specified id and value.
|
|
64
32
|
*/
|
|
65
|
-
|
|
33
|
+
createNode(id, val) {
|
|
66
34
|
return new AVLTreeNode(id, val);
|
|
67
|
-
}
|
|
35
|
+
}
|
|
68
36
|
/**
|
|
69
37
|
* The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
|
|
70
38
|
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add.
|
|
@@ -72,12 +40,12 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
72
40
|
* `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
|
|
73
41
|
* @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
|
|
74
42
|
*/
|
|
75
|
-
|
|
76
|
-
|
|
43
|
+
add(id, val) {
|
|
44
|
+
const inserted = super.add(id, val);
|
|
77
45
|
if (inserted)
|
|
78
46
|
this.balancePath(inserted);
|
|
79
47
|
return inserted;
|
|
80
|
-
}
|
|
48
|
+
}
|
|
81
49
|
/**
|
|
82
50
|
* The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
|
|
83
51
|
* then balances the tree if necessary.
|
|
@@ -88,66 +56,55 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
88
56
|
* `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
|
|
89
57
|
* @returns The method is returning an array of `AVLTreeDeleted<N>` objects.
|
|
90
58
|
*/
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
var needBalanced = deletedResults_1_1.value.needBalanced;
|
|
97
|
-
if (needBalanced) {
|
|
98
|
-
this.balancePath(needBalanced);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
103
|
-
finally {
|
|
104
|
-
try {
|
|
105
|
-
if (deletedResults_1_1 && !deletedResults_1_1.done && (_a = deletedResults_1.return)) _a.call(deletedResults_1);
|
|
59
|
+
remove(id, isUpdateAllLeftSum) {
|
|
60
|
+
const deletedResults = super.remove(id, isUpdateAllLeftSum);
|
|
61
|
+
for (const { needBalanced } of deletedResults) {
|
|
62
|
+
if (needBalanced) {
|
|
63
|
+
this.balancePath(needBalanced);
|
|
106
64
|
}
|
|
107
|
-
finally { if (e_1) throw e_1.error; }
|
|
108
65
|
}
|
|
109
66
|
return deletedResults;
|
|
110
|
-
}
|
|
67
|
+
}
|
|
111
68
|
/**
|
|
112
69
|
* The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
|
|
113
70
|
* height of its right subtree.
|
|
114
71
|
* @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
|
|
115
72
|
* @returns The balance factor of the given AVL tree node.
|
|
116
73
|
*/
|
|
117
|
-
|
|
74
|
+
balanceFactor(node) {
|
|
118
75
|
if (!node.right) // node has no right subtree
|
|
119
76
|
return -node.height;
|
|
120
77
|
else if (!node.left) // node has no left subtree
|
|
121
78
|
return +node.height;
|
|
122
79
|
else
|
|
123
80
|
return node.right.height - node.left.height;
|
|
124
|
-
}
|
|
81
|
+
}
|
|
125
82
|
/**
|
|
126
83
|
* The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
|
|
127
84
|
* @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
128
85
|
*/
|
|
129
|
-
|
|
86
|
+
updateHeight(node) {
|
|
130
87
|
if (!node.left && !node.right) // node is a leaf
|
|
131
88
|
node.height = 0;
|
|
132
89
|
else if (!node.left) {
|
|
133
90
|
// node has no left subtree
|
|
134
|
-
|
|
91
|
+
const rightHeight = node.right ? node.right.height : 0;
|
|
135
92
|
node.height = 1 + rightHeight;
|
|
136
93
|
}
|
|
137
94
|
else if (!node.right) // node has no right subtree
|
|
138
95
|
node.height = 1 + node.left.height;
|
|
139
96
|
else
|
|
140
97
|
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
141
|
-
}
|
|
98
|
+
}
|
|
142
99
|
/**
|
|
143
100
|
* The `balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
|
|
144
101
|
* each node in the path from the given node to the root.
|
|
145
102
|
* @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
146
103
|
*/
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
for (
|
|
150
|
-
|
|
104
|
+
balancePath(node) {
|
|
105
|
+
const path = this.getPathToRoot(node);
|
|
106
|
+
for (let i = path.length - 1; i >= 0; i--) {
|
|
107
|
+
const A = path[i];
|
|
151
108
|
this.updateHeight(A);
|
|
152
109
|
switch (this.balanceFactor(A)) {
|
|
153
110
|
case -2:
|
|
@@ -171,14 +128,14 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
171
128
|
}
|
|
172
129
|
}
|
|
173
130
|
}
|
|
174
|
-
}
|
|
131
|
+
}
|
|
175
132
|
/**
|
|
176
133
|
* The `balanceLL` function performs a left-left rotation on an AVL tree to balance it.
|
|
177
134
|
* @param A - The parameter A is an AVLTreeNode object.
|
|
178
135
|
*/
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
136
|
+
balanceLL(A) {
|
|
137
|
+
const parentOfA = A.parent;
|
|
138
|
+
const B = A.left; // A is left-heavy and B is left-heavy
|
|
182
139
|
A.parent = B;
|
|
183
140
|
if (B && B.right) {
|
|
184
141
|
B.right.parent = A;
|
|
@@ -205,15 +162,15 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
205
162
|
this.updateHeight(A);
|
|
206
163
|
if (B)
|
|
207
164
|
this.updateHeight(B);
|
|
208
|
-
}
|
|
165
|
+
}
|
|
209
166
|
/**
|
|
210
167
|
* The `balanceLR` function performs a left-right rotation to balance an AVL tree.
|
|
211
168
|
* @param A - A is an AVLTreeNode object.
|
|
212
169
|
*/
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
170
|
+
balanceLR(A) {
|
|
171
|
+
const parentOfA = A.parent;
|
|
172
|
+
const B = A.left; // A is left-heavy
|
|
173
|
+
let C = null;
|
|
217
174
|
if (B) {
|
|
218
175
|
C = B.right; // B is right-heavy
|
|
219
176
|
}
|
|
@@ -254,14 +211,14 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
254
211
|
this.updateHeight(A); // Adjust heights
|
|
255
212
|
B && this.updateHeight(B);
|
|
256
213
|
C && this.updateHeight(C);
|
|
257
|
-
}
|
|
214
|
+
}
|
|
258
215
|
/**
|
|
259
216
|
* The `balanceRR` function performs a right-right rotation on an AVL tree to balance it.
|
|
260
217
|
* @param A - The parameter A is an AVLTreeNode object.
|
|
261
218
|
*/
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
219
|
+
balanceRR(A) {
|
|
220
|
+
const parentOfA = A.parent;
|
|
221
|
+
const B = A.right; // A is right-heavy and B is right-heavy
|
|
265
222
|
A.parent = B;
|
|
266
223
|
if (B) {
|
|
267
224
|
if (B.left) {
|
|
@@ -289,15 +246,15 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
289
246
|
}
|
|
290
247
|
this.updateHeight(A);
|
|
291
248
|
B && this.updateHeight(B);
|
|
292
|
-
}
|
|
249
|
+
}
|
|
293
250
|
/**
|
|
294
251
|
* The `balanceRL` function performs a right-left rotation to balance an AVL tree.
|
|
295
252
|
* @param A - A is an AVLTreeNode object.
|
|
296
253
|
*/
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
254
|
+
balanceRL(A) {
|
|
255
|
+
const parentOfA = A.parent;
|
|
256
|
+
const B = A.right; // A is right-heavy
|
|
257
|
+
let C = null;
|
|
301
258
|
if (B) {
|
|
302
259
|
C = B.left; // B is left-heavy
|
|
303
260
|
}
|
|
@@ -338,7 +295,6 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
338
295
|
this.updateHeight(A); // Adjust heights
|
|
339
296
|
B && this.updateHeight(B);
|
|
340
297
|
C && this.updateHeight(C);
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
}(bst_1.BST));
|
|
298
|
+
}
|
|
299
|
+
}
|
|
344
300
|
exports.AVLTree = AVLTree;
|
|
@@ -8,26 +8,22 @@ exports.BinaryIndexedTree = void 0;
|
|
|
8
8
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
9
|
* @license MIT License
|
|
10
10
|
*/
|
|
11
|
-
|
|
11
|
+
class BinaryIndexedTree {
|
|
12
12
|
/**
|
|
13
13
|
* The constructor initializes an array with a specified length and fills it with zeros.
|
|
14
14
|
* @param {number} n - The parameter `n` represents the size of the array that will be used to store the sum tree. The
|
|
15
15
|
* sum tree is a binary tree data structure used to efficiently calculate the sum of a range of elements in an array.
|
|
16
16
|
* The size of the sum tree array is `n + 1` because
|
|
17
17
|
*/
|
|
18
|
-
|
|
18
|
+
constructor(n) {
|
|
19
19
|
this._sumTree = new Array(n + 1).fill(0);
|
|
20
20
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
enumerable: false,
|
|
26
|
-
configurable: true
|
|
27
|
-
});
|
|
28
|
-
BinaryIndexedTree.lowBit = function (x) {
|
|
21
|
+
get sumTree() {
|
|
22
|
+
return this._sumTree;
|
|
23
|
+
}
|
|
24
|
+
static lowBit(x) {
|
|
29
25
|
return x & (-x);
|
|
30
|
-
}
|
|
26
|
+
}
|
|
31
27
|
/**
|
|
32
28
|
* The update function updates the values in a binary indexed tree by adding a delta value to the specified index and
|
|
33
29
|
* its ancestors.
|
|
@@ -36,12 +32,12 @@ var BinaryIndexedTree = /** @class */ (function () {
|
|
|
36
32
|
* @param {number} delta - The "delta" parameter represents the change in value that needs to be added to the element
|
|
37
33
|
* at index "i" in the "_sumTree" array.
|
|
38
34
|
*/
|
|
39
|
-
|
|
35
|
+
update(i, delta) {
|
|
40
36
|
while (i < this._sumTree.length) {
|
|
41
37
|
this._sumTree[i] += delta;
|
|
42
38
|
i += BinaryIndexedTree.lowBit(i);
|
|
43
39
|
}
|
|
44
|
-
}
|
|
40
|
+
}
|
|
45
41
|
/**
|
|
46
42
|
* The function calculates the prefix sum of an array using a binary indexed tree.
|
|
47
43
|
* @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
|
|
@@ -49,14 +45,14 @@ var BinaryIndexedTree = /** @class */ (function () {
|
|
|
49
45
|
* @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
|
|
50
46
|
* `i`.
|
|
51
47
|
*/
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
getPrefixSum(i) {
|
|
49
|
+
let sum = 0;
|
|
54
50
|
while (i > 0) {
|
|
55
51
|
sum += this._sumTree[i];
|
|
56
52
|
i -= BinaryIndexedTree.lowBit(i);
|
|
57
53
|
}
|
|
58
54
|
return sum;
|
|
59
|
-
}
|
|
55
|
+
}
|
|
60
56
|
/**
|
|
61
57
|
* The function `getRangeSum` calculates the sum of a range of numbers in an array.
|
|
62
58
|
* @param {number} start - The start parameter is the starting index of the range for which we want to calculate the
|
|
@@ -65,14 +61,13 @@ var BinaryIndexedTree = /** @class */ (function () {
|
|
|
65
61
|
* the sum.
|
|
66
62
|
* @returns the sum of the elements in the range specified by the start and end indices.
|
|
67
63
|
*/
|
|
68
|
-
|
|
64
|
+
getRangeSum(start, end) {
|
|
69
65
|
if (!(0 <= start && start <= end && end <= this._sumTree.length))
|
|
70
66
|
throw 'Index out of bounds';
|
|
71
67
|
return this.getPrefixSum(end) - this.getPrefixSum(start);
|
|
72
|
-
}
|
|
73
|
-
|
|
68
|
+
}
|
|
69
|
+
_setSumTree(value) {
|
|
74
70
|
this._sumTree = value;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
}());
|
|
71
|
+
}
|
|
72
|
+
}
|
|
78
73
|
exports.BinaryIndexedTree = BinaryIndexedTree;
|
|
@@ -6,42 +6,21 @@
|
|
|
6
6
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
7
7
|
* @license MIT License
|
|
8
8
|
*/
|
|
9
|
-
var __extends = (this && this.__extends) || (function () {
|
|
10
|
-
var extendStatics = function (d, b) {
|
|
11
|
-
extendStatics = Object.setPrototypeOf ||
|
|
12
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
13
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
14
|
-
return extendStatics(d, b);
|
|
15
|
-
};
|
|
16
|
-
return function (d, b) {
|
|
17
|
-
if (typeof b !== "function" && b !== null)
|
|
18
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
19
|
-
extendStatics(d, b);
|
|
20
|
-
function __() { this.constructor = d; }
|
|
21
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
22
|
-
};
|
|
23
|
-
})();
|
|
24
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
10
|
exports.BinaryTree = exports.BinaryTreeNode = void 0;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
function BinaryTreeNode() {
|
|
30
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
31
|
-
}
|
|
32
|
-
return BinaryTreeNode;
|
|
33
|
-
}(abstract_binary_tree_1.AbstractBinaryTreeNode));
|
|
11
|
+
const abstract_binary_tree_1 = require("./abstract-binary-tree");
|
|
12
|
+
class BinaryTreeNode extends abstract_binary_tree_1.AbstractBinaryTreeNode {
|
|
13
|
+
}
|
|
34
14
|
exports.BinaryTreeNode = BinaryTreeNode;
|
|
35
|
-
|
|
36
|
-
__extends(BinaryTree, _super);
|
|
15
|
+
class BinaryTree extends abstract_binary_tree_1.AbstractBinaryTree {
|
|
37
16
|
/**
|
|
38
17
|
* This is a constructor function for a binary tree class that takes an optional options parameter.
|
|
39
18
|
* @param {BinaryTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
40
19
|
* constructor of the `BinaryTree` class. It allows you to customize the behavior of the binary tree by providing
|
|
41
20
|
* different configuration options.
|
|
42
21
|
*/
|
|
43
|
-
|
|
44
|
-
|
|
22
|
+
constructor(options) {
|
|
23
|
+
super(options);
|
|
45
24
|
}
|
|
46
25
|
/**
|
|
47
26
|
* The function creates a new binary tree node with an optional value.
|
|
@@ -51,9 +30,8 @@ var BinaryTree = /** @class */ (function (_super) {
|
|
|
51
30
|
* stored in the node.
|
|
52
31
|
* @returns a new instance of a BinaryTreeNode with the specified id and value.
|
|
53
32
|
*/
|
|
54
|
-
|
|
33
|
+
createNode(id, val) {
|
|
55
34
|
return new BinaryTreeNode(id, val);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
}(abstract_binary_tree_1.AbstractBinaryTree));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
59
37
|
exports.BinaryTree = BinaryTree;
|