Heap (Priority Queue)
A complete binary tree data structure that satisfies the heap property: in a max heap, parent nodes are greater than or equal to children; in a min heap, parents are less than or equal to children. Heaps provide O(1) access to the maximum/minimum element and O(log n) insertion and deletion. They're typically implemented as arrays for efficiency and are the foundation of heap sort and priority queues.
Visualization
Interactive visualization for Heap (Priority Queue)
Interactive visualization with step-by-step execution
Implementation
1class MinHeap {
2 private heap: number[] = [];
3
4 insert(val: number): void {
5 this.heap.push(val);
6 this.bubbleUp(this.heap.length - 1);
7 }
8
9 extractMin(): number | undefined {
10 if (this.heap.length === 0) return undefined;
11 if (this.heap.length === 1) return this.heap.pop();
12
13 const min = this.heap[0];
14 this.heap[0] = this.heap.pop()!;
15 this.bubbleDown(0);
16 return min;
17 }
18
19 private bubbleUp(index: number): void {
20 while (index > 0) {
21 const parentIndex = Math.floor((index - 1) / 2);
22 if (this.heap[parentIndex] <= this.heap[index]) break;
23 [this.heap[parentIndex], this.heap[index]] = [this.heap[index], this.heap[parentIndex]];
24 index = parentIndex;
25 }
26 }
27
28 private bubbleDown(index: number): void {
29 while (true) {
30 let smallest = index;
31 const left = 2 * index + 1;
32 const right = 2 * index + 2;
33
34 if (left < this.heap.length && this.heap[left] < this.heap[smallest]) {
35 smallest = left;
36 }
37 if (right < this.heap.length && this.heap[right] < this.heap[smallest]) {
38 smallest = right;
39 }
40 if (smallest === index) break;
41
42 [this.heap[index], this.heap[smallest]] = [this.heap[smallest], this.heap[index]];
43 index = smallest;
44 }
45 }
46}Deep Dive
Theoretical Foundation
Heap is a complete binary tree stored in an array where parent-child relationships are defined by indices: parent at i, left child at 2i+1, right child at 2i+2. The heap property ensures the max/min element is always at the root. Operations maintain this property through 'bubble up' (heapify up) after insertion and 'bubble down' (heapify down) after deletion.
Complexity
Time
O(1) peek, O(log n) insert/delete
O(log n)
O(log n)
Space
O(n)
Applications
Industry Use
Priority queue in operating systems (process scheduling)
Dijkstra's shortest path algorithm
Huffman coding for data compression
Event simulation systems
Median maintenance in streaming data
K-way merge in external sorting
Load balancing algorithms
Finding top K elements
Use Cases
Related Algorithms
Binary Search Tree (BST)
A hierarchical data structure where each node has at most two children, maintaining the property that all values in the left subtree are less than the node's value, and all values in the right subtree are greater. This ordering property enables efficient O(log n) operations on average for search, insert, and delete. BSTs form the foundation for many advanced tree structures and are fundamental in computer science.
Stack
LIFO (Last-In-First-Out) data structure with O(1) push/pop operations. Stack is a fundamental linear data structure where elements are added and removed from the same end (top). It's essential for function calls, expression evaluation, backtracking algorithms, and undo operations in applications.
Queue
FIFO (First-In-First-Out) data structure with O(1) enqueue/dequeue operations. Queue is a fundamental linear data structure where elements are added at one end (rear) and removed from the other end (front). Essential for breadth-first search, task scheduling, and buffering systems.
Hash Table (Hash Map)
A data structure that implements an associative array abstract data type, mapping keys to values using a hash function. Hash tables provide O(1) average-case time complexity for insertions, deletions, and lookups, making them one of the most efficient data structures for key-value storage. The hash function computes an index into an array of buckets from which the desired value can be found.