Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
fenwick_tree.cpp File Reference

Fenwick Tree algorithm implementation More...

#include <cassert>
#include <iostream>
#include <vector>
Include dependency graph for fenwick_tree.cpp:

Classes

class  range_queries::fenwick_tree
 The class that initializes the Fenwick Tree. More...
 

Namespaces

namespace  range_queries
 Algorithms and Data Structures that support range queries and updates.
 

Functions

static void tests ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Fenwick Tree algorithm implementation

From Wikipedia, the free encyclopedia.

A Fenwick tree or binary indexed tree (BIT) is a clever implementation of a datastructure called bionomal tree. It can update values and solve range queries with operations that is; commulative, associative and has an inverse for this type of element. It can also solve immutable range queries (min/max), when operations only are associative over this type of element. Some of these restrictions can be removed, by storing redunant information; like in max/min range queries.

Author
Mateusz Grzegorzek
David Leal

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
132 {
133 tests(); // run self-test implementations
134 return 0;
135}
static void tests()
Self-test implementations.
Definition fenwick_tree.cpp:114
Here is the call graph for this function:

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void
114 {
115 std::vector<int> arr = {1, 2, 3, 4, 5};
116 range_queries::fenwick_tree fenwick_tree(arr);
117
118 assert(fenwick_tree.sum_range(0, 0) == 1);
119 assert(fenwick_tree.sum_range(0, 1) == 3);
120 assert(fenwick_tree.sum_range(0, 2) == 6);
121 assert(fenwick_tree.sum_range(0, 3) == 10);
122 assert(fenwick_tree.sum_range(0, 4) == 15);
123
124 fenwick_tree.update(0, 6);
125 std::cout << "All tests have successfully passed!\n";
126}
The class that initializes the Fenwick Tree.
Definition fenwick_tree.cpp:32
Here is the call graph for this function: