Skip to main content

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 for the latest published version.

Quick start

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):

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 and the Detailed Reference, with a dedicated page for every method.

Local tests

python -m unittest discover -s tests -v

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

Metadata

Release files for pytreeds 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytreeds 0.2.2
File Size Uploaded Provenance Actions
pytreeds-0.2.2.tar.gz 9.7 kB Without provenance Details for pytreeds-0.2.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytreeds 0.2.2
File Interpreter ABI Platform Actions
pytreeds-0.2.2-py3-none-any.whl Python 3 none any Details for pytreeds-0.2.2-py3-none-any.whl

Total release size: 17.1 kB

Release files / pytreeds-0.2.2.tar.gz

Download URL pytreeds-0.2.2.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
58d0c9ef93707a03b0fe3342b1cdd69e2c96bcf7f6816367522932885d1f6492
BLAKE2b-256 checksum
How to use checksums
1b7b53fb7b4cb405fcc5fa6d5fa90d55d8971ac06e2d3013557b9b08ab9e550c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.12.16

Release files / pytreeds-0.2.2-py3-none-any.whl

Download URL pytreeds-0.2.2-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
86c22c1cc7859684a99b1524176187190649f574988fdee37af66512e1ea7990
BLAKE2b-256 checksum
How to use checksums
7fa607830bc95054f7e9fe64a4442444f906093ae230a7c537952165bb45aa08
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.12.16

Security for pytreeds 0.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page