┌──────────────────────────────────────────────────┐
│ 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.
Metadata
Release files for codechu-treedata 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| codechu_treedata-0.1.0.tar.gz | 12.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| codechu_treedata-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.2 kB
Release files / codechu_treedata-0.1.0.tar.gz
| Download URL | codechu_treedata-0.1.0.tar.gz |
|---|---|
| Size | 12.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cf48855f051e62ae00ca06d5f781b447bc9831275e8eac88e74740ab29123aa3
|
|
BLAKE2b-256 checksum How to use checksums |
e6384a15fc0dc3fe0aa72a1e653043225d9200a29fefce9d07762d99aa685ac3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 20, 2026.
Transparency logRelease files / codechu_treedata-0.1.0-py3-none-any.whl
| Download URL | codechu_treedata-0.1.0-py3-none-any.whl |
|---|---|
| Size | 10.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
92d022691667add19299902f1fcb7397f0fd4c9d74b4db7c49d303b69b337947
|
|
BLAKE2b-256 checksum How to use checksums |
41516b06b02aa6ab729b4bd5f2e32f302551700cb62becd1afc52b804a3fe776
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 20, 2026.
Transparency log