data-structure-typed 0.8.6
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/.idea/data-structure-typed.iml +12 -0
- package/.idea/modules.xml +8 -0
- package/.idea/vcs.xml +6 -0
- package/README.md +2 -0
- package/dist/data-structures/binary-tree/aa-tree.js +6 -0
- package/dist/data-structures/binary-tree/avl-tree.js +231 -0
- package/dist/data-structures/binary-tree/b-tree.js +6 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +31 -0
- package/dist/data-structures/binary-tree/binary-tree.js +992 -0
- package/dist/data-structures/binary-tree/bst.js +431 -0
- package/dist/data-structures/binary-tree/index.js +20 -0
- package/dist/data-structures/binary-tree/rb-tree.js +6 -0
- package/dist/data-structures/binary-tree/segment-tree.js +151 -0
- package/dist/data-structures/binary-tree/splay-tree.js +6 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +16 -0
- package/dist/data-structures/binary-tree/two-three-tree.js +6 -0
- package/dist/data-structures/graph/abstract-graph.js +648 -0
- package/dist/data-structures/graph/directed-graph.js +268 -0
- package/dist/data-structures/graph/index.js +19 -0
- package/dist/data-structures/graph/undirected-graph.js +142 -0
- package/dist/data-structures/hash/coordinate-map.js +24 -0
- package/dist/data-structures/hash/coordinate-set.js +21 -0
- package/dist/data-structures/hash/hash-table.js +2 -0
- package/dist/data-structures/hash/index.js +17 -0
- package/dist/data-structures/hash/pair.js +2 -0
- package/dist/data-structures/hash/tree-map.js +2 -0
- package/dist/data-structures/hash/tree-set.js +2 -0
- package/dist/data-structures/heap/heap.js +114 -0
- package/dist/data-structures/heap/index.js +19 -0
- package/dist/data-structures/heap/max-heap.js +22 -0
- package/dist/data-structures/heap/min-heap.js +22 -0
- package/dist/data-structures/index.js +25 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +259 -0
- package/dist/data-structures/linked-list/index.js +18 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +660 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -0
- package/dist/data-structures/matrix/index.js +19 -0
- package/dist/data-structures/matrix/matrix.js +14 -0
- package/dist/data-structures/matrix/matrix2d.js +119 -0
- package/dist/data-structures/matrix/navigator.js +78 -0
- package/dist/data-structures/matrix/vector2d.js +161 -0
- package/dist/data-structures/priority-queue/index.js +19 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +15 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +15 -0
- package/dist/data-structures/priority-queue/priority-queue.js +174 -0
- package/dist/data-structures/queue/deque.js +132 -0
- package/dist/data-structures/queue/index.js +17 -0
- package/dist/data-structures/queue/queue.js +113 -0
- package/dist/data-structures/stack/index.js +17 -0
- package/dist/data-structures/stack/stack.js +97 -0
- package/dist/data-structures/trampoline.js +52 -0
- package/dist/data-structures/trie/index.js +17 -0
- package/dist/data-structures/trie/trie.js +141 -0
- package/dist/index.js +17 -0
- package/dist/types/data-structures/binary-tree/aa-tree.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +21 -0
- package/dist/types/data-structures/binary-tree/b-tree.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +140 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +32 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +33 -0
- package/dist/types/data-structures/binary-tree/splay-tree.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +11 -0
- package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +2 -0
- package/dist/types/data-structures/graph/abstract-graph.d.ts +126 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +51 -0
- package/dist/types/data-structures/graph/index.d.ts +3 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +24 -0
- package/dist/types/data-structures/hash/coordinate-map.d.ts +8 -0
- package/dist/types/data-structures/hash/coordinate-set.d.ts +7 -0
- package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
- package/dist/types/data-structures/hash/index.d.ts +1 -0
- package/dist/types/data-structures/hash/pair.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
- package/dist/types/data-structures/heap/heap.d.ts +72 -0
- package/dist/types/data-structures/heap/index.d.ts +3 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +14 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +14 -0
- package/dist/types/data-structures/index.d.ts +9 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +59 -0
- package/dist/types/data-structures/linked-list/index.d.ts +2 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +358 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.d.ts +3 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +9 -0
- package/dist/types/data-structures/matrix/matrix2d.d.ts +25 -0
- package/dist/types/data-structures/matrix/navigator.d.ts +31 -0
- package/dist/types/data-structures/matrix/vector2d.d.ts +74 -0
- package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +4 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +4 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +36 -0
- package/dist/types/data-structures/queue/deque.d.ts +37 -0
- package/dist/types/data-structures/queue/index.d.ts +1 -0
- package/dist/types/data-structures/queue/queue.d.ts +76 -0
- package/dist/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/data-structures/stack/stack.d.ts +69 -0
- package/dist/types/data-structures/trampoline.d.ts +25 -0
- package/dist/types/data-structures/trie/index.d.ts +1 -0
- package/dist/types/data-structures/trie/trie.d.ts +28 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +17 -0
- package/dist/types/types/index.d.ts +1 -0
- package/dist/types/types/utils.d.ts +46 -0
- package/dist/types/utils.d.ts +122 -0
- package/dist/types/utils.js +53 -0
- package/dist/utils.js +569 -0
- package/package.json +75 -0
- package/src/data-structures/binary-tree/aa-tree.ts +3 -0
- package/src/data-structures/binary-tree/avl-tree.ts +232 -0
- package/src/data-structures/binary-tree/b-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +33 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1088 -0
- package/src/data-structures/binary-tree/bst.ts +404 -0
- package/src/data-structures/binary-tree/index.ts +4 -0
- package/src/data-structures/binary-tree/rb-tree.ts +3 -0
- package/src/data-structures/binary-tree/segment-tree.ts +164 -0
- package/src/data-structures/binary-tree/splay-tree.ts +3 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +21 -0
- package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
- package/src/data-structures/graph/abstract-graph.ts +789 -0
- package/src/data-structures/graph/directed-graph.ts +322 -0
- package/src/data-structures/graph/index.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +154 -0
- package/src/data-structures/hash/coordinate-map.ts +24 -0
- package/src/data-structures/hash/coordinate-set.ts +20 -0
- package/src/data-structures/hash/hash-table.ts +1 -0
- package/src/data-structures/hash/index.ts +1 -0
- package/src/data-structures/hash/pair.ts +1 -0
- package/src/data-structures/hash/tree-map.ts +1 -0
- package/src/data-structures/hash/tree-set.ts +1 -0
- package/src/data-structures/heap/heap.ts +136 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +22 -0
- package/src/data-structures/heap/min-heap.ts +24 -0
- package/src/data-structures/index.ts +10 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +258 -0
- package/src/data-structures/linked-list/index.ts +2 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +750 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/data-structures/matrix/index.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +13 -0
- package/src/data-structures/matrix/matrix2d.ts +125 -0
- package/src/data-structures/matrix/navigator.ts +99 -0
- package/src/data-structures/matrix/vector2d.ts +189 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -0
- package/src/data-structures/priority-queue/priority-queue.ts +208 -0
- package/src/data-structures/queue/deque.ts +139 -0
- package/src/data-structures/queue/index.ts +1 -0
- package/src/data-structures/queue/queue.ts +123 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +104 -0
- package/src/data-structures/trampoline.ts +91 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +153 -0
- package/src/index.ts +1 -0
- package/src/types/index.ts +1 -0
- package/src/types/patches/index.d.ts +0 -0
- package/src/types/utils.ts +158 -0
- package/src/utils.ts +605 -0
- package/tsconfig.json +52 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<module type="WEB_MODULE" version="4">
|
|
3
|
+
<component name="NewModuleRootManager">
|
|
4
|
+
<content url="file://$MODULE_DIR$">
|
|
5
|
+
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
|
6
|
+
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
|
7
|
+
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
|
8
|
+
</content>
|
|
9
|
+
<orderEntry type="inheritedJdk" />
|
|
10
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
|
11
|
+
</component>
|
|
12
|
+
</module>
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<project version="4">
|
|
3
|
+
<component name="ProjectModuleManager">
|
|
4
|
+
<modules>
|
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/data-structure-ts.iml" filepath="$PROJECT_DIR$/.idea/data-structure-ts.iml" />
|
|
6
|
+
</modules>
|
|
7
|
+
</component>
|
|
8
|
+
</project>
|
package/.idea/vcs.xml
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
# data-structure-ts
|
|
2
|
+
Hash (CoordinateSet, CoordinateMap) Heap (MaxHeap, MinHeap) Binary Tree (AVL Tree, Binary Indexed Tree, Binary Search Tree, Segment Tree, Tree Multiset) Graph (Directed Graph, Undirected Graph) Linked List (Singly Linked List, Doubly Linked List) Matrix Priority Queue (Max Priority Queue, Min Priority Queue) Queue (Queue, Dequeue) Stack Trie
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AVLTree = exports.AVLTreeNode = void 0;
|
|
4
|
+
const bst_1 = require("./bst");
|
|
5
|
+
class AVLTreeNode extends bst_1.BSTNode {
|
|
6
|
+
clone() {
|
|
7
|
+
return new AVLTreeNode(this.id, this.val, this.count);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.AVLTreeNode = AVLTreeNode;
|
|
11
|
+
class AVLTree extends bst_1.BST {
|
|
12
|
+
createNode(id, val, count) {
|
|
13
|
+
return new AVLTreeNode(id, val, count);
|
|
14
|
+
}
|
|
15
|
+
put(id, val, count) {
|
|
16
|
+
const inserted = super.put(id, val, count);
|
|
17
|
+
if (inserted)
|
|
18
|
+
this.balancePath(inserted);
|
|
19
|
+
return inserted;
|
|
20
|
+
}
|
|
21
|
+
remove(id, isUpdateAllLeftSum) {
|
|
22
|
+
const deletedResults = super.remove(id, isUpdateAllLeftSum);
|
|
23
|
+
for (const { needBalanced } of deletedResults) {
|
|
24
|
+
if (needBalanced) {
|
|
25
|
+
this.balancePath(needBalanced);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return deletedResults;
|
|
29
|
+
}
|
|
30
|
+
balanceFactor(node) {
|
|
31
|
+
if (!node.right) // node has no right subtree
|
|
32
|
+
return -node.height;
|
|
33
|
+
else if (!node.left) // node has no left subtree
|
|
34
|
+
return +node.height;
|
|
35
|
+
else
|
|
36
|
+
return node.right.height - node.left.height;
|
|
37
|
+
}
|
|
38
|
+
updateHeight(node) {
|
|
39
|
+
if (!node.left && !node.right) // node is a leaf
|
|
40
|
+
node.height = 0;
|
|
41
|
+
else if (!node.left) {
|
|
42
|
+
// node has no left subtree
|
|
43
|
+
const rightHeight = node.right ? node.right.height : 0;
|
|
44
|
+
node.height = 1 + rightHeight;
|
|
45
|
+
}
|
|
46
|
+
else if (!node.right) // node has no right subtree
|
|
47
|
+
node.height = 1 + node.left.height;
|
|
48
|
+
else
|
|
49
|
+
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
50
|
+
}
|
|
51
|
+
balancePath(node) {
|
|
52
|
+
const path = this.getPathToRoot(node);
|
|
53
|
+
for (let i = path.length - 1; i >= 0; i--) {
|
|
54
|
+
const A = path[i];
|
|
55
|
+
this.updateHeight(A);
|
|
56
|
+
switch (this.balanceFactor(A)) {
|
|
57
|
+
case -2:
|
|
58
|
+
if (A && A.left) {
|
|
59
|
+
if (this.balanceFactor(A.left) <= 0) {
|
|
60
|
+
this.balanceLL(A); // Perform LL rotation
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
this.balanceLR(A); // Perform LR rotation
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
67
|
+
case +2:
|
|
68
|
+
if (A && A.right) {
|
|
69
|
+
if (this.balanceFactor(A.right) >= 0) {
|
|
70
|
+
this.balanceRR(A); // Perform RR rotation
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
this.balanceRL(A); // Perform RL rotation
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
balanceLL(A) {
|
|
80
|
+
const parentOfA = A.parent;
|
|
81
|
+
const B = A.left; // A is left-heavy and B is left-heavy
|
|
82
|
+
A.parent = B;
|
|
83
|
+
if (B && B.right) {
|
|
84
|
+
B.right.parent = A;
|
|
85
|
+
}
|
|
86
|
+
if (B)
|
|
87
|
+
B.parent = parentOfA;
|
|
88
|
+
if (A === this.root) {
|
|
89
|
+
if (B)
|
|
90
|
+
this.root = B;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
if ((parentOfA === null || parentOfA === void 0 ? void 0 : parentOfA.left) === A) {
|
|
94
|
+
parentOfA.left = B;
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
if (parentOfA)
|
|
98
|
+
parentOfA.right = B;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (B) {
|
|
102
|
+
A.left = B.right; // Make T2 the left subtree of A
|
|
103
|
+
B.right = A; // Make A the left child of B
|
|
104
|
+
}
|
|
105
|
+
this.updateHeight(A);
|
|
106
|
+
if (B)
|
|
107
|
+
this.updateHeight(B);
|
|
108
|
+
}
|
|
109
|
+
balanceLR(A) {
|
|
110
|
+
const parentOfA = A.parent;
|
|
111
|
+
const B = A.left; // A is left-heavy
|
|
112
|
+
let C = null;
|
|
113
|
+
if (B) {
|
|
114
|
+
C = B.right; // B is right-heavy
|
|
115
|
+
}
|
|
116
|
+
if (A)
|
|
117
|
+
A.parent = C;
|
|
118
|
+
if (B)
|
|
119
|
+
B.parent = C;
|
|
120
|
+
if (C) {
|
|
121
|
+
if (C.left) {
|
|
122
|
+
C.left.parent = B;
|
|
123
|
+
}
|
|
124
|
+
if (C.right) {
|
|
125
|
+
C.right.parent = A;
|
|
126
|
+
}
|
|
127
|
+
C.parent = parentOfA;
|
|
128
|
+
}
|
|
129
|
+
if (A === this.root) {
|
|
130
|
+
if (C)
|
|
131
|
+
this.root = C;
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
if (parentOfA) {
|
|
135
|
+
if (parentOfA.left === A) {
|
|
136
|
+
parentOfA.left = C;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
parentOfA.right = C;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (C) {
|
|
144
|
+
A.left = C.right; // Make T3 the left subtree of A
|
|
145
|
+
if (B)
|
|
146
|
+
B.right = C.left; // Make T2 the right subtree of B
|
|
147
|
+
C.left = B;
|
|
148
|
+
C.right = A;
|
|
149
|
+
}
|
|
150
|
+
this.updateHeight(A); // Adjust heights
|
|
151
|
+
B && this.updateHeight(B);
|
|
152
|
+
C && this.updateHeight(C);
|
|
153
|
+
}
|
|
154
|
+
balanceRR(A) {
|
|
155
|
+
const parentOfA = A.parent;
|
|
156
|
+
const B = A.right; // A is right-heavy and B is right-heavy
|
|
157
|
+
A.parent = B;
|
|
158
|
+
if (B) {
|
|
159
|
+
if (B.left) {
|
|
160
|
+
B.left.parent = A;
|
|
161
|
+
}
|
|
162
|
+
B.parent = parentOfA;
|
|
163
|
+
}
|
|
164
|
+
if (A === this.root) {
|
|
165
|
+
if (B)
|
|
166
|
+
this.root = B;
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
if (parentOfA) {
|
|
170
|
+
if (parentOfA.left === A) {
|
|
171
|
+
parentOfA.left = B;
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
parentOfA.right = B;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (B) {
|
|
179
|
+
A.right = B.left; // Make T2 the right subtree of A
|
|
180
|
+
B.left = A;
|
|
181
|
+
}
|
|
182
|
+
this.updateHeight(A);
|
|
183
|
+
B && this.updateHeight(B);
|
|
184
|
+
}
|
|
185
|
+
balanceRL(A) {
|
|
186
|
+
const parentOfA = A.parent;
|
|
187
|
+
const B = A.right; // A is right-heavy
|
|
188
|
+
let C = null;
|
|
189
|
+
if (B) {
|
|
190
|
+
C = B.left; // B is left-heavy
|
|
191
|
+
}
|
|
192
|
+
A.parent = C;
|
|
193
|
+
if (B)
|
|
194
|
+
B.parent = C;
|
|
195
|
+
if (C) {
|
|
196
|
+
if (C.left) {
|
|
197
|
+
C.left.parent = A;
|
|
198
|
+
}
|
|
199
|
+
if (C.right) {
|
|
200
|
+
C.right.parent = B;
|
|
201
|
+
}
|
|
202
|
+
C.parent = parentOfA;
|
|
203
|
+
}
|
|
204
|
+
if (A === this.root) {
|
|
205
|
+
if (C)
|
|
206
|
+
this.root = C;
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
if (parentOfA) {
|
|
210
|
+
if (parentOfA.left === A) {
|
|
211
|
+
parentOfA.left = C;
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
parentOfA.right = C;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if (C)
|
|
219
|
+
A.right = C.left; // Make T2 the right subtree of A
|
|
220
|
+
if (B && C)
|
|
221
|
+
B.left = C.right; // Make T3 the left subtree of B
|
|
222
|
+
if (C)
|
|
223
|
+
C.left = A;
|
|
224
|
+
if (C)
|
|
225
|
+
C.right = B;
|
|
226
|
+
this.updateHeight(A); // Adjust heights
|
|
227
|
+
B && this.updateHeight(B);
|
|
228
|
+
C && this.updateHeight(C);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
exports.AVLTree = AVLTree;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BinaryIndexedTree = void 0;
|
|
4
|
+
class BinaryIndexedTree {
|
|
5
|
+
constructor(n) {
|
|
6
|
+
this._sumTree = new Array(n + 1).fill(0);
|
|
7
|
+
}
|
|
8
|
+
update(i, delta) {
|
|
9
|
+
while (i < this._sumTree.length) {
|
|
10
|
+
this._sumTree[i] += delta;
|
|
11
|
+
i += BinaryIndexedTree.lowBit(i);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
getPrefixSum(i) {
|
|
15
|
+
let sum = 0;
|
|
16
|
+
while (i > 0) {
|
|
17
|
+
sum += this._sumTree[i];
|
|
18
|
+
i -= BinaryIndexedTree.lowBit(i);
|
|
19
|
+
}
|
|
20
|
+
return sum;
|
|
21
|
+
}
|
|
22
|
+
getRangeSum(start, end) {
|
|
23
|
+
if (!(0 <= start && start <= end && end <= this._sumTree.length))
|
|
24
|
+
throw 'Index out of bounds';
|
|
25
|
+
return this.getPrefixSum(end) - this.getPrefixSum(start);
|
|
26
|
+
}
|
|
27
|
+
static lowBit(x) {
|
|
28
|
+
return x & (-x);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.BinaryIndexedTree = BinaryIndexedTree;
|