Efficient and generic traversals for any Python tree structure with full ancestor paths.
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tree_traversals-0.1.0a0.tar.gz.
File metadata
- Download URL: tree_traversals-0.1.0a0.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e33acce1b819f272be87f826ccd0658dea149a5128a7871de8e892165ff70a3
|
|
| MD5 |
0bb2c82c2835f245a8085f2379c66306
|
|
| BLAKE2b-256 |
1a0ded593a44fdd71a0623d79f011d235339c1c78e09aed989ec56435615260a
|
File details
Details for the file tree_traversals-0.1.0a0-py2.py3-none-any.whl.
File metadata
- Download URL: tree_traversals-0.1.0a0-py2.py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c993b7979bddfb370e31ff004909eccdfd7b9cd7b995c0ae72b315d005b20374
|
|
| MD5 |
b66f524e6b226da969356d1d82efb6fc
|
|
| BLAKE2b-256 |
363c9839c1bd004320787033f91af214760688ba88d4828701b3bea8ac419a78
|