pub struct TreeNode<T> { /* private fields */ }Expand description
A node in a tree structure that represents a single element with optional children.
The TreeNode struct is a fundamental building block for tree-based genetic programming in Radiate. It represents a node in a tree that can have zero or more child nodes, forming a hierarchical structure. Each node has a value of type T and maintains an optional list of child nodes.
§Type Parameters
T- The type of value stored in the node. This type must implementClone,PartialEq, and other traits required by the genetic programming operations.
§Fields
value- The actual value stored in the nodearity- Optional Arity that specifies how many children the node can havechildren- Optional vector of child nodes
§Examples
use radiate_gp::{collections::{TreeNode}, Arity, Node};
// Create a new node with value 42
let node = TreeNode::new(42);
// Create a node with specific arity
let node_with_arity = TreeNode::with_arity(42, Arity::Exact(2));
let other_node_with_arity = TreeNode::from((42, Arity::Exact(2)));
assert_eq!(node_with_arity.arity(), other_node_with_arity.arity());
// Create a node with children
let node_with_children = TreeNode::with_children(42, vec![
TreeNode::new(1),
TreeNode::new(2)
]);
let other_node_with_children = TreeNode::from((42, vec![
TreeNode::new(1),
TreeNode::new(2),
]));§Node Types and Arity
The node’s type and arity determine its behavior and validity:
Leafnodes have no children (arity is Arity::Zero)Vertexnodes can have any number of children (arity is Arity::Any)Rootnodes are the starting point of the tree and can have any number of children
§Tree Operations
The struct provides several methods for tree manipulation:
new()- Creates a new node with no childrenwith_arity()- Creates a node with a specific aritywith_children()- Creates a node with a list of childrenadd_child()- Adds a child to the nodeattach()- Attaches a child and returns self for method chainingdetach()- Removes a child at a specific indexswap_subtrees()- Swaps subtrees between two nodes
§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 Properties
The struct provides methods to query tree properties:
is_leaf()- Checks if the node has no children - must have Arity::Zerosize()- Returns the total number of nodes in the subtreeheight()- Returns the height of the subtree
§Validity
A node is considered valid based on its arity:
- Nodes with Arity::Zero must have no children
- Nodes with Arity::Exact must have exactly n children
- Nodes with Arity::Any can have any number of children
§Implementation Details
The struct implements several traits:
Node- Provides common node behavior and access to value and type informationGene- Enables genetic operations for the node making it compatible with genetic algorithmsValid- Defines validity rules for the nodeDebug- Provides debug formattingClone,PartialEq- Required for genetic programming operationsFormat- Provides pretty-printing of the tree structure
§Evaluation
When T implements the Eval trait, the node can be evaluated with input data:
use radiate_gp::{Op, Eval, TreeNode};
let tree = TreeNode::new(Op::add())
.attach(TreeNode::new(Op::constant(2.0)))
.attach(TreeNode::new(Op::constant(3.0)));
let result = tree.eval(&[]); // Evaluates to 5.0Implementations§
Source§impl<T> TreeNode<T>
impl<T> TreeNode<T>
pub fn new(val: T) -> Self
pub fn with_arity(val: T, arity: Arity) -> Self
pub fn with_children<N>(val: T, children: Vec<N>) -> Self
pub fn is_leaf(&self) -> bool
pub fn add_child(&mut self, child: impl Into<TreeNode<T>>)
pub fn attach(self, other: impl Into<TreeNode<T>>) -> Self
pub fn detach(&mut self, index: usize) -> Option<TreeNode<T>>
pub fn children(&self) -> Option<&[TreeNode<T>]>
pub fn children_mut(&mut self) -> Option<&mut Vec<TreeNode<T>>>
pub fn take_children(&mut self) -> Option<Vec<TreeNode<T>>>
pub fn size(&self) -> usize
pub fn height(&self) -> usize
pub fn get_mut(&mut self, index: usize) -> Option<&mut TreeNode<T>>
Trait Implementations§
Source§impl<T, V> Eval<[V], V> for &TreeNode<T>
impl<T, V> Eval<[V], V> for &TreeNode<T>
Because a Tree has only a single root node, this can only be used to return a single value. We assume here that each leaf can eval the incoming input - this is a safe and the only real logical assumption we can make.
Source§impl<T> FromIterator<TreeNode<T>> for TreeChromosome<T>
impl<T> FromIterator<TreeNode<T>> for TreeChromosome<T>
Source§impl<T> Gene for TreeNode<T>
impl<T> Gene for TreeNode<T>
type Allele = T
Source§fn allele_mut(&mut self) -> &mut Self::Allele
fn allele_mut(&mut self) -> &mut Self::Allele
allele of the Gene.Source§fn new_instance(&self) -> Self
fn new_instance(&self) -> Self
Source§fn with_allele(&self, allele: &Self::Allele) -> Self
fn with_allele(&self, allele: &Self::Allele) -> Self
allele.Source§fn set_allele(&mut self, allele: Self::Allele)
fn set_allele(&mut self, allele: Self::Allele)
allele of the Gene to the given value.Source§impl<T> Node for TreeNode<T>
impl<T> Node for TreeNode<T>
type Value = T
Source§fn node_type(&self) -> NodeType
fn node_type(&self) -> NodeType
GraphNode with 0 incoming connections is likely an Input,
while a TreeNode with 0 children is likely a Leaf.Source§fn arity(&self) -> Arity
fn arity(&self) -> Arity
impl<T: PartialEq> StructuralPartialEq for TreeNode<T>
Source§impl<T> TreeIterator<T> for TreeNode<T>
Implement the TreeIterator trait for TreeNode
impl<T> TreeIterator<T> for TreeNode<T>
Implement the TreeIterator trait for TreeNode
This allows for traversal of a single node and its children in pre-order, post-order, and breadth-first order.