From d9fc87896213c3fb562fb6fadb68821310f5ced7 Mon Sep 17 00:00:00 2001 From: matgrz1993 Date: Sun, 21 Jun 2020 21:41:08 +0200 Subject: [PATCH] fix: Remove FenwickTree (#856) * Remove FenwickTree FenwickTree is the same Data Structure as Binary Indexed Tree located in file "range_queries/bit.cpp" so it could be removed. * Fix cpplint errors * docs: Update documentation * docs: Update FenwickTree documentation * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- DIRECTORY.md | 3 +- range_queries/bit.cpp | 60 ------------------------- range_queries/fenwick_tree.cpp | 82 ++++++++++++++++++++++++++++++++++ range_queries/fenwicktree.cpp | 54 ---------------------- 4 files changed, 83 insertions(+), 116 deletions(-) delete mode 100644 range_queries/bit.cpp create mode 100644 range_queries/fenwick_tree.cpp delete mode 100644 range_queries/fenwicktree.cpp diff --git a/DIRECTORY.md b/DIRECTORY.md index 9cb6abcfa..4989050df 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -181,8 +181,7 @@ * [Poisson Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/probability/poisson_dist.cpp) ## Range Queries - * [Bit](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/bit.cpp) - * [Fenwicktree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/fenwicktree.cpp) + * [Fenwick Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/fenwick_tree.cpp) * [Mo](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/mo.cpp) * [Segtree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/segtree.cpp) diff --git a/range_queries/bit.cpp b/range_queries/bit.cpp deleted file mode 100644 index a1878705b..000000000 --- a/range_queries/bit.cpp +++ /dev/null @@ -1,60 +0,0 @@ -// Binary Indexed Tree. -#include - -using namespace std; - -class Bit { - int n; - vector bit; - inline int offset(int x) { return (x & (-x)); } - - public: - Bit(vector& arr) { - n = arr.size(); - bit.assign(n + 1, 0); - for (int i = 0; i < n; ++i) { - update(i, arr[i]); - } - } - Bit(int x) { - n = x; - bit.assign(n + 1, 0); - } - - void update(int id, int val) { - // Add val at id - id++; - while (id <= n) { - bit[id] += val; - id += offset(id); - } - } - - int sum(int id) { - // Get prefix sum upto id. - id++; - int res = 0; - while (id > 0) { - res += bit[id]; - id -= offset(id); - } - return res; - } - - int sum_range(int l, int r) { return sum(r) - sum(l - 1); } -}; - -int main() { - int n = 5; - vector arr = {1, 2, 3, 4, 5}; - Bit x(arr); - - assert(x.sum_range(0, 0) == 1); - assert(x.sum_range(0, 1) == 3); - assert(x.sum_range(0, 2) == 6); - x.update(0, 6); - assert(x.sum_range(0, 0) == 6); - assert(x.sum_range(0, 1) == 8); - assert(x.sum_range(0, 2) == 11); - return 0; -} diff --git a/range_queries/fenwick_tree.cpp b/range_queries/fenwick_tree.cpp new file mode 100644 index 000000000..a2498dc6a --- /dev/null +++ b/range_queries/fenwick_tree.cpp @@ -0,0 +1,82 @@ +/** + * @file + * @brief Fenwick tree + * + * A Fenwick tree or binary indexed tree is a data structure + * that can efficiently update elements and calculate + * prefix sums in a table of numbers. + */ +#include +#include +#include + +/** + * n --> No. of elements present in input array. + * bit[0..n] --> Array that represents Binary Indexed Tree. + */ +class FenwickTree { + int n; + std::vector bit; + + /** Returns the highest power of two which is not more than x */ + inline int offset(int x) { return (x & (-x)); } + + public: + /** Constructor + * \param[in] arr --> Input array for which prefix sum is evaluated. + */ + explicit FenwickTree(const std::vector& arr) { + n = arr.size(); + bit.assign(n + 1, 0); + for (int i = 0; i < n; ++i) { + update(i, arr[i]); + } + } + + /** Constructor + * \param[in] x --> Size of array that represents Binary Indexed Tree. + */ + explicit FenwickTree(int x) { + n = x; + bit.assign(n + 1, 0); + } + + /** Add val at id */ + void update(int id, int val) { + id++; + while (id <= n) { + bit[id] += val; + id += offset(id); + } + } + + /** Get prefix sum upto id */ + int sum(int id) { + id++; + int res = 0; + while (id > 0) { + res += bit[id]; + id -= offset(id); + } + return res; + } + + /** Returns the prefix sum in range from l to r */ + int sum_range(int l, int r) { return sum(r) - sum(l - 1); } +}; + +/** Main function */ +int main() { + int n = 5; + std::vector arr = {1, 2, 3, 4, 5}; + FenwickTree fenwick_tree(arr); + + assert(fenwick_tree.sum_range(0, 0) == 1); + assert(fenwick_tree.sum_range(0, 1) == 3); + assert(fenwick_tree.sum_range(0, 2) == 6); + fenwick_tree.update(0, 6); + assert(fenwick_tree.sum_range(0, 0) == 6); + assert(fenwick_tree.sum_range(0, 1) == 8); + assert(fenwick_tree.sum_range(0, 2) == 11); + return 0; +} diff --git a/range_queries/fenwicktree.cpp b/range_queries/fenwicktree.cpp deleted file mode 100644 index fb7cbaac4..000000000 --- a/range_queries/fenwicktree.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include -using namespace std; - -/** - * ` lowbit(x) ` aims to find the last 1 in binary of a positive number - * twos complement works good on this - * also using ` x - (x & (x - 1)) ` - */ -#define lowbit(x) (x & (-x)) - -const int maxn = 1e5 + 7; -int tree[maxn] = {0}, - range; // segement of [1...range], notice it must be less than `maxn` - -void update(int x, int c) { - while (x <= range) { - tree[x] += c; - x += lowbit(x); - } -} -int query(int x) { - int ans = 0; - while (x) { - ans += tree[x]; - x -= lowbit(x); - } - return ans; -} -int query_segement(int l, int r) { return query(r) - query(l - 1); } - -int main() { - cin >> range; - for (int i = 1; i <= range; i++) { - int num; - cin >> num; - update(i, num); - } - int q; - cin >> q; - while (q--) { - int op; - cin >> op; - if (op == 0) { - int l, r; - cin >> l >> r; - cout << query_segement(l, r) << endl; - } else { - int x, c; - cin >> x >> c; - update(x, c); - } - } - return 0; -}