Complete the code to insert a value into a Binary Search Tree (BST).
function insertNode(root, value) {
if (root === null) {
return { val: value, left: null, right: null };
}
if (value [1] root.val) {
root.left = insertNode(root.left, value);
} else {
root.right = insertNode(root.right, value);
}
return root;
}In a BST, values less than the current node go to the left subtree. So we check if value < root.val.
Complete the code to check if a key exists in a JavaScript Map (hash map).
function hasKey(map, key) {
return map.[1](key);
}The Map object in JavaScript uses the has method to check for keys.
Fix the error in the code to find the minimum value node in a BST.
function findMinNode(node) {
while (node.[1] !== null) {
node = node.left;
}
return node;
}The minimum value in a BST is found by going as far left as possible, so we check node.left.
Fill both blanks to create a Map from an array of keys with initial value 0, but only for keys longer than 3 characters.
const keys = ['apple', 'bat', 'carrot', 'dog']; const map = new Map(); for (const key of keys) { if (key.[1] > 3) { map.[2](key, 0); } }
We check the length of the string with key.length and add to Map using map.set(key, 0).
Fill all three blanks to create an object that maps keys to their lengths, but only include keys with length less than 5.
const keys = ['sun', 'moon', 'star', 'sky']; const result = { [1]: [2] for [3] of keys if [2] < 5 };
This is a JavaScript object comprehension style (conceptual) where key is the property, key.length is the value, and we loop with for key.