Skip to content

Commit

Permalink
Add documentation for FenwickTree (#492)
Browse files Browse the repository at this point in the history
Add documentation for FenwickTree
  • Loading branch information
eulerkochy authored and kmsquire committed Apr 9, 2019
1 parent e0cc5d0 commit 640379b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -28,6 +28,7 @@ This package implements a variety of data structures, including
- Sorted Dict, Sorted Multi-Dict and Sorted Set
- DataStructures.IntSet
- Priority Queue
- Fenwick Tree

Resources
---------
Expand Down
1 change: 1 addition & 0 deletions docs/make.jl
Expand Up @@ -12,6 +12,7 @@ makedocs(
"circ_deque.md",
"stack_and_queue.md",
"priority-queue.md",
"fenwick.md",
"accumulators.md",
"disjoint_sets.md",
"heaps.md",
Expand Down
25 changes: 25 additions & 0 deletions docs/src/fenwick.md
@@ -0,0 +1,25 @@
# Fenwick Tree

The `FenwickTree` type is data structures which is used for handling increment and decrement to prefix-sums of an array efficiently.

Usage:

```julia
FenwickTree{T}(n) # Constructs a Fenwick Tree of length `n`
FenwickTree{T}(counts) # Constructs a Fenwick Tree from an array of `counts`
inc!(ft, ind, val) # Increases the value of the FenwickTree `ft` by `val` from the index `ind` upto the length of `ft`
dec!(ft, ind, val) # Decreases the value of the FenwickTree `ft` by `val` from the index `ind` upto the length of `ft`
incdec!(ft, left, right, val) # Increases the value of the FenwickTree `ft` by `val` from the indices from `left` and decreases it from the `right`
prefixsum(ft, ind) # Return the cumulative sum from index 1 upto `ind` of the FenwickTree `ft`
```

Examples:

```julia
julia> f = FenwickTree{Int}(6)
julia> inc!(f, 2, 5)
julia> prefixsum(f, 1)
0
julia> prefixsum(f, 3)
5
```
12 changes: 6 additions & 6 deletions src/fenwick.jl
@@ -1,18 +1,18 @@
struct FenwickTree{T}
bi_tree::Vector{T} #bi_tree is shorthand for Binary Indexed Tree, an alternative name for Fenwick Tree
n::Int
end

"""
FenwickTree{T}(n)
Constructs a [`FenwickTree`](https://en.wikipedia.org/wiki/Fenwick_tree) of length `n`.
"""
struct FenwickTree{T}
bi_tree::Vector{T} #bi_tree is shorthand for Binary Indexed Tree, an alternative name for Fenwick Tree
n::Int
end

FenwickTree{T}(n::Integer) where T = FenwickTree{T}(zeros(T, n), n)

"""
FenwickTree(arr::AbstractArray)
FenwickTree(counts::AbstractArray)
Constructs a [`FenwickTree`](https://en.wikipedia.org/wiki/Fenwick_tree) from an array of `counts`
Expand Down

0 comments on commit 640379b

Please sign in to comment.