Coder Social home page Coder Social logo

segment_tree's Introduction

Segment Tree with range operations

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 operations
  • Extensible to support more semigroup operations
  • Limited support for multidimensional trees
  • Python 2 is not currently supported

Installation

pip3 install segment-tree

Segment Tree (one-dimensional)

Basic usage

    from segment_tree import *
    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

    from segment_tree import *
    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

Operations and their complexity

n is total number of elements in the array.

Function Description Complexity Additional storage
SegmentTree(array) Builds a segment tree from an array O(n) O(n)
update(position, value) Updates an old value at position to value O(log n) O(1)
update_range(start, end, value) Sets value on a [start, end] range O(log n) O(1)
query(start, end, function) Returns function([start, end]) O(log n) O(1)

Multidimensional Segment Tree (alpha version, might have bugs)

Basic usage

    from segment_tree import *
    a = [[[1, 2], [1, 3]], [[-1, -2], [-1, -3]]]
    tree = MultidimensionalSegmentTree(a)

    tree.query([(0, 1), (0, 0), (0, 0)], max) # 1
    tree.query([(1, 1), (0, 1), (0, 1)], sum) # -7
    tree.query([(0, 1), (1, 1), (0, 1)], min) # -3

Operations and their complexity

n is total number of elements in the array, p is the number of dimensions.

Function Description Complexity Additional storage
MultidimensionalSegmentTree(array) Builds a multidimensional segment tree from an array O(4^p * n) O(4^p * n)
update(position, value) (not implemented yet) Updates an old value at position to value O(log^p n) O(1)
query(start, end, function) Returns function([start, end]) O(log^p n) O(1)

Tests

Execute python3 setup.py test to run tests.

segment_tree's People

Contributors

evgeth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

segment_tree's Issues

Segmetation fault if creating SegmentTree from empty array

>>> import sys
>>> import segment_tree
>>> 
>>> sys.setrecursionlimit(10**7)
>>> segment_tree.SegmentTree([])
Segmentation fault (core dumped)
>>> import segment_tree
>>> 
>>> segment_tree.SegmentTree([])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/anaconda3/lib/python3.6/site-packages/segment_tree/segment_tree.py", line 25, in __init__
    self.root = SegmentTreeNode(0, len(array) - 1, self)
  File "/home/user/anaconda3/lib/python3.6/site-packages/segment_tree/segment_tree.py", line 88, in __init__
    segment_tree)
  File "/home/user/anaconda3/lib/python3.6/site-packages/segment_tree/segment_tree.py", line 88, in __init__
    segment_tree)
  File "/home/user/anaconda3/lib/python3.6/site-packages/segment_tree/segment_tree.py", line 88, in __init__
    segment_tree)
  [Previous line repeated 244 more times]
RecursionError: maximum recursion depth exceeded
>>> 

If you create SegmentTree from empty array the infinite recursion occurs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.