Skip to main content

Augmented AVL tree to index and search intervals.

Project description

Augmented AVL Tree for Interval Search

Overview

This project implements an augmented AVL tree (a self-balancing binary search tree) in Python, designed specifically for storing and querying intervals efficiently. The tree allows you to insert intervals (with associated keys) and quickly search for the smallest interval containing a given point. This implementation is particularly useful for tasks like BIN (Bank Identification Number) range searches, where each interval represents a range of possible values.

Features

  • Self-Balancing Tree: The tree remains balanced after each insertion, ensuring that operations like search, insert, and delete remain efficient.
  • Interval Queries: Efficiently find the smallest interval that contains a given point.
  • Pure Python Implementation: The core logic is implemented purely in Python, making it easy to understand and modify.

Installation

Using pip

  1. Ensure you have Python 3.9 or higher installed.

  2. Install the package using pip:

    pip install avl_range_tree
    

Building from Source

If you'd like to build the package from source:

  1. Clone the repository:

    git clone git@github.com:mcoira/python-range-tree.git
    cd python-range-tree/
    
  2. Install the package:

    pip install .
    

Usage

Importing the Tree

from avl_range_tree.avl_tree import RangeTree

Inserting Intervals

Each interval is defined by a start value, an end value, and a key (a unique identifier for that interval).

tree = RangeTree()
tree.insert(123456000000000000, 123456999999999999, "key1")
tree.insert(987654000000000000, 987654999999999999, "key2")

Searching for Intervals

To search for the smallest interval containing a given point:

search_value = int(str(12345678).ljust(16, '0'))  # Convert 123456 to 1234567800000000
result = tree.search(search_value)

if result:
    start, end, key = result
    print(f"Found interval: [{start}, {end}] with key: {key}")
else:
    print("No interval found containing the point.")

Example Output

Found interval: [123456000000000000, 123456999999999999] with key: key1

Use Cases

1. BIN Range Lookup for Financial Transactions

In financial systems, BIN ranges are used to identify the issuing bank for credit and debit cards and some of the card's features. This tree can efficiently store and query BIN ranges, ensuring quick lookups during transaction processing.

2. IP Address Range Search

This implementation can also be adapted to store and search IP address ranges, allowing for quick identification of network blocks.

3. Date Range Queries

You can store and search for date ranges, which can be useful in applications like booking systems, where you need to check if a date falls within a certain range.

Performance

Thanks to the self-balancing nature of AVL trees, this implementation is highly efficient for both insertion and search operations. Unlike Red-Black trees, which are approximately balanced, AVL trees maintain strict balance, ensuring the tree's height is minimized. This strict balancing results in more rotations during insertion, making AVL trees slightly more expensive to build. However, this additional overhead pays off during queries, as the tree's height is kept to a minimum, improving search performance.

This characteristic is particularly beneficial in scenarios like BIN number management, where data is periodically updated but queried frequently. In such cases, the AVL tree offers a superior balance between build time and query performance, ensuring that the time complexity for operations remains O(log n + m), where n is the number of elements in the tree, and m is the number of overlapping intervals that contain the point.

License

This project is licensed under the MIT License. See the LICENSE file 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

avl_range_tree-0.4.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

avl_range_tree-0.4.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file avl_range_tree-0.4.0.tar.gz.

File metadata

  • Download URL: avl_range_tree-0.4.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Darwin/23.6.0

File hashes

Hashes for avl_range_tree-0.4.0.tar.gz
Algorithm Hash digest
SHA256 7337718218f6d5f570968f0ad14b810f03abd8e8b824fea8229e2316b0c351e0
MD5 d901c475dfcb99e05823c08a25409c4f
BLAKE2b-256 0ef6d9564a0647b46069265b49db27c60b0493ea169107638d45028be1e8ad9c

See more details on using hashes here.

File details

Details for the file avl_range_tree-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: avl_range_tree-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Darwin/23.6.0

File hashes

Hashes for avl_range_tree-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9a29762658ff59f2a882bd4a69955a3669b173e64368804d8740569802312e64
MD5 776a1043a1973692528d8d2b4708e26c
BLAKE2b-256 c86d77c9318184b37c3bc559f6991fa1abda04219e0cc65afcb81cb51e4b4204

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