Stdlib-only N-ary tree data structures and algorithms.
Project description
┌──────────────────────────────────────────────────┐
│ c o d e c h u — t r e e d a t a │
│ root │
│ ├── a │
│ │ └── a1 │
│ └── b │
│ ······N-ary trees, in and out of memory········ │
└──────────────────────────────────────────────────┘
Generic N-ary tree primitives and algorithms.
codechu-treedata
Stdlib-only N-ary tree data structures — a generic Node, traversal,
search, prune/map/filter, JSON & ASCII & DOT serialization, structural
diff/merge, and an O(1) lookup index. No domain coupling: works for
any hierarchy. Python 3.10+.
Install
pip install codechu-treedata
API
from codechu_treedata import (
Node, Index,
dfs_pre, dfs_post, bfs,
find, find_all, find_by_path,
prune, map_tree, filter_tree,
to_dict, from_dict, to_ascii, to_dot, to_lines,
diff, merge,
)
root = Node("root", payload=1)
a = root.add(Node("a", 2))
a.add(Node("a1", 3))
root.add(Node("b", 5))
# Traversal
[n.name for n in dfs_pre(root)] # ['root', 'a', 'a1', 'b']
[n.name for n in bfs(root)] # ['root', 'a', 'b', 'a1']
# Search
find(root, lambda n: n.payload == 3) # → Node('a1')
find_by_path(root, ["root", "a", "a1"])
# Mutation
prune(root, lambda n: n.name == "a") # in place
double = map_tree(root, lambda p: (p or 0) * 2)
# Serialization
print(to_ascii(root))
to_dict(root) # JSON-friendly
from_dict(d) # round-trip
# Diff / merge
diff(old_tree, new_tree) # [(path, action, payload), …]
merge(a, b, conflict_resolver=lambda na, nb: max(na.payload, nb.payload))
# O(1) index (snapshot — rebuild after mutations)
idx = Index(root)
idx.get("a1")
Design
- Pure stdlib. Zero third-party dependencies.
- Generic.
payloadis opaque — attach any object. The library never inspects it (except for equality indiff/merge). - Predictable. Pre-order DFS is the default for
find_all,to_lines, andIndexinsertion order. - Snapshot index.
Indexis built once; structural changes do not auto-invalidate it. Callers rebuild after mutations. Keeps the read path branch-free.
Tests
pip install -e ".[dev]"
ruff check .
pytest -q
License
MIT — see LICENSE.
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 codechu_treedata-0.1.0.tar.gz.
File metadata
- Download URL: codechu_treedata-0.1.0.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf48855f051e62ae00ca06d5f781b447bc9831275e8eac88e74740ab29123aa3
|
|
| MD5 |
af4d409c79357f6e94e68d4d386e840d
|
|
| BLAKE2b-256 |
e6384a15fc0dc3fe0aa72a1e653043225d9200a29fefce9d07762d99aa685ac3
|
Provenance
The following attestation bundles were made for codechu_treedata-0.1.0.tar.gz:
Publisher:
release.yml on codechu/treedata-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
codechu_treedata-0.1.0.tar.gz -
Subject digest:
cf48855f051e62ae00ca06d5f781b447bc9831275e8eac88e74740ab29123aa3 - Sigstore transparency entry: 1582339676
- Sigstore integration time:
-
Permalink:
codechu/treedata-py@ebce415f28f1cca54011d454a6de5d866a8c0fbc -
Branch / Tag:
refs/heads/main - Owner: https://github.com/codechu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@ebce415f28f1cca54011d454a6de5d866a8c0fbc -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file codechu_treedata-0.1.0-py3-none-any.whl.
File metadata
- Download URL: codechu_treedata-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92d022691667add19299902f1fcb7397f0fd4c9d74b4db7c49d303b69b337947
|
|
| MD5 |
a618cc0257f1b2d3920c181bd1b1e385
|
|
| BLAKE2b-256 |
41516b06b02aa6ab729b4bd5f2e32f302551700cb62becd1afc52b804a3fe776
|
Provenance
The following attestation bundles were made for codechu_treedata-0.1.0-py3-none-any.whl:
Publisher:
release.yml on codechu/treedata-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
codechu_treedata-0.1.0-py3-none-any.whl -
Subject digest:
92d022691667add19299902f1fcb7397f0fd4c9d74b4db7c49d303b69b337947 - Sigstore transparency entry: 1582339841
- Sigstore integration time:
-
Permalink:
codechu/treedata-py@ebce415f28f1cca54011d454a6de5d866a8c0fbc -
Branch / Tag:
refs/heads/main - Owner: https://github.com/codechu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@ebce415f28f1cca54011d454a6de5d866a8c0fbc -
Trigger Event:
workflow_dispatch
-
Statement type: