Documentation ¶
Overview ¶
Package fenwick provides a list data structure supporting prefix sums.
A Fenwick tree, or binary indexed tree, is a space-efficient list data structure that can efficiently update elements and calculate prefix sums in a list of numbers.
Compared to a common array, a Fenwick tree achieves better balance between element update and prefix sum calculation – both operations run in O(log n) time – while using the same amount of memory. This is achieved by representing the list as an implicit tree, where the value of each node is the sum of the numbers in that subtree.
Example ¶
Compute the sum of the first 4 elements in a list.
package main import ( "fmt" "github.com/yourbasic/fenwick" ) func main() { a := fenwick.New(1, 2, 3, 4, 5) fmt.Println(a.Sum(4)) }
Output: 10
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type List ¶
type List struct {
// contains filtered or unexported fields
}
List represents a list of numbers with support for efficient prefix sum computation. The zero value is an empty list.