Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/xanewok/segment_tree

Simple segment tree C++ implementation for range sum queries
https://github.com/xanewok/segment_tree

Last synced: 24 days ago
JSON representation

Simple segment tree C++ implementation for range sum queries

Awesome Lists containing this project

README

        

# segment_tree
simple segment tree C++ implementation for range sum queries

Heap-allocated template segment tree for all your insert/range sum query needs.
Currently implements
```c++
segment_tree::segment_tree(T min, T max)
void segment_tree::insert(T key, T value)
T segment_tree::query(T key, T value) const
void segment_tree::clear()
```

# Example
```c++
segment_tree tree(0, 100); // Construct a tree for an interval [0-100]

tree.insert(20, 2);
assert(tree.query(15, 20) == 2);
assert(tree.query(15, 25) == 2);
tree.insert(21, 3);
assert(tree.query(15, 20) == 2);
assert(tree.query(21, 25) == 3);
assert(tree.query(15, 25) == 5);
```