Consider you have two trees: a plain binary tree and a binary search tree (BST). Which reason best explains why BST is preferred for searching?
Think about how the order of elements affects the speed of finding a value.
A BST keeps nodes in sorted order so that at each step you can decide to go left or right, reducing search time to O(log n) on average. A plain binary tree has no order, so searching may require checking every node.
Given the following C++ code snippets for searching a value in a plain binary tree and a BST, what is the output when searching for value 7?
struct Node {
int val;
Node* left;
Node* right;
};
// Plain binary tree search (DFS)
bool searchPlain(Node* root, int target) {
if (!root) return false;
if (root->val == target) return true;
return searchPlain(root->left, target) || searchPlain(root->right, target);
}
// BST search
bool searchBST(Node* root, int target) {
if (!root) return false;
if (root->val == target) return true;
if (target < root->val) return searchBST(root->left, target);
else return searchBST(root->right, target);
}
// Assume tree is built with nodes: 5 (root), 3 (left), 7 (right)Both functions check if the value 7 exists in the tree.
Both searchPlain and searchBST will find the value 7 in the tree. searchPlain does a full search, searchBST uses the BST property to find it faster.
Consider inserting values 10, 5, 15, 3, 7 into an empty tree. What is the printed in-order traversal output for a BST and a plain binary tree where nodes are inserted as left child if empty, else right child?
// In-order traversal prints nodes in sorted order for BST // Plain binary tree inserts nodes always to left if empty, else right // Insert order: 10, 5, 15, 3, 7 // Print in-order traversal after insertions
Remember BST in-order traversal prints sorted values. Plain binary tree insertion order affects traversal.
BST in-order traversal prints sorted values: 3,5,7,10,15. Plain binary tree insertion as left if empty else right creates a skewed tree, so in-order prints nodes in insertion order.
Given the code below, why does searching for a value in a plain binary tree take longer than in a BST?
bool searchPlain(Node* root, int target) { if (!root) return false; if (root->val == target) return true; return searchPlain(root->left, target) || searchPlain(root->right, target); } bool searchBST(Node* root, int target) { if (!root) return false; if (root->val == target) return true; if (target < root->val) return searchBST(root->left, target); else return searchBST(root->right, target); }
Think about how the tree structure affects the number of nodes checked.
Plain binary tree has no ordering, so search may visit every node. BST uses ordering to decide which branch to search, reducing nodes visited.
You need to store a large dataset of unique numbers and perform many search queries. Which data structure is best and why?
Consider search time complexity and data ordering.
BST provides efficient O(log n) average search time by keeping data sorted. Plain binary tree and linked list have O(n) search time. Unsorted array also requires O(n) search.