0
0
DSA Javascriptprogramming~10 mins

BST Inorder Predecessor 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 return the left child of a node in a BST.

DSA Javascript
function getLeftChild(node) {
  return node.[1];
}
Drag options to blanks, or click blank then click option'
Aright
Bleft
Cparent
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' to get the left child.
2fill in blank
medium

Complete the code to move to the right child of a node in a BST.

DSA Javascript
function getRightChild(node) {
  return node.[1];
}
Drag options to blanks, or click blank then click option'
Aright
Bparent
Cleft
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' to get the right child.
3fill in blank
hard

Fix the error in the code to find the inorder predecessor when the left subtree exists.

DSA Javascript
function inorderPredecessor(node) {
  let current = node.[1];
  while (current.right !== null) {
    current = current.right;
  }
  return current;
}
Drag options to blanks, or click blank then click option'
Aright
Bvalue
Cleft
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from node.right instead of node.left.
4fill in blank
hard

Fill both blanks to check if a node has a left child and then find its rightmost child.

DSA Javascript
if (node.[1] !== null) {
  let current = node.[2];
  while (current.right !== null) {
    current = current.right;
  }
  return current;
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cparent
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using different properties for check and traversal.
5fill in blank
hard

Fill all three blanks to complete the function that finds the inorder predecessor when no left child exists.

DSA Javascript
function inorderPredecessor(node) {
  if (node.left !== null) {
    let current = node.left;
    while (current.right !== null) {
      current = current.right;
    }
    return current;
  } else {
    let current = node;
    let parent = node.[1];
    while (parent !== null && current === parent.[2]) {
      current = parent;
      parent = parent.[3];
    }
    return parent;
  }
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cparent
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' in the while condition.
Not moving up the parent chain correctly.