Tree-Iter
A Rust library for iterating over tree structures in different traversal orders.
Features
- Generic support for any tree-like structure
- Breadth-first and depth-first traversal orders
- Immutable and mutable iteration
- Modifying the tree structure during mutable iteration
Usage
Basic Example
use *;
use Node;
// Create a simple tree
let tree = Node ;
// Iterate over the tree in depth-first order
let values: = tree.
.map
.collect;
assert_eq!;
// Iterate over the tree in breadth-first order
let values: = tree.
.map
.collect;
assert_eq!;
Mutable Iteration
Mutate nodes during iteration, including modifying values and changing the structure by adding or removing children:
use *;
use Node;
let mut tree = Node ;
// Add 10 to each value in the tree, and add a child to node with value 2
let mut iter = tree.;
while let Some = iter.next
// Verify values were changed in breadth-first order
let values: = tree..map.collect;
assert_eq!;
Forest Iteration
Iterate over a forest of trees:
use *;
use Node;
// Create a forest with two trees
let mut forest = vec!;
// Create a mutable iterator over the forest
let mut iter = new;
while let Some = iter.next
// Verify the modified structure
let value = new
.map
.;
assert_eq!;
Custom Tree Structures
Implement the traits for your own tree structures:
use *;
// Implement TreeNode for immutable iteration
// Implement TreeNodeMut for mutable iteration
// Now you can use all iterators with your custom tree