Skip to main content

Data structures implemented from scratch for learning and experimentation.

Project description

py-ds-academy

PyPI - Version PyPI - License CI status PyPI - Python Version

A small playground project for implementing classic data structures from scratch in Python.

๐Ÿ“– Documentation & Installation

pip install py-ds-academy

The goal is learning + correctness (with tests), not squeezing out every last micro-optimization.


๐Ÿงฑ Project Goals

  • Implement core data structures from scratch in Python
  • Use type hints, clean APIs, and unit tests
  • Compare different implementations (e.g., list-backed vs linked)
  • Practice algorithmic reasoning & complexity analysis

๐Ÿ“ฆ Project Layout

py-ds-academy/
โ”œโ”€ pyproject.toml
โ”œโ”€ README.md
โ”œโ”€ .python-version
โ”œโ”€ src/
โ”‚  โ””โ”€โ”€ py_ds/
โ”‚     โ”œโ”€โ”€ __init__.py
โ”‚     โ””โ”€โ”€ datastructures/
โ”‚        โ”œโ”€โ”€ __init__.py
โ”‚        โ”œโ”€โ”€ stack.py
โ”‚        โ”œโ”€โ”€ queue.py
โ”‚        โ”œโ”€โ”€ heaps.py
โ”‚        โ”œโ”€โ”€ linked_lists/
โ”‚        โ”‚  โ”œโ”€โ”€ __init__.py
โ”‚        โ”‚  โ”œโ”€โ”€ singly_linked.py
โ”‚        โ”‚  โ””โ”€โ”€ doubly_linked.py
โ”‚        โ””โ”€โ”€ trees/
โ”‚           โ”œโ”€โ”€ __init__.py
โ”‚           โ”œโ”€โ”€ base.py
โ”‚           โ”œโ”€โ”€ binary_search_tree.py
โ”‚           โ””โ”€โ”€ avl.py
โ””โ”€ tests/
   โ”œโ”€ test_stack.py
   โ”œโ”€ test_queue.py
   โ”œโ”€ test_linked_list.py
   โ”œโ”€ test_doubly_linked_list.py
   โ”œโ”€ test_max_heap.py
   โ”œโ”€ test_min_heap.py
   โ”œโ”€ test_binary_search_tree.py
   โ””โ”€ test_avl_tree.py

All importable code lives under src/py_ds/.


๐Ÿš€ Getting Started

Requires uv.

# create venv from .python-version
uv venv

# install dependencies (if any)
uv sync

# run tests
uv run pytest

You can also drop into a REPL:

uv run python
>>> from py_ds import Stack, Queue, LinkedList, DoublyLinkedList
>>> from py_ds import MinHeap, MaxHeap, BinarySearchTree, AVLTree

>>> # Stack example
>>> s = Stack([1, 2, 3])
>>> s.pop()
3

>>> # Queue example
>>> q = Queue([1, 2, 3])
>>> q.dequeue()
1

>>> # Linked List example
>>> ll = LinkedList([1, 2, 3])
>>> ll.append(4)
>>> list(ll)
[1, 2, 3, 4]

>>> # Heap example
>>> h = MinHeap([3, 1, 4, 1, 5])
>>> h.pop()
1

>>> # BST example
>>> bst = BinarySearchTree([5, 3, 7, 2, 4])
>>> list(bst.inorder())
[2, 3, 4, 5, 7]

๐Ÿ“š Data Structures Roadmap

1. Linear Structures

Stacks โœ…

  • Stack backed by Python list
  • Operations: push, pop, peek, is_empty, __len__, clear, extend, __list__
  • Iteration support (__iter__)

Queues โœ…

  • Queue backed by Python list
  • Operations: enqueue, dequeue, peek, is_empty, __len__, clear, extend, __list__
  • Iteration support (__iter__)

Linked Lists โœ…

  • LinkedList
    • append, prepend, insert, remove, pop, find
    • Iteration support (__iter__)
    • Indexing support (__getitem__, __setitem__)
    • head(), tail(), clear()
  • DoublyLinkedList
    • Efficient O(1) append and prepend (with tail pointer)
    • Bidirectional traversal (__iter__, reverse_iter)
    • All operations from LinkedList
    • Optimized indexing with bidirectional search

