Crate easy_tree

source ·
Expand description

A simple tree structure library for Rust.

easy-tree is a simple and efficient tree structure library for Rust. It allows you to create and manipulate tree structures where each node can have multiple children and a single parent. easy-tree also supports recursively traversing the tree in a depth-first manner, with two callbacks: one before processing any children and one after processing the subtree belonging to that node (meaning children, and their children, and so on).

§Examples

§Traverse a tree

use easy_tree::Tree;

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",
]);

§Iterate over the nodes in a tree

use easy_tree::Tree;

// 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), &[]);

§Modify the nodes in a tree

use easy_tree::Tree;

// 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));

Re-exports§

Structs§

  • A node in the tree containing data and references to its parent and children.
  • A tree structure containing nodes.