📖 Scenario: You are working with a simple binary tree structure that stores numbers. You want to find out how many nodes are in this tree.
🎯 Goal: Build a Rust program that creates a binary tree, then counts and prints the total number of nodes in it.
📋 What You'll Learn
Create a binary tree with exactly 3 nodes: root with value 10, left child with value 5, and right child with value 15
Add a helper function to count nodes recursively
Print the total count of nodes in the tree
💡 Why This Matters
🌍 Real World
Counting nodes in a binary tree is a basic operation useful in many applications like searching, sorting, and organizing data.
💼 Career
Understanding tree structures and recursion is important for software engineering roles that involve data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the Binary Tree Structure and Nodes
Define a struct called Node with fields value: i32, left: Option>, and right: Option>. Then create a variable called root that is a Node with value 10, left child node with value 5, and right child node with value 15.
DSA Rust
Hint
Use Option> for left and right children to allow empty branches.
2
Add a Function to Count Nodes
Add a function called count_nodes that takes a reference to an Option> and returns an i32. The function should return 0 if the node is None, otherwise return 1 + count_nodes(left) + count_nodes(right).
DSA Rust
Hint
Use match to check if the node is None or Some.
3
Call the Count Function on the Root Node
Inside main, create a variable called total_nodes and set it to the result of calling count_nodes with a reference to Some(Box::new(root)).
DSA Rust
Hint
Wrap root in Some(Box::new(...)) to match the function parameter type.
4
Print the Total Number of Nodes
Add a println! statement to print the text Total nodes: followed by the value of total_nodes.
DSA Rust
Hint
Use println!("Total nodes: {}", total_nodes); to print the result.
Practice
(1/5)
1. What does the function to count total nodes in a binary tree do?
easy
A. It returns the sum of all node values.
B. It returns the total number of nodes in the tree.
C. It returns the height of the tree.
D. It returns the number of leaf nodes only.
Solution
Step 1: Understand the function's purpose
The function is designed to count how many nodes exist in the entire binary tree.
Step 2: Differentiate from other tree properties
Counting nodes is different from calculating height, sum of values, or counting leaves.
Final Answer:
It returns the total number of nodes in the tree. -> Option B
Quick Check:
Counting nodes = total nodes [OK]
Hint: Count all nodes, not just leaves or height [OK]
Common Mistakes:
Confusing node count with tree height
Counting only leaf nodes
Summing node values instead of counting
2. Which Rust code snippet correctly defines a binary tree node struct for counting nodes?
A. Using unwrap() twice consumes the value causing a move error.
B. The function should return usize, not i32.
C. The base case should return 1 instead of 0.
D. The function should use references instead of ownership.
Solution
Step 1: Understand ownership and unwrap behavior
Calling unwrap() consumes the Option, so calling it twice on the same variable causes a move error.
Step 2: Identify correct fix
Store the result of unwrap() in a variable to avoid multiple moves or use references.
Final Answer:
Using unwrap() twice consumes the value causing a move error. -> Option A
Quick Check:
unwrap() moves value; multiple calls cause error [OK]
Hint: Call unwrap() once, store result before use [OK]
Common Mistakes:
Calling unwrap() multiple times on same Option
Ignoring ownership and move semantics
Returning wrong base case value
5. Given a binary tree where each node has a value and pointers to left and right children, how would you modify the counting function to count only nodes with values greater than 10?
Choose the correct Rust function implementation.
hard
A. fn count_nodes_gt_10(root: &Option) -> i32 {
if root.is_none() {
return 0;
}
let node = root.unwrap();
if node.value > 10 {
1 + count_nodes_gt_10(&node.left) + count_nodes_gt_10(&node.right)
} else {
0
}
}