BST vs Hash Map Trade-offs for Ordered Data in DSA Javascript - Complexity Comparison
We want to understand how fast different data structures work when we store and find data in order.
Which one is better for ordered data: a Binary Search Tree or a Hash Map?
Analyze the time complexity of these basic operations on BST and Hash Map.
// Insert and search in Binary Search Tree (BST)
function bstSearch(root, value) {
if (!root || root.value === value) return root;
if (value < root.value) return bstSearch(root.left, value);
else return bstSearch(root.right, value);
}
// Insert and search in Hash Map
const map = new Map();
map.set(key, value); // insert
map.get(key); // search
This code shows how we search in a BST using recursion and how we insert/search in a Hash Map.
Look at what repeats when we search or insert:
- Primary operation in BST: Comparing values and moving down one child node.
- How many times: Up to the height of the tree (number of levels).
- Primary operation in Hash Map: Computing a hash and accessing a bucket.
- How many times: Usually once, but sometimes more if collisions happen.
As we add more data (n):
| Input Size (n) | BST Operations (height) | Hash Map Operations |
|---|---|---|
| 10 | About 4 steps | Usually 1 step |
| 100 | About 7 steps | Usually 1 step |
| 1000 | About 10 steps | Usually 1 step |
BST steps grow slowly with input size (logarithmic), Hash Map steps stay almost the same.
Time Complexity: O(log n) for BST (balanced), O(1) average for Hash Map
BST takes more steps as data grows but keeps order; Hash Map is faster but does not keep order.
[X] Wrong: "Hash Maps always work in constant time no matter what."
[OK] Correct: Sometimes many keys hash to the same spot, making operations slower. Also, Hash Maps don't keep data sorted.
Knowing when to use BST or Hash Map shows you understand trade-offs between speed and order, a key skill in coding interviews.
What if we used a balanced BST like an AVL tree instead of a simple BST? How would the time complexity change?
Practice
Solution
Step 1: Understand the need for sorted order
Maintaining elements in sorted order requires a structure that keeps data sorted as elements are added or removed.Step 2: Compare BST and Hash Map for sorted data
BST keeps elements sorted and supports efficient range queries, while Hash Map does not maintain order.Final Answer:
Binary Search Tree (BST) -> Option AQuick Check:
Sorted data + range queries = BST [OK]
- Thinking hash maps keep data sorted
- Confusing stack or queue with sorted structures
- Assuming hash maps support range queries
Solution
Step 1: Recall JavaScript Map syntax
JavaScript uses the Map object with the syntax new Map() to create a hash map.Step 2: Check each option
const map = new Map(); uses new Map(), which is correct. const map = Map(); misses 'new'. const map = {}; creates a plain object, not a Map. const map = new HashMap(); uses a non-existent HashMap class.Final Answer:
const map = new Map(); -> Option DQuick Check:
Map creation syntax = new Map() [OK]
- Omitting 'new' keyword
- Using plain objects instead of Map
- Assuming HashMap class exists in JS
const bst = new BST(); bst.insert(10); bst.insert(5); bst.insert(15); console.log(bst.inorderTraversal());
What will be the output?
Solution
Step 1: Understand inorder traversal of BST
Inorder traversal visits nodes in ascending order: left subtree, root, right subtree.Step 2: Trace insertion and traversal
Insert 10 as root, 5 to left, 15 to right. Inorder traversal visits 5, then 10, then 15.Final Answer:
[5, 10, 15] -> Option BQuick Check:
Inorder traversal = sorted nodes [OK]
- Assuming insertion order equals traversal order
- Confusing preorder or postorder with inorder
- Expecting hash map order instead of BST order
const map = new Map(); map.set(3, 'c'); map.set(1, 'a'); map.set(2, 'b'); console.log([...map.keys()]);
Solution
Step 1: Understand Map key order behavior
JavaScript Map preserves insertion order, not sorted order of keys.Step 2: Analyze the output of keys()
Keys will be in insertion order [3,1,2], not sorted [1,2,3].Final Answer:
Keys are not guaranteed to be in sorted order -> Option AQuick Check:
Map keys order = insertion order, not sorted [OK]
- Thinking Map sorts keys automatically
- Believing Map keys must be strings
- Confusing Map with Object key behavior
Solution
Step 1: Analyze requirements for insertions, lookups, and sorted retrieval
Frequent insertions and lookups need efficient operations; sorted retrieval requires order maintenance.Step 2: Compare data structures for these needs
Hash Map offers fast lookups but no order; sorting keys on demand is costly. Balanced BST maintains sorted order and supports efficient insertions and lookups.Final Answer:
Use a Balanced BST to store keys and values -> Option CQuick Check:
Balanced BST = fast insert, lookup + sorted keys [OK]
- Choosing hash map without sorting cost consideration
- Using array causing slow lookups
- Using queue which doesn't support sorted retrieval
Ready to prove it?
Pass the test and this pattern moves unknown
