Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary tree that stores partial sums: Name and existing implementations

Consider a sequence of n positive real numbers, (ai), and its partial sum sequence, (si). Given a number x ∊ (0, sn], we have to find i such that si−1 < x ≤ si. Also we want to be able to change one of the ai’s without having to update all partial sums. Both can be done in O(log n) time by using a binary tree with the ai’s as leaf node values, and the values of the non-leaf nodes being the sum of the values of the respective children. If n is known and fixed, the tree doesn’t have to be self-balancing and can be stored efficiently in a linear array. Furthermore, if n is a power of two, only 2 n − 1 array elements are required. See Blue et al., Phys. Rev. E 51 (1995), pp. R867–R868 for an application. Given the genericity of the problem and the simplicity of the solution, I wonder whether this data structure has a specific name and whether there are existing implementations (preferably in C++). I’ve already implemented it myself, but writing data structures from scratch always seems like reinventing the wheel to me—I’d be surprised if nobody had done it before.

like image 776
Philipp Avatar asked Sep 29 '10 13:09

Philipp


People also ask

What is use of Fenwick tree?

A Fenwick tree or binary indexed tree is a data structure that helps compute prefix sums efficiently. Computing prefix sums are often important in various other algorithms, not to mention several competitive programming problems. For example, they are used to implement the arithmetic coding algorithm.

What are the two methods of binary tree implementation?

Here we will see how to represent a binary tree in computers memory. There are two different methods for representing. These are using array and using linked list.

What is binary indexed tree used for?

Binary Indexed trees are used to implement the arithmetic coding algorithm. Development of operations it supports were primarily motivated by use in that case. Binary Indexed Tree can be used to count inversions in an array in O(N*logN) time.

Is binary tree a sum tree?

A binary tree is a sum tree if the value of each node is equal to the sum of nodes present in the left and the right subtree. An empty tree is a sum tree with 0 sums. A leaf node is also considered a sum tree with a sum equal to the value of the leaf node.


2 Answers

This is known as a finger tree in functional programming but apparently there are implementations in imperative languages. In the articles there is a link to a blog post explaining an implementation of this data structure in C# which could be useful to you.

like image 199
Daniel Avatar answered Oct 07 '22 16:10

Daniel


Fenwick tree (aka Binary indexed tree) is a data structure that maintains a sequence of elements, and is able to compute cumulative sum of any range of consecutive elements in O(logn) time. Changing value of any single element needs O(logn) time as well.

like image 21
Branimir Avatar answered Oct 07 '22 15:10

Branimir