Metadata-Version: 2.4
Name: pytreeds
Version: 0.2.2
Summary: Beginner-friendly rooted trees with consistent string navigation and traversal
Author: pytreeds contributors
License-Expression: MIT
Project-URL: Source, https://gitlab.com/ujen.teaching/pytreeds
Project-URL: Issues, https://gitlab.com/ujen.teaching/pytreeds/-/issues
Keywords: tree,data structures,education,dfs,bfs,pyodide
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Education
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Education
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# pytreeds

A dependency-free, beginner-focused Python tree library suitable for Pyodide. One `Tree` variable holds all nodes, and navigation/traversal uses names (strings) consistently. Attach optional Python data to individual nodes.

**Version:** The current source release is `0.2.2`. See the [PyPI project page](https://pypi.org/project/pytreeds/) for the latest published version.

## Quick start

```python
from pytreeds import Tree

org = Tree()
org.add_node("CEO", data={"level": 1})
org.add_node("CTO", parent="CEO", data=[90, 85])
org.add_node("CFO", parent="CEO")
org.add_node("COO", parent="CEO")
org.add_node("Engineering", parent="CTO")
org.add_node("DevOps", parent="CTO")
print(org)

print(org.get_node_data("CTO"))  # [90, 85]
org.set_node_data("CTO", {"team": "Technology"})
print(org.get_node_data("CTO"))

print(list(org.dfs()))
print(list(org.bfs()))

org.move_node("CTO", parent="COO")  # The entire CTO subtree moves
org.delete_node("CFO")             # Works on leaf or subtree
print(org)

# Three layouts for the same tree
print(org.get_diagram(style="branch"))
print(org.get_diagram(style="horizontal"))
print(org.get_diagram(style="vertical"))

# Include node data or display a smaller subtree
print(org.get_diagram("CTO", max_depth=1, show_data=True))
```

Omitting `parent` in `add_node()` creates a root **only when the tree is empty**. Every later node requires a parent. One `Tree` contains exactly one root.

## Install

Regular Python: `pip install pytreeds`

Pyodide (asynchronous package loader):

```python
import micropip
await micropip.install("pytreeds")
```

## API summary

**Build:** `Tree()`, `add_node(name, parent=None, data=None)`.

**Edit:** `delete_node()`, `move_node()`, `rename_node()`, `copy()`. Removing a node removes all its descendants; moving one moves its entire subtree.

**Inspect:** `get_root()`, `get_size()`, `get_height()`, `get_depth()`, `get_leaf_count()`, `is_leaf()`, `contains()`.

**Relationships:** `get_parent()`, `get_children()`, `get_siblings()`, `get_ancestors()`, `get_descendants()`, `get_leaves()`.

**Data:** `get_node_data(name)`, `set_node_data(name, data)`. Any Python object can be attached, including lists and dictionaries. Values are returned by reference; `copy()` deep-copies data, so objects incompatible with `deepcopy()` may cause an error when copying.

**Traversal:** `dfs()`, `bfs()`, and ordinary `for name in tree`. The `dfs()` method uses left-to-right preorder (visits parents before their children).

**Text:** `print(tree)` for display, or `get_diagram(name=None, style='branch', max_depth=None, show_data=False)` to retrieve a diagram as a string.

See the [illustrated terminology guide](docs/index.rst) and the [Detailed Reference](docs/api.rst), with a dedicated page for every method.

## Local tests

```bash
python -m unittest discover -s tests -v
```

Releases are gated by automated unit tests and a strict Sphinx documentation build.
