https://github.com/bigspider/segtree
A simple implementation of a segment tree for range sums.
https://github.com/bigspider/segtree
complexity range-query range-sum segment-tree
Last synced: 8 months ago
JSON representation
A simple implementation of a segment tree for range sums.
- Host: GitHub
- URL: https://github.com/bigspider/segtree
- Owner: bigspider
- License: apache-2.0
- Created: 2016-05-03T22:14:09.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-05-13T07:54:27.000Z (about 10 years ago)
- Last Synced: 2025-02-05T17:45:42.207Z (over 1 year ago)
- Topics: complexity, range-query, range-sum, segment-tree
- Language: Python
- Size: 6.84 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# segtree
A simple implementation of a fixed-length segment tree for range sums. See for example http://www.geeksforgeeks.org/segment-tree-set-1-sum-of-given-range/.
NOTE: the name "segment tree" is used in literature for different structures used in computational geometry. Here we refer to a data structure that provides random access to an array, augmented with efficient range queries.
The SumSegmentTree class stores an array A of size n, providing access with three methods:
- `get(i)`: returns the element at index i
- `set(i, x)`: set the element at index i to x
- `sum(i, j)`: return the sum of all the elements between indexes i and j (inclusive)
The complexity of `get` is O(1); the complexity of `set` and `sum` is O(log n)
With easy modifications, the structure can be easily adapted to other kinds of range queries for example: minimum, maximum, product, and so on. More generally, range queries for any associative binary operator can be implemented in O(log n).
# Usage example
```
In [1]: from segtree import SumSegmentTree
In [2]: T = SumSegmentTree([1,5,3,2,-2,4,0,12])
In [3]: T.get_data()
Out[3]: [1, 5, 3, 2, -2, 4, 0, 12]
In [4]: T.get(1)
Out[4]: 5
In [5]: T.sum(2, 4)
Out[5]: 3
In [6]: T.set(3, 10)
In [7]: T.sum(2, 4)
Out[7]: 11
In [8]: T.get_data()
Out[8]: [1, 5, 3, 10, -2, 4, 0, 12]
```