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 segment tree #822

Open
wants to merge 92 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
d3ca8f5
Create Segment_tree.jl
AliceRoselia Mar 13, 2022
c31a7d9
Create test_segment_tree.jl
AliceRoselia Mar 13, 2022
43f672d
Update test_segment_tree.jl
AliceRoselia Mar 13, 2022
1f99500
Update test_segment_tree.jl
AliceRoselia Mar 13, 2022
4ca9a3b
Update test_segment_tree.jl
AliceRoselia Mar 13, 2022
b2fce0f
Update test_segment_tree.jl
AliceRoselia Mar 13, 2022
220c695
More code
AliceRoselia Mar 14, 2022
71e5c7a
Laying foundations
AliceRoselia Mar 14, 2022
2711be5
Update Segment_tree.jl
AliceRoselia Mar 29, 2022
0f4ddd9
Update Segment_tree.jl
AliceRoselia Mar 29, 2022
b0599e5
Update test_segment_tree.jl
AliceRoselia Mar 29, 2022
b84afae
Update Segment_tree.jl
AliceRoselia Mar 30, 2022
d4cd076
Finishing lots of things.
AliceRoselia Apr 11, 2022
b927996
Update Segment_tree.jl
AliceRoselia Apr 11, 2022
bcba1d2
Update Segment_tree.jl
AliceRoselia Apr 11, 2022
bcd0dec
Update Segment_tree.jl
AliceRoselia Apr 15, 2022
0a11172
Update Segment_tree.jl
AliceRoselia Apr 15, 2022
e377697
Update Segment_tree.jl
AliceRoselia Apr 15, 2022
725dd6f
Update Segment_tree.jl
AliceRoselia Apr 15, 2022
c7a02f0
Update Segment_tree.jl
AliceRoselia Apr 15, 2022
90be734
Update Segment_tree.jl
AliceRoselia Apr 15, 2022
5964c78
Update Segment_tree.jl
AliceRoselia Apr 15, 2022
e587bce
Update Segment_tree.jl
AliceRoselia Apr 15, 2022
e980c3e
Update Segment_tree.jl
AliceRoselia Apr 15, 2022
052f0ca
Update Segment_tree.jl
AliceRoselia Apr 15, 2022
811ae21
Update Segment_tree.jl
AliceRoselia Apr 15, 2022
192ebbd
Update Segment_tree.jl
AliceRoselia Apr 15, 2022
30bcdb7
Update test_segment_tree.jl
AliceRoselia Apr 16, 2022
b467ab6
Update Segment_tree.jl
AliceRoselia Apr 17, 2022
a19cf9d
Update Segment_tree.jl
AliceRoselia Apr 18, 2022
16f8d5e
Backup.
AliceRoselia May 21, 2022
ddba963
Create segment_tree.jl
AliceRoselia May 22, 2022
4ccae34
Update segment_tree.jl
AliceRoselia May 22, 2022
045f530
Update segment_tree.jl
AliceRoselia May 22, 2022
47cbf1c
Update segment_tree.jl
AliceRoselia May 22, 2022
66d691a
Artificial_identity OPs.
AliceRoselia May 22, 2022
3c924d4
Update segment_tree.jl
AliceRoselia May 22, 2022
22e0213
Update segment_tree.jl
AliceRoselia May 23, 2022
c7e9287
Update segment_tree.jl
AliceRoselia May 23, 2022
2aaa920
Update segment_tree.jl
AliceRoselia May 23, 2022
2b0c31d
Update segment_tree.jl
AliceRoselia May 24, 2022
8ef4adc
Update segment_tree.jl
AliceRoselia May 24, 2022
a50d636
Fixing scoping problems?
AliceRoselia May 24, 2022
a4d361a
Update segment_tree.jl
AliceRoselia May 24, 2022
38969a2
Update segment_tree.jl
AliceRoselia May 25, 2022
2b8a9d2
Adding the get_range to it.
AliceRoselia May 25, 2022
44ce817
Update segment_tree.jl
AliceRoselia May 26, 2022
60c4f25
Update segment_tree.jl
AliceRoselia May 26, 2022
b06b3e9
Update segment_tree.jl
AliceRoselia May 27, 2022
2a502f6
Update segment_tree.jl
AliceRoselia May 27, 2022
6085fda
Update segment_tree.jl
AliceRoselia May 27, 2022
f5d8249
Mark the checkpoint 2
AliceRoselia May 27, 2022
ae119c8
Cleaned up
AliceRoselia May 27, 2022
1b38dc4
Update segment_tree.jl
AliceRoselia May 27, 2022
a1189d6
Update segment_tree.jl
AliceRoselia May 29, 2022
7e4ef92
Update segment_tree.jl
AliceRoselia May 30, 2022
2ddda23
Update segment_tree.jl
AliceRoselia Jun 1, 2022
07845bb
The "6 functions plan" checkpoint.
AliceRoselia Jun 1, 2022
9d7190c
Update segment_tree.jl
AliceRoselia Jun 1, 2022
e8d7549
Trying to make set_range.
AliceRoselia Jun 3, 2022
975f62d
Update segment_tree.jl
AliceRoselia Jun 3, 2022
e33f462
Update segment_tree.jl
AliceRoselia Jun 3, 2022
79f952d
Update segment_tree.jl
AliceRoselia Jun 3, 2022
3e05488
Update segment_tree.jl
AliceRoselia Jun 8, 2022
36459d5
Update segment_tree.jl
AliceRoselia Jun 13, 2022
bf77117
Update segment_tree.jl
AliceRoselia Jun 18, 2022
a545edd
Update segment_tree.jl
AliceRoselia Jun 19, 2022
c091fc0
Update segment_tree.jl
AliceRoselia Jun 19, 2022
7adc876
Update segment_tree.jl
AliceRoselia Jun 19, 2022
5e36349
Update segment_tree.jl
AliceRoselia Jun 19, 2022
b5d9b09
Update segment_tree.jl
AliceRoselia Jun 19, 2022
59f7d05
Update segment_tree.jl
AliceRoselia Jun 19, 2022
7e92312
Changing something.
AliceRoselia Jun 20, 2022
5c2705e
Remove backups.
AliceRoselia Jun 20, 2022
55e527a
Update DataStructures.jl
AliceRoselia Jun 20, 2022
0dec0c8
Update test_segment_tree.jl
AliceRoselia Jun 20, 2022
84f3f64
Fixing many bugs.
AliceRoselia Jul 2, 2022
b9d4c91
Fixing some more bugs. A few deep bugs remaining.
AliceRoselia Jul 3, 2022
0c2c6db
Desperate debug!
AliceRoselia Jul 5, 2022
a4655f6
Writing a new debug tool.
AliceRoselia Jul 11, 2022
a087c77
Fixing ONE bug.
AliceRoselia Jul 11, 2022
4131882
Fixing a VERY LARGE class of bugs.
AliceRoselia Jul 11, 2022
fa3c267
Update test_segment_tree.jl
AliceRoselia Jul 11, 2022
fdcd8a7
"Finished" except something with matmul?
AliceRoselia Jul 14, 2022
72ad6d5
Update test_segment_tree.jl
AliceRoselia Jul 14, 2022
6b544ec
Finally! Finished!
AliceRoselia Jul 19, 2022
e78608d
Renaming and adding inbounds.
AliceRoselia Jul 19, 2022
83070f8
Create bench_segment_tree.jl
AliceRoselia Jul 19, 2022
d2d935f
Moving benchmark code.
AliceRoselia Jul 19, 2022
1ff9784
Update bench_segment_tree.jl
AliceRoselia Jul 19, 2022
617fe3b
Create segment_tree.md
AliceRoselia Jul 20, 2022
f265dd3
Merge branch 'JuliaCollections:master' into Add_Segment_tree
AliceRoselia Jul 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions docs/src/segment_tree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# SegmentTree