2. Trees

Binary Tree (generic node-based) โœ…

  • BinaryTree base class with _BinaryNode
  • Traversals:
    • Preorder (preorder())
    • Inorder (inorder())
    • Postorder (postorder())
    • Level-order / BFS (level_order())
  • Tree height calculation
  • Tree visualization (__str__)

Binary Search Tree (BST) โœ…

  • BinarySearchTree implementation
  • Insert
  • Search (__contains__)
  • Delete (remove) - handles 0, 1, 2 children
  • Find min / max (min(), max())
  • Inherits all traversals from BinaryTree

Self-Balancing Trees โœ…

  • AVLTree - self-balancing BST
    • Automatic rebalancing on insert/remove
    • Rotations: left, right, left-right, right-left
    • Balance factor calculation
    • Inherits all BST operations

3. Heaps / Priority Queues โœ…

Binary Heap

  • Heap abstract base class
  • MinHeap implementation
  • MaxHeap implementation
  • Operations: push, pop, peek
  • Heap construction from iterable
  • heapify_up and heapify_down operations
  • Use cases: priority queue, heap sort

4. Hash-Based Structures

Hash Map

  • Array of buckets
  • Collision handling via chaining (linked lists) or open addressing
  • Operations: get, set, delete, __contains__
  • Basic resizing & load factor

Hash Set

  • Built on top of HashMap
  • Operations: add, remove, contains, iteration

5. Graphs

Graph Representations

  • Adjacency list representation
  • Optional: adjacency matrix

Algorithms

  • BFS (breadth-first search)
  • DFS (depth-first search)
  • Path search (e.g. has_path(u, v))

Stretch:

  • Topological sort
  • Dijkstraโ€™s algorithm (weighted graphs)

โœจ Implemented Features Summary

The following data structures are fully implemented and tested:

  • โœ… Stack - LIFO stack with list backing
  • โœ… Queue - FIFO queue with list backing
  • โœ… LinkedList - Single-direction linked list
  • โœ… DoublyLinkedList - Double-direction linked list with O(1) append/prepend
  • โœ… MinHeap - Minimum binary heap
  • โœ… MaxHeap - Maximum binary heap
  • โœ… BinarySearchTree - Binary search tree with insert, remove, search, min/max
  • โœ… AVLTree - Self-balancing AVL tree (extends BST)

๐Ÿงช Testing

Each data structure gets its own test module under tests/.

Run the whole suite:

uv run pytest

๐Ÿง  Design Principles

  • Prefer clear, readable code over cleverness
  • Use type hints everywhere
  • Raise the right built-in exceptions
  • Document time complexity in docstrings

This project is mainly for learning + fun. No guarantees โ€” just data structures implemented by hand.

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

py_ds_academy-0.5.19.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

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

py_ds_academy-0.5.19-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file py_ds_academy-0.5.19.tar.gz.

File metadata

  • Download URL: py_ds_academy-0.5.19.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.15 {"installer":{"name":"uv","version":"0.9.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for py_ds_academy-0.5.19.tar.gz
Algorithm Hash digest
SHA256 a0740e3d340c3b4ac3ca2637ba3ea96e36fed7af125b01f5142c2170673ca593
MD5 c0676063a45786db0400f0cf6927e203
BLAKE2b-256 e319a58bd7df6f99b27e2a03c6b54e87c426f5a69f52f184e74b0773c624f90d

See more details on using hashes here.

File details

Details for the file py_ds_academy-0.5.19-py3-none-any.whl.

File metadata

  • Download URL: py_ds_academy-0.5.19-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.15 {"installer":{"name":"uv","version":"0.9.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for py_ds_academy-0.5.19-py3-none-any.whl
Algorithm Hash digest
SHA256 9865eec4abcdadffed5a21ee08aadc84cabab1b49f2ab73a436201aea677219c
MD5 a7b4173ebb178a89ae7128efc2c81e61
BLAKE2b-256 549769aea1b555f0d0b9ca01f7e4430f0de860b1a8c2366fda9f89bbd730abd5

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