A Binary Search Tree is a tree data structure where each node has at most two children. For every node, all values in the left subtree are smaller, and all values in the right subtree are larger than the node's value.
Click to reveal answer
beginner
How does the search operation work in a BST?
Start at the root. If the value matches the node, return it. If the value is smaller, search the left subtree. If larger, search the right subtree. Repeat until found or reach a leaf.
Click to reveal answer
intermediate
What is the time complexity of searching in a BST?
The average time complexity is O(log n) when the tree is balanced. In the worst case (unbalanced), it can be O(n).
Click to reveal answer
beginner
What happens if the search value is not in the BST?
The search will reach a null (empty) child pointer, indicating the value is not present in the tree.
Click to reveal answer
beginner
Show a simple Rust function signature for BST search.
fn search(root: &Option, value: i32) -> bool
Click to reveal answer
In a BST, if the search value is less than the current node's value, where do you search next?
ALeft subtree
BRight subtree
CCurrent node
DRoot node
✗ Incorrect
In a BST, smaller values are always in the left subtree.
What is the best case time complexity of searching in a balanced BST?
AO(n)
BO(log n)
CO(1)
DO(n log n)
✗ Incorrect
The best case is when the target is at the root, found in O(1) time.
If the search value is not found in the BST, what will the search function return?
AThe root node
BThe closest larger value
CThe closest smaller value
DNull or false
✗ Incorrect
The search ends at a null pointer indicating the value is not present.
Which data structure property helps BST search be efficient?
ANodes have multiple parents
BLeft child values are smaller, right child values are larger
CAll nodes have the same value
DNodes are stored in a linked list
✗ Incorrect
This property allows the search to decide which subtree to explore.
In Rust, what type is commonly used to represent a BST node's child?
AOption<Box<Node>>
BHashMap<Node>
CVec<Node>
DRc<Node>
✗ Incorrect
Option> allows for nullable pointers and heap allocation.
Explain step-by-step how to search for a value in a Binary Search Tree.
Think about how the BST property guides the search direction.
You got /6 concepts.
Describe how the structure of a BST affects the efficiency of the search operation.
Consider how the shape of the tree changes search steps.
You got /5 concepts.
Practice
(1/5)
1. What does the search operation in a Binary Search Tree (BST) do?
easy
A. Counts the number of nodes in the BST
B. Inserts a new value into the BST
C. Deletes a value from the BST
D. Finds whether a given value exists in the BST
Solution
Step 1: Understand the purpose of BST search
The search operation checks if a specific value is present in the BST by comparing values and moving left or right accordingly.
Step 2: Differentiate from other BST operations
Insertion adds new nodes, deletion removes nodes, and counting nodes is a separate operation. Search only checks presence.
Final Answer:
Finds whether a given value exists in the BST -> Option D
Quick Check:
Search = Find value [OK]
Hint: Search checks presence, not insertion or deletion [OK]
Common Mistakes:
Confusing search with insert or delete
Thinking search counts nodes
Assuming search modifies the tree
2. Which of the following is the correct Rust function signature for a BST search method that returns a boolean?
easy
A. fn search(&mut self, value: i32) -> i32
B. fn search(value: i32) -> bool
C. fn search(&self, value: i32) -> bool
D. fn search(&self, value: i32) -> Option<Node>
Solution
Step 1: Identify method receiver and return type
Search should borrow the tree immutably (&self) and return true/false (bool) indicating presence.
Step 2: Check parameters and return correctness
Value parameter is i32, and return type is bool. Mutable borrow or returning Node is incorrect for simple search.
Step 1: Trace search for value 7 starting at root 5
7 > 5, so search right subtree with root 8.
Step 2: Search right subtree node with value 8 for 7
7 < 8, so search left subtree of node 8, which is None, so returns false.
Final Answer:
false -> Option A
Quick Check:
7 not found in tree = false [OK]
Hint: Follow BST rules: left < node < right [OK]
Common Mistakes:
Assuming 7 is found because 8 exists
Confusing left and right subtree directions
Ignoring None case returns false
4. Identify the bug in this Rust BST search method:
fn search(&self, val: i32) -> bool {
if val == self.value {
true
} else if val < self.value {
self.left.as_ref().unwrap().search(val)
} else {
self.right.as_ref().unwrap().search(val)
}
}
medium
A. Using unwrap() without checking if child exists can cause panic
B. Missing return keyword before recursive calls
C. Incorrect comparison operators used
D. Function should return Option<bool> instead of bool
Solution
Step 1: Analyze use of unwrap() on Option
unwrap() assumes the Option is Some, but if child is None, it causes runtime panic.
Step 2: Understand safe alternative
Should check if child exists using match or if let before calling search to avoid panic.
Final Answer:
Using unwrap() without checking if child exists can cause panic -> Option A
Quick Check:
unwrap() on None = panic [OK]
Hint: Never unwrap Option without checking Some first [OK]
Common Mistakes:
Ignoring possibility of None causing panic
Thinking unwrap() is safe always
Confusing return types
5. You want to search for a value in a BST that may contain duplicate values on the right subtree. How should you modify the search operation to find if the value exists anywhere in the tree?
hard
A. Search left subtree if val <= node, else search right subtree
B. Search left subtree if val < node, else search both right subtree and current node for val
C. Search left subtree if val < node, else search right subtree and if not found, search left subtree again
D. Search both left and right subtrees regardless of comparison, then check current node
Solution
Step 1: Understand duplicates on right subtree
Duplicates are stored on the right, so if val equals node value, we found it; else if val > node, search right subtree.
Step 2: Modify search to check current node and right subtree for duplicates
If val equals node value, return true immediately. If val > node value, search right subtree. If val < node value, search left subtree.
Final Answer:
Search left subtree if val < node, else search both right subtree and current node for val -> Option B
Quick Check:
Duplicates on right: check current and right subtree [OK]
Hint: Check current node before right subtree for duplicates [OK]
Common Mistakes:
Ignoring duplicates on right subtree
Searching only one subtree without checking current node
Searching left subtree unnecessarily for duplicates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.