0
0
DSA C++programming~20 mins

Why BST Over Plain Binary Tree in DSA C++ - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is a Binary Search Tree (BST) preferred over a plain Binary Tree for searching?

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?

ABST keeps elements in sorted order, allowing faster search using the tree structure.
BBST uses less memory than a plain binary tree because it stores fewer nodes.
CBST allows duplicate values while plain binary tree does not.
DBST nodes have more children than plain binary tree nodes, making search faster.
Attempts:
2 left
💡 Hint

Think about how the order of elements affects the speed of finding a value.

Predict Output
intermediate
2:00remaining
Output of Searching in BST vs Plain Binary Tree

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?

DSA C++
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)
ABoth searchPlain and searchBST return false.
BBoth searchPlain and searchBST return true.
CsearchPlain returns true, searchBST returns false.
DsearchPlain returns false, searchBST returns true.
Attempts:
2 left
💡 Hint

Both functions check if the value 7 exists in the tree.

Predict Output
advanced
2:30remaining
Resulting Tree Structure After Insertions in BST vs Plain Binary Tree

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?

DSA C++
// 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
ABST: 3 -> 5 -> 7 -> 10 -> 15 -> null; Plain: 3 -> 5 -> 7 -> 10 -> 15 -> null
BBST: 10 -> 5 -> 3 -> 7 -> 15 -> null; Plain: 3 -> 5 -> 7 -> 10 -> 15 -> null
CBST: 3 -> 5 -> 7 -> 10 -> 15 -> null; Plain: 10 -> 5 -> 3 -> 7 -> 15 -> null
DBST: 10 -> 5 -> 3 -> 7 -> 15 -> null; Plain: 10 -> 5 -> 3 -> 7 -> 15 -> null
Attempts:
2 left
💡 Hint

Remember BST in-order traversal prints sorted values. Plain binary tree insertion order affects traversal.

🔧 Debug
advanced
2:00remaining
Why does searching in a plain binary tree take longer than in a BST?

Given the code below, why does searching for a value in a plain binary tree take longer than in a BST?

DSA C++
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);
}
APlain binary tree search uses more memory, causing slower performance.
BPlain binary tree search uses recursion; BST search uses iteration, making it faster.
CPlain binary tree search only checks left children; BST search checks both children.
DPlain binary tree search checks all nodes because it has no order; BST search uses order to skip half the nodes.
Attempts:
2 left
💡 Hint

Think about how the tree structure affects the number of nodes checked.

🚀 Application
expert
3:00remaining
Choosing Data Structure for Fast Lookup in Large Dataset

You need to store a large dataset of unique numbers and perform many search queries. Which data structure is best and why?

AUse a Binary Search Tree because it keeps data sorted and allows O(log n) average search time.
BUse a plain Binary Tree because it is simpler to implement and uses less memory.
CUse a Linked List because it allows easy insertion and deletion.
DUse an unsorted array because searching is faster without ordering overhead.
Attempts:
2 left
💡 Hint

Consider search time complexity and data ordering.