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 {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
Node* insert(Node* root, int val) {
if (root == nullptr) {
return new [1](val);
}
// rest of insert logic
return root;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pointer type 'Node*' instead of the struct name 'Node'.
Using an undefined type like 'Tree' or 'BST'.
✗ Incorrect
We create a new node by calling the Node constructor with the value.
2fill in blank
mediumComplete the code to insert the value into the left subtree if it is smaller than the root's data.
DSA C++
if (val < root->data) { root->left = [1](root->left, val); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that does not exist like 'newNode' or 'createNode'.
Not calling the function recursively.
✗ Incorrect
We recursively call insert on the left child to place the new value correctly.
3fill in blank
hardFix the error in the code to insert the value into the right subtree if it is greater than the root's data.
DSA C++
if (val > root->data) { root->right = [1](root->right, val); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect function names like 'insertNode' or 'addRight'.
Not calling the function recursively.
✗ Incorrect
The correct function to call recursively is 'insert' to maintain BST properties.
4fill in blank
hardFill both blanks to complete the insert function that returns the root after insertion.
DSA C++
Node* insert(Node* root, int val) {
if (root == nullptr) {
return new [1](val);
}
if (val < root->data) {
root->left = [2](root->left, val);
} else if (val > root->data) {
root->right = insert(root->right, val);
}
return root;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pointer type 'Node*' instead of 'Node' for new node creation.
Using wrong function names for recursion.
✗ Incorrect
We create a new Node and recursively call insert on the left subtree.
5fill in blank
hardFill all three blanks to complete the insert function with handling for equal values (ignore duplicates).
DSA C++
Node* insert(Node* root, int val) {
if (root == nullptr) {
return new [1](val);
}
if (val < root->data) {
root->left = [2](root->left, val);
} else if (val > root->data) {
root->right = [3](root->right, val);
} else {
// Duplicate value, do nothing
return root;
}
return root;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pointer type 'Node*' instead of 'Node' for new node creation.
Not handling duplicates properly.
Using wrong function names for recursion.
✗ Incorrect
We create a new Node and recursively call insert on left or right subtree. Equal values are ignored.