0
0
DSA Javascriptprogramming~10 mins

BST Insert Operation in DSA Javascript - Interactive Practice

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

Complete 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'
AinsertNode
BNode
CcreateNode
DnewNode
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.
2fill in blank
medium

Complete 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'
A===
B<
C>
D!==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes wrong subtree traversal.
Using '===' or '!==' causes logic errors.
3fill in blank
hard

Fix 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'
Aroot.value = value
Breturn null
CinsertNode(root, value)
Dreturn root
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to insert duplicates again causing infinite recursion.
Replacing root.value overwrites existing node.
4fill in blank
hard

Fill 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'
Ai
Bj
Cvalues.length
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong loop variable names inconsistently.
Incorrect loop condition causing infinite or no iteration.
5fill in blank
hard

Fill 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'
Aleft
Bvalue
Cright
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Visiting right child before left causes descending order.
Pushing root instead of root.value causes wrong output.