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));
The search starts at the root (10). Since 7 is less than 10, it moves left to 5. Then 7 is greater than 5, so it moves right to 7. It finds the value and returns true.
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));
The search moves from 10 to 5 (since 6 < 10), then from 5 to 7 (since 6 > 5). Since 7 has no left child, the search ends without finding 6, returning false.
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;
}The condition uses assignment (=) instead of comparison (===). This assigns current.value to value, which is truthy, so the if condition is always true, causing an infinite loop.
Search path: 10 (root) → 15 (right child) → 12 (left child of 15). Total nodes visited = 3.
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));
Search path: 10 → 15 → 18. Since 20 > 18 and 18 has no right child, search ends returning false.
