Skip to main content

The most comprehensive implementation of Segment Tree.

Project description

Segment Tree with range operations
==================================

.. figure:: https://img.shields.io/badge/license-MIT-blue.svg
:alt: LicenseLink

LicenseLink

This is a general implementation of a segment tree for Python 3.

- Semigroup range operations in O(logN) time
- Built-in support for ``max``, ``min``, ``sum`` functions
- Extensible to support more semigroup operations
- Only Python 3 for now

Example usage
=============

Basic queries
-------------

.. code:: python


array = [3, 1, 5, 3, 13, 7, 2, 7, 2]
tree = SegmentTree(array)

t.query(1, 3, "sum") # 9
t.update(0, 10) # [10, 1, 5, 3, 13, 7, 2, 7, 2]
t.query(0, 8, "min") # 0
t.update(2, -1) # [10, 1, -1, 3, 13, 7, 2, 0, 2]
t.query(0, 2, "min") # -1

Updates on ranges
-----------------

.. code:: python

array = [1, 2, 3, 4, 5]
t = SegmentTree(array)
t.update_range(0, 2, 6) # 6 6 6 4 5
t.update_range(1, 4, 2) # 6 2 2 2 2
t.query(0, 3, "min") # 2

Installation
============

``pip3 install segment-tree``

Supported operations and complexity
===================================

+---------+------------+--------------+----------+
| Functio | Descriptio | Complexity | Addition |
| n | n | | al |
| | | | storage |
+=========+============+==============+==========+
| ``Segme | Builds a | O(n log n) | O(n log |
| ntTree( | segment | | n) |
| array)` | tree from | | |
| ` | an | | |
| | ``array`` | | |
+---------+------------+--------------+----------+
| ``updat | Updates an | O(log n) | O(1) |
| e(posit | old value | | |
| ion, va | at | | |
| lue)`` | ``position | | |
| | `` | | |
| | to | | |
| | ``value`` | | |
+---------+------------+--------------+----------+
| ``updat | Sets | O(log n) | O(1) |
| e_range | ``value`` | | |
| (start, | on a | | |
| end, v | ``[start, | | |
| alue)`` | end]`` | | |
| | range | | |
+---------+------------+--------------+----------+
| ``query | Returns | O(log n) | O(1) |
| (start, | ``function | | |
| end, f | ([start, e | | |
| unction | nd])`` | | |
| )`` | | | |
+---------+------------+--------------+----------+

Tests
=====

Execute ``python3 setup.py test`` to run tests.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

segment_tree-0.2.2.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

segment_tree-0.2.2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file segment_tree-0.2.2.tar.gz.

File metadata

  • Download URL: segment_tree-0.2.2.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for segment_tree-0.2.2.tar.gz
Algorithm Hash digest
SHA256 bb638d95d2d771eb643779dc4afc1042b2c29bd400ab3402716eb2f4d0ea775b
MD5 d9523d835a710ba8900bbb16081dda6a
BLAKE2b-256 6ee1756aeb15c3093d0066c780767273f10abb525c064b14b1e15929c90f5076

See more details on using hashes here.

File details

Details for the file segment_tree-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for segment_tree-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5c1c7180c8858758223bbe8f69ecf23a57bf95eb87ee6715820677d0326b0cff
MD5 af9adc923cfd898465170c9d775e9c3e
BLAKE2b-256 f0ca5667ddf6d5376911b9607d8a637810c51ef174e88bb06b911ceebf81e83e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page