Metadata-Version: 2.4
Name: codechu-treedata
Version: 0.1.0
Summary: Stdlib-only N-ary tree data structures and algorithms.
Author: Codechu
License: MIT License
        
        Copyright (c) 2026 Codechu
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/codechu/treedata-py
Project-URL: Source, https://github.com/codechu/treedata-py
Project-URL: Issues, https://github.com/codechu/treedata-py/issues
Keywords: tree,data-structures,n-ary,traversal,stdlib
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-cov>=4.1; extra == "dev"
Requires-Dist: ruff>=0.4; extra == "dev"
Dynamic: license-file

```text
   ┌──────────────────────────────────────────────────┐
   │  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

```bash
pip install codechu-treedata
```

## API

```python
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

```bash
pip install -e ".[dev]"
ruff check .
pytest -q
```

## License

MIT — see [LICENSE](LICENSE).
