━━━━━━━━━━ 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. ━━━━━━
Generic N-ary tree primitives and algorithms.
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.
pip install codechu-treedataPython 3.10+. Zero third-party dependencies.
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), …]Node— generic N-ary node with.name,.payload,.children,.parent;add()returns the inserted child.- Traversal —
dfs_pre,dfs_post,bfs. Pre-order DFS is the family default. - Search —
find,find_all,find_by_path. Predicate is a callable. - Mutation —
prune,map_tree,filter_tree.map_treeis out-of-place;pruneis in-place. - Serialization —
to_dict/from_dict(JSON-friendly),to_ascii(GNUtree(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).
- API reference — every public symbol with signatures and edge-case tables.
- Changelog
| 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.
- Standard tree algorithms; ASCII output follows GNU
tree(1)convention.
MIT — see LICENSE.
Part of Codechu.