pub struct Tree<T> { /* private fields */ }Expand description
A tree structure that represents a hierarchical collection of nodes.
The Tree struct is a fundamental data structure in Radiate’s genetic programming system.
It provides a way to represent and manipulate tree-based expressions, where each node
can have zero or more child nodes. The tree is rooted, meaning it has a single root node
from which all other nodes descend.
§Type Parameters
T- The type of value stored in each node. This type must implementClone,PartialEq, and other traits required by the genetic programming operations.
§Fields
root- An optionalTreeNode<T>that serves as the root of the tree. WhenNone, the tree is considered empty.
§Examples
use radiate_gp::{Tree, TreeNode, Op, Eval};
// Create a simple tree representing the expression (1 + 2) * 3
let tree = Tree::new(
TreeNode::new(Op::mul())
.attach(
TreeNode::new(Op::add())
.attach(TreeNode::new(Op::constant(1.0)))
.attach(TreeNode::new(Op::constant(2.0)))
)
.attach(TreeNode::new(Op::constant(3.0)))
);
// Evaluate the tree
let result = tree.eval(&[]); // Evaluates to 9.0
assert_eq!(result, 9.0);§Tree Creation
The struct provides several ways to create trees:
new()- Creates a tree with a given root nodewith_depth()- Creates a tree of specified depth using nodes from aNodeStoredefault()- Creates an empty tree
§Tree Operations
The struct provides methods for tree manipulation and traversal:
root()- Gets a reference to the root noderoot_mut()- Gets a mutable reference to the root nodetake_root()- Takes ownership of the root nodesize()- Returns the total number of nodes in the treeheight()- Returns the height of the tree
§Tree Traversal
The struct implements the TreeIterator trait, providing three traversal methods:
iter_pre_order()- Traverses the tree in pre-order (root, then children)iter_post_order()- Traverses the tree in post-order (children, then root)iter_breadth_first()- Traverses the tree level by level
§Tree Building
The struct provides a builder pattern for creating trees of specific depths:
use radiate_gp::{Tree, NodeType, Op};
let store = vec![
(NodeType::Vertex, vec![Op::add(), Op::sub(), Op::mul()]),
(NodeType::Leaf, vec![Op::constant(1.0), Op::constant(2.0)]),
];
// Create a tree of depth 3
let tree = Tree::with_depth(3, store);
assert_eq!(tree.height(), 3);§Tree Evaluation
When T implements the Eval trait, the tree can be evaluated with input data:
use radiate_gp::{Tree, TreeNode, Op, Eval};
let tree = Tree::new(
TreeNode::new(Op::add())
.attach(TreeNode::new(Op::var(0)))
.attach(TreeNode::new(Op::constant(2.0)))
);
assert_eq!(tree.eval(&[1.0]), 3.0);
assert_eq!(tree.eval(&[2.0]), 4.0);§Tree Properties
The tree maintains several important properties:
- It is always rooted (has a single root node)
- It is acyclic (no node is its own ancestor)
- Each node can have zero or more children
- The tree’s height is the length of the longest path from root to leaf
- The tree’s size is the total number of nodes
§Implementation Details
The struct implements several traits:
Clone- Allows cloning of the entire tree structurePartialEq- Enables equality comparison between treesDefault- Provides a way to create an empty treeDebug- Provides debug formatting for the treeAsRef<TreeNode<T>>- Allows treating the tree as a reference to its root nodeAsMut<TreeNode<T>>- Allows treating the tree as a mutable reference to its root node
§Genetic Programming
The Tree struct is particularly useful in genetic programming as it can represent:
- Mathematical expressions
- Program syntax trees
- Decision trees
- Other hierarchical structures
Implementations§
Source§impl<T: Clone + Default> Tree<T>
impl<T: Clone + Default> Tree<T>
Sourcepub fn with_depth(depth: usize, nodes: impl Into<NodeStore<T>>) -> Self
pub fn with_depth(depth: usize, nodes: impl Into<NodeStore<T>>) -> Self
Create a tree with the given depth, where each node is a random node from the node store. This obeys the rules of the NodeStore’s NodeType’s arity, and will create a tree that is as balanced as possible.
Note that the root node will try to be a NodeType::Root if it is available in the NodeStore, otherwise it will be a NodeType::Vertex. This allows caller’s to specify what the root node is if desired, otherwise it will be a random vertex node from the NodeStore.
§The NodeStore must contain at least one NodeType::Root or one NodeType::Vertex
§Arguments
depth- The depth of the tree.nodes- The node store to use for the tree.
§Returns
A tree with the given depth, where each node is a random node from the node store.
Trait Implementations§
Source§impl<F> BatchFitnessFunction<Tree<Op<F>>, F> for Regression<F>
impl<F> BatchFitnessFunction<Tree<Op<F>>, F> for Regression<F>
Source§impl<F> FitnessFunction<Tree<Op<F>>, F> for Regression<F>
— Trees —
impl<F> FitnessFunction<Tree<Op<F>>, F> for Regression<F>
— Trees —
impl<T: PartialEq> StructuralPartialEq for Tree<T>
Source§impl<T> TreeIterator<T> for Tree<T>
Implement the TreeIterator trait for Tree
impl<T> TreeIterator<T> for Tree<T>
Implement the TreeIterator trait for Tree
This allows for traversal of the entire tree in pre-order, post-order, and breadth-first order.