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

Go to the source code of this file.

Classes

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

Namespaces

namespace  range_queries
 for std::vector
 

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

Definition in file fenwick_tree.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 132 of file fenwick_tree.cpp.

132 {
133 tests(); // run self-test implementations
134 return 0;
135}
static void tests()
Self-test implementations.

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void

Definition at line 114 of file fenwick_tree.cpp.

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.