#tree-traversal #tree #recursion #traversal #node-tree #hierarchical

easy-tree

一个简单高效的Rust树结构库,支持递归遍历

3个版本

0.1.2 2024年6月19日
0.1.1 2024年6月3日
0.1.0 2024年6月3日

#589 in 数据结构

MIT 许可证

20KB
219

easy-tree

Crates.io Documentation Build and test

easy-tree 是一个简单高效的Rust树结构库。它允许您创建和操作具有多个子节点和单个父节点的树结构。 easy-tree 还支持递归以深度优先的方式遍历树,有两个回调:一个在处理任何子节点之前,一个在处理属于该节点的子树之后(即子节点及其子节点等)。

easy-treecrates.io 上可用,并且 API 文档在 docs.rs 上可用

用法

将以下内容添加到您的 Cargo.toml

[dependencies]
easy-tree = "0.1"

要启用使用 rayon crate 的并行迭代,请添加以下内容

[dependencies]
easy-tree = { version = "0.1", features = ["rayon"] }

示例

以深度优先的方式遍历树

use easy_tree::Tree;

fn main() {
    let mut tree = Tree::new();
    let root = tree.add_node(0); // Root node with data 0
    let child1 = tree.add_child(root, 1); // Child node with data 1
    let child2 = tree.add_child(root, 2); // Child node with data 2
    let child3 = tree.add_child(child1, 3); // Child node with data 3

    let mut result = vec![];

    tree.traverse(|index, node, result| {
        result.push(format!("Calling handler for node {}: {}", index, node))
    }, |index, node, result| {
        result.push(format!("Finished handling node {} and all it's children", index))
    }, &mut result);

    assert_eq!(result, vec![
        "Calling handler for node 0: 0",
        "Calling handler for node 1: 1",
        "Calling handler for node 3: 3",
        "Finished handling node 3 and all it's children",
        "Finished handling node 1 and all it's children",
        "Calling handler for node 2: 2",
        "Finished handling node 2 and all it's children",
        "Finished handling node 0 and all it's children",
    ]);
}
use easy_tree::Tree;

fn main() {
    // Create a new tree and add nodes
    let mut tree = Tree::new();
    let root = tree.add_node(0); // Root node with data 0
    let child1 = tree.add_child(root, 1); // Child node with data 1
    let child2 = tree.add_child(root, 2); // Child node with data 2
    let child3 = tree.add_child(child1, 3); // Child node with data 3

// Access nodes and their relationships
    assert_eq!(tree.get(root), Some(&0));
    assert_eq!(tree.get(child1), Some(&1));
    assert_eq!(tree.get(child2), Some(&2));
    assert_eq!(tree.get(child3), Some(&3));

    assert_eq!(tree.parent_index_unchecked(child1), Some(root));
    assert_eq!(tree.parent_index_unchecked(child2), Some(root));
    assert_eq!(tree.parent_index_unchecked(child3), Some(child1));

    assert_eq!(tree.children(root), &[child1, child2]);
    assert_eq!(tree.children(child1), &[child3]);
    assert_eq!(tree.children(child2), &[]);
    assert_eq!(tree.children(child3), &[]);
}
use easy_tree::Tree;

fn main() {
    // Create a new tree and add nodes
    let mut tree = Tree::new();
    let root = tree.add_node(0); // Root node with data 0
    let child1 = tree.add_child(root, 1); // Child node with data 1
    let child2 = tree.add_child(root, 2); // Child node with data 2
    let child3 = tree.add_child(child1, 3); // Child node with data 3

// Iterate over the nodes in the tree
    for (index, data) in tree.iter() {
        println!("Node {}: {}", index, data);
    }

// Iterate over the nodes in the tree mutably
    for (index, data) in tree.iter_mut() {
        *data += 1;
    }

// Check the modified values
    assert_eq!(tree.get(root), Some(&1));
    assert_eq!(tree.get(child1), Some(&2));
    assert_eq!(tree.get(child2), Some(&3));
    assert_eq!(tree.get(child3), Some(&4));
}

文档

文档在 docs.rs 上提供.

贡献

欢迎每个人以任何形式或方式做出贡献!有关更多详细信息,请阅读 CONTRIBUTING.md

作者

还可以查看参与此项目的贡献者名单。

许可证

本项目采用MIT许可证 - 请参阅 LICENSE.md 文件以获取详细信息

依赖关系

~0–265KB