An AVL tree set implementation in Python.
Project description
avlset
avlset is a Python package that provides a self-sorting, non-duplicating data structure using a custom AVL Tree
implementation. The AVL Tree is a binary search tree with automatic tree balancing. The AVLSet
object allows users
to insert data with confidence that it will be returned sorted and unique. avlset also provides basic type hinting
support in addition to support for multiple built-in Python operators.
Prerequisites
The avlset package requires Python 3.6+.
Installing avlset
avlset is nearly written using raw Python, except for pulling in the built-in typing module for type-hinting support in less modern Python versions. Downloading avlset.py and placing it in your project directory is to use. Alternatively, it can be installed from PyPI using pip:
pip install avlset
Using avlset
from avlset import AVLSet
# Create an instance of AVLSet. The type hint is optional, but recommended.
# Data must be comparable using <, =, and >.
# Using mismatched data types in the same set *will* cause issues.
# This will also work with more complex types like lists ans tuples, if the columns match.
# For example, (str, int) and (str, int) will compare just fine, but (str, str) and (str, int) will break everything.
# Using proper type hinting will help prevent this in larger applications.
myset: AVLSet[int] = AVLSet()
test_data = [3, 5, 2, 7, 2, 4, 2, 6, 2, 8, 7, 10]
# Insert data into set
for n in test_data:
myset.insert(n)
print(list(myset)) # [2, 3, 4, 5, 6, 7, 8, 10]
# Remove specific data from set
myset.remove(6)
print(list(myset)) # [2, 3, 4, 5, 7, 8, 10]
# Remove and retrieve first (min) item in set
print(myset.pop()) # 2 # OR myset.pop_min()
print(list(myset)) # [3, 4, 5, 7, 8, 10]
# Remove and retrieve last (max) item in set
print(myset.pop_max()) # 10
print(list(myset)) # [3, 4, 5, 7, 8]
# Iterating through set, ascending and descending
for n in myset:
print(n)
for n in reversed(myset):
print(n)
# Checking if items exist in set
print(4 in myset) # True
print(4 not in myset) # False
print(17 in myset) # False
print(17 not in myset) # True
# AVLSet is falsy when empty, and truthy when not
print(bool(myset)) # True
while myset: # Breaks when list is empty
myset.pop()
print(bool(myset)) # False
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file avlset-0.1.0.tar.gz
.
File metadata
- Download URL: avlset-0.1.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef2b600064ec7f4db2292ab241311a2271763b7ef345e3835f31a06e11e7dcf0 |
|
MD5 | 45345b33b88ac98322b4c8203137174a |
|
BLAKE2b-256 | 9b4eddc8d3970fb49fe57cf9e1ba27eec82a069ce98395aec4d6a80155232cd9 |
File details
Details for the file avlset-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: avlset-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7dc8f935b8c43d339ef25d3058824fbbe3cd79e887007dab3ae180d885c65c09 |
|
MD5 | 5c7d749773db65a12925858e70aae5bc |
|
BLAKE2b-256 | 43d9da1e3e111ce4c19469ed97f67d1b736c893acabfc841a3b6b6a6755039b3 |