13 releases

0.4.2 Feb 20, 2021
0.4.1 Jan 25, 2021
0.3.0 Feb 21, 2020
0.2.1 Nov 9, 2018
0.1.4 May 26, 2018

#139 in Data structures

Download history 16187/week @ 2024-06-19 16781/week @ 2024-06-26 14353/week @ 2024-07-03 15064/week @ 2024-07-10 17380/week @ 2024-07-17 19400/week @ 2024-07-24 17890/week @ 2024-07-31 19505/week @ 2024-08-07 17469/week @ 2024-08-14 17238/week @ 2024-08-21 16138/week @ 2024-08-28 19403/week @ 2024-09-04 12812/week @ 2024-09-11 8127/week @ 2024-09-18 15938/week @ 2024-09-25 13368/week @ 2024-10-02

53,933 downloads per month
Used in 101 crates (18 directly)

MIT/Apache

195KB
3.5K SLoC

This project provides trees data structure serving for general purpose.

Quickstart

Impatient readers can start with the notations.

Features

  1. Step-by-step creating, reading, updating, deleting and iterating nodes with assocated data items.

  2. Compact notations to express trees: -,/ encoded or tuple encoded trees.

  3. Depth first search cursor.

  4. Breadth first search iterators.

  5. Trees can be built by stages, with nodes stored scatteredly among memory.

  6. Trees can be built once through, with nodes stored contiguously.

  7. Support exclusive ownership with static borrow check.

  8. Support shared ownership with dynamic borrow check.

Examples

  • notation of a literal tree

    use trees::tr;
    
    let scattered_tree = tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) );
    let piled_tree = trees::Tree::from(( 0, (1,2,3), (4,5,6) ));
    

    They both encode a tree drawn as follows:

    .............
    .     0     .
    .   /   \   .
    .  1     4  .
    . / \   / \ .
    .2   3 5   6.
    .............
    
  • use tree notation to reduce syntax noise, quoted from crate reflection_derive, version 0.1.1:

    quote! {
        #(
            -( ::reflection::variant( stringify!( #vnames ))
                /(
                    #(
                        -( ::reflection::field(
                                #fnames,
                                <#ftypes1 as ::reflection::Reflection>::ty(),
                                <#ftypes2 as ::reflection::Reflection>::name(),
                                Some( <#ftypes3 as ::reflection::Reflection>::members )))
                    )*
                )
            )
        )*
    }
    

    The starting of tree operations are denoted by -( and /( which are humble enough to let the reader focusing on the data part.

  • use iterators if the tree travesal is a "driving wheel"( you can iterate over the tree on your own ).

    use trees::{Node, tr};
    use std::fmt::Display;
    
    let tree = tr(0)
        /( tr(1) /tr(2)/tr(3) )
        /( tr(4) /tr(5)/tr(6) );
    
    fn tree_to_string<T:Display>( node: &Node<T> ) -> String {
        if node.has_no_child() {
            node.data.to_string()
        } else {
            format!( "{}( {})", node.data,
                node.iter().fold( String::new(),
                    |s,c| s + &tree_to_string(c) + &" " ))
        }
    }
    
    assert_eq!( tree_to_string( &tree ), "0( 1( 2 3 ) 4( 5 6 ) )" );
    
  • use TreeWalk when the tree travesal is a "driven wheel"( driven by other library ). Quoted from crate tsv, version 0.1.0:

        fn next_value_seed<V:DeserializeSeed<'de>>( &mut self, seed: V ) -> Result<V::Value> {
            let result = self.next_element_in_row( seed )?;
            self.de.next_column();
            self.de.row += 1;
            self.de.pop_stack(); // finish key-value pair
            self.de.next_column();
            self.de.columns.revisit();
            Ok( result )
        }
    

    The serde library is driving on the schema tree when (de)serializing variables. Use TreeWalk methods such as next_column and revisit to follow the step.

License

Under Apache License 2.0 or MIT License, at your will.

No runtime deps

Features