📖 Scenario: You are building a simple binary tree to organize numbers. Each node in the tree can have up to two children: a left child and a right child. This structure helps in searching and sorting numbers efficiently.
🎯 Goal: Create a binary tree manually by defining nodes and linking them as left and right children. Then print the tree nodes in a simple format to show the structure.
📋 What You'll Learn
Define a struct called Node with fields value, left, and right
Create nodes with values 10, 5, and 15
Link nodes to form a binary tree with 10 as root, 5 as left child, and 15 as right child
Print the values of the root, its left child, and its right child
💡 Why This Matters
🌍 Real World
Binary trees are used in many applications like searching, sorting, and organizing hierarchical data such as file systems or decision trees.
💼 Career
Understanding how to create and manipulate binary trees is fundamental for software engineers working with data structures, algorithms, and system design.
Progress0 / 4 steps
1
Define the Node struct and create the root node
Define a struct called Node with a field value of type i32, and two fields left and right of type Option>. Then create a variable called root which is a Node with value 10 and left and right set to None.
DSA Rust
Hint
Use Option> for left and right to allow empty children.
2
Create left and right child nodes
Create two variables called left_child and right_child which are Node structs with value 5 and 15 respectively, and both have left and right set to None.
DSA Rust
Hint
Create two separate Node variables for the children with None for their children.
3
Link the child nodes to the root node
Create a new variable called root which is a Node with value 10, left set to Some(Box::new(left_child)), and right set to Some(Box::new(right_child)).
DSA Rust
Hint
Use Some(Box::new(...)) to wrap child nodes when linking.
4
Print the values of the root and its children
Print the values of root.value, root.left.as_ref().unwrap().value, and root.right.as_ref().unwrap().value using println! statements.
DSA Rust
Hint
Use as_ref().unwrap() to access the child nodes inside Option.
Practice
(1/5)
1. What is the main purpose of creating a binary tree manually in Rust using structs and references?
easy
A. To store data in a single linear sequence
B. To create a list of numbers in ascending order
C. To build a tree structure where each node can have up to two children
D. To perform arithmetic operations on numbers
Solution
Step 1: Understand binary tree structure
A binary tree is a data structure where each node has at most two children, called left and right.
Step 2: Purpose of manual creation in Rust
Manually creating a binary tree in Rust involves defining nodes and linking them with references to build this two-child structure.
Final Answer:
To build a tree structure where each node can have up to two children -> Option C
Quick Check:
Binary tree = nodes with up to two children [OK]
Hint: Binary tree nodes have max two children [OK]
Common Mistakes:
Confusing binary tree with linked list
Thinking binary tree nodes have more than two children
Assuming binary tree stores data linearly
2. Which of the following Rust code snippets correctly defines a binary tree node struct with optional left and right children?
A. Error: cannot move `node` into `Some` because `node` does not implement Copy; fix by wrapping node in Box::new()
B. Error: missing semicolon after node declaration; fix by adding semicolon
C. Error: left field must be a reference; fix by using &node
D. No error; code compiles successfully
Solution
Step 1: Identify the error cause
left field expects Option, but Some(node) tries to move Node directly, causing type mismatch.
Step 2: Fix by boxing node
Wrap node in Box::new(node) inside Some to match Option type.
Final Answer:
Error: cannot move `node` into `Some` because `node` does not implement Copy; fix by wrapping node in Box::new() -> Option A
Quick Check:
Box node before Some to fix type error [OK]
Hint: Wrap node in Box::new() for Option [OK]
Common Mistakes:
Trying to use node directly without Box
Using references without lifetimes
Ignoring compiler type mismatch errors
5. You want to manually create a binary tree in Rust with root value 10, left child 5, and right child 15. Which code snippet correctly builds this tree and prints the right child's value?
hard
A. fn main() {
struct Node {
value: i32,
left: Option,
right: Option,
}
let left = Node { value: 5, left: None, right: None };
let right = Node { value: 15, left: None, right: None };
let root = Node {
value: 10,
left: Some(left),
right: Some(right),
};
println!("{}", root.right.as_ref().unwrap().value);
}