Skip to main content

A Red Black Tree implementation in python

Project description

RBTree-Py

A minimal, self-contained Red-Black Tree implementation in pure Python.

  • Fast ordered insertsO(log n) balancing via rotations
  • Pretty printing – human-readable ASCII layout
  • Pluggable keys – override two small methods to store any comparable object
  • Property checker – verifies all five RB-tree invariants
  • Stress test – inserts 5 000 random nodes on every run

Installation

  1. Open the repository page in your browser.
  2. Download rbtree.py (look for the Raw button or the Download file link).
  3. Place rbtree.py alongside your own code and import it:
from rbtree import Node, RBTree

No build steps, compilers, or package managers required—just a single file.
Works on Python 3.9 + with no third‑party dependencies.


Quick start

from rbtree import Node, RBTree

tree = RBTree()
tree.insert(Node(42))
tree.insert(Node(8))
tree.insert(Node(1337))

print(tree)
========= RBTree
[42 BLACK None]
l:    [8 RED LEFT]
r:    [1337 RED RIGHT]

Run the built‑in stress test

python -m rbtree          # inserts 5 000 random values and verifies the tree

Public API

Object Description
class RBTree Container with insert(node) and __str__()
class Node(value) Default integer key node
verify_rb_tree(tree) Raises AssertionError if any RB property fails
test() Stress test: populates random nodes & verifies

Custom keys

Override two hooks:

class PersonNode(Node):
    def __init__(self, name: str, age: int):
        super().__init__((name, age))
        self.name, self.age = name, age

    def compare_for_right(self, other: "PersonNode") -> bool:
        return self.age > other.age          # order by age

    def _label(self) -> str:
        return f"{self.name}:{self.age} {self.color}"

Everything else—rotations, recolouring, verification—stays the same.


Implementation notes

  • Satisfies the five textbook RB-tree rules (enumerated inside the code).
  • Each Node tracks side ("LEFT", "RIGHT", or None) for easy debugging.
  • verify_rb_tree depth‑first walks the tree, checking:
    1. root is black, parent/side are None
    2. every node is red or black
    3. red nodes have black children
    4. equal black‑height on all paths to leaves
    5. side matches actual position
  • Rotations update all pointers and side fields so invariants remain intact.

Contributing

  1. Fork → feature branch → pull request.
  2. Run python -m rbtree before committing (CI rejects failures).
  3. Open issues for ideas or improvements.

License

MIT © 2025 Theo Lincke

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

redblacktreepy-0.0.1.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

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

redblacktreepy-0.0.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file redblacktreepy-0.0.1.tar.gz.

File metadata

  • Download URL: redblacktreepy-0.0.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for redblacktreepy-0.0.1.tar.gz
Algorithm Hash digest
SHA256 e7d968a147b4d7631318196d0197fc97d9979334d0f4f2fa772dc426270e9f35
MD5 89cdf7d05fa1e50adb6db47f90bbe40b
BLAKE2b-256 cb41a11e411226a1096f648fa6a3b897f6a80ef65e307821001b0a2f81d4b16e

See more details on using hashes here.

File details

Details for the file redblacktreepy-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: redblacktreepy-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for redblacktreepy-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b4d36e9ce925553151ddfdf27477d654197d2c5220ee553adbc10051da0b678e
MD5 905eb349d1036be61fb524e31fe0ec50
BLAKE2b-256 b83f55d4bca1f020114099c95718d9bd4bbd450733bfaf92389961b1d7a2eb51

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