Skip to main content
Pre-release

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 cowlist stack 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.

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)

Source distribution for tree-traversals 0.1.0a0
File Size Uploaded
tree_traversals-0.1.0a0.tar.gz 4.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tree-traversals 0.1.0a0
File Interpreter ABI Platform
tree_traversals-0.1.0a0-py2.py3-none-any.whl Python 2, Python 3 none any Details

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

Release history Release notifications | RSS feed

This release

0.1.0a0 This release

2 release files

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