tagged-tree

Crates.iotagged-tree
lib.rstagged-tree
version0.4.0
sourcesrc
created_at2021-09-25 06:49:51.334142
updated_at2021-09-30 04:00:00.987283
descriptionA tree-like data structure where the values are tagged.
homepage
repositoryhttps://github.com/Person-93/tagged-tree
max_upload_size
id456089
size33,604
person93 (Person-93)

documentation

https://docs.rs/tagged-tree/

README

Tagged Tree

This crate provides a simple tree structure in which all the nodes (except the root) are tagged with an arbitrary type.

Example

use tagged_tree::Tree;

fn main() {
    // create the tree with the root node
    let mut tree = Tree::<&'static str, i32>::new(42);
    assert_eq!(*tree.value(), 42);

    // add the value 1 tagged with "hello"
    let (old_val, child) = tree.add_child("hello", 1);
    assert!(old_val.is_none());
    assert_eq!(*child.value(), 1);

    // replace it with 5
    let (old_val, child) = tree.add_child("hello", 5);
    assert_eq!(old_val.unwrap(), 1);
    assert_eq!(*child.value(), 5);

    // add a child to the child
    child.add_child("world", 2);

    // add another child to the original
    tree.add_child("greetings", 3);

    // iterate over the direct children
    for (key, child) in tree.iter_single() {
        println!("{}: {}", key, child.value());
    }

    // depth first traversal
    for (key, value) in tree.iter_depth_first() {
        println!("{}: {}", key, value);
    }

    // breadth first traversal
    for (key, value) in tree.iter_breadth_first() {
        println!("{}: {}", key, value);
    }
}
Commit count: 27

cargo fmt