A Red Black Tree implementation in python
Project description
RBTree-Py
A minimal, self-contained Red-Black Tree implementation in pure Python.
- Fast ordered inserts –
O(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
- Open the repository page in your browser.
- Download
rbtree.py(look for the Raw button or the Download file link). - Place
rbtree.pyalongside 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
Nodetracksside("LEFT","RIGHT", orNone) for easy debugging. verify_rb_treedepth‑first walks the tree, checking:- root is black,
parent/sideareNone - every node is red or black
- red nodes have black children
- equal black‑height on all paths to leaves
sidematches actual position
- root is black,
- Rotations update all pointers and
sidefields so invariants remain intact.
Contributing
- Fork → feature branch → pull request.
- Run
python -m rbtreebefore committing (CI rejects failures). - Open issues for ideas or improvements.
License
MIT © 2025 Theo Lincke
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file redblacktreepy-0.0.2.tar.gz.
File metadata
- Download URL: redblacktreepy-0.0.2.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0029b71e48824e7a0b9c53a7a9cf480c26462f552e55cef2c9a3c381f9c8eb91
|
|
| MD5 |
67c15acf2a9d3baf4dffd186aaa3fdc5
|
|
| BLAKE2b-256 |
f1d58ddbbd455cc4441ee2f7cea6e425772693ec6cb168210278dfff45693aeb
|
File details
Details for the file redblacktreepy-0.0.2-py3-none-any.whl.
File metadata
- Download URL: redblacktreepy-0.0.2-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14033745c2b93f66e157513ef063f889ebf8e924343d7536ae3cd759d707e62d
|
|
| MD5 |
95f19b940f18217e61cbe81e7762debb
|
|
| BLAKE2b-256 |
2f5e7af76542e238f3d30811b76ff5202df6b0247b6a1e641131f84811144dfd
|