Skip to main content

A high-performance, enterprise-grade Segment Tree implementation for Python

Project description

🌳 Segee

Python 3.12+ MIT License

Python data structures library for efficient range queries and updates.

✨ Features

  • Segment Trees: Range queries with any associative operation (sum, min, max, GCD, etc.)
  • Binary Indexed Trees: Efficient sum queries and updates for additive operations
  • Type Safety: Generic type hints with protocol-based constraints
  • Pure Python: Zero dependencies, works with Python 3.12+
  • Comprehensive Testing: 232 tests including real-world problem validation
  • Pythonic API: Full sequence protocol support (tree[i], len(tree), etc.)

🚀 Quick Start

from segee import SumSegmentTree, MinSegmentTree, BinaryIndexedTree

# Segment Trees - for any associative operation
sum_tree = SumSegmentTree(5)
sum_tree[0:5] = [1, 2, 3, 4, 5]
print(sum_tree.sum(1, 4))     # 9 (sum of indices 1-3)

min_tree = MinSegmentTree(5)
min_tree[0:5] = [10, 5, 20, 15, 8]
print(min_tree.minimum(1, 4))  # 5 (min of indices 1-3)

# Binary Indexed Trees - optimized for additive operations
bit = BinaryIndexedTree([1, 2, 3, 4, 5])
bit.add(2, 10)  # Add 10 to index 2
print(bit.sum(0, 5))  # 25 (sum of all elements)

# Custom operations with generic segment tree
import math
from segee import GenericSegmentTree
gcd_tree = GenericSegmentTree(5, 0, math.gcd)

📦 Installation

pip install segee

🏗️ Available Data Structures

Segment Trees

  • GenericSegmentTree[T] - Generic implementation for any associative operation
  • SumSegmentTree - Optimized for sum operations
  • MinSegmentTree - Optimized for minimum operations
  • MaxSegmentTree - Optimized for maximum operations

Binary Indexed Trees

  • GenericBinaryIndexedTree[T] - Generic implementation for additive operations
  • BinaryIndexedTree - Optimized for int/float types
  • RangeAddBinaryIndexedTree - Supports efficient range updates

🎯 Interactive CLI

# Launch interactive segment tree CLI
segee

🏛️ Architecture

segee/
├── segment_tree/           # Segment tree module
│   ├── backbone/          # Generic implementations
│   └── specialized/       # Sum/Min/Max classes
├── binary_indexed_tree/   # Binary indexed tree module
│   ├── backbone/          # Generic implementations
│   └── specialized/       # Optimized classes
├── shared/               # Shared protocols and mixins
└── segee_cli/           # Interactive CLI application

📚 Documentation

🤔 When to Use

  • Segment Trees: When you need range queries with custom operations (min, max, GCD, XOR, etc.)
  • Binary Indexed Trees: When you need fast sum queries and updates, or range sum with range updates

📄 License

MIT License - see LICENSE for details.

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

segee-0.2.0.tar.gz (34.8 kB view details)

Uploaded Source

Built Distribution

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

segee-0.2.0-py3-none-any.whl (35.3 kB view details)

Uploaded Python 3

File details

Details for the file segee-0.2.0.tar.gz.

File metadata

  • Download URL: segee-0.2.0.tar.gz
  • Upload date:
  • Size: 34.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for segee-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b0d87952276ab9beb7984f254ab811580415fdeaae114f45f31acc36ebd7618b
MD5 3b6cef02dc2c6d1e69fb67ea3e997be6
BLAKE2b-256 883a0a00b44439d80df52249186b2c8bb5410a7492bc8ff078e56506e64ab188

See more details on using hashes here.

File details

Details for the file segee-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: segee-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 35.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for segee-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 12d67eb393707ed89e1dc28c30e83af4b6cead84f19eeb501d9cdc1777319de1
MD5 66fa089e4741f7c96ca27dbb5ec812af
BLAKE2b-256 a2a4add8fd36a51526f6a01e0f65dfea5d21f4feb26c49b7911dca5f8b1fbf7a

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