Fenwick Tree
Fenwick Tree
• Complexity analysis
• Implementation details
• Range query
• Point Updates
• Code Implementation
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
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 = ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅
P = 0 ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅
P = 0 5 ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅
P = 0 5 2 ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅
P = 0 5 2 8 ∅ ∅ ∅ ∅ ∅ ∅ ∅
P = 0 5 2 8 9 ∅ ∅ ∅ ∅ ∅ ∅
P = 0 5 2 8 9 9 ∅ ∅ ∅ ∅ ∅
P = 0 5 2 8 9 9 5 ∅ ∅ ∅ ∅
P = 0 5 2 8 9 9 5 16 ∅ ∅ ∅
P = 0 5 2 8 9 9 5 16 22 ∅ ∅
P = 0 5 2 8 9 9 5 16 22 24 ∅
P = 0 5 2 8 9 9 5 16 22 24 31
P = 0 5 2 8 9 9 5 16 22 24 31
P = 0 5 2 8 9 9 5 16 22 24 31
P = 0 5 2 8 9 9 5 16 22 24 31
P = 0 5 2 8 9 9 5 16 22 24 31
Fenwick Tree
Motivation
Question: What about if we want to update
our initial array with some new value?
0 1 2 3 4 5 6 7 8 9
A = 5 -3 6 1 3 -4 11 6 2 7
P = 0 5 2 8 9 12 8 19 25 27 34
A[4] = 3
Fenwick Tree
Motivation
Question: What about if we want to update
our initial array with some new value?
0 1 2 3 4 5 6 7 8 9
A = 5 -3 6 1 3 -4 11 6 2 7
P = 0 5 2 8 9 12 8 19 25 27 34
12 011002
_
11 010112
_
10 010102
_
9 _ 010012
8 _ 010002
7 _ 001112
6 _ 001102
5 _ 001012
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002
15 011112 _
Unlike a regular array, in
14 011102 _
a Fenwick tree a specific
13 011012 cell is responsible for
other cells as well.
_
12 011002
_
11 010112
_
9 010012
determines the range of
_
8 010002
responsibility that cell has
_
12 011002
_
11 010112
_
9 010012
determines the range of
_
8 010002
responsibility that cell has
_
2 000102
cells below itself.
_
1 _ 000012
16 100002
15 011112 _
Unlike a regular array, in
14 011102 _
a Fenwick tree a specific
13 011012 cell is responsible for
other cells as well.
_
12 011002
_
11 010112
_
9 010012
determines the range of
_
8 010002
responsibility that cell has
_
2 000102
cells below itself.
_
1 _ 000012
16 100002
15 011112 _
Unlike a regular array, in
14 011102 _
a Fenwick tree a specific
13 011012 cell is responsible for
other cells as well.
_
12 011002
_
11 010112
_
9 010012
determines the range of
_
8 010002
responsibility that cell has
_
3 000112
index is responsible for 21-1 = 1
_
2 000102
cell (itself).
_
1 _ 000012
16 100002
15 011112 _
14 011102 _
13 011012
_
12 011002
_
range of responsibility
10 010102
_
for that cell NOT value.
9 _ 010012
8 010002
_
14 011102 _
13 011012
_
12 011002
_
range of responsibility
10 010102
_
for that cell NOT value.
9 _ 010012
8 _ 010002
7 001112 Numbers with their least
significant bit in the second
_
6 001102
position have a range of two.
_
5 _ 001012
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002
15 011112 _
14 011102 _
13 011012
_
12 011002
_
range of responsibility
10 010102
_
for that cell NOT value.
9 _ 010012
8 _ 010002
7 001112 Numbers with their least
significant bit in the third
_
6 001102
position have a range of four.
_
5 _ 001012
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002
15 011112 _
14 011102 _
13 011012
_
12 011002
_
range of responsibility
10 010102
_
for that cell NOT value.
9 _ 010012
8 _ 010002
7 001112 Numbers with their least
significant bit in the fourth
_
6 001102
position have a range of eight.
_
5 _ 001012
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002
15 011112 _
14 011102 _
13 011012
_
12 011002
_
range of responsibility
10 010102
_
for that cell NOT value.
9 _ 010012
8 010002
Numbers with their least
_
9 _ 010012
8 _ 010002
7 _ 001112
6 _ 001102
5 _ 001012
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002 Range Queries
15 011112 _
9 010012
Idea: Suppose you want to find
_
8 010002
the prefix sum of [1, i], then
_
sum = A[7]
6 _ 001102
5 _ 001012
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002 Range Queries
15 011112 _
9 _ 010012
8 _ 010002
7 _ 001112
6 _ 001102
5 _ 001012
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002 Range Queries
15 011112 _
14 011102 _
9 _ 010012
8 _ 010002
7 _ 001112
6 _ 001102
5 _ 001012
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002 Range Queries
15 011112 _
14 011102 _
9 _ 010012
8 010002
First we compute the prefix
_
7 001112
_
1 _ 000012
16 100002 Range Queries
15 011112
Compute the interval sum
_
14 011102
between [11, 15].
_
13 011012
_
11 010112
compute the prefix sum of
_
10 010102
_
14 011102
between [11, 15].
_
13 011012
_
11 010112
compute the prefix sum of
_
10 010102
_
14 011102
between [11, 15].
_
13 011012
_
11 010112
compute the prefix sum of
_
10 010102
_
14 011102
between [11, 15].
_
13 011012
_
11 010112
compute the prefix sum of
_
10 010102
_
14 011102
between [11, 15].
_
13 011012
_
11 010112
compute the prefix sum of
_
10 010102
_
14 011102
between [11, 15].
_
13 011012
_
11 010112
compute the prefix sum of
_
10 010102
_
14 011102
between [11, 15].
_
13 011012
_
11 010112
compute the prefix sum of
_
10 010102
_
14 011102 _
13 011012
Notice that in the worst case
_
12 011002
_
6 001102
query might make us have to
_
5 001012
_
10 010102
_
9 _ 010012
8 _ 010002
7 _ 001112
6 _ 001102
5 _ 001012
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002 Point Updates
15 011112 _
10 010102
_
10 010102
_
12 = 11002
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002 Point Updates
15 011112 _
10 010102
_
10 010102
_
1 _ 000012
16 100002 Point Updates
15 011112
Point updates are the
_
14 011102
opposite of this, we want to
_
13 011012
_
9 _ 010012
8 _ 010002
7 _ 001112
6 _ 001102
5 _ 001012
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002 Point Updates
15 011112
Point updates are the
_
14 011102
opposite of this, we want to
_
13 011012
_
9 _ 010012
8 010002
10 = 10102
_
7 _ 001112
6 _ 001102
5 _ 001012
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002 Point Updates
15 011112
Point updates are the
_
14 011102
opposite of this, we want to
_
13 011012
_
9 _ 010012
8 010002
10 = 10102, 10102+00102 = 11002
_
7 _ 001112
6 _ 001102 12 = 11002
5 _ 001012
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002 Point Updates
15 011112
Point updates are the
_
14 011102
opposite of this, we want to
_
13 011012
_
9 _ 010012
8 010002
10 = 10102, 10102+00102 = 11002
_
7 _ 001112
6 _ 001102 12 = 11002, 11002+01002 = 100002
5 _ 001012
4 _ 001002 16 = 100002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002 Point Updates
15 011112
Point updates are the
_
14 011102
opposite of this, we want to
_
13 011012
_
9 _ 010012
8 010002
10 = 10102, 10102+00102 = 11002
_
7 _ 001112
6 _ 001102 12 = 11002, 11002+01002 = 100002
5 _ 001012
4 _ 001002 16 = 100002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002 Point Updates
15 011112
Point updates are the
_
14 011102
opposite of this, we want to
_
13 011012
_
9 _ 010012
8 010002
10 = 10102, 10102+00102 = 11002
_
7 _ 001112
6 _ 001102 12 = 11002, 11002+01002 = 100002
5 _ 001012
4 _ 001002 16 = 100002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002 Point Updates
15 011112 _
14 011102
If we add x to position 6 in
the Fenwick tree which cells
_
13 011012
do we also need to modify?
_
12 011002
_
11 010112
_
10 010102
_
9 _ 010012
8 _ 010002
7 _ 001112
6 _ 001102
5 _ 001012
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002 Point Updates
15 011112 _
14 011102
If we add x to position 6 in
the Fenwick tree which cells
_
13 011012
do we also need to modify?
_
12 011002
_
11 010112
_
10 010102
_
6 = 01102
9 _ 010012
8 _ 010002
7 _ 001112
6 _ 001102
5 _ 001012
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002 Point Updates
15 011112 _
14 011102
If we add x to position 6 in
the Fenwick tree which cells
_
13 011012
do we also need to modify?
_
12 011002
_
11 010112
_
10 010102
_
6 = 01102, 01102+00102 = 10002
9 _ 010012
8 _ 010002 8 = 10002
7 _ 001112
6 _ 001102
5 _ 001012
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002 Point Updates
15 011112 _
14 011102
If we add x to position 6 in
the Fenwick tree which cells
_
13 011012
do we also need to modify?
_
12 011002
_
11 010112
_
10 010102
_
6 = 01102, 01102+00102 = 10002
9 _ 010012
8 _ 010002 8 = 10002, 10002+10002 = 100002
7 _ 001112
6 _ 001102 16 = 100002
5 _ 001012
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002 Point Updates
15 011112 _
14 011102
If we add x to position 6 in
the Fenwick tree which cells
_
13 011012
do we also need to modify?
_
12 011002
_
11 010112
_
10 010102
_
6 = 01102, 01102+00102 = 10002
9 _ 010012
8 _ 010002 8 = 10002, 10002+10002 = 100002
7 _ 001112
6 _ 001102 16 = 100002
5 _ 001012
4 _ 001002
3 _ 000112
2 _ 000102
1 _ 000012
16 100002 Point Updates
15 011112 _
14 011102
If we add x to position 6 in
the Fenwick tree which cells
_
13 011012
do we also need to modify?
_
12 011002
_
11 010112
_
10 010102
_
6 = 01102, 01102+00102 = 10002
9 _ 010012
8 _ 010002 8 = 10002, 10002+10002 = 100002
7 _ 001112
6 _ 001102 16 = 100002
5 _ 001012 Required Updates:
4 001002
A[6] = A[6] + x
_
3 000112
A[8] = A[8] + x
_
2 000102
A[16] = A[16] + x
_
1 _ 000012
Point update algorithm
To update the cell at index i in
the a Fenwick tree of size N:
9 _ 10012 -9
8 _ 10002 -8
7 _ 01112 5
6 _ 01102 11
5 _ 01012 3
4 _ 01002 7
3 _ 00112 -2
2 _ 00102 4
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
Idea: Add the value in
10 10102 2
the current cell to the
_
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
3 _ 00112 -2
2 _ 00102 4
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 2
9 _ 10012 -9
8 _ 10002 -8
7 _ 01112 5 i = 1 = 00012
6 01102 11
j = 00012 + 00012 = 00102
_
5 _ 01012 3 = 2
4 _ 01002 7
3 _ 00112 -2
2 _ 00102 4
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 2
9 _ 10012 -9
8 _ 10002 -8
7 _ 01112 5 i = 1 = 00012
6 01102 11
j = 00012 + 00012 = 00102
_
5 _ 01012 3 = 2
4 _ 01002 7
3 _ 00112 -2
2 _ 00102 4+3
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 2
9 _ 10012 -9
8 _ 10002 -8
7 _ 01112 5 i = 2 = 00102
6 01102 11
j = 00102 + 00102 = 01002
_
5 _ 01012 3 = 4
4 _ 01002 7
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 2
9 _ 10012 -9
8 _ 10002 -8
7 _ 01112 5 i = 2 = 00102
6 01102 11
j = 00102 + 00102 = 01002
_
5 _ 01012 3 = 4
4 _ 01002 7+7
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 2
9 _ 10012 -9
8 _ 10002 -8
7 _ 01112 5 i = 3 = 00112
6 01102 11
j = 00112 + 00012 = 01002
_
5 _ 01012 3 = 4
4 _ 01002 14
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 2
9 _ 10012 -9
8 _ 10002 -8
7 _ 01112 5 i = 3 = 00112
6 01102 11
j = 00112 + 00012 = 01002
_
5 _ 01012 3 = 4
4 _ 01002 14+-2
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 2
9 _ 10012 -9
8 _ 10002 -8
7 _ 01112 5 i = 4 = 01002
6 01102 11
j = 01002 + 01002 = 10002
_
5 _ 01012 3 = 8
4 _ 01002 12
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 2
9 _ 10012 -9
8 _ 10002 -8+12
7 _ 01112 5 i = 4 = 01002
6 01102 11
j = 01002 + 01002 = 10002
_
5 _ 01012 3 = 8
4 _ 01002 12
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 2
9 _ 10012 -9
8 _ 10002 4
7 _ 01112 5 i = 5 = 01012
6 01102 11
j = 01012 + 00012 = 01102
_
5 _ 01012 3 = 6
4 _ 01002 12
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 2
9 _ 10012 -9
8 _ 10002 4
7 _ 01112 5 i = 5 = 01012
6 01102 11+3
j = 01012 + 00012 = 01102
_
5 _ 01012 3 = 6
4 _ 01002 12
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 2
9 _ 10012 -9
8 _ 10002 4
7 _ 01112 5 i = 6 = 01102
6 01102 14
j = 01102 + 00102 = 10002
_
5 _ 01012 3 = 8
4 _ 01002 12
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 2
9 _ 10012 -9
8 _ 10002 4+14
7 _ 01112 5 i = 6 = 01102
6 01102 14
j = 01102 + 00102 = 10002
_
5 _ 01012 3 = 8
4 _ 01002 12
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 2
9 _ 10012 -9
8 _ 10002 18
7 _ 01112 5 i = 7 = 01112
6 01102 14
j = 01112 + 00012 = 10002
_
5 _ 01012 3 = 8
4 _ 01002 12
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 2
9 _ 10012 -9
8 _ 10002 18+5
7 _ 01112 5 i = 7 = 01112
6 01102 14
j = 01112 + 00012 = 10002
_
5 _ 01012 3 = 8
4 _ 01002 12
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 2
9 _ 10012 -9
8 _ 10002 23
7 _ 01112 5 i = 8 = 10002
6 01102 14
j = 10002 + 10002 = 100002
_
5 _ 01012 3 = 16
4 _ 01002 12
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 2
9 _ 10012 -9
8 _ 10002 23
7 _ 01112 5 i = 9 = 10012
6 01102 14
j = 10012 + 00012 = 10102
_
5 _ 01012 3 = 10
4 _ 01002 12
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 2+-9
9 _ 10012 -9
8 _ 10002 23
7 _ 01112 5 i = 9 = 10012
6 01102 14
j = 10012 + 00012 = 10102
_
5 _ 01012 3 = 10
4 _ 01002 12
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -8
Linear Construction
11 _ 10112 4
10 _ 10102 -7
9 _ 10012 -9
8 _ 10002 23
7 _ 01112 5 i = 10 = 10102
6 01102 14
j = 10102 + 00102 = 11002
_
5 _ 01012 3 = 12
4 _ 01002 12
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -8+-7
Linear Construction
11 _ 10112 4
10 _ 10102 -7
9 _ 10012 -9
8 _ 10002 23
7 _ 01112 5 i = 10 = 10102
6 01102 14
j = 10102 + 00102 = 11002
_
5 _ 01012 3 = 12
4 _ 01002 12
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -15
Linear Construction
11 _ 10112 4
10 _ 10102 -7
9 _ 10012 -9
8 _ 10002 23
7 _ 01112 5 i = 11 = 10112
6 01102 14
j = 10112 + 00012 = 11002
_
5 _ 01012 3 = 12
4 _ 01002 12
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -15+4
Linear Construction
11 _ 10112 4
10 _ 10102 -7
9 _ 10012 -9
8 _ 10002 23
7 _ 01112 5 i = 11 = 10112
6 01102 14
j = 10112 + 00012 = 11002
_
5 _ 01012 3 = 12
4 _ 01002 12
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
12 _ 11002 -11
Linear Construction
11 _ 10112 4
10 _ 10102 -7
9 _ 10012 -9
8 _ 10002 23
7 _ 01112 5 i = 12 = 11002
6 01102 14
j = 11002 + 01002 = 100002
_
5 _ 01012 3 = 16
4 _ 01002 12
1 _ 00012 3
12 _ 11002 -11
Linear Construction
11 _ 10112 4
Constructed Fenwick
10 10102 -7
tree! We can now
_
7 _ 01112 5
6 _ 01102 14
5 _ 01012 3
4 _ 01002 12
3 _ 00112 -2
2 _ 00102 7
1 _ 00012 3
Construction Algorithm
# Make sure values is 1-based!
function construct(values):
N := length(values)
for i = 1,2,3, … N:
j := i + LSB(i)
if j < N:
tree[j] = tree[j] + tree[i]
return tree
Fenwick Tree source code
follows in next video!
William Fiset
Source Code Link
Implementation source code
and tests can all be found
at the following link:
github.com/williamfiset/data-structures
10 _ 01010
9 _ 01001
8 _ 01000
7 _ 00111
6 _ 00110
5 _ 00101
4 _ 00100
3 _ 00011
2 _ 00010
1 _ 00001
16 10000
Least Significant Bit
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100 Idea: Place an edge between values
11 01011 who only differ by their Least
Significant Bit (LSB) in binary.
_
10 _ 01010
9 _ 01001
8 _ 01000
7 _ 00111 Least significant bits
6 _ 00110
5 _ 00101
4 _ 00100
3 _ 00011
2 _ 00010
1 _ 00001
16 10000
Least Significant Bit
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100 Idea: Place an edge between values
11 01011 who only differ by their Least
Significant Bit (LSB) in binary.
_
10 _ 01010
9 01001
In other words: Place an
_
8 01000
edge between nodes i and i
_
7 00111
_
10 _ 01010
9 01001
In other words: Place an
_
8 01000
edge between nodes i and i
_
7 00111
_
10 _ 01010
9 01001
In other words: Place an
_
8 01000
edge between nodes i and i
_
7 00111
_
10 _ 01010
9 01001
In other words: Place an
_
8 01000
edge between nodes i and i
_
7 00111
_
10 _ 01010
9 01001
In other words: Place an
_
8 01000
edge between nodes i and i
_
7 00111
_
3 _ 00011
2 _ 00010 1111
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 16
10 _ 01010
9 _ 01001
8 _ 01000
14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 00100
15 -> 14
_
3 _ 00011
2 _ 00010 1111 -> 1110
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 16
10 _ 01010
9 _ 01001 12
8 _ 01000
14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 00100
15 -> 14 -> 12
_
3 _ 00011
2 _ 00010 1111 -> 1110 -> 1100
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 8 16
10 _ 01010
9 _ 01001 12
8 _ 01000
14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 00100
15 -> 14 -> 12 -> 8
_
3 _ 00011
2 _ 00010 1111 -> 1110 -> 1100 -> 1000
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 8 16
10 _ 01010
9 _ 01001 12
8 _ 01000
14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 00100
15 -> 14 -> 12 -> 8 -> 0
_
3 _ 00011
2 _ 00010 1111 -> 1110 -> 1100 -> 1000 -> 0000
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 8 16
10 _ 01010
9 _ 01001 12
8 _ 01000
13 14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 _ 00100
3 _ 00011 13 -> 12
2 _ 00010 1101 -> 1100
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 8 16
10 _ 01010
9 _ 01001 12
8 _ 01000
11 13 14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 _ 00100
3 _ 00011 11
2 _ 00010 1011
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 8 16
10 _ 01010
9 _ 01001 10 12
8 _ 01000
11 13 14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 _ 00100
3 _ 00011 11 -> 10
2 _ 00010 1011 -> 1010
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 8 16
10 _ 01010
9 _ 01001 10 12
8 _ 01000
11 13 14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 _ 00100
3 _ 00011 11 -> 10 -> 8
2 _ 00010 1011 -> 1010 -> 1000
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 8 16
10 _ 01010
9 _ 01001 9 10 12
8 _ 01000
11 13 14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 _ 00100
3 _ 00011 9 -> 8
1001 -> 1000
2 _ 00010
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 8 16
10 _ 01010
9 _ 01001 9 10 12
8 _ 01000
7 11 13 14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 00100
7
_
3 00011
0111
_
2 _ 00010
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 8 16
10 _ 01010
9 _ 01001 6 9 10 12
8 _ 01000
7 11 13 14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 00100
7 -> 6
_
3 00011
0111 -> 0110
_
2 _ 00010
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 4 8 16
10 _ 01010
9 _ 01001 6 9 10 12
8 _ 01000
7 11 13 14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 00100
7 -> 6 -> 4
_
3 00011
0111 -> 0110 -> 0100
_
2 _ 00010
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 4 8 16
10 _ 01010
9 _ 01001 6 9 10 12
8 _ 01000
7 11 13 14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 00100
7 -> 6 -> 4 -> 0
_
3 00011
0111 -> 0110 -> 0100 -> 0000
_
2 _ 00010
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 4 8 16
10 _ 01010
9 _ 01001 5 6 9 10 12
8 _ 01000
7 11 13 14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 00100
_
5 -> 4
3 00011
0101 -> 0100
_
2 _ 00010
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 4 8 16
10 _ 01010
9 _ 01001 3 5 6 9 10 12
8 _ 01000
7 11 13 14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 _ 00100 3
3 _ 00011 0011
2 _ 00010
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 2 4 8 16
10 _ 01010
9 _ 01001 3 5 6 9 10 12
8 _ 01000
7 11 13 14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 _ 00100 3 -> 2
3 _ 00011 0011 -> 0010
2 _ 00010
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 2 4 8 16
10 _ 01010
9 _ 01001 3 5 6 9 10 12
8 _ 01000
7 11 13 14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 _ 00100 3 -> 2 -> 0
3 _ 00011 0011 -> 0010 -> 0000
2 _ 00010
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 1 2 4 8 16
10 _ 01010
9 _ 01001 3 5 6 9 10 12
8 _ 01000
7 11 13 14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 _ 00100 1 -> 0
3 _ 00011 0001 -> 0000
2 _ 00010
1 _ 00001
16 10000
Fenwick Tree Visualization
15 _ 01111
14 _ 01110 0
13 _ 01101
12 _ 01100
11 _ 01011 1 2 4 8 16
10 _ 01010
9 _ 01001 3 5 6 9 10 12
8 _ 01000
7 11 13 14
7 _ 00111
6 _ 00110
15
5 _ 00101
4 _ 00100
3 _ 00011
2 _ 00010
1 _ 00001
Fenwick Tree Analysis
21-1 = 1 = 0b000001
22-1 = 3 = 0b000011
23-1 = 7 = 0b000111
4
2 -1 = 15 = 0b001111
25-1 = 31 = 0b011111
26-1 = 63 = 0b111111
Fenwick Tree Analysis
Odd nodes are always leaves in our Fenwick Tree
because their LSB is always a 1.
1 2 4 8 16
3 5 6 9 10 12
7 11 13 14
15