Why BST Over Plain Binary Tree in DSA C++ - Performance Analysis
We want to understand why using a Binary Search Tree (BST) is faster than a plain Binary Tree for searching data.
How does the structure affect the time it takes to find something?
Analyze the time complexity of searching for a value in these two tree types.
// Searching in a plain binary tree (no order)
bool searchPlain(TreeNode* root, int target) {
if (!root) return false;
if (root->val == target) return true;
return searchPlain(root->left, target) || searchPlain(root->right, target);
}
// Searching in a BST (ordered)
bool searchBST(TreeNode* 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);
}
This code shows how searching works differently in a plain binary tree and a BST.
Look at what repeats when searching.
- Primary operation: Checking nodes one by one.
- How many times in plain tree: Might check almost all nodes because no order.
- How many times in BST: Only follow one path down the tree, skipping half the nodes each step.
As the number of nodes grows, searching in a plain tree grows much faster than in a BST.
| Input Size (n) | Approx. Operations Plain Tree | Approx. Operations BST |
|---|---|---|
| 10 | Up to 10 checks | About 4 checks |
| 100 | Up to 100 checks | About 7 checks |
| 1000 | Up to 1000 checks | About 10 checks |
Pattern observation: Plain tree search grows directly with number of nodes, but BST search grows slowly as it cuts the search space in half each step.
Time Complexity: O(n) for plain binary tree search, O(log n) for BST search
In simple words, BST search is much faster because it uses the order to skip many nodes, while plain tree search might check every node.
[X] Wrong: "Searching a BST is always as slow as a plain binary tree because both are trees."
[OK] Correct: BST keeps nodes in order, so it can ignore half the tree at each step, making search much faster than checking every node.
Understanding why BSTs are faster helps you explain how data structure choices affect performance, a key skill in coding interviews and real projects.
"What if the BST becomes unbalanced and looks like a linked list? How would the time complexity change then?"
Practice
Solution
Step 1: Understand the structure of BST
A BST stores elements so that left child nodes are smaller and right child nodes are larger than the parent node, maintaining sorted order.Step 2: Compare search efficiency
This sorted structure allows searching by comparing values and moving left or right, reducing search time to O(log n) on average, unlike a plain Binary Tree which has no order and requires O(n) search.Final Answer:
Because BST keeps elements in sorted order allowing faster search -> Option AQuick Check:
BST sorted order = faster search [OK]
- Thinking BST uses less memory than plain tree
- Confusing number of children allowed in BST
- Assuming BST nodes don't have child pointers
Solution
Step 1: Identify correct pointer usage for children
A BST node must have pointers to left and right child nodes, so Node* left and Node* right are needed.Step 2: Check each option
struct Node { int data; Node* left; Node* right; }; correctly declares pointers to left and right children. struct Node { int data; Node left; Node right; }; incorrectly uses Node left and right (not pointers). struct Node { int data; Node* parent; }; only has parent pointer, missing children. struct Node { int data; Node* next; }; uses next pointer which is for linked lists, not BST.Final Answer:
struct Node { int data; Node* left; Node* right; }; -> Option BQuick Check:
BST node needs left and right pointers [OK]
- Using objects instead of pointers for children
- Confusing parent pointer with child pointers
- Using 'next' pointer like in linked lists
#include <iostream>
struct Node {
int data;
Node* left;
Node* right;
};
Node* insert(Node* root, int val) {
if (!root) {
Node* n = new Node{val, nullptr, nullptr};
return n;
}
if (val < root->data) root->left = insert(root->left, val);
else root->right = insert(root->right, val);
return root;
}
void inorder(Node* node) {
if (!node) return;
inorder(node->left);
std::cout << node->data << " ";
inorder(node->right);
}
int main() {
Node* root = nullptr;
root = insert(root, 10);
root = insert(root, 5);
root = insert(root, 15);
root = insert(root, 7);
inorder(root);
return 0;
}
What is the output of the inorder traversal?Solution
Step 1: Understand insertion order and BST property
Values inserted are 10 (root), 5 (left of 10), 15 (right of 10), 7 (right of 5). BST property keeps left < parent < right.Step 2: Perform inorder traversal
Inorder traversal visits left subtree, node, then right subtree. So nodes visited in order: 5, 7, 10, 15.Final Answer:
5 7 10 15 -> Option CQuick Check:
Inorder traversal of BST = sorted order [OK]
- Confusing preorder or postorder with inorder
- Misplacing 7 as left child of 10
- Ignoring BST insertion rules
Node* insert(Node* root, int val) {
if (root == nullptr) {
Node* n = new Node{val, nullptr, nullptr};
return n;
}
if (val <= root->data) {
insert(root->left, val);
} else {
insert(root->right, val);
}
return root;
}
What is the bug and how to fix it?Solution
Step 1: Identify missing assignment in recursion
The recursive calls insert(root->left, val) and insert(root->right, val) do not update root's left or right pointers with the returned new nodes.Step 2: Fix by assigning returned nodes
Assign the recursive call results back: root->left = insert(root->left, val); root->right = insert(root->right, val); so the tree links update correctly.Final Answer:
The recursive calls do not assign returned nodes to root's children; fix by root->left = insert(root->left, val) and root->right = insert(root->right, val) -> Option AQuick Check:
Assign recursive insert results to children [OK]
- Ignoring returned node from recursion
- Changing base case unnecessarily
- Changing comparison operator without reason
Solution
Step 1: Understand search in BST vs plain Binary Tree
BST maintains sorted order so at each node, you decide to go left or right, effectively halving the search space each step.Step 2: Compare time complexities
This halving leads to average search time O(log n). Plain Binary Tree has no order, so search may check every node, O(n) time.Final Answer:
BST allows skipping half of the nodes at each step due to ordering, reducing search time to O(log n) on average -> Option DQuick Check:
BST halves search space each step [OK]
- Thinking BST uses linked lists internally
- Believing plain Binary Tree has no pointers
- Assuming BST uses hashing
Ready to prove it?
Pass the test and this pattern moves unknown
