📖 Scenario: You are working with a binary search tree (BST) that stores numbers. You want to find the kth smallest number in this tree. This is useful when you want to quickly find a specific ranked value, like the 3rd smallest score in a game leaderboard.
🎯 Goal: Build a Rust program that creates a BST, sets a value k, and finds the kth smallest element in the BST using an in-order traversal.
📋 What You'll Learn
Create a BST with the exact nodes and structure given
Create a variable k with the exact value given
Implement an in-order traversal function to find the kth smallest element
Print the kth smallest element exactly as shown
💡 Why This Matters
🌍 Real World
Finding the kth smallest element is useful in ranking systems, like leaderboards, or when you want to find a specific percentile value in sorted data stored in a BST.
💼 Career
Understanding BST traversal and selection algorithms is important for software engineers working with databases, search engines, and any system that requires efficient data retrieval and ranking.
Progress0 / 4 steps
1
Create the BST nodes
Create a binary search tree with the following structure using Node structs and Option> for children. The root node should have value 5, with left child 3 and right child 7. The left child 3 has children 2 (left) and 4 (right). The right child 7 has children 6 (left) and 8 (right). Define the Node struct with value: i32, left: Option>, and right: Option>. Name the root variable root.
DSA Rust
Hint
Define the Node struct first. Then create the root variable with nested Some(Box::new(Node { ... })) for each child.
2
Set the value of k
Create a variable called k and set it to 3. This means we want to find the 3rd smallest element in the BST.
DSA Rust
Hint
Just add let k = 3; inside the main function after the BST creation.
3
Implement in-order traversal to find kth smallest
Write a function called kth_smallest that takes root: &Option> and k: usize and returns Option. Use in-order traversal (left, root, right) to visit nodes in ascending order. Use a mutable counter to track how many nodes have been visited. Return the value of the kth visited node. Call this function inside main with root and k.
DSA Rust
Hint
Use a helper function inorder inside kth_smallest to do the traversal. Keep track of how many nodes you visited with count. When count == k, save the node's value in result.
4
Print the kth smallest element
Print the kth smallest element found by kth_smallest. Use println!("{}", value) where value is the integer inside the Option. You can unwrap safely because k is valid.
DSA Rust
Hint
Use println!("{}", result.unwrap()); to print the value inside the Option. We unwrap safely because k is valid.
Practice
(1/5)
1. What does the Kth Smallest Element in BST problem ask you to find?
easy
A. The largest element in the BST
B. The element that is the Kth smallest when the BST elements are sorted
C. The root node of the BST
D. The height of the BST
Solution
Step 1: Understand BST property
A Binary Search Tree (BST) stores elements so that left child nodes are smaller and right child nodes are larger.
Step 2: Define Kth smallest element
The Kth smallest element is the element that appears in position K when all elements are sorted in ascending order.
Final Answer:
The element that is the Kth smallest when the BST elements are sorted -> Option B
Quick Check:
Kth smallest = Kth element in sorted order [OK]
Hint: Think sorted order of BST nodes in ascending order [OK]
Common Mistakes:
Confusing Kth smallest with largest element
Thinking root is always Kth smallest
Mixing up height with element value
2. Which Rust code snippet correctly defines a BST node struct for the Kth smallest element problem?