0
0
DSA Javascriptprogramming~10 mins

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

DSA Javascript
function leftChild(node) {
  return node[1];
}
Drag options to blanks, or click blank then click option'
A.left
B.right
C.parent
D.child
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.right' instead of '.left' to access the left child.
Using '.parent' which points to the parent node, not the child.
2fill in blank
medium

Complete the code to find the minimum node in a BST subtree.

DSA Javascript
function findMin(node) {
  while (node[1]) {
    node = node.left;
  }
  return node;
}
Drag options to blanks, or click blank then click option'
A.right
B.child
C.parent
D.left
Attempts:
3 left
💡 Hint
Common Mistakes
Checking '.right' instead of '.left' in the loop condition.
Not updating the node inside the loop.
3fill in blank
hard

Fix the error in the code to find the inorder successor when the node has no right child.

DSA Javascript
function inorderSuccessor(node) {
  if (node.right) {
    return findMin(node.right);
  }
  let parent = node[1];
  while (parent && node === parent.right) {
    node = parent;
    parent = parent.parent;
  }
  return parent;
}
Drag options to blanks, or click blank then click option'
A.parent
B.left
C.right
D.child
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.left' or '.right' instead of '.parent' to move up the tree.
Not updating the parent inside the loop.
4fill in blank
hard

Fill both blanks to complete the function that finds the inorder successor of a node in a BST.

DSA Javascript
function inorderSuccessor(node) {
  if (node.right) {
    return [1](node.right);
  }
  let parent = node.[2];
  while (parent && node === parent.right) {
    node = parent;
    parent = parent.parent;
  }
  return parent;
}
Drag options to blanks, or click blank then click option'
AfindMin
BfindMax
Cparent
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'findMax' instead of 'findMin' for the right subtree.
Using 'right' or 'left' instead of 'parent' to move up.
5fill in blank
hard

Fill all three blanks to complete the function that returns the inorder successor of a node in a BST.

DSA Javascript
function inorderSuccessor(node) {
  if (node.[1]) {
    return findMin(node.[2]);
  }
  let parent = node.[3];
  while (parent && node === parent.right) {
    node = parent;
    parent = parent.parent;
  }
  return parent;
}
Drag options to blanks, or click blank then click option'
Aright
Cparent
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Using different properties for the first two blanks.
Using 'left' instead of 'parent' to move up.