0
0
DSA Javascriptprogramming~20 mins

BST Search Operation in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of searching for 7 in this BST?
Consider the following JavaScript code that builds a simple Binary Search Tree (BST) and searches for the value 7. What will be the output printed?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

class BST {
  constructor() {
    this.root = null;
  }

  insert(value) {
    const newNode = new Node(value);
    if (!this.root) {
      this.root = newNode;
      return;
    }
    let current = this.root;
    while (true) {
      if (value < current.value) {
        if (!current.left) {
          current.left = newNode;
          return;
        }
        current = current.left;
      } else {
        if (!current.right) {
          current.right = newNode;
          return;
        }
        current = current.right;
      }
    }
  }

  search(value) {
    let current = this.root;
    while (current) {
      if (value === current.value) {
        return true;
      } else if (value < current.value) {
        current = current.left;
      } else {
        current = current.right;
      }
    }
    return false;
  }
}

const bst = new BST();
bst.insert(10);
bst.insert(5);
bst.insert(15);
bst.insert(3);
bst.insert(7);
bst.insert(12);
bst.insert(18);

console.log(bst.search(7));
Afalse
Btrue
Cundefined
DTypeError
Attempts:
2 left
💡 Hint
Trace the path from root 10 to find 7 by comparing values.
Predict Output
intermediate
2:00remaining
What is the output when searching for 6 in this BST?
Given the same BST as before, what will be the output of searching for the value 6?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

class BST {
  constructor() {
    this.root = null;
  }

  insert(value) {
    const newNode = new Node(value);
    if (!this.root) {
      this.root = newNode;
      return;
    }
    let current = this.root;
    while (true) {
      if (value < current.value) {
        if (!current.left) {
          current.left = newNode;
          return;
        }
        current = current.left;
      } else {
        if (!current.right) {
          current.right = newNode;
          return;
        }
        current = current.right;
      }
    }
  }

  search(value) {
    let current = this.root;
    while (current) {
      if (value === current.value) {
        return true;
      } else if (value < current.value) {
        current = current.left;
      } else {
        current = current.right;
      }
    }
    return false;
  }
}

const bst = new BST();
bst.insert(10);
bst.insert(5);
bst.insert(15);
bst.insert(3);
bst.insert(7);
bst.insert(12);
bst.insert(18);

console.log(bst.search(6));
Afalse
Btrue
Cnull
DReferenceError
Attempts:
2 left
💡 Hint
Check if 6 exists by moving left or right from root.
🔧 Debug
advanced
2:00remaining
What error does this BST search code produce?
Examine the following BST search method. What error will it cause when searching for any value?
DSA Javascript
search(value) {
  let current = this.root;
  while (current !== null) {
    if (value = current.value) {
      return true;
    } else if (value < current.value) {
      current = current.left;
    } else {
      current = current.right;
    }
  }
  return false;
}
ANo error, returns correct result
BTypeError
CInfinite loop
DSyntaxError
Attempts:
2 left
💡 Hint
Look carefully at the if condition inside the while loop.
🧠 Conceptual
advanced
2:00remaining
How many nodes are visited when searching for 12 in this BST?
Given the BST constructed by inserting values in this order: 10, 5, 15, 3, 7, 12, 18. How many nodes will the search method visit when searching for 12?
A3
B4
C5
D2
Attempts:
2 left
💡 Hint
Trace the path from root to 12 counting nodes visited.
Predict Output
expert
2:00remaining
What is the output of searching for 20 in this BST after these operations?
After inserting 10, 5, 15, 3, 7, 12, 18 into a BST, the following code runs. What is the output?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

class BST {
  constructor() {
    this.root = null;
  }

  insert(value) {
    const newNode = new Node(value);
    if (!this.root) {
      this.root = newNode;
      return;
    }
    let current = this.root;
    while (true) {
      if (value < current.value) {
        if (!current.left) {
          current.left = newNode;
          return;
        }
        current = current.left;
      } else {
        if (!current.right) {
          current.right = newNode;
          return;
        }
        current = current.right;
      }
    }
  }

  search(value) {
    let current = this.root;
    while (current) {
      if (value === current.value) {
        return true;
      } else if (value < current.value) {
        current = current.left;
      } else {
        current = current.right;
      }
    }
    return false;
  }
}

const bst = new BST();
bst.insert(10);
bst.insert(5);
bst.insert(15);
bst.insert(3);
bst.insert(7);
bst.insert(12);
bst.insert(18);

console.log(bst.search(20));
ARangeError
Btrue
Cundefined
Dfalse
Attempts:
2 left
💡 Hint
20 is greater than all inserted values; check rightmost path.