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.
Click to reveal answer
beginner
How does the search operation work in a BST?
Start at the root. If the value matches, return found. If the value is smaller, search the left subtree. If larger, search the right subtree. Repeat until found or no nodes left.
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 (skewed tree), it becomes O(n).
Click to reveal answer
intermediate
Show the C code snippet for searching a value in a BST.
struct Node* search(struct Node* root, int key) {
if (root == NULL || root->data == key)
return root;
if (key < root->data)
return search(root->left, key);
else
return search(root->right, key);
}
Click to reveal answer
beginner
What happens if the search key is not present in the BST?
The search function will eventually reach a NULL node and return NULL, indicating the key is not found in the tree.
Click to reveal answer
In a BST, if the search key is less than the current node's value, where do you search next?
ARight subtree
BBoth subtrees
CRoot node
DLeft subtree
✗ Incorrect
If the key is less than the current node's value, the search continues in the left subtree.
What is the best case time complexity of searching in a balanced BST?
AO(log n)
BO(1)
CO(n)
DO(n log n)
✗ Incorrect
The best case occurs when the key matches the root, taking O(1) time.
What does the search function return if the key is found in the BST?
ANULL
BRoot node
CPointer to the node containing the key
DBoolean false
✗ Incorrect
The search returns a pointer to the node containing the key.
If the BST is skewed (like a linked list), what is the worst-case search time?
AO(n)
BO(log n)
CO(1)
DO(n log n)
✗ Incorrect
In a skewed BST, search time degrades to O(n).
Which of these is NOT true about BST search?
ASearch always checks both left and right subtrees
BSearch moves right if key is larger
CSearch moves left if key is smaller
DSearch starts at the root
✗ Incorrect
Search does NOT check both subtrees; it chooses one based on comparison.
Explain how the search operation works in a Binary Search Tree step-by-step.
Think of searching like looking for a name in a sorted phone book.
You got /6 concepts.
Describe the time complexity of searching in a BST and what affects it.
Balanced trees are faster to search than skewed ones.
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 delete a value from the BST
B. To find whether a given value exists in the BST
C. To insert a new value into the BST
D. To traverse all nodes in the BST
Solution
Step 1: Understand the search operation
The search operation checks if a specific value is present in the BST by comparing values starting from the root.
Step 2: Differentiate from other operations
Insertion adds new nodes, deletion removes nodes, and traversal visits all nodes. Search only checks for presence.
Final Answer:
To find whether a given value exists in the BST -> Option B
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 C syntax to start searching for a value in a BST given a pointer root and an integer key?
easy
A. search(root, key);
B. search(key, root);
C. search(root->key, key);
D. search(key);
Solution
Step 1: Understand function parameters for BST search
The search function typically takes the root node pointer first, then the key to find.
Step 2: Check each option's parameter order and types
search(root, key); correctly passes root and key. search(key, root); reverses parameters. search(root->key, key); passes a value, not a node pointer. search(key); misses root.
Final Answer:
search(root, key); -> Option A
Quick Check:
Function(root, key) = Correct call [OK]
Hint: Root pointer first, key second in search call [OK]
Common Mistakes:
Swapping parameter order
Passing node value instead of pointer
Omitting root parameter
3. Consider the following BST and the search function call search(root, 15);. What will be the output if the BST contains nodes with values 10, 5, 15, 12, 20?
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* search(struct Node* root, int key) {
if (root == NULL || root->data == key)
return root;
if (key < root->data)
return search(root->left, key);
else
return search(root->right, key);
}
// Assume root points to the BST described above
struct Node* result = search(root, 15);
if (result != NULL) printf("Found: %d", result->data);
else printf("Not Found");
medium
A. Found: 20
B. Found: 10
C. Not Found
D. Found: 15
Solution
Step 1: Trace the search for key 15 in the BST
Start at root (10). Since 15 > 10, go right to node 15.
Step 2: Check if node 15 matches key
Node 15's data equals key 15, so return this node.
Final Answer:
Found: 15 -> Option D
Quick Check:
Search found node with data 15 [OK]
Hint: Compare key with node, go left or right accordingly [OK]
Common Mistakes:
Stopping at root without checking children
Returning wrong node data
Assuming search always returns root
4. The following C code attempts to search for a key in a BST but has a bug. What is the error?
struct Node* search(struct Node* root, int key) {
if (root == NULL)
return NULL;
if (root->data == key)
return root;
if (key > root->data)
return search(root->left, key);
else
return search(root->right, key);
}
medium
A. Missing base case for empty tree
B. Incorrect return type for search function
C. The recursive calls for left and right subtrees are swapped
D. No error, code is correct
Solution
Step 1: Analyze recursive calls for BST search
For BST, if key > root->data, search right subtree; if less, search left subtree.
Step 2: Check the code's recursive calls
Code calls search(root->left, key) when key > root->data, which is incorrect. It should call search(root->right, key).
Final Answer:
The recursive calls for left and right subtrees are swapped -> Option C
Quick Check:
Key > root means go right, not left [OK]
Hint: Go right if key > node, left if less [OK]
Common Mistakes:
Swapping left and right subtree calls
Ignoring base case
Confusing comparison operators
5. You have a BST with nodes: 8, 3, 10, 1, 6, 14, 4, 7, 13. You want to search for the value 13. Which sequence of nodes will the search visit in order?
hard
A. 8 -> 10 -> 14 -> 13
B. 8 -> 3 -> 6 -> 7 -> 13
C. 8 -> 10 -> 13
D. 8 -> 14 -> 13
Solution
Step 1: Start at root and compare with key 13
Root is 8. Since 13 > 8, go right to 10.
Step 2: Compare 13 with 10 and move accordingly
13 > 10, go right to 14. Then 13 < 14, go left to 13.
Step 3: Sequence of visited nodes
Visited nodes are 8, 10, 14, then 13.
Final Answer:
8 -> 10 -> 14 -> 13 -> Option A
Quick Check:
Follow BST rules to trace path [OK]
Hint: Compare and move left or right until found [OK]
Common Mistakes:
Skipping nodes in path
Going left when should go right
Assuming direct jump to node
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.