High-performance red-black tree implementation in C with Python bindings
Project description
py-rbtree
A high-performance red-black tree implementation in C with Python bindings.
Features
- Fast: O(log n) insert, search, and delete operations
- Self-balancing: Automatically maintains optimal tree structure
Installation
make build
Quick Start
import rbtree
# Create a tree
tree = rbtree.RBTree()
# Insert key-value pairs
tree.insert(10, "ten")
tree.insert(5, "five")
tree.insert(15, "fifteen")
# Search
value = tree.search(10) # Returns "ten"
# Check membership
if 10 in tree:
print("Found!")
# Size
print(len(tree)) # 3
# Sorted traversal
items = tree.inorder() # [(5, 'five'), (10, 'ten'), (15, 'fifteen')]
# Delete
tree.delete(5)
API Reference
RBTree()
Create a new red-black tree.
insert(key: int, value: Any) -> None
Insert a key-value pair. Keys must be integers. Values can be any Python object.
Raises: ValueError if key already exists.
search(key: int) -> Any
Search for a value by key.
Raises: KeyError if key not found.
delete(key: int) -> None
Delete a key from the tree.
Raises: KeyError if key not found.
inorder() -> List[Tuple[int, Any]]
Return inorder traversal as a sorted list of (key, value) tuples.
len(tree) -> int
Return the number of items in the tree.
key in tree -> bool
Check if a key exists in the tree.
Running Tests
make test
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
py_red_black_tree-1.0.0.tar.gz
(44.2 kB
view details)
File details
Details for the file py_red_black_tree-1.0.0.tar.gz.
File metadata
- Download URL: py_red_black_tree-1.0.0.tar.gz
- Upload date:
- Size: 44.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2062aac9171e4121795ff8afe4850d39ce609a90deaff947aef2d0541f1d8848
|
|
| MD5 |
a12d4ee70ae14acc355ad626a0f65910
|
|
| BLAKE2b-256 |
e37b63294329758f85412fd9582e09a7360efc2559cc5812a75e2c3870de14fc
|