Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for FenwickTree #492

Merged
merged 5 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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