25 releases (14 stable)

Uses old Rust 2015

1.8.0 Apr 13, 2021
1.7.0 Sep 1, 2019
1.6.0 Aug 24, 2019
1.5.0 Mar 23, 2019
0.2.4 Oct 17, 2016

#147 in Data structures

Download history 3801/week @ 2024-06-09 2155/week @ 2024-06-16 1820/week @ 2024-06-23 3203/week @ 2024-06-30 1757/week @ 2024-07-07 1418/week @ 2024-07-14 1423/week @ 2024-07-21 2292/week @ 2024-07-28 2433/week @ 2024-08-04 1955/week @ 2024-08-11 1717/week @ 2024-08-18 1478/week @ 2024-08-25 1561/week @ 2024-09-01 1230/week @ 2024-09-08 1193/week @ 2024-09-15 982/week @ 2024-09-22

5,162 downloads per month
Used in 15 crates (13 directly)

MIT license

180KB
3K SLoC

id_tree

Build Status Build status Coverage Status Docs.rs Crates.io

A library for creating and modifying Tree structures.

Overview

In this implementation, the Tree owns all of the Nodes and all inter-Node relationships are managed with NodeIds.

Trees in this library are "just" trees. They do not allow cycles. They do not allow the creation of arbitrary Graph structures. There is no weight associated with edges between Nodes. In addition, each Node can have an arbitrary number of child Nodes.

It is important to note that this library does not support comparison-based Node insertion. In other words, this is not a Binary Search Tree (or any other kind of search tree) library. It is purely a library for storing data in a hierarchical manner. The caller must know the structure that they wish to build and then use this library to do so; this library will not make those structural decisions for you.

Example Usage

use id_tree::*;

fn main() {
    use id_tree::InsertBehavior::*;

    //      0
    //     / \
    //    1   2
    //   / \
    //  3   4
    let mut tree: Tree<i32> = TreeBuilder::new()
        .with_node_capacity(5)
        .build();

    let root_id: NodeId = tree.insert(Node::new(0), AsRoot).unwrap();
    let child_id: NodeId = tree.insert(Node::new(1), UnderNode(&root_id)).unwrap();
    tree.insert(Node::new(2), UnderNode(&root_id)).unwrap();
    tree.insert(Node::new(3), UnderNode(&child_id)).unwrap();
    tree.insert(Node::new(4), UnderNode(&child_id)).unwrap();

    println!("Pre-order:");
    for node in tree.traverse_pre_order(&root_id).unwrap() {
        print!("{}, ", node.data());
    }
    // results in the output "0, 1, 3, 4, 2, "
}

Project Goals

  • Allow caller control of as many allocations as possible (through pre-allocation)
  • Fast Node insertion and removal
  • Arbitrary Tree structure creation and manipulation

Non-Goals

  • Arbitrary Graph structure creation and manipulation
  • Comparison-based node insertion of any kind

Contributors

Dependencies

~180KB