New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat : add fenwick tree 2D data structure, struct fenwick2D #1230
base: master
Are you sure you want to change the base?
Conversation
Review existing code and standards: https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/fenwick_tree.cpp |
Changes have been made. |
code looks fine but documentation is not up to standard |
changes have been made in the documentation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation is still not up to the repository standards.
Please see the typical structure of a program, that might help. 🙂
this seems to be stale |
#include <iostream> | ||
#include <cassert> | ||
#include <vector> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include <iostream> | |
#include <cassert> | |
#include <vector> | |
#include <iostream> /// for I/O operations | |
#include <cassert> /// for assert | |
#include <vector> /// for std::vector |
needs documentation
@@ -0,0 +1,129 @@ | |||
#include <iostream> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
document the header
int n = 5; | ||
|
||
fenwick2D<int> fenwick_tree_2D(n); | ||
|
||
fenwick_tree_2D.update(1, 1, 2); | ||
fenwick_tree_2D.update(1, 2, 3); | ||
fenwick_tree_2D.update(2, 1, 3); | ||
fenwick_tree_2D.update(1, 3, 4); | ||
fenwick_tree_2D.update(2, 2, 4); | ||
fenwick_tree_2D.update(3, 1, 4); | ||
fenwick_tree_2D.update(1, 4, 5); | ||
fenwick_tree_2D.update(2, 3, 5); | ||
fenwick_tree_2D.update(3, 2, 5); | ||
fenwick_tree_2D.update(3, 3, 6); | ||
|
||
assert(fenwick_tree_2D.query(1, 1, 2, 2) == 12); | ||
assert(fenwick_tree_2D.query(1, 2, 3, 3) == 27); | ||
fenwick_tree_2D.update(1, 1, -3); | ||
assert(fenwick_tree_2D.query(1, 1, 2, 2) == 9); | ||
assert(fenwick_tree_2D.query(1, 2, 3, 3) == 27); | ||
|
||
std::cout << "All Test Passed\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be moved to a function
void test() {};
} | ||
|
||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/** | |
* @brief Main function | |
* @returns 0 on exit | |
*/ |
} | ||
|
||
// Calculates a prefix sum till index idx | ||
T sum(int idx) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might want to check if idx <= siz
|
||
// update the values at idx by delta | ||
// update all the indices which include idx in their sum | ||
void update(int idx, T delta) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
similarly, could check the bounds of idx
} | ||
|
||
// find sum in range l to r | ||
T query(int l, int r) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
finally, here are even more implicit assumptions on the input, 0 < l <= r <= siz
(I believe)
return an; | ||
} | ||
|
||
// sum of region {x1 to x} and {y1 to y} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: For clarity, I think it would be better to call them x1
, x2
and y1
, y2
Description of Change
Checklist
Notes: