Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL instead of nullptr
Comparing to 0 instead of nullptr
✗ Incorrect
In C++, the correct way to check for a null pointer is using nullptr.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect member names like 'value' or 'key'
Trying to access non-existent members
✗ Incorrect
The node's stored value is typically accessed with data in BST nodes.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' for left subtree
Using non-existent pointers like 'parent' or 'child'
✗ Incorrect
To search in the left subtree, we must use root->left.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' for the subtree
Using incorrect member names
✗ Incorrect
We compare with root->data and recurse into root->right when key is greater.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL instead of nullptr
Using wrong member names for node data
✗ Incorrect
The function checks if root is nullptr, compares key with root->data, and recurses accordingly.