3. Given the following Rust code inserting values into a BST, what is the in-order traversal output?
struct Node { value: i32, left: Option, right: Option }
fn insert(root: &mut Option, val: i32) {
match root {
None => *root = Some(Box::new(Node { value: val, left: None, right: None })),
Some(node) => {
if val < node.value {
insert(&mut node.left, val);
} else {
insert(&mut node.right, val);
}
}
}
}
fn inorder(root: &Option, vals: &mut Vec<i32>) {
if let Some(node) = root {
inorder(&node.left, vals);
vals.push(node.value);
inorder(&node.right, vals);
}
}
fn main() {
let mut root = None;
insert(&mut root, 10);
insert(&mut root, 5);
insert(&mut root, 15);
insert(&mut root, 7);
let mut result = Vec::new();
inorder(&root, &mut result);
println!("{:?}", result);
}