Bird
Raised Fist0
DSA C++programming~10 mins

BST Search Operation in DSA C++ - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the current node is null in the BST search function.

DSA C++
if (root == [1]) {
    return false;
}
Drag options to blanks, or click blank then click option'
Aroot
Bnullptr
C0
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL instead of nullptr
Comparing to 0 instead of nullptr
2fill in blank
medium

Complete the code to compare the search key with the current node's data.

DSA C++
if (key == root->[1]) {
    return true;
}
Drag options to blanks, or click blank then click option'
Akey
Bvalue
Cdata
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect member names like 'value' or 'key'
Trying to access non-existent members
3fill in blank
hard

Fix the error in the recursive call to search in the left subtree.

DSA C++
return searchBST(root->[1], key);
Drag options to blanks, or click blank then click option'
Aleft
Bparent
Cright
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' for left subtree
Using non-existent pointers like 'parent' or 'child'
4fill in blank
hard

Fill both blanks to complete the recursive search in the right subtree when key is greater.

DSA C++
if (key > root->[1]) {
    return searchBST(root->[2], key);
}
Drag options to blanks, or click blank then click option'
Adata
Bleft
Cright
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' for the subtree
Using incorrect member names
5fill in blank
hard

Fill all three blanks to complete the BST search function.

DSA C++
bool searchBST(Node* root, int key) {
    if (root == [1]) return false;
    if (key == root->[2]) return true;
    if (key < root->[3]) return searchBST(root->left, key);
    else return searchBST(root->right, key);
}
Drag options to blanks, or click blank then click option'
Anullptr
Bdata
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL instead of nullptr
Using wrong member names for node data

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 node in the BST
C. To delete a node from the BST
D. To traverse all nodes in the BST

Solution

  1. 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.
  2. Step 2: Differentiate from other operations

    Insertion adds nodes, deletion removes nodes, traversal visits nodes. Search only checks presence.
  3. Final Answer:

    To find whether a given value exists in the BST -> Option A
  4. Quick Check:

    Search = Find value [OK]
Hint: Search checks presence, not insertion or deletion [OK]
Common Mistakes:
  • Confusing search with insertion
  • Thinking search modifies the tree
  • Mixing search with traversal
2. Which of the following is the correct syntax to start a recursive search function in C++ for a BST node pointer root and integer key?
easy
A. void search(Node root, int key) { ... }
B. bool search(Node* root, int key) { ... }
C. int search(Node* root, int key) { ... }
D. Node search(Node* root, int key) { ... }

Solution

  1. Step 1: Identify return type for search

    Search returns true/false if key found, so return type should be bool.
  2. Step 2: Check parameter types

    Root is a pointer to Node, key is int. So signature is bool search(Node* root, int key).
  3. Final Answer:

    bool search(Node* root, int key) { ... } -> Option B
  4. Quick Check:

    Return bool and pointer param [OK]
Hint: Search returns bool, takes Node pointer and key [OK]
Common Mistakes:
  • Using void return type for search
  • Passing Node by value instead of pointer
  • Returning Node instead of bool
3. Consider the following BST and the search function call search(root, 15). What will be the output?
struct Node {
  int data;
  Node* left;
  Node* right;
};

bool search(Node* root, int key) {
  if (!root) return false;
  if (root->data == key) return true;
  if (key < root->data) return search(root->left, key);
  else return search(root->right, key);
}

// BST structure:
//       10
//      /  \
//     5    20
//         /  \
//        15  30

// Call:
// bool found = search(root, 15);
// std::cout << (found ? "Found" : "Not Found");
medium
A. Found
B. Not Found
C. Compilation Error
D. Runtime Error

Solution

  1. Step 1: Trace search path for key 15

    Start at root 10, 15 > 10, go right to 20. Then 15 < 20, go left to 15.
  2. Step 2: Check node data at 15

    Node data matches key 15, so function returns true and prints "Found".
  3. Final Answer:

    Found -> Option A
  4. Quick Check:

    Key found in BST = Found [OK]
Hint: Follow BST rules left/right to find key [OK]
Common Mistakes:
  • Stopping search too early
  • Confusing left and right child directions
  • Assuming key not found when present
4. Identify the error in the following BST search function and choose the correct fix:
bool search(Node* root, int key) {
  if (root == nullptr) return false;
  if (root->data == key) return true;
  if (key > root->data) return search(root->left, key);
  else return search(root->right, key);
}
medium
A. Remove base case for nullptr
B. Change return type to int
C. Swap left and right in recursive calls; should be key < root->data for left
D. Add print statements for debugging

Solution

  1. Step 1: Analyze recursive calls logic

    For BST, if key < root->data, search left; if key > root->data, search right.
  2. Step 2: Identify incorrect direction

    Code searches left when key > root->data, which is wrong. Left subtree has smaller values.
  3. Step 3: Correct the recursive calls

    Swap left and right calls: if (key < root->data) search left; else search right.
  4. Final Answer:

    Swap left and right in recursive calls; should be key < root->data for left -> Option C
  5. Quick Check:

    BST property fixes search direction [OK]
Hint: Left subtree < node, right subtree > node [OK]
Common Mistakes:
  • Mixing left and right subtree directions
  • Ignoring base case
  • Changing return type unnecessarily
5. Given a BST with nodes inserted in this order: 25, 15, 50, 10, 22, 35, 70. You want to search for the value 22. How many nodes will the search function visit before finding 22?
hard
A. 2
B. 5
C. 4
D. 3

Solution

  1. Step 1: Build BST structure from insertion order

    Insert 25 (root), then 15 (left of 25), 50 (right of 25), 10 (left of 15), 22 (right of 15), 35 (left of 50), 70 (right of 50).
  2. Step 2: Trace search path for 22

    Start at 25 (visit 1), 22 < 25 go left to 15 (visit 2), 22 > 15 go right to 22 (visit 3), found key.
  3. Final Answer:

    3 -> Option D
  4. Quick Check:

    Nodes visited = 3 [OK]
Hint: Count nodes visited following BST search path [OK]
Common Mistakes:
  • Counting nodes not visited
  • Misplacing nodes in BST
  • Stopping count before finding key
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard