Skip to main content

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. payload is opaque — attach any object. The library never inspects it (except for equality in diff / merge).
  • Predictable. Pre-order DFS is the default for find_all, to_lines, and Index insertion order.
  • Snapshot index. Index is 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

codechu_treedata-0.1.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

codechu_treedata-0.1.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

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

Hashes for codechu_treedata-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cf48855f051e62ae00ca06d5f781b447bc9831275e8eac88e74740ab29123aa3
MD5 af4d409c79357f6e94e68d4d386e840d
BLAKE2b-256 e6384a15fc0dc3fe0aa72a1e653043225d9200a29fefce9d07762d99aa685ac3

See more details on using hashes here.

Provenance

The following attestation bundles were made for codechu_treedata-0.1.0.tar.gz:

Publisher: release.yml on codechu/treedata-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file codechu_treedata-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for codechu_treedata-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 92d022691667add19299902f1fcb7397f0fd4c9d74b4db7c49d303b69b337947
MD5 a618cc0257f1b2d3920c181bd1b1e385
BLAKE2b-256 41516b06b02aa6ab729b4bd5f2e32f302551700cb62becd1afc52b804a3fe776

See more details on using hashes here.

Provenance

The following attestation bundles were made for codechu_treedata-0.1.0-py3-none-any.whl:

Publisher: release.yml on codechu/treedata-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page