0
0
DSA C++programming

Why BST Over Plain Binary Tree in DSA C++ - Why This Pattern

Choose your learning style9 modes available
Mental Model
A Binary Search Tree (BST) keeps data sorted so we can find things fast, unlike a plain binary tree which has no order.
Analogy: Imagine a phone book: a BST is like a phone book sorted by names so you can quickly find a contact, while a plain binary tree is like a random pile of papers where you have to check each one.
Plain Binary Tree:
      5
     / \
    3   8
   /   / \
  7   1   9

BST:
      5
     / \
    3   8
   /     \
  1       9
Dry Run Walkthrough
Input: Plain binary tree with nodes 5,3,8,7,1,9; search for 7 BST with nodes 5,3,8,1,9; search for 7
Goal: Show how searching for 7 is slower in plain binary tree and faster in BST
Step 1: Start search at root node 5 in plain binary tree
Plain BT: [5] -> 3 -> 8 -> 7 -> 1 -> 9
BST: [5] -> 3 -> 8 -> 1 -> 9
Why: We start at the root to look for 7
Step 2: In plain binary tree, check left child 3 (not 7), then right child 8 (not 7)
Plain BT: 5 -> [3] -> 8 -> 7 -> 1 -> 9
BST: 5 -> [3] -> 8 -> 1 -> 9
Why: No order, so we must check both sides
Step 3: In plain binary tree, check left child of 3 which is 7 (found)
Plain BT: 5 -> 3 -> [7] -> 1 -> 9
BST: 5 -> 3 -> 8 -> 1 -> 9
Why: We found 7 but had to check multiple nodes
Step 4: In BST, compare 7 with root 5; since 7 > 5, go right to 8
BST: 5 -> 3 -> [8] -> 1 -> 9
Plain BT: 5 -> 3 -> 7 -> 1 -> 9
Why: BST uses order to decide direction
Step 5: Compare 7 with 8; since 7 < 8, go left child which is null (7 not found)
BST: 5 -> 3 -> 8 -> null
Plain BT: 5 -> 3 -> 7 -> 1 -> 9
Why: BST quickly concludes 7 is not in tree without checking all nodes
Result:
Plain Binary Tree final state: 5 -> 3 -> 7 -> 1 -> 9 (7 found after multiple checks)
BST final state: 5 -> 3 -> 8 -> 1 -> 9 (7 not found quickly by skipping unnecessary nodes)
Annotated Code
DSA C++
#include <iostream>
using namespace std;

struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int v) : val(v), left(nullptr), right(nullptr) {}
};

// Plain binary tree search (no order)
bool searchPlain(Node* root, int target) {
    if (!root) return false;
    if (root->val == target) return true;
    // search left subtree
    if (searchPlain(root->left, target)) return true;
    // search right subtree
    return searchPlain(root->right, target);
}

// BST search (uses order)
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);
}

int main() {
    // Plain binary tree construction
    Node* plainRoot = new Node(5);
    plainRoot->left = new Node(3);
    plainRoot->right = new Node(8);
    plainRoot->left->left = new Node(7);
    plainRoot->right->left = new Node(1);
    plainRoot->right->right = new Node(9);

    // BST construction
    Node* bstRoot = new Node(5);
    bstRoot->left = new Node(3);
    bstRoot->right = new Node(8);
    bstRoot->left->left = new Node(1);
    bstRoot->right->right = new Node(9);

    int target = 7;

    cout << "Plain Binary Tree search for " << target << ": " << (searchPlain(plainRoot, target) ? "Found" : "Not Found") << endl;
    cout << "BST search for " << target << ": " << (searchBST(bstRoot, target) ? "Found" : "Not Found") << endl;

    return 0;
}
if (!root) return false;
stop search if node is null
if (root->val == target) return true;
found target node
if (searchPlain(root->left, target)) return true;
search left subtree in plain binary tree
return searchPlain(root->right, target);
search right subtree if not found in left
if (target < root->val) return searchBST(root->left, target);
go left if target less than current node in BST
else return searchBST(root->right, target);
go right if target greater or equal in BST
OutputSuccess
Plain Binary Tree search for 7: Found BST search for 7: Not Found
Complexity Analysis
Time: O(n) for plain binary tree because it may check every node; O(h) for BST where h is tree height because it skips half the nodes each step
Space: O(h) for recursion stack in both, where h is height of tree
vs Alternative: BST is faster than plain binary tree for search because it uses ordering to skip unnecessary nodes, reducing time from linear to logarithmic in balanced trees
Edge Cases
Empty tree
Search returns false immediately
DSA C++
if (!root) return false;
Target is root node
Search returns true immediately
DSA C++
if (root->val == target) return true;
Target not present in tree
Search explores necessary nodes and returns false
DSA C++
return searchPlain(root->right, target);
When to Use This Pattern
When you need fast search in a tree and the data can be kept sorted, reach for BST because it uses order to reduce search time.
Common Mistakes
Mistake: Trying to search a BST like a plain binary tree by checking both children always
Fix: Use the BST property to decide to go left or right based on comparison with current node
Mistake: Assuming BST search always finds the target even if not present
Fix: Return false when reaching null node to indicate target not found
Summary
Shows why BST is better than plain binary tree for searching by using order to skip nodes.
Use BST when you want faster search, insert, and delete operations on sorted data.
The key insight is BST keeps nodes ordered so you only explore one path, not the whole tree.