Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given value.
DSA Javascript
function createNode(value) {
return {
value: value,
left: null,
right: null
};
}
const newNode = [1](10); 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 insertNode.
Trying to use 'new' keyword with a plain object function.
✗ Incorrect
The function to create a new node is named createNode, so calling createNode(10) creates the new node.
2fill in blank
mediumComplete the code to insert a value into the BST recursively.
DSA Javascript
function insertNode(root, value) {
if (root === null) {
return createNode(value);
}
if (value [1] root.value) {
root.left = insertNode(root.left, value);
} else {
root.right = insertNode(root.right, value);
}
return root;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes wrong subtree traversal.
Using '===' or '!==' causes logic errors.
✗ Incorrect
In BST, if the value to insert is less than the current node's value, we go to the left subtree.
3fill in blank
hardFix the error in the insert function to correctly handle duplicate values by ignoring them.
DSA Javascript
function insertNode(root, value) {
if (root === null) {
return createNode(value);
}
if (value < root.value) {
root.left = insertNode(root.left, value);
} else if (value > root.value) {
root.right = insertNode(root.right, value);
} else {
[1];
}
return root;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to insert duplicates again causing infinite recursion.
Replacing root.value overwrites existing node.
✗ Incorrect
When the value equals root.value, we do nothing and return the current root to ignore duplicates.
4fill in blank
hardFill both blanks to insert multiple values into the BST using a loop.
DSA Javascript
function buildBST(values) {
let root = null;
for (let [1] = 0; [2] < values.length; [1]++) {
root = insertNode(root, values[[1]]);
}
return root;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong loop variable names inconsistently.
Incorrect loop condition causing infinite or no iteration.
✗ Incorrect
The loop variable is 'i' and the condition is 'i < values.length' to iterate over all values.
5fill in blank
hardFill all three blanks to print the BST values in ascending order using in-order traversal.
DSA Javascript
function inOrderTraversal(root, result) {
if (root !== null) {
inOrderTraversal(root.[1], result);
result.push(root.[2]);
inOrderTraversal(root.[3], result);
}
}
const result = [];
inOrderTraversal(bstRoot, result);
console.log(result); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Visiting right child before left causes descending order.
Pushing root instead of root.value causes wrong output.
✗ Incorrect
In-order traversal visits left subtree, then node value, then right subtree to get sorted order.