lineartree
A simple and easy-to-use tree data structure for rust.
This crate implements trees using a single vector to hold all nodes, hence the name.
Basically it's a Vec<Node<T>>
, where each Node<T>
has indices of parents and children.
On top of that, there's some convenience functions to iterate depth-first and breadth-first across nodes, find children, and so on.
Quick Start
Tree creation
use ;
/* This builds the following tree
* "/"
* / \
* etc usr
* / \
* bin lib
*/
let mut tree = new;
// Trees usually have a root node
let fs_root = tree.root?;
// Using .root() or .node() return a NodeRef object
// which can be later used to identify and manipulate
// node values.
let usr = tree.node;
tree.append_child?;
// Add multiple children at once
let bin = tree.node;
let lib = tree.node;
tree.append_children?;
// You can also add nodes to a parent in a single go
let etc = tree.child_node?;
Getting, changing and removing nodes
// Get node values (this is O(1))
assert_eq!;
assert_eq!;
assert_eq!;
// Remove node, this won't resize the underlying Vec
// because otherwise node references will be invalidated.
tree.remove?;
Getting number of nodes
// .len() is also O(1)
assert_eq!;
Traverse tree
// Here are the basic hierarchical operators
assert_eq!;
assert_eq!;
// Iterate depth first over a node children.
// Use .depth_first() to iterate the entire tree.
for node in tree.depth_first_of?