📖 Scenario: Imagine you are organizing books on a shelf by their unique ID numbers. You want to keep the shelf sorted so you can quickly find any book. This is similar to how a Binary Search Tree (BST) keeps data organized.
🎯 Goal: You will create a simple Binary Search Tree node structure in Rust, add values following the BST property, and then print the tree in order to see the sorted values.
📋 What You'll Learn
Create a BST node struct with integer value and optional left and right children
Create a root node with a specific value
Add two child nodes following the BST property: left child less than root, right child greater than root
Print the values of the BST in sorted order using in-order traversal
💡 Why This Matters
🌍 Real World
Binary Search Trees are used in databases, file systems, and many software systems to keep data sorted and allow fast searching.
💼 Career
Understanding BSTs is important for software engineers, especially those working with data structures, algorithms, and performance optimization.
Progress0 / 4 steps
1
Create the BST Node Structure
Create a struct called BSTNode with a field value of type i32, and two optional boxed children called left and right of type Option>.
DSA Rust
Hint
Use struct BSTNode with fields value: i32, left: Option>, and right: Option>.
2
Create the Root Node
Create a variable called root of type BSTNode with value set to 10, and both left and right set to None.
DSA Rust
Hint
Use let root = BSTNode { value: 10, left: None, right: None }; inside fn main().
3
Add Left and Right Child Nodes Following BST Property
Inside fn main(), create two variables: left_child with value 5 and right_child with value 15, both with left and right set to None. Then create a new root node with value 10, left set to Some(Box::new(left_child)), and right set to Some(Box::new(right_child)).
DSA Rust
Hint
Create left_child and right_child with values 5 and 15, then assign them as boxed children of root.
4
Print the BST Values In-Order
Write a function called in_order that takes a reference to a BSTNode and prints the values in sorted order using in-order traversal. Then call in_order(&root) inside fn main().
DSA Rust
Hint
Use recursion to visit left child, print current node value, then visit right child. Call in_order(&root) in main.
Practice
(1/5)
1. What is the main property that defines a Binary Search Tree (BST)?
easy
A. For each node, all values in the left subtree are smaller, and all values in the right subtree are larger.
B. Each node has exactly two children.
C. All leaf nodes are at the same depth.
D. Nodes are arranged in a complete binary tree structure.
Solution
Step 1: Understand BST definition
A BST requires that for every node, values in the left subtree are smaller and values in the right subtree are larger.
Step 2: Compare options to BST property
For each node, all values in the left subtree are smaller, and all values in the right subtree are larger. matches this definition exactly, while others describe different tree types or properties.
Final Answer:
For each node, all values in the left subtree are smaller, and all values in the right subtree are larger. -> Option A
Quick Check:
BST property = For each node, all values in the left subtree are smaller, and all values in the right subtree are larger. [OK]
Hint: Remember: left < node < right in BST [OK]
Common Mistakes:
Confusing BST with complete binary tree
Thinking all nodes must have two children
Assuming leaf nodes must be at same depth
2. Which Rust code snippet correctly declares a BST node struct with optional left and right children?