use crate::{BinaryNode, break_tree, for_tree, prune};
pub fn find_value_example() {
let root = create_sample_tree();
let target = 5;
println!("Searching for value {} in tree:", target);
let mut found = false;
for_tree!(node in &root; |_| true; |node| {
let mut children = Vec::new();
if let Some(left) = &node.left {
children.push(left.as_ref());
}
if let Some(right) = &node.right {
children.push(right.as_ref());
}
children
} => {
println!("Checking node with value: {}", node.value);
if node.value == target {
println!("Found target value: {}", target);
found = true;
break_tree!();
}
});
if !found {
println!("Value {} not found in tree", target);
}
}
pub fn pruning_example() {
let root = create_sample_tree();
println!("Traversing tree, pruning negative values:");
for_tree!(node in &root; |_| true; |node| {
let mut children = Vec::new();
if let Some(left) = &node.left {
children.push(left.as_ref());
}
if let Some(right) = &node.right {
children.push(right.as_ref());
}
children
} => {
println!("Visiting node with value: {}", node.value);
if node.value < 0 {
println!(" Pruning at negative value: {}", node.value);
prune!();
}
});
}
pub fn fibonacci_example() {
println!("Generating Fibonacci sequence up to 1000:");
for_tree!(fib in (1, 1); |fib| fib.0 < 1000; |fib| vec![(fib.1, fib.0 + fib.1)] => {
println!("Fibonacci number: {}", fib.0);
});
}
pub fn bfs_emulation_example() {
let root = create_sample_tree();
println!("BFS-like traversal by tracking levels:");
for_tree!(item in (&root, 0); |_| true; |item| {
let (node, level) = *item;
let mut children = Vec::new();
if let Some(left) = &node.left {
children.push((left.as_ref(), level + 1));
}
if let Some(right) = &node.right {
children.push((right.as_ref(), level + 1));
}
children.sort_by_key(|&(_, l)| l);
children
} => {
let (node, level) = *item;
println!("Level {}: value {}", level, node.value);
});
}
pub struct FileSystemNode {
pub name: String,
pub is_directory: bool,
pub children: Vec<FileSystemNode>,
}
pub fn filesystem_example() {
let fs_root = FileSystemNode {
name: "root".to_string(),
is_directory: true,
children: vec![
FileSystemNode {
name: "documents".to_string(),
is_directory: true,
children: vec![
FileSystemNode {
name: "report.docx".to_string(),
is_directory: false,
children: vec![],
},
FileSystemNode {
name: "data.xlsx".to_string(),
is_directory: false,
children: vec![],
},
],
},
FileSystemNode {
name: "pictures".to_string(),
is_directory: true,
children: vec![FileSystemNode {
name: "vacation.jpg".to_string(),
is_directory: false,
children: vec![],
}],
},
FileSystemNode {
name: "config.cfg".to_string(),
is_directory: false,
children: vec![],
},
],
};
println!("File system traversal:");
for_tree!(node in &fs_root; |_| true; |node| {
if node.is_directory {
node.children.iter().collect()
} else {
Vec::new()
}
} => {
let indent = " ".repeat(get_depth(node, &fs_root));
let node_type = if node.is_directory { "DIR" } else { "FILE" };
println!("{}{}: {}", indent, node_type, node.name);
});
}
fn get_depth(node: &FileSystemNode, root: &FileSystemNode) -> usize {
if node.name == root.name {
0
} else {
let mut current = Some(root);
let mut depth = 0;
while let Some(n) = current {
for child in &n.children {
if child.name == node.name {
return depth + 1;
}
}
depth += 1;
current = n.children.first();
}
depth
}
}
fn create_sample_tree() -> BinaryNode<i32> {
BinaryNode::with_children(
1,
Some(Box::new(BinaryNode::with_children(
2,
Some(Box::new(BinaryNode::with_children(
4,
Some(Box::new(BinaryNode::new(-8))),
Some(Box::new(BinaryNode::new(9))),
))),
Some(Box::new(BinaryNode::new(5))),
))),
Some(Box::new(BinaryNode::with_children(
3,
Some(Box::new(BinaryNode::new(-7))),
Some(Box::new(BinaryNode::new(6))),
))),
)
}