Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fc63bb6

Browse files
committed
Add tests for Fenwick tree + remove bug
1 parent fd6a2c2 commit fc63bb6

File tree

3 files changed

+99
-5
lines changed

3 files changed

+99
-5
lines changed

src/data_structures/fenwick.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ This is handled in the `sum(int l, int r)` method.
127127
Also this implementation supports two constructors.
128128
You can create a Fenwick tree initialized with zeros, or you can convert an existing array into the Fenwick form.
129129

130-
```cpp
130+
```cpp fenwick_sum
131131
struct FenwickTree {
132132
vector<int> bit; // binary indexed tree
133133
int n;
@@ -166,7 +166,7 @@ It is obvious that there is no easy way of finding minimum of range $[l; r]$ usi
166166
Additionally, each time a value is `update`'d, the new value has to be smaller than the current value (because the $min$ function is not reversible).
167167
These, of course, are significant limitations.
168168

169-
```cpp
169+
```cpp fenwick_min
170170
struct FenwickTreeMin {
171171
vector<int> bit;
172172
int n;
@@ -266,7 +266,7 @@ As you can see, the main benefit of this approach is that the binary operations
266266

267267
The following implementation can be used like the other implementations, however it uses one-based indexing internally.
268268

269-
```cpp
269+
```cpp fenwick_sum_onebased
270270
struct FenwickTreeOneBasedIndexing {
271271
vector<int> bit; // binary indexed tree
272272
int n;
@@ -278,7 +278,6 @@ struct FenwickTreeOneBasedIndexing {
278278

279279
FenwickTreeOneBasedIndexing(vector<int> a)
280280
: FenwickTreeOneBasedIndexing(a.size()) {
281-
init(a.size());
282281
for (size_t i = 0; i < a.size(); i++)
283282
add(i, a[i]);
284283
}

test/test_fenwick_tree.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <cassert>
2+
#include <algorithm>
3+
#include <vector>
4+
using namespace std;
5+
6+
namespace Fenwick_Sum {
7+
#include "fenwick_sum.h"
8+
}
9+
10+
namespace Fenwick_Min {
11+
#include "fenwick_min.h"
12+
}
13+
14+
namespace Fenwick_Sum_Onebased {
15+
#include "fenwick_sum_onebased.h"
16+
}
17+
18+
19+
void test_fenwick_sum() {
20+
using namespace Fenwick_Sum;
21+
vector<int> a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
22+
int n = a.size();
23+
FenwickTree tree(a);
24+
for (int i = 0; i < n; i++) {
25+
int expected_sum = 0;
26+
for (int j = i; j < n; j++) {
27+
expected_sum += a[j];
28+
assert(expected_sum == tree.sum(i, j));
29+
}
30+
}
31+
for (int k = 0; k < n; k++) {
32+
tree.add(k, 100);
33+
a[k] += 100;
34+
for (int i = 0; i < n; i++) {
35+
int expected_sum = 0;
36+
for (int j = i; j < n; j++) {
37+
expected_sum += a[j];
38+
assert(expected_sum == tree.sum(i, j));
39+
}
40+
}
41+
}
42+
}
43+
44+
void test_fenwick_min() {
45+
using namespace Fenwick_Min;
46+
vector<int> a = {36, 22, 69, 86, 9, 30, 69, 91, 94, 64};
47+
int n = a.size();
48+
FenwickTreeMin tree(a);
49+
int expected_min = std::numeric_limits<int>::max();
50+
for (int j = 0; j < n; j++) {
51+
expected_min = min(expected_min, a[j]);
52+
assert(expected_min == tree.getmin(j));
53+
}
54+
vector<int> b = {10, 10, 9, 4, 4, 8, 0, 4, 9, 1};
55+
for (int k = 0; k < n; k++) {
56+
tree.update(k, b[k]);
57+
a[k] = b[k];
58+
int expected_min = std::numeric_limits<int>::max();
59+
for (int j = 0; j < n; j++) {
60+
expected_min = min(expected_min, a[j]);
61+
assert(expected_min == tree.getmin(j));
62+
}
63+
}
64+
}
65+
66+
void test_fenwick_sum_onebased() {
67+
using namespace Fenwick_Sum_Onebased;
68+
vector<int> a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
69+
int n = a.size();
70+
FenwickTreeOneBasedIndexing tree(a);
71+
for (int i = 0; i < n; i++) {
72+
int expected_sum = 0;
73+
for (int j = i; j < n; j++) {
74+
expected_sum += a[j];
75+
assert(expected_sum == tree.sum(i, j));
76+
}
77+
}
78+
for (int k = 0; k < n; k++) {
79+
tree.add(k, 100);
80+
a[k] += 100;
81+
for (int i = 0; i < n; i++) {
82+
int expected_sum = 0;
83+
for (int j = i; j < n; j++) {
84+
expected_sum += a[j];
85+
assert(expected_sum == tree.sum(i, j));
86+
}
87+
}
88+
}
89+
}
90+
91+
int main() {
92+
test_fenwick_sum();
93+
test_fenwick_min();
94+
test_fenwick_sum_onebased();
95+
}

test/test_segment_tree.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void test_section_implementation() {
5353
assert(expected_sum == sum(1, 0, n-1, i, j));
5454
}
5555
}
56-
for (int k = 0; k < 0; k++) {
56+
for (int k = 0; k < n; k++) {
5757
update(1, 0, n-1, k, 100);
5858
for (int i = 0; i < n; i++) {
5959
int expected_sum = 0;

0 commit comments

Comments
 (0)