A Binary Search Tree is a special kind of binary tree where each node's left child contains values less than the node, and the right child contains values greater than the node. This helps in faster searching.
Click to reveal answer
beginner
Why is searching faster in a BST compared to a plain binary tree?
Because BST keeps values in order, you can decide to go left or right at each node, cutting the search space in half each time. In a plain binary tree, you might have to check every node.
Click to reveal answer
beginner
How does the structure of a plain binary tree affect search time?
A plain binary tree has no order rules, so searching for a value might require checking every node, leading to slower search times.
Click to reveal answer
beginner
What real-life example can help understand why BST is better than a plain binary tree?
Think of a phone book (BST) where names are sorted alphabetically, so you can quickly find a name by jumping to the right section. A plain list of names (plain binary tree) would require checking each name one by one.
Click to reveal answer
intermediate
What is the average time complexity for search in a BST vs a plain binary tree?
In a balanced BST, search takes about O(log n) time because you halve the search space each step. In a plain binary tree, search can take O(n) time because you might check every node.
Click to reveal answer
What property makes BST faster for searching than a plain binary tree?
ANodes are arranged so left child < parent < right child
BNodes have more children
CNodes are connected randomly
DNodes store duplicate values
✗ Incorrect
BST keeps nodes ordered so you can decide which side to search, making search faster.
In the worst case, what is the time complexity of searching in a plain binary tree?
AO(log n)
BO(1)
CO(n)
DO(n log n)
✗ Incorrect
Without order, you may need to check every node, so search is O(n).
Which real-life example best illustrates the advantage of BST?
AA pile of unsorted papers
BA shuffled deck of cards
CA random list of phone numbers
DA phone book sorted by names
✗ Incorrect
A phone book sorted by names lets you find entries quickly, like a BST.
What happens if a BST becomes unbalanced?
ASearch time becomes similar to plain binary tree
BBoth B and C
CTree becomes a linked list
DSearch time improves
✗ Incorrect
An unbalanced BST can degrade to a linked list, making search O(n), like a plain binary tree.
Which operation benefits most from BST's ordering?
ASearching for a value
BAdding random nodes
CDeleting the root node only
DTraversing all nodes
✗ Incorrect
Searching is faster because BST ordering guides the search path.
Explain why a Binary Search Tree is preferred over a plain binary tree for searching.
Think about how ordering helps find things faster.
You got /4 concepts.
Describe a real-life analogy that helps understand the advantage of BST over a plain binary tree.
Consider how you find a name in a phone book.
You got /4 concepts.
Practice
(1/5)
1. Why is a Binary Search Tree (BST) preferred over a plain Binary Tree for searching elements?
easy
A. Because BST does not require pointers to child nodes
B. Because BST uses less memory than a plain Binary Tree
C. Because BST nodes can have more than two children
D. Because BST keeps elements in sorted order allowing faster search
Solution
Step 1: Understand the structure difference
A plain Binary Tree has no order, so searching requires checking many nodes. BST keeps nodes sorted so left child is smaller and right child is larger.
Step 2: Analyze search efficiency
Because of sorting, BST allows searching by comparing and moving left or right, reducing search time to O(log n) on average, unlike plain Binary Tree which can be O(n).
Final Answer:
Because BST keeps elements in sorted order allowing faster search -> Option D
Quick Check:
Sorted structure = faster search [OK]
Hint: BST is sorted, so search is faster than plain tree [OK]
Common Mistakes:
Thinking BST uses less memory
Confusing number of children per node
Assuming BST has no pointers
2. Which of the following is the correct way to declare a node class for a BST in Java?
easy
A. class Node { int data; Node left; Node right; }
B. class Node { int data; Node left, right; void insert(int val); }
C. class Node { int data; Node left; Node right; Node parent; }
D. class Node { int data; Node left; Node right; int height; }
Solution
Step 1: Identify minimal BST node structure
A BST node must store data and references to left and right child nodes. class Node { int data; Node left; Node right; } shows this minimal structure.
Step 2: Check other options for extra or missing parts
class Node { int data; Node left, right; void insert(int val); } adds a method which is not part of declaration only. class Node { int data; Node left; Node right; Node parent; } adds parent pointer which is optional. class Node { int data; Node left; Node right; int height; } adds height used in AVL trees, not basic BST.
Final Answer:
class Node { int data; Node left; Node right; } -> Option A
Quick Check:
Minimal node = data + left + right [OK]
Hint: Basic BST node has data and two child pointers only [OK]
Common Mistakes:
Adding unnecessary fields like parent or height
Confusing method declarations with node fields
Using incorrect syntax for multiple variables
3. What will be the output of the following Java code snippet that inserts values into a BST and then performs an inorder traversal?
class Node {
int data;
Node left, right;
Node(int d) { data = d; left = right = null; }
}
class BST {
Node root;
void insert(int val) { root = insertRec(root, val); }
Node insertRec(Node root, int val) {
if (root == null) return new Node(val);
if (val < root.data) root.left = insertRec(root.left, val);
else root.right = insertRec(root.right, val);
return root;
}
void inorder(Node root) {
if (root != null) {
inorder(root.left);
System.out.print(root.data + " ");
inorder(root.right);
}
}
}
public class Main {
public static void main(String[] args) {
BST tree = new BST();
tree.insert(10);
tree.insert(5);
tree.insert(15);
tree.insert(7);
tree.inorder(tree.root);
}
}
medium
A. 10 5 7 15
B. 5 7 10 15
C. 7 5 10 15
D. 15 10 7 5
Solution
Step 1: Trace insertion order and BST structure
Insert 10 as root. Insert 5 to left of 10. Insert 15 to right of 10. Insert 7 to right of 5.
Step 2: Perform inorder traversal
Inorder visits left subtree, root, then right subtree. So nodes visited in order: 5, 7, 10, 15.
Final Answer:
5 7 10 15 -> Option B
Quick Check:
Inorder traversal = sorted values [OK]
Hint: Inorder traversal of BST prints sorted values [OK]
Common Mistakes:
Confusing preorder or postorder with inorder
Misplacing 7 in the tree structure
Ignoring recursion order in traversal
4. The following code attempts to insert a value into a BST but causes a runtime error. What is the error?
class Node {
int data;
Node left, right;
Node(int d) { data = d; }
}
class BST {
Node root;
void insert(int val) { root = insertRec(root, val); }
Node insertRec(Node root, int val) {
if (root == null) return new Node(val);
if (val < root.data) root.left = insertRec(root.left, val);
else root.right = insertRec(root.right, val);
return root;
}
}
medium
A. Compilation error due to missing return statement
B. StackOverflowError due to infinite recursion
C. No error, code runs fine
D. NullPointerException because left and right are not initialized in Node constructor
Solution
Step 1: Check Node constructor initialization
In Java, Node left and right are automatically initialized to null by default. The constructor only sets data, which is sufficient.
Step 2: Analyze insertRec logic
The base case handles null roots. Recursive calls like root.left = insertRec(root.left, val) are safe since insertRec(null) returns new Node without dereferencing null. All paths return a value. No runtime errors occur.
Final Answer:
No error, code runs fine -> Option C
Quick Check:
Default null + null base case = safe execution [OK]
Hint: Java defaults references to null; recursion base case prevents issues [OK]
Common Mistakes:
Thinking uninitialized fields cause immediate NPE (defaults handle it)
Suspecting infinite recursion (base case exists)
Overlooking that all code paths return
5. You have a plain Binary Tree and want to convert it into a BST without changing its structure (only node values). Which approach is correct?
hard
A. Perform inorder traversal to collect values, sort them, then assign back during inorder traversal
B. Insert all nodes into a new BST and discard the old tree
C. Swap left and right children of all nodes to sort the tree
D. Replace each node value with the sum of its children
Solution
Step 1: Understand the problem constraints
The tree structure must remain the same, so only node values can change. This means rearranging values to satisfy BST property without changing links.
Step 2: Use inorder traversal to collect and assign values
Inorder traversal of BST gives sorted values. Collect all node values from the plain tree, sort them, then assign them back to nodes during inorder traversal to maintain structure but satisfy BST order.
Final Answer:
Perform inorder traversal to collect values, sort them, then assign back during inorder traversal -> Option A
Quick Check:
Sort values and reassign inorder = convert to BST [OK]
Hint: Sort values and reassign inorder to fix tree values [OK]
Common Mistakes:
Changing tree structure instead of values
Swapping children does not sort values
Replacing values with sums breaks BST property
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.