Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
range_queries::fenwick_tree Class Reference

The class that initializes the Fenwick Tree. More...

Collaboration diagram for range_queries::fenwick_tree:
[legend]

Public Member Functions

template<typename T >
 fenwick_tree (const std::vector< T > &arr)
 Class Constructor.
 
template<typename T >
 fenwick_tree (T x)
 Class Constructor.
 
template<typename T >
void update (T id, T val)
 Updates the value of an element in original array and accordingly updates the values in BIT array.
 
template<typename T >
int sum (T id)
 Returns the sum of elements in range from 0 to ID.
 
int sum_range (int l, int r)
 Returns the prefix sum in range from L to R.
 

Private Member Functions

int offset (int x)
 Returns the highest power of two which is not more than x.
 

Private Attributes

size_t n = 0
 No. of elements present in input array.
 
std::vector< int > bit {}
 Array that represents Binary Indexed Tree.
 

Detailed Description

The class that initializes the Fenwick Tree.

Constructor & Destructor Documentation

◆ fenwick_tree() [1/2]

template<typename T >
range_queries::fenwick_tree::fenwick_tree ( const std::vector< T > & arr)
inlineexplicit

Class Constructor.

Template Parameters
Tthe type of the array
Parameters
[in]arrInput array for which prefix sum is evaluated.
Returns
void
50 : n(arr.size()) {
51 bit.assign(n + 1, 0);
52 for (int i = 0; i < n; ++i) {
53 update(i, arr[i]);
54 }
55 }
T assign(T... args)
void update(T id, T val)
Updates the value of an element in original array and accordingly updates the values in BIT array.
Definition fenwick_tree.cpp:75
std::vector< int > bit
Array that represents Binary Indexed Tree.
Definition fenwick_tree.cpp:34
size_t n
No. of elements present in input array.
Definition fenwick_tree.cpp:33
T size(T... args)
Here is the call graph for this function:

◆ fenwick_tree() [2/2]

template<typename T >
range_queries::fenwick_tree::fenwick_tree ( T x)
inlineexplicit

Class Constructor.

Template Parameters
Tthe type of the variable
Parameters
[in]xSize of array that represents Binary Indexed Tree.
Returns
void
64: n(x) { bit.assign(n + 1, 0); }
Here is the call graph for this function:

Member Function Documentation

◆ offset()

int range_queries::fenwick_tree::offset ( int x)
inlineprivate

Returns the highest power of two which is not more than x.

Parameters
xIndex of element in original array.
Returns
Offset of index.
41{ return (x & (-x)); }

◆ sum()

template<typename T >
int range_queries::fenwick_tree::sum ( T id)
inline

Returns the sum of elements in range from 0 to ID.

Template Parameters
Tthe type of the variables
Parameters
idIndex in original array up to which sum is evaluated.
Returns
Sum of elements in range from 0 to id.
90 {
91 id++;
92 T res = 0;
93 while (id > 0) {
94 res += bit[id];
95 id -= offset(id);
96 }
97 return res;
98 }
int offset(int x)
Returns the highest power of two which is not more than x.
Definition fenwick_tree.cpp:41
Here is the call graph for this function:

◆ sum_range()

int range_queries::fenwick_tree::sum_range ( int l,
int r )
inline

Returns the prefix sum in range from L to R.

Parameters
lLeft index of range.
rRight index of range.
Returns
Sum of elements in range from L to R.
106{ return sum(r) - sum(l - 1); }
int sum(T id)
Returns the sum of elements in range from 0 to ID.
Definition fenwick_tree.cpp:90
Here is the call graph for this function:

◆ update()

template<typename T >
void range_queries::fenwick_tree::update ( T id,
T val )
inline

Updates the value of an element in original array and accordingly updates the values in BIT array.

Template Parameters
Tthe type of the variables
Parameters
idIndex of element in original array.
valValue with which element's value is updated.
Returns
void
75 {
76 id++;
77 while (id <= n) {
78 bit[id] += val;
79 id += offset(id);
80 }
81 }
Here is the call graph for this function:

Member Data Documentation

◆ bit

std::vector<int> range_queries::fenwick_tree::bit {}
private

Array that represents Binary Indexed Tree.

34{}; ///< Array that represents Binary Indexed Tree.

The documentation for this class was generated from the following file: