12
DSA + algorithms / Intermediate
Trees, heaps, and graphs
Hierarchical structures, priority queues, graph models, BFS/DFS, and how these structures map to real software problems.
01Trees
Trees represent hierarchy: file systems, DOM nodes, organization structures, indexes, and syntax trees. Binary-search trees maintain ordering; balanced variants keep height near logarithmic.
02Heaps and priority queues
A heap is optimized for repeatedly retrieving the minimum or maximum priority. It is ideal for schedulers, shortest-path algorithms, top-K processing, and event queues.
cppMax heap via priority_queue
std::priority_queue<int> highest;
highest.push(5);
highest.push(20);
highest.push(8);
std::cout << highest.top(); // 20
03Graphs
A graph contains vertices and edges. Use it for networks, roads, dependencies, social connections, device topology, and state transitions.
pythonBreadth-first traversal
from collections import deque
def bfs(graph, start):
seen = {start}
queue = deque([start])
while queue:
node = queue.popleft()
yield node
for neighbor in graph.get(node, []):
if neighbor not in seen:
seen.add(neighbor)
queue.append(neighbor)