A BST is a tree where each node has at most two children. The left child's value is less than the parent's value, and the right child's value is greater. This helps in fast searching.
Click to reveal answer
beginner
How does the search operation work in a BST?
Start at the root. If the value matches, return it. If the value is smaller, go to the left child. If larger, go to the right child. Repeat until found or no child exists.
Click to reveal answer
intermediate
Why is searching in a BST efficient?
Because each comparison lets you skip half of the remaining tree, similar to binary search in a list. This makes search time proportional to the tree height.
Click to reveal answer
beginner
What happens if the search value is not in the BST?
The search will reach a leaf node without finding the value, and then it returns 'not found' or None.
Click to reveal answer
intermediate
Show the Python code snippet for searching a value in a BST node.
def search_bst(node, val):
if node is None:
return None
if val == node.val:
return node
elif val < node.val:
return search_bst(node.left, val)
else:
return search_bst(node.right, val)
Click to reveal answer
In a BST, if the search value is less than the current node's value, where do you go next?
ATo the right child
BTo the left child
CTo the parent node
DStop searching
✗ Incorrect
In BST, smaller values are always in the left subtree.
What is the time complexity of searching in a balanced BST?
AO(log n)
BO(1)
CO(n log n)
DO(n)
✗ Incorrect
Balanced BST search takes O(log n) time because it halves the search space each step.
If the search value is equal to the current node's value, what should the search operation do?
AReturn the current node
BGo to the left child
CGo to the right child
DContinue searching both children
✗ Incorrect
When the value matches, the search is successful and returns the node.
What does the search operation return if the value is not found in the BST?
AThe root node
BThe last visited node
CNone or 'not found'
DAn error
✗ Incorrect
If the value is not found, the search returns None or a similar indication.
Which traversal method is used in the BST search operation?
AIn-order traversal
BPre-order traversal
CPost-order traversal
DDirected search based on value comparison
✗ Incorrect
BST search uses directed search, choosing left or right child based on value comparison.
Explain step-by-step how to search for a value in a BST.
Think about how you decide which child to visit next.
You got /6 concepts.
Write a simple recursive function to search a value in a BST and explain how it works.
Focus on the conditions and recursive calls.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the search operation in a Binary Search Tree (BST)?
easy
A. To find whether a given value exists in the BST
B. To insert a new value into the BST
C. To delete a value from the BST
D. To traverse all nodes in the BST
Solution
Step 1: Understand the purpose of search in BST
The search operation is used to check if a specific value is present in the BST.
Step 2: Differentiate from other operations
Insertion adds new values, deletion removes values, and traversal visits all nodes. Search only checks for existence.
Final Answer:
To find whether a given value exists in the BST -> Option A
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 returns all nodes
Assuming search modifies the tree
2. Which of the following is the correct Python function signature for a BST search method that takes a root node and a key to find?
easy
A. def search(key, root):
B. def search(key):
C. def search(root):
D. def search(root, key):
Solution
Step 1: Identify required parameters for BST search
The search needs the root node to start and the key to find.
Step 2: Match parameters order
Common convention is root first, then key, so def search(root, key): is correct.
Final Answer:
def search(root, key): -> Option D
Quick Check:
Function(root, key) correct signature [OK]
Hint: Root node first, key second in search function [OK]
Common Mistakes:
Swapping parameter order
Missing one parameter
Using only one parameter
3. Given the following BST and the search function call search(root, 15), what will be the output?
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def search(root, key):
if root is None or root.val == key:
return root
if key < root.val:
return search(root.left, key)
return search(root.right, key)
root = Node(10)
root.left = Node(5)
root.right = Node(20)
root.left.left = Node(3)
root.left.right = Node(7)
root.right.left = Node(15)
root.right.right = Node(25)
result = search(root, 15)
print(result.val if result else 'Not found')
medium
A. 15
B. 20
C. Not found
D. 10
Solution
Step 1: Trace search path for key 15
Start at root (10). Since 15 > 10, go right to node 20. Then 15 < 20, go left to node 15.
Step 2: Check node value
Node value is 15, matches key, so return this node.
Final Answer:
15 -> Option A
Quick Check:
Search found node with value 15 [OK]
Hint: Follow BST rules: left < node < right [OK]
Common Mistakes:
Stopping search too early
Going wrong direction at node 20
Returning root value instead of found node
4. The following code is intended to search for a key in a BST, but it has a bug. What is the error?
def search(root, key):
if root is None:
return None
if root.val == key:
return root
if key > root.val:
return search(root.left, key)
else:
return search(root.right, key)
medium
A. The function does not return anything
B. Missing base case for root is None
C. The recursive calls to left and right are swapped
D. The comparison operator should be >= instead of >
Solution
Step 1: Analyze recursive calls
For BST, if key > root.val, search should go right subtree, but code calls search(root.left, key).
Step 2: Identify correct direction
Correct is to go right when key > root.val and left when key < root.val.
Final Answer:
The recursive calls to left and right are swapped -> Option C
Quick Check:
Swap left/right calls to fix search [OK]
Hint: Remember: key > node means go right subtree [OK]
Common Mistakes:
Swapping left and right recursive calls
Forgetting base case for None
Incorrect comparison operators
5. You want to search for the value 8 in the BST below. Which sequence of nodes will the search visit?
BST structure: 10 / \ 5 15 / \ \ 3 7 20
Choose the correct sequence of visited nodes for search(root, 8).
hard
A. [10, 5, 7]
B. [10, 5, 7, None]
C. [10, 15, 20]
D. [10, 5, 3]
Solution
Step 1: Start at root and compare with 8
8 < 10, go left to 5.
Step 2: Compare with 5 and go right
8 > 5, go right to 7.
Step 3: Compare with 7 and go right
8 > 7, go right which is None (no node), search ends.
Final Answer:
[10, 5, 7, None] -> Option B
Quick Check:
Search path includes None when key not found [OK]
Hint: Follow path until None if key missing [OK]
Common Mistakes:
Stopping at last node without None
Going left instead of right at 7
Ignoring None as end of search
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.