Why BST Over Plain Binary Tree in DSA Typescript - 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 a value?
Analyze the time complexity of searching a value in these two tree types.
// Plain binary tree search (no order)
function searchPlainTree(node: TreeNode | null, target: number): boolean {
if (!node) return false;
if (node.value === target) return true;
return searchPlainTree(node.left, target) || searchPlainTree(node.right, target);
}
// BST search (ordered)
function searchBST(node: TreeNode | null, target: number): boolean {
if (!node) return false;
if (node.value === target) return true;
if (target < node.value) return searchBST(node.left, target);
else return searchBST(node.right, target);
}
The first searches all nodes without order; the second uses order to skip parts.
Look at what repeats when searching.
- Primary operation: Checking nodes one by one.
- How many times: Plain tree may check every node; BST checks nodes along one path.
As the tree grows, how many nodes do we check?
| Input Size (n) | Approx. Operations Plain Tree | Approx. Operations BST |
|---|---|---|
| 10 | Up to 10 checks | About 4 checks (height ~ log2 10) |
| 100 | Up to 100 checks | About 7 checks (height ~ log2 100) |
| 1000 | Up to 1000 checks | About 10 checks (height ~ log2 1000) |
Pattern observation: Plain tree search grows linearly with nodes; BST search grows slowly with tree height.
Time Complexity: O(n) for plain tree, O(log n) for balanced BST
BST lets us find values much faster by skipping large parts of the tree.
[X] Wrong: "BST search always checks half the nodes like binary search on arrays."
[OK] Correct: BST speed depends on tree shape; if unbalanced, it can be as slow as plain tree.
Knowing why BSTs are faster helps you explain data structure choices clearly and shows you understand how structure affects speed.
What if the BST becomes unbalanced and looks like a linked list? How would the time complexity change?
Practice
Solution
Step 1: Understand the structure of BST vs plain Binary Tree
A BST stores nodes so that left child is smaller and right child is larger, keeping data sorted. A plain Binary Tree has no order.Step 2: Analyze search efficiency
Because BST is sorted, searching can skip half the tree at each step, making it faster than searching a plain Binary Tree which may require checking all nodes.Final Answer:
Because BST keeps elements in sorted order allowing faster search -> Option AQuick Check:
Sorted structure = faster search [OK]
- Thinking BST uses less memory
- Confusing number of children in BST
- Assuming BST has no pointers
Solution
Step 1: Identify correct TypeScript class syntax for BST node
class Node { value: number; left: Node | null; right: Node | null; } correctly declares a class with value and nullable left and right nodes using TypeScript types.Step 2: Check other options for correctness
function Node(value) { this.value = value; this.left = null; this.right = null; } is JavaScript function style, not TypeScript class. interface Node { value: string; left: Node; right: Node; } uses string value and non-nullable children which is incorrect. type Node = { value: number; left?: Node; right?: Node } uses optional children but is a type alias, not a class.Final Answer:
class Node { value: number; left: Node | null; right: Node | null; } -> Option AQuick Check:
TypeScript class with nullable children = class Node { value: number; left: Node | null; right: Node | null; } [OK]
- Confusing JavaScript function with TypeScript class
- Using non-nullable children causing errors
- Using wrong value types
Solution
Step 1: Insert values into BST in given order
Insert 10 as root, 5 to left, 15 to right, 3 to left of 5, 7 to right of 5.Step 2: Perform in-order traversal (left, root, right)
Traverse left subtree: 3, 5, 7; then root 10; then right subtree 15. Combined: [3, 5, 7, 10, 15]Final Answer:
[3, 5, 7, 10, 15] -> Option DQuick Check:
In-order traversal of BST = sorted values [OK]
- Confusing traversal order
- Inserting nodes incorrectly
- Mixing up left and right children
function insert(root: Node | null, val: number): Node {
if (!root) return new Node(val);
if (val < root.value) insert(root.left, val);
else insert(root.right, val);
return root;
}Solution
Step 1: Analyze recursive calls in insertion
The recursive calls insert(root.left, val) and insert(root.right, val) do not assign the returned node back to root.left or root.right.Step 2: Identify missing return assignments
Without assigning, the tree structure does not update. Correct code should be root.left = insert(root.left, val) and root.right = insert(root.right, val).Final Answer:
Missing return statements for recursive calls -> Option BQuick Check:
Recursive insert must update child links [OK]
- Not assigning recursive call results
- Using wrong comparison operators
- Ignoring duplicates silently
Solution
Step 1: Compare searching in BST vs plain Binary Tree
BST keeps nodes sorted, enabling binary search-like behavior with average O(log n) time. Plain Binary Tree has no order, so search is O(n).Step 2: Evaluate other operations
Insertion in BST is not always O(1), deletion may require rebalancing, and traversal visits all nodes in both trees equally.Final Answer:
Searching for a value, because BST allows O(log n) average time search -> Option CQuick Check:
BST speeds up search, not insertion or traversal [OK]
- Thinking insertion is always O(1)
- Assuming deletion never needs rebalancing
- Believing traversal is faster in BST
Ready to prove it?
Pass the test and this pattern moves unknown
