Comparison Mode
Side-by-side: use case, requirements, complexity, strengths, weaknesses, example problems, and a clear “choose this when…”.
BFS vs DFSDijkstra vs Bellman-FordKruskal vs PrimMerge Sort vs Quick SortHeap vs Priority QueueHash Map vs Tree MapBFS vs DijkstraSliding Window vs Two PointersPrefix Sum vs Segment TreeGreedy vs Dynamic ProgrammingMemoization vs TabulationTarjan vs KosarajuSegment Tree vs Fenwick TreeArray vs Linked ListStack vs QueueQuick Sort vs Heap SortKMP vs Rabin-KarpUnion-Find vs DFSTrie vs Hash MapAVL Tree vs Red-Black Tree
Hash MapHashing | Tree MapTrees | |
|---|---|---|
| Use case | Exact-key lookup, counting, grouping, caching. | Ordered keys: predecessor/successor, range counts, sorted iteration, sliding-window medians. |
| Requirements | Hashable keys; a load factor kept below ~0.75 by resizing. | Comparable keys; a self-balancing tree (red-black in most standard libraries). |
| Time complexity | Expected O(1) get/put/delete; worst O(n) under collisions. | Guaranteed O(log n) get/put/delete/floor/ceiling; sorted iteration O(n). |
| Space complexity | O(n) plus slack from unused buckets. | O(n) with per-node pointer overhead. |
| Strengths | Fastest point lookups; simplest to use; no ordering constraints on keys. | Ordered operations are native; worst-case guarantees; no hashing of keys required. |
| Weaknesses | No ordering: no min/max, floor/ceiling or range queries; iteration order is arbitrary; resize spikes. | Slower constant factors than hashing; more memory per entry; JavaScript has no built-in. |
| Example problems | Two sum, group anagrams, subarray sum equals k, LRU cache. | Kth smallest in BST, my calendar (booking overlaps), count of range sums, closest values. |
| Choose this when | Choose a hash map when every query is "exact key present, and what is its value" and order never matters. | Choose a tree map when you need floor, ceiling, min, max, or iteration in key order, or when a worst-case bound is required. |