0
0
DSA Javascriptprogramming~10 mins

Why BST Over Plain Binary Tree in DSA Javascript - Test Your Knowledge

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert a value into a Binary Search Tree (BST).

DSA Javascript
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;
}
Drag options to blanks, or click blank then click option'
A<
B>
C===
D!==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes wrong placement of nodes.
Using '===' or '!==' causes logical errors in comparisons.
2fill in blank
medium

Complete the code to search for a value in a BST.

DSA Javascript
function searchBST(root, target) {
  if (root === null) return false;
  if (root.val === target) return true;
  if (target [1] root.val) {
    return searchBST(root.left, target);
  } else {
    return searchBST(root.right, target);
  }
}
Drag options to blanks, or click blank then click option'
A<
B!==
C===
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' leads to wrong subtree search.
Not handling null root causes errors.
3fill in blank
hard

Complete the code to find the minimum value in a BST.

DSA Javascript
function findMin(root) {
  while (root.[1] !== null) {
    root = root.left;
  }
  return root.val;
}
Drag options to blanks, or click blank then click option'
Aval
Bright
Cleft
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' will not find the minimum value.
Using root.val in condition is incorrect.
4fill in blank
hard

Fill both blanks to create a function that counts nodes in a BST.

DSA Javascript
function countNodes(root) {
  if (root === null) return 0;
  return 1 + countNodes(root.[1]) + countNodes(root.[2]);
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cval
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Using root.val or root.parent instead of left/right causes errors.
Not adding 1 for current node leads to wrong count.
5fill in blank
hard

Fill all three blanks to create a function that deletes a node from a BST.

DSA Javascript
function deleteNode(root, key) {
  if (root === null) return root;
  if (key [1] root.val) {
    root.left = deleteNode(root.left, key);
  } else if (key [2] root.val) {
    root.right = deleteNode(root.right, key);
  } else {
    if (root.left === null) return root.[3];
    else if (root.right === null) return root.left;
    let minNode = findMinNode(root.right);
    root.val = minNode.val;
    root.right = deleteNode(root.right, minNode.val);
  }
  return root;
}
Drag options to blanks, or click blank then click option'
A<
B>
Cright
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping < and > causes wrong traversal.
Returning root.left instead of root.right when left is null causes errors.