The Segment tree is a data structure implicitly representing an array with a reduce operation. It has two operations.
set_range Sets the value of the array from a to b.
get_range calculates the reduced value of the sub-array from element a to element b, using the provided operation.
The array is implicit.

# Constructor

The recommended way is to use the constructor function as follow

```julia
function SegmentTree(type::Type, size, op; iterated_op=nothing, identity=nothing)
# type: the type of the argument.
# size: the array size.
# op: the function taking two arguments of type specified, outputting one answer of that type. (function (type,type)->type)
# Must be associative. That is, op(op(a,b),c) must be equal to op(a, op(b,c)) for every a, b, and c.
#
# iterated_op: optional function, taking two arguments, equivalent to reducing the same value to a single value
# For example, the iterated_op of +(addition) would be *(multiplication) and the iterated_op of *(multiplication) would be ^(exponentiation).
# If not provided and specialized repeat_op not possible, it would fall back to using the "exponentiation by squaring" rule.
# function iterated_op(base, n)
# identity: the element with the property op(a, identity) == op(identity, a) == a for all a.
# If not provided, and no specialized identity is made,
# a singleton struct called "artificial_identity" will be used and the operations will be altered slightly to handle these cases.
# That means that the value stored would be a union of the type you specified and this special type.
# If the identity is not of bits type (can be tested with isbits), must instead be represented via a function ()->identity.

#Implicitly sets the array to identity for every element.
```
# API
The recommended API uses. Other methods implement the inner working of the data structure and should not be used.
```julia
function get_range(X::SegmentTree,low,high)
#returns reduce(op, array[low:high]) implicitly. O(logn) complexity where n is the size of the array.
function set_range!(X::SegmentTree, low, high, value)
#Equivalent to setting every element of the array from array[low] to array[high] to value "value". O(logn) complexity.
```

# Correctness
This was tested on multiple test cases.

# Benchmark
This segment tree has a slower, but still acceptable performance on set_range! compared to my old c++ code,
but a very fast get_range performance.

# Abstract Segment trees
This file provides a baseline abstract Segment tree as a baseline structure for those who want to make their own variants.
Please note that since the segment tree is not a single algorithm, but a set of algorithms, there will be many variants supporting different operations
with varying complexity. The author simply provides one basic variant of it, but feel free to extend the code.

6 changes: 4 additions & 2 deletions src/DataStructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ module DataStructures
include("default_dict.jl")
include("dict_support.jl")
include("trie.jl")

include("int_set.jl")

include("fenwick.jl")

include("segment_tree.jl")

export Segment_tree, get_range, set_range!

include("list.jl")
include("mutable_list.jl")
include("balanced_tree.jl")
Expand Down
Loading