Crates.io | easy-tree |
lib.rs | easy-tree |
version | 0.1.3 |
source | src |
created_at | 2024-06-03 12:58:55.484933 |
updated_at | 2024-11-18 11:42:30.655839 |
description | A simple and efficient tree structure library for Rust with recursive traversal |
homepage | https://github.com/antouhou/easy-tree |
repository | https://github.com/antouhou/easy-tree |
max_upload_size | |
id | 1260066 |
size | 36,330 |
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.
Add easy-tree
to your Cargo.toml
:
[dependencies]
easy-tree = "0.1"
To enable parallel iteration:
[dependencies]
easy-tree = { version = "0.1", features = ["rayon"] }
Each node in the tree has:
None
for the root)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
use easy_tree::Tree;
fn main() {
let mut tree = Tree::new();
let root = tree.add_node("root");
let child = tree.add_child(root, "child");
let grandchild = tree.add_child(child, "grandchild");
assert_eq!(tree.get(root), Some(&"root"));
assert_eq!(tree.get(grandchild), Some(&"grandchild"));
}
use easy_tree::Tree;
fn main() {
let mut tree = Tree::new();
let root = tree.add_node("root");
let child1 = tree.add_child(root, "child1");
let grandchild1 = tree.add_child(child1, "grandchild1");
let child2 = tree.add_child(root, "child2");
let mut log = vec![];
tree.traverse(
|idx, data, log| log.push(format!("Visiting node {}: {}", idx, data)),
|idx, data, log| log.push(format!("Finished node {}: {}", idx, data)),
&mut log,
);
println!("{:?}", log);
}
use easy_tree::Tree;
fn main() {
let mut tree = Tree::new();
let root = tree.add_node(0);
let _child1 = tree.add_child(root, 1);
let _child2 = tree.add_child(root, 2);
#[cfg(feature = "rayon")]
{
tree.par_iter().for_each(|(idx, data)| {
println!("Processing node {}: {}", idx, data);
});
}
}
use easy_tree::Tree;
fn main() {
let mut fs = Tree::new();
let root = fs.add_node("root/");
let home = fs.add_child(root, "home/");
let user = fs.add_child(home, "user/");
let file = fs.add_child(user, "file.txt");
println!("Tree structure:");
fs.traverse(
|_, data, _| println!("Entering {}", data),
|_, data, _| println!("Leaving {}", data),
&mut (),
);
}
rayon
feature for concurrent processing.