📖 Scenario: You are working with a Binary Search Tree (BST) that stores numbers. You want to find the smallest number in the tree. This is useful when you want to quickly find the minimum value in a sorted structure.
🎯 Goal: Build a simple BST in Rust and write code to find and print the minimum element in the BST.
📋 What You'll Learn
Create a BST node struct with integer values
Manually create a BST with specific nodes
Write a function to find the minimum element in the BST
Print the minimum element found
💡 Why This Matters
🌍 Real World
Finding the minimum element in a BST is useful in databases and search engines where quick access to sorted data is needed.
💼 Career
Understanding BST operations is important for software engineers working with data structures, algorithms, and performance optimization.
Progress0 / 4 steps
1
Create BST Node Struct and Build Tree
Create a struct called Node with fields value: i32, left: Option>, and right: Option>. Then create a variable called root that represents the root node of the BST with value 10. Add a left child node with value 5 and a 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 Find Minimum Value
Add a function called find_min that takes a reference to a Node and returns the minimum i32 value in the BST. The function should follow the left child nodes until it finds the leftmost node.
DSA Rust
Hint
Use recursion to keep going left until there is no left child.
3
Call the Function to Find Minimum
Inside main, create a variable called min_value and set it to the result of calling find_min with a reference to root.
DSA Rust
Hint
Call find_min with &root and store the result in min_value.
4
Print the Minimum Value
Add a println! statement to print the text "Minimum value in BST: " followed by the value of min_value.
DSA Rust
Hint
Use println!("Minimum value in BST: {}", min_value); to print the result.
Practice
(1/5)
1. In a Binary Search Tree (BST), where is the minimum element always located?
easy
A. At the leftmost node
B. At the rightmost node
C. At the root node
D. At any leaf node
Solution
Step 1: Understand BST property
In a BST, left child nodes have smaller values than their parents.
Step 2: Locate minimum element
The smallest value is found by going left until no more left child exists.
Final Answer:
At the leftmost node -> Option A
Quick Check:
Minimum element = leftmost node [OK]
Hint: Minimum is always the leftmost node in BST [OK]
Common Mistakes:
Thinking minimum is at root
Confusing leftmost with rightmost
Assuming minimum can be any leaf
2. Which Rust code snippet correctly finds the minimum element in a BST node structure?
easy
A. while let Some(ref left) = current.left { current = left; } current.value
B. while let Some(ref right) = current.right { current = right; } current.value
C. if current.right.is_none() { current.value } else { current.right.as_ref().unwrap().value }
D. if current.left.is_none() { current.value } else { current.right.as_ref().unwrap().value }
Solution
Step 1: Traverse left children
To find minimum, move left while left child exists.
Step 2: Return current node's value
When no left child, current node holds minimum value.
Final Answer:
while let Some(ref left) = current.left { current = left; } current.value -> Option A
Quick Check:
Traverse left until None, then return value [OK]
Hint: Use while let to traverse left children [OK]
Common Mistakes:
Traversing right instead of left
Returning wrong node's value
Using if incorrectly without loop
3. Given this Rust code for a BST node, what will be printed?