GithubHelp home page GithubHelp logo

avlset's Introduction

avlset

GitHub repo size GitHub contributors GitHub stars GitHub forks PyPI

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

avlset's People

Watchers

Gabe Rust avatar

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.