TheAlgorithms/C++ 1.0.0
All the 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.

Definition at line 32 of file fenwick_tree.cpp.

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

Definition at line 50 of file fenwick_tree.cpp.

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 }
void update(T id, T val)
Updates the value of an element in original array and accordingly updates the values in BIT array.
std::vector< int > bit
Array that represents Binary Indexed Tree.
size_t n
No. of elements present in input array.

◆ 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

Definition at line 64 of file fenwick_tree.cpp.

64: n(x) { bit.assign(n + 1, 0); }

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.

Definition at line 41 of file fenwick_tree.cpp.

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.

Definition at line 90 of file fenwick_tree.cpp.

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.

◆ 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.

Definition at line 106 of file fenwick_tree.cpp.

106{ return sum(r) - sum(l - 1); }
int sum(T id)
Returns the sum of elements in range from 0 to ID.

◆ 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

Definition at line 75 of file fenwick_tree.cpp.

75 {
76 id++;
77 while (id <= n) {
78 bit[id] += val;
79 id += offset(id);
80 }
81 }

Member Data Documentation

◆ bit

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

Array that represents Binary Indexed Tree.

Definition at line 34 of file fenwick_tree.cpp.

34{};

◆ n

size_t range_queries::fenwick_tree::n = 0
private

No. of elements present in input array.

Definition at line 33 of file fenwick_tree.cpp.


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