This release is a pre-release and may not be stable for production use.
tree-traversals
Efficient and generic traversals for any Python tree structure with full ancestor paths.
Installation
pip install tree-traversals
Supported Traversal Functions
| Traversal Function | Order | Yields |
|---|---|---|
pre_order_dfs |
Node, then children (depth-first) | (ancestry, node) in preorder |
post_order_dfs |
Children, then node (depth-first) | (ancestry, node) in postorder |
layer_order_bfs |
By tree level (breadth-first) | (ancestry, node) in layer order |
All traversals:
- Take a starting node and a function to get descendants,
- Return an immutable
cowliststack representing the full path from root to the current node, along with the current node itself.
Usage
# coding=utf-8
from __future__ import print_function
from typing import Dict, List
from tree_traversals import (
pre_order_dfs,
post_order_dfs,
layer_order_bfs
)
# Consider a simple binary tree (as a dict)
# - A
# - B
# - D
# - E
# - C
# - F
tree = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': [],
'F': []
} # type: Dict[str, List[str]]
def get_children(n):
# type: (str) -> List[str]
return tree[n]
# Then, you can perform any traversal easily:
print("Preorder DFS:")
for path, node in pre_order_dfs('A', get_children):
print(repr(path), '->', repr(node))
# Preorder DFS:
# COWList([]) -> 'A'
# COWList(['A']) -> 'B'
# COWList(['A', 'B']) -> 'D'
# COWList(['A', 'B']) -> 'E'
# COWList(['A']) -> 'C'
# COWList(['A', 'C']) -> 'F'
print("\nPostorder DFS:")
for path, node in post_order_dfs('A', get_children):
print(repr(path), '->', repr(node))
# Postorder DFS:
# COWList(['A', 'B']) -> 'D'
# COWList(['A', 'B']) -> 'E'
# COWList(['A']) -> 'B'
# COWList(['A', 'C']) -> 'F'
# COWList(['A']) -> 'C'
# COWList([]) -> 'A
print("\nLayer Order BFS:")
for path, node in layer_order_bfs('A', get_children):
print(repr(path), '->', repr(node))
# Layer Order BFS
# COWList([]) -> 'A'
# COWList(['A']) -> 'B'
# COWList(['A']) -> 'C'
# COWList(['A', 'B']) -> 'D'
# COWList(['A', 'B']) -> 'E'
# COWList(['A', 'C']) -> 'F'
Motivation
When working with trees, structure is easy - but traversal patterns are often rewritten, customized, and bug-prone.
tree-traversals lets you:
- Traverse any tree with a single function and a child-getter.
- Access the full ancestor path at every node.
- Clean up and de-duplicate your recursive tree code.
- Support any tree-shaped data—dicts, lists, ASTs, DOMs, file trees, you name it.
Contributing
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
License
This project is licensed under the MIT License.
Metadata
Release files for tree-traversals 0.1.0a0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tree_traversals-0.1.0a0.tar.gz | 4.0 kB | Without provenance | Details for tree_traversals-0.1.0a0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tree_traversals-0.1.0a0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for tree_traversals-0.1.0a0-py2.py3-none-any.whl |
Total release size: 8.4 kB
Release files / tree_traversals-0.1.0a0.tar.gz
| Download URL | tree_traversals-0.1.0a0.tar.gz |
|---|---|
| Size | 4.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2e33acce1b819f272be87f826ccd0658dea149a5128a7871de8e892165ff70a3
|
|
BLAKE2b-256 checksum How to use checksums |
1a0ded593a44fdd71a0623d79f011d235339c1c78e09aed989ec56435615260a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.2
|
Release files / tree_traversals-0.1.0a0-py2.py3-none-any.whl
| Download URL | tree_traversals-0.1.0a0-py2.py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
c993b7979bddfb370e31ff004909eccdfd7b9cd7b995c0ae72b315d005b20374
|
|
BLAKE2b-256 checksum How to use checksums |
363c9839c1bd004320787033f91af214760688ba88d4828701b3bea8ac419a78
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.2
|
Security for tree-traversals 0.1.0a0
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.