Which of the following best explains why a Binary Search Tree (BST) is preferred over a plain binary tree for searching operations?
Think about how searching works in a sorted structure compared to an unsorted one.
A BST keeps its elements in sorted order, so when searching, it can decide to go left or right subtree based on comparison, effectively skipping half the nodes each time. This makes search operations faster than in a plain binary tree where nodes are unordered.
Consider the following Java code snippets for searching a value in two trees: a BST and a plain binary tree. What will be the output when searching for value 7? (Assume the same tree structure is used for both.)
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } // BST search method boolean searchBST(Node root, int key) { if (root == null) return false; if (root.val == key) return true; if (key < root.val) return searchBST(root.left, key); else return searchBST(root.right, key); } // Plain binary tree search method boolean searchPlain(Node root, int key) { if (root == null) return false; if (root.val == key) return true; return searchPlain(root.left, key) || searchPlain(root.right, key); } // Tree setup: root=5, left=3, right=8, 8.left=7 (contains 7, valid BST)
Both methods search for the value 7, but one uses BST logic and the other searches all nodes. The tree contains 7.
Both search methods will find the value 7 since it exists in the tree. The BST search uses the sorted property to find it efficiently (O(log n)), while the plain binary tree search checks all nodes recursively (O(n)).
Given the following Java method to search a BST, why does it fail to find some values?
boolean searchBST(Node root, int key) { if (root == null) return false; if (key <= root.val) return searchBST(root.left, key); if (root.val == key) return true; return searchBST(root.right, key); }
Think about what happens when key equals root.val and the condition uses <=.
Since the equality check comes after the <= condition, when key == root.val, the if (key <= root.val) evaluates to true and recurses left without ever reaching the equality check, missing the node at the current position.
Which of the following Java methods correctly inserts a new integer value into a BST?
Remember to assign the recursive call result back to the child node pointer.
Option C correctly assigns the recursive insert result to root.left or root.right and returns root. It also handles no duplicates (if val == root.val, does nothing). Option C allows duplicates on left (common alternative). Option C reverses left/right logic. Option C does not assign recursive calls, so tree is not updated.
Assuming a tree with n nodes, what is the average time complexity of searching for a value in a balanced BST compared to a plain binary tree?
Think about how balanced trees reduce the height compared to unstructured trees.
A balanced BST has height about log n, so searching takes O(log n) time on average. A plain binary tree with no order may require checking all nodes in the worst/average case, so O(n) time.
