Crate easy_tree

source ·
Expand description

A simple tree structure library for Rust.

This library provides a basic implementation of a tree structure where each node can have multiple children and a single parent.

§Examples

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