Skip to content

Repository files navigation

━━━━━━━━━━ c o d e c h u  ·  t r e e d a t a ━━━━━━━━━━

   build              traverse              serialize
   ─────              ────────              ─────────
   root               root                  root
   ├── a       →      ├── a       →         ├── a
   │   └── a1         │   └── a1            │   └── a1
   └── b              └── b                 └── b
                                            (dfs_pre / bfs / to_dict / to_dot)

━━━━━━ N-ary trees. opaque payload. zero deps. ━━━━━━

PyPI Python CI License: MIT

Generic N-ary tree primitives and algorithms.

codechu-treedata

Stdlib-only N-ary tree data structures: a generic Node, traversal (DFS/BFS), search, prune/map/filter, JSON + ASCII + DOT serialization, structural diff/merge, and an O(1) lookup index. No domain coupling — the payload is opaque, works for any hierarchy.

Install

pip install codechu-treedata

Python 3.10+. Zero third-party dependencies.

Quick example

from codechu_treedata import Node, dfs_pre, find, to_ascii, diff

root = Node("root", payload=1)
a = root.add(Node("a", 2))
a.add(Node("a1", 3))
root.add(Node("b", 5))

[n.name for n in dfs_pre(root)]                    # ['root', 'a', 'a1', 'b']
find(root, lambda n: n.payload == 3)               # Node('a1')

print(to_ascii(root))
# root
# ├── a
# │   └── a1
# └── b

diff(old_tree, root)                               # [(path, action, payload), …]

What you get

  • Node — generic N-ary node with .name, .payload, .children, .parent; add() returns the inserted child.
  • Traversaldfs_pre, dfs_post, bfs. Pre-order DFS is the family default.
  • Searchfind, find_all, find_by_path. Predicate is a callable.
  • Mutationprune, map_tree, filter_tree. map_tree is out-of-place; prune is in-place.
  • Serializationto_dict / from_dict (JSON-friendly), to_ascii (GNU tree(1) style), to_dot (Graphviz), to_lines (one node per line).
  • diff / merge — structural delta and three-way merge with caller-supplied conflict resolver.
  • Index — O(1) name→node lookup, snapshot semantics (rebuild after mutations; keeps the read path branch-free).

Read more

Family

Library Purpose
codechu-treeviz Treemap + sunburst layouts
codechu-fs Filesystem primitives — atomic write, XDG trash
codechu-fmt Human-readable sizes, durations, rates
codechu-cli CLI primitives — colors, progress, prompts
codechu-config Schema-driven config — atomic save, migrations

Full ecosystem: github.com/codechu.

Credits

  • Standard tree algorithms; ASCII output follows GNU tree(1) convention.

License

MIT — see LICENSE.

Part of Codechu.

About

N-ary tree data structures — traversal, search, prune, diff/merge, serializers

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages