easy-tree
easy-tree
is a lightweight Rust library for managing and traversing hierarchical data structures. It provides a simple interface for creating trees and supports recursive depth-first traversal with pre- and post-processing callbacks.
Key Features
- Depth-first traversal: Easily process nodes with callbacks before and after their subtrees.
- Simple API: Add, modify, and retrieve nodes effortlessly.
- Customizable traversal logic: Use callbacks to handle specific traversal behaviors.
- Optional parallel iteration: Boost performance with rayon.
Why Use easy-tree?
- Designed for Hierarchical Data: Ideal for file systems, DOM structures, organizational charts, and more.
- Traverse with Precision: Depth-first traversal with pre- and post-order processing in one simple call.
- Memory Efficient: Minimal overhead with direct references between nodes.
- Extensible: Integrates easily into larger systems and workflows.
Installation
Add easy-tree
to your Cargo.toml
:
[]
= "0.1"
To enable parallel iteration:
[]
= { = "0.1", = ["rayon"] }
How It Works
Tree Structure
Each node in the tree has:
- A data payload (your custom type)
- A single parent (or
None
for the root) - A list of child indices
Traversal Flow
Here’s an illustration of a depth-first traversal order:
Root
├── Child 1
│ └── Grandchild 1
├── Child 2
└── Child 3
Traversal output:
Visiting Child 1
Visiting Grandchild 1
Leaving Grandchild 1
Leaving Child 1
- ...
Examples
1. Basic Tree Creation
use Tree;
2. Depth-First Traversal
use Tree;
3. Parallel Iteration (Optional)
use Tree;
Use Cases
Represent a File System
use Tree;
Performance
- Low Memory Overhead: Nodes are stored contiguously in a vector.
- Efficient Traversal: Iterative depth-first traversal minimizes recursion overhead.
- Parallel Ready: Enable the
rayon
feature for concurrent processing.
Advanced Features
- Custom Traversal Logic: Use pre- and post-processing callbacks for fine-grained control.
- Flexible Node Access: Retrieve, update, or delete nodes efficiently.