Fenwick Tree
(Binary Indexed Tree)
William Fiset
Outline
•
•
•
Discussion & Examples
•
Data structure motivation
•
What is a Fenwick tree?
•
Complexity analysis
Implementation details
•
Range query
•
Point Updates
•
Fenwick tree construction
Code Implementation
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
A =
0
1
2
3
4
5
6
7
8
9
5
-3
6
1
0
-4
11
6
2
7
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
A =
0
1
2
3
4
5
6
7
8
9
5
-3
6
1
0
-4
11
6
2
7
Sum of A from [2,7) = 6 + 1 + 0 + -4 + 11 = 14
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
A =
0
1
2
3
4
5
6
7
8
9
5
-3
6
1
0
-4
11
6
2
7
Sum of A from [2,7) = 6 + 1 + 0 + -4 + 11 = 14
Sum of A from [0,4) = 5 + -3 + 6 + 1 = 9
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
A =
0
1
2
3
4
5
6
7
8
9
5
-3
6
1
0
-4
11
6
2
7
Sum of A from [2,7) = 6 + 1 + 0 + -4 + 11 = 14
Sum of A from [0,4) = 5 + -3 + 6 + 1 = 9
Sum of A from [7,8) = 6 = 6
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
0
1
2
3
4
5
6
7
8
9
A =
5
-3
6
1
0
-4
11
6
2
7
P =
∅
∅
∅
∅
∅
∅
∅
∅
∅
∅
Let P be an array containing all
the prefix sums of A.
∅
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
0
1
2
3
4
5
6
7
8
9
A =
5
-3
6
1
0
-4
11
6
2
7
P =
0
∅
∅
∅
∅
∅
∅
∅
∅
∅
Let P be an array containing all
the prefix sums of A.
∅
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
0
1
2
3
4
5
6
7
8
9
A =
5
-3
6
1
0
-4
11
6
2
7
P =
0
5
∅
∅
∅
∅
∅
∅
∅
∅
Let P be an array containing all
the prefix sums of A.
∅
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
0
1
2
3
4
5
6
7
8
9
A =
5
-3
6
1
0
-4
11
6
2
7
P =
0
5
2
∅
∅
∅
∅
∅
∅
∅
Let P be an array containing all
the prefix sums of A.
∅
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
0
1
2
3
4
5
6
7
8
9
A =
5
-3
6
1
0
-4
11
6
2
7
P =
0
5
2
8
∅
∅
∅
∅
∅
∅
Let P be an array containing all
the prefix sums of A.
∅
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
0
1
2
3
4
5
6
7
8
9
A =
5
-3
6
1
0
-4
11
6
2
7
P =
0
5
2
8
9
∅
∅
∅
∅
∅
Let P be an array containing all
the prefix sums of A.
∅
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
0
1
2
3
4
5
6
7
8
9
A =
5
-3
6
1
0
-4
11
6
2
7
P =
0
5
2
8
9
9
∅
∅
∅
∅
Let P be an array containing all
the prefix sums of A.
∅
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
0
1
2
3
4
5
6
7
8
9
A =
5
-3
6
1
0
-4
11
6
2
7
P =
0
5
2
8
9
9
5
∅
∅
∅
Let P be an array containing all
the prefix sums of A.
∅
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
0
1
2
3
4
5
6
7
8
9
A =
5
-3
6
1
0
-4
11
6
2
7
P =
0
5
2
8
9
9
5
16
∅
∅
Let P be an array containing all
the prefix sums of A.
∅
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
0
1
2
3
4
5
6
7
8
9
A =
5
-3
6
1
0
-4
11
6
2
7
P =
0
5
2
8
9
9
5
16
22
∅
Let P be an array containing all
the prefix sums of A.
∅
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
0
1
2
3
4
5
6
7
8
9
A =
5
-3
6
1
0
-4
11
6
2
7
P =
0
5
2
8
9
9
5
16
22
24
Let P be an array containing all
the prefix sums of A.
∅
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
0
1
2
3
4
5
6
7
8
9
A =
5
-3
6
1
0
-4
11
6
2
7
P =
0
5
2
8
9
9
5
16
22
24
Let P be an array containing all
the prefix sums of A.
31
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
0
1
2
3
4
5
6
7
8
9
A =
5
-3
6
1
0
-4
11
6
2
7
P =
0
5
2
8
9
9
5
16
22
24
31
Sum of A from [2,7) = P[7] - P[2] = 16 - 2 = 14
Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
0
1
2
3
4
5
6
7
8
9
A =
5
-3
6
1
0
-4
11
6
2
7
P =
0
5
2
8
9
9
5
16
22
24
31
Sum of A from [2,7) = P[7] - P[2] = 16 - 2 = 14
Sum of A from [0,4) = P[4] - P[0] = 9 - 0 = 9