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.

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
โ”‚        โ”‚  โ”œโ”€โ”€ base.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, SinglyLinkedList, 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 = SinglyLinkedList([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, to_list
  • Iteration support (__iter__)

Queues โœ…

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

Linked Lists โœ…

  • SinglyLinkedList
    • 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 SinglyLinkedList
    • 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
  • โœ… SinglyLinkedList - 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.7.tar.gz (10.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.7-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py_ds_academy-0.5.7.tar.gz
  • Upload date:
  • Size: 10.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.7.tar.gz
Algorithm Hash digest
SHA256 884ba81b49e5364fd8005b83ee92095106161606ca7bcd08f1e0f1c9b2e8395c
MD5 78d78a763abe648f9e50358b23a30674
BLAKE2b-256 162df9c32902eac2a5e9234a90d7d3cd90650e9baf107d72d51a4a29367c6028

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_ds_academy-0.5.7-py3-none-any.whl
  • Upload date:
  • Size: 17.1 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.7-py3-none-any.whl
Algorithm Hash digest
SHA256 53fd67a07b0786fe06069652df0fbc1e725de4bfda3b6e6e8828fbee86eb8572
MD5 5a97744ae7b8f9327477533b378b9b01
BLAKE2b-256 09b84e6e4e356b06b99d02cc181cb5e64399928d6f4e0f2f70f7726922fbf03a

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