Little-Known Awesome Algorithms: Fenwick Range Trees
swageroo.com
swageroo.com
A Fenwick Binary Indexed Tree allows you to calculate
the sum of elements in an array for any given index
range in O(Log(N)) time
I can modify a B-tree so each branch node keeps track of the sum total of values of nodes under it and the absolute number of nodes under it. It's pretty easy to find the sum of elements within a range, the range of elements that equal a given sum and so-forth. All O(log(n)).Why the weirdo data structure?
Also:
idx -= (1 << ctz(idx));
I used to write this as: idx -= idx&-idx;The extra functionality is a simple enough addition to any binary-tree-type data structure that I was annoyed that Boost/Qt didn't have it already included.
Care to name some, also having a non-restrictive license? (That excludes GPL).
http://www.touc.org/btree.html
This c++ template is fairly simple and I have tested it a reasonable amount.
But the method I give is pretty generic rather than special-purpose. Such a generic method have both the advantage that they can be extended relatively easily and the advantage that you can understand what's happening relatively easily, allowing easier debugging.
Binary indexed tree: https://community.topcoder.com/tc?module=Static&d1=tutor...
Cumulative frequency table (which interestingly enough Simon Tatham claims to have independently invented): http://www.chiark.greenend.org.uk/~sgtatham/algorithms/cumul...