Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a value into a Binary Search Tree (BST).
DSA C
if (root == NULL) { root = (Node*)malloc(sizeof(Node)); root->data = [1]; root->left = root->right = NULL; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'root' instead of 'value' to assign data.
Assigning NULL to data field.
✗ Incorrect
We assign the new value to the data field of the new node when root is NULL.
2fill in blank
mediumComplete the code to decide whether to insert into left or right subtree in BST insertion.
DSA C
if (value < root->data) { root->left = insert(root->left, [1]); } else { root->right = insert(root->right, value); }
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'root' instead of 'value' in recursive call.
Passing 'data' which is undefined here.
✗ Incorrect
We recursively insert the same value into the left subtree if it is less than root's data.
3fill in blank
hardFix the error in the BST search function to correctly compare values.
DSA C
if (root == NULL || root->data == [1]) { return root; } else if (value < root->data) { return search(root->left, value); } else { return search(root->right, value); }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing root->data with root instead of value.
Using NULL in comparison.
✗ Incorrect
We compare root's data with the value we are searching for.
4fill in blank
hardFill both blanks to create a function that counts nodes in a BST recursively.
DSA C
int countNodes(Node* root) {
if (root == NULL) {
return 0;
}
return 1 + countNodes([1]) + countNodes([2]);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'root' instead of child pointers.
Using NULL which causes no recursion.
✗ Incorrect
We count nodes by adding 1 for current node plus counts of left and right subtrees.
5fill in blank
hardFill all three blanks to create a function that finds the minimum value in a BST.
DSA C
int findMin(Node* root) {
if (root == NULL) {
return -1; // Empty tree
}
while (root->[1] != [2]) {
root = root->[3];
}
return root->data;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' to find minimum.
Comparing to 'data' instead of NULL.
✗ Incorrect
The minimum value in BST is found by going left until left child is NULL.
