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.


  • [Snapshot]
  • [Snapshot]
  • [Snapshot]


Each node stores the sum of the interval in the original list, so by starting at any node and adding up the values of all of the nodes on the way to the root, the sum of the interval (its prefix sum) is computed. With this, to compute the sum of any given interval , it is only necessary to compute .
The tree is "binary indexed" because the parent of each node is obtained by setting to 0 the least significant bit that is 1 from the node's index in base 2.
Queries can be executed with time complexity after a one-time precomputation step that takes . If the array is modified, the tree can be updated in time for each modified position.
    • Share:

Embed Interactive Demonstration New!

Just copy and paste this snippet of JavaScript code into your website or blog to put the live Demonstration on your site. More details »

Files require Wolfram CDF Player or Mathematica.

Mathematica »
The #1 tool for creating Demonstrations
and anything technical.
Wolfram|Alpha »
Explore anything with the first
computational knowledge engine.
MathWorld »
The web's most extensive
mathematics resource.
Course Assistant Apps »
An app for every course—
right in the palm of your hand.
Wolfram Blog »
Read our views on math,
science, and technology.
Computable Document Format »
The format that makes Demonstrations
(and any information) easy to share and
interact with.
STEM Initiative »
Programs & resources for
educators, schools & students.
Computerbasedmath.org »
Join the initiative for modernizing
math education.
Step-by-Step Solutions »
Walk through homework problems one step at a time, with hints to help along the way.
Wolfram Problem Generator »
Unlimited random practice problems and answers with built-in step-by-step solutions. Practice online or make a printable study sheet.
Wolfram Language »
Knowledge-based programming for everyone.
Powered by Wolfram Mathematica © 2017 Wolfram Demonstrations Project & Contributors  |  Terms of Use  |  Privacy Policy  |  RSS Give us your feedback
Note: To run this Demonstration you need Mathematica 7+ or the free Mathematica Player 7EX
Download or upgrade to Mathematica Player 7EX
I already have Mathematica Player or Mathematica 7+