A pure python3 red black tree implementation
Project description
Pure python3 implementation of a red black tree can be used as a set or a dictionary or both at the same time. It is tested and stable. If you need any feature implemented or god forbid find a bug just open an issue on https://github.com/leryss/py-redblacktree
Installation
pip install redblacktree
Examples
>>> from redblacktree import rbtree
# Can initialize using lists, dicts or other iterables
>>> rbtree([1,2,3,4,5,6,7,8])
>>> Depth=4
┌───────────────4───────────────┐
┌───────2───────┐ ┌───────6───────┐
1 3 5 7───┐
8
# You can store values with keys or not or both
>>> rbtree([1,2,(3,'three'),4,5,(6,'six'),7,8])
>>> Depth=4
┌───────────────4───────────────┐
┌───────2───────┐ ┌─────6:six─────┐
1 3:three 5 7───┐
8
# Inserting
tree.insert(5) # Inserts a key with no value
tree[5] = None # Same as above
tree.insert(5, 'five')
tree[5] = 'five'
# Removing
tree.remove(5)
del tree[5]
# Query
x = tree[5]
if 5 in tree:
print('5 is in the tree')
# Slicing also supported
tree[5:10] # Returns (key, value) pairs of all keys >= 5 and <= 10
tree[:5] # All (key, value) pairs for keys <= 5
# Beware slicing with a step for example tree[1:100:-1] wont work
# Simple iteration:
for k, v in tree:
print(k, v)
# Iterate in reverse order
for k, v in reversed(tree):
print(k, v)
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
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 redblacktree-1.0.3.tar.gz.
File metadata
- Download URL: redblacktree-1.0.3.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2382b63cc221786f7d4fba136e828d1071fe694e7c8aac8363dd50433df6c536
|
|
| MD5 |
b8c6814cf5590714f0a35c5577a2a42a
|
|
| BLAKE2b-256 |
239faf23d94bd2cd6476a727a02470573c7aef982902e5b77a73249dd7d5969c
|
File details
Details for the file redblacktree-1.0.3-py3-none-any.whl.
File metadata
- Download URL: redblacktree-1.0.3-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abef057278fa3bee3f6468cca76ab1de91248e5e1005f03c9a542465898986be
|
|
| MD5 |
53fac6c99e95c9c659a6bb493edbb972
|
|
| BLAKE2b-256 |
0f95dde2eeb0fef6ff5b802d9e5efa4a341a05d843238fdfe88014cc176f88a9
|