Binary Indexed Tree
Binary Indexed Tree
This Demonstration implements a binary indexed tree (also known as a Fenwick tree), a data structure used mainly to calculate prefix sums (or sums of ranges) in a list efficiently. To compute the sum of the values in a range of the list, move the start and end sliders to specify it. Hover over the entries in the "tree" row to see which elements in the list are contained in that node.
The prefix sum of a list in a certain index is the sum of all of the elements in the list up to and including that index. This algorithm speeds up the computation of prefix sums when the list's items can change; there are more efficient algorithms to compute prefix sums of static lists.
Green nodes represent the values that are added to compute the answer, while red nodes represent the values that are subtracted. Olive-colored nodes are added and subtracted as part of the algorithm, but they do not affect the answer.