Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given value.
DSA C
struct Node* newNode(int value) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = [1];
node->left = NULL;
node->right = NULL;
return node;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using an undefined variable instead of 'value'.
Assigning node instead of node->data.
✗ Incorrect
The new node's data field should be set to the given value parameter.
2fill in blank
mediumComplete the code to insert a value into the BST recursively.
DSA C
struct Node* insert(struct Node* root, int value) {
if (root == NULL) {
return newNode([1]);
}
if (value < root->data) {
root->left = insert(root->left, value);
} else if (value > root->data) {
root->right = insert(root->right, value);
}
return root;
}Attempts:
3 left
💡 Hint
Common Mistakes
Passing root->data instead of value to newNode.
Returning NULL instead of a new node.
✗ Incorrect
When root is NULL, create a new node with the value to insert.
3fill in blank
hardFix the error in the comparison to decide where to insert the new value.
DSA C
if (value [1] root->data) { root->left = insert(root->left, value); } else if (value > root->data) { root->right = insert(root->right, value); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes wrong subtree insertion.
Using '==' causes no insertion.
✗ Incorrect
If value is less than root->data, insert into the left subtree.
4fill in blank
hardFill both blanks to complete the recursive insertion and return the updated root.
DSA C
struct Node* insert(struct Node* root, int value) {
if (root == NULL) {
return [1];
}
if (value [2] root->data) {
root->left = insert(root->left, value);
} else if (value > root->data) {
root->right = insert(root->right, value);
}
return root;
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning root instead of new node when root is NULL.
Using wrong comparison operator for left subtree.
✗ Incorrect
Return a new node when root is NULL and check if value is less than root->data to go left.
5fill in blank
hardFill all three blanks to complete the BST insert function with proper recursion and return.
DSA C
struct Node* insert(struct Node* root, int value) {
if (root == NULL) {
return [1];
}
if (value [2] root->data) {
root->left = insert(root->left, value);
} else if (value [3] root->data) {
root->right = insert(root->right, value);
}
return root;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators for comparisons.
Not returning the updated root.
✗ Incorrect
Create a new node if root is NULL, insert left if value < root->data, insert right if value > root->data.
