Skip to content

Fenwick Tree: Rewrite to focus on 1-based indexing #1346

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

Open
jxu opened this issue Oct 9, 2024 · 5 comments
Open

Fenwick Tree: Rewrite to focus on 1-based indexing #1346

jxu opened this issue Oct 9, 2024 · 5 comments

Comments

@jxu
Copy link
Contributor

jxu commented Oct 9, 2024

Article: Fenwick Tree

Problem:
Would you support a rewrite of the article to introduce 1-based indexing first? I believe this would simplify the exposition a lot (it did for me). That's also how Fenwick introduces it in his original paper https://web.archive.org/web/20160307203033/http://olds.blogcn.com/wp-content/uploads/67/6754/2009/01/bit.pdf
Apparently the getsum index operation i -= i & -i can also be written i &= i-1

Btw, wikipedia says the BIT was first introduced by Boris Ryabko in 1989, but I can't actually read it (and it doesn't have nice diagrams) https://web.archive.org/web/20190717074430/http://boris.ryabko.net/dan1989.pdf (English https://boris.ryabko.net/[ryabko1992.pdf](https://boris.ryabko.net/ryabko1992.pdf))

@mhayter
Copy link
Contributor

mhayter commented Oct 11, 2024

Lol no one's updated the website in months. Was I right it was I right?

No one's gonna make additions. Instead submit a pull request and pray it gets review on the next year

@adamant-pwn
Copy link
Member

Hi! It's actually mentioned in the article here. But I generally agree that the code with 0-based Fenwick is pretty ugly + makes our life harder e.g. when we want to treat all segments uniformly but we can't because of the lacks of the value for the zero-length prefix. That being said, feel free to make a pull request, I think overall it would be a welcomed addition.

@UtsavKash19
Copy link

#include
#include

class FenwickTree {
private:
std::vector tree;
int size;

public:
// Constructor to initialize the Fenwick Tree with the given size
FenwickTree(int n) {
size = n;
tree.resize(n + 1, 0); // 1-based indexing, so we allocate n + 1
}

// Update the value at index `index` by `value`
void update(int index, int value) {
    for (; index <= size; index += index & -index) {
        tree[index] += value;
    }
}

// Query the prefix sum from 1 to `index`
int query(int index) const {
    int sum = 0;
    for (; index > 0; index -= index & -index) {
        sum += tree[index];
    }
    return sum;
}

// Query the sum in the range [left, right]
int rangeQuery(int left, int right) const {
    return query(right) - query(left - 1);
}

};

int main() {
int n = 10; // Size of the Fenwick Tree
FenwickTree fenwick(n);

// Example updates
fenwick.update(1, 5);   // Add 5 at index 1
fenwick.update(2, 3);   // Add 3 at index 2
fenwick.update(3, 7);   // Add 7 at index 3

// Query the sum from 1 to 3
std::cout << "Sum from 1 to 3: " << fenwick.query(3) << std::endl; // Output: 15

// Query the sum in range [2, 3]
std::cout << "Sum from 2 to 3: " << fenwick.rangeQuery(2, 3) << std::endl; // Output: 10

return 0;

}

@adamant-pwn
Copy link
Member

@UtsavKashyap1710 thanks for the code, but issues section is only for text discussions, actual contribuitions should be done via pull requests.

@adamant-pwn
Copy link
Member

Also take a note of #1298, which addresses a similar concern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants