tree_traversal
A Rust library for finding the optimal leaf node in a tree structure. This crate implements several tree traversal algorithms to find the best (the lowest cost) leaf node in a tree.
Algorithms
- Breadth First Search
- Depth First Search
- Beam Search
- Branch and Bound Search
- Greedy Search
- Priority First Search
Using this crate
APIs
Functional API
The crate exposes a functional-style API under traversal::functional which provides traversal
algorithms as standalone functions you can call with pure function arguments (successor, cost,
leaf-check, etc.). This style is lightweight and works well when your problem can be expressed
with small functions or closures. The example above demonstrates calling traversal::functional::bbs:
- Where: use
traversal::functional. - What: functions like
bbs,bfs,dfs,bms, andgds. - Inputs: initial state,
successor_fn,leaf_check_fn,cost_fn, bounds/limits. - Outputs:
(cost, best_node)orOption/Resultdepending on the algorithm.
Use this API when you prefer functional composition, want minimal boilerplate, or are using
small/anonymous state representations (tuples, Vec, simple structs).
Functional Example
The following demonstrates using the functional bbs function from traversal::functional.
use bbs;
type Node = ;
let weights = ;
let profits = ;
let capacity = 8 as u32;
let total_items = weights.len;
let successor_fn = ;
let total_profit = ;
let lower_bound_fn = ;
let cost_fn = ;
let leaf_check_fn = ;
let max_ops = usizeMAX;
let time_limit = from_secs;
let = bbs
.unwrap;
let cost = u32MAX - cost;
dbg!;
OOP API
The crate also provides a more object-oriented API which groups traversal behavior together with state in structs and traits. This style is useful when you have complex state, want to encapsulate helper methods, or want to implement custom strategies by implementing traits.
- Where: top-level
traversalmodules and structs (see source for concrete types). - What: traversal types that can own state and expose methods to run searches.
- Inputs: implement or instantiate the provided traits/structs, then call the method on the traversal object to execute the search.
Use the OOP API when you prefer encapsulation, need to share rich state between helpers, or when building reusable components around a traversal (for example, packing problem solvers or search strategies that maintain internal caches or statistics).
If you're not sure which to pick, start with the Functional API for quick experiments and move to the OOP API when your state or logic grows in complexity.
OOP Example
Below is a minimal example showing how to implement the TreeNode trait for a simple
knapsack example from the examples directory and shows a more complete, idiomatic use of
the OOP API (uses LowerBound, TreeNode, and BranchAndBoundTraversal).
use Rc;
use ;
let weights = ;
let profits = ;
let capacity = 8;
let root_node = Node ;
let mut traversal = new;
let result = find_best;
if let Some = result else
Note
The Functional API is derived from the great pathfinding crate.