forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FenwickTree.c
51 lines (41 loc) · 800 Bytes
/
FenwickTree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
**Fenwick Tree**
#include <stdio.h>
int FWtree[100] = {0};
int SIZE;
int get_sum(int i)
{
int sum = FWtree[i];
while(i)
{
i -= (i & (-i));
sum += FWtree[i];
}
return sum;
}
void add(int i, int value)
{
while(i < SIZE)
{
FWtree[i] += value;
i += (i & (-i));
}
}
void init_fw_tree(int my_array[], int start, int end)
{
SIZE = end-start+2;
for(int i = 1; i <= end-start+2; i++)
{
add(i, my_array[ start+i-1 ]);
}
}
int main()
{
int my_array[] = {1, 3, 2, 4, 5 ,9, 6, 5 ,0, 3, 4, 3, 2, 2};
init_fw_tree(my_array, 0, 13);
//get sum of all the numbers in the array
printf("Sum of all numbers in the array is = %d\n",get_sum(14));
// update 5th index with value 9
add(5,9);
printf("New sum after updating 5th index with value 8 is = %d\n",get_sum(14));
return 0;
}