Skip to content

Commit 79cbf4e

Browse files
committed
refactor: move the fenwick tree data structure to the project
1 parent c446ebc commit 79cbf4e

4 files changed

Lines changed: 56 additions & 19 deletions

File tree

data structures/Fenwick Tree 2D.rs renamed to competitive_programming/src/data_structures/fenwick_tree_2d.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
1-
/*
2-
* Author: Thiago Felipe Bastos da Silva
3-
* Created: 2025-12-28
4-
* Description: Simple Fenwick Tree 2D data structure for sum operations.
5-
*/
1+
use std::ops::{Add, AddAssign, Sub};
62

7-
struct FenwickTree2D<T> {
3+
pub struct FenwickTree2D<T> {
84
data: Vec<Vec<T>>,
9-
rows: usize,
10-
columns: usize
5+
pub rows: usize,
6+
pub columns: usize,
117
}
128

139
impl<T: Default + Copy + AddAssign + Sub<Output = T> + Add<Output = T>> FenwickTree2D<T> {
14-
1510
/**
1611
* create a new instance of FenwickTree2D
1712
* @param rows the number of rows of the matrix
1813
* @param columns the number of columns of the matrix
1914
* @return a new FenwickTree2D
2015
*/
21-
fn new(rows: usize, columns: usize) -> Self {
22-
16+
pub fn new(rows: usize, columns: usize) -> Self {
2317
Self {
24-
data: vec![vec![T::default(); columns + 1]; rows + 1],
25-
rows: rows,
26-
columns: columns
18+
data: vec![vec![T::default(); columns + 1]; rows + 1],
19+
rows,
20+
columns,
2721
}
2822
}
2923

@@ -49,14 +43,15 @@ impl<T: Default + Copy + AddAssign + Sub<Output = T> + Add<Output = T>> FenwickT
4943
x -= x & -x;
5044
}
5145

52-
return answer;
46+
answer
5347
}
5448

5549
/**
5650
* calculate the sum of all values inside the submatrix (x0, y0) x (x1, y1)
5751
*/
58-
fn calculate(&self, x0: i32, y0: i32, x1: i32, y1: i32) -> T {
59-
return self.query(x1, y1) - self.query(x1, y0 - 1) - self.query(x0 - 1, y1) + self.query(x0 - 1, y0 - 1);
52+
pub fn calculate(&self, x0: i32, y0: i32, x1: i32, y1: i32) -> T {
53+
self.query(x1, y1) - self.query(x1, y0 - 1) - self.query(x0 - 1, y1)
54+
+ self.query(x0 - 1, y0 - 1)
6055
}
6156

6257
/**
@@ -65,12 +60,11 @@ impl<T: Default + Copy + AddAssign + Sub<Output = T> + Add<Output = T>> FenwickT
6560
* @param y the column position of the matrix
6661
* @param value the number that will be added to position (x, y)
6762
*/
68-
fn update(&mut self, mut x: i32, mut y: i32, value: T) {
63+
pub fn update(&mut self, mut x: i32, mut y: i32, value: T) {
6964
x += 1;
7065
y += 1;
7166

7267
while x as usize <= self.rows {
73-
7468
let mut k = y;
7569

7670
while k as usize <= self.columns {

competitive_programming/src/data_structures/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
mod fenwick_tree;
2+
mod fenwick_tree_2d;
23
mod recursive_segment_tree;
34
mod segment_tree;
45
mod sparse_table;
56
mod union_find;
67

78
pub use fenwick_tree::{FenwickTree, FenwickTreeConstants};
9+
pub use fenwick_tree_2d::FenwickTree2D;
810
pub use recursive_segment_tree::SegTree as RecursiveSegTree;
911
pub use segment_tree::{SegTree, SegTreeConstants};
1012
pub use sparse_table::SparseTable;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use competitive_programming::data_structures::FenwickTree2D;
4+
5+
#[test]
6+
fn test_sum_query() {
7+
const R: i32 = 5;
8+
const C: i32 = 5;
9+
10+
let mut ft = FenwickTree2D::<i32>::new(R as usize, C as usize);
11+
12+
for x0 in 0..R {
13+
for x1 in x0..R {
14+
for y0 in 0..C {
15+
for y1 in y0..C {
16+
let sum = ft.calculate(x0, y0, x1, y1);
17+
assert_eq!(sum, 0);
18+
}
19+
}
20+
}
21+
}
22+
23+
for i in 0..R {
24+
for j in 0..C {
25+
ft.update(i, j, 1); // TODO add random numbers
26+
}
27+
}
28+
29+
for x0 in 0..R {
30+
for x1 in x0..R {
31+
for y0 in 0..C {
32+
for y1 in y0..C {
33+
let sum = ft.calculate(x0, y0, x1, y1);
34+
assert_eq!(sum, (x1 - x0 + 1) * (y1 - y0 + 1))
35+
}
36+
}
37+
}
38+
}
39+
}
40+
}

competitive_programming/tests/data_structures/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod fenwick_tree;
2+
pub mod fenwick_tree_2d;
23
pub mod recursive_segment_tree;
34
pub mod segment_tree;
45
pub mod sparse_table;

0 commit comments

Comments
 (0)