Estructura de datos en árbol binario donde el elemento con mayor prioridad está en la raíz.
Útil para implementar colas de prioridad. El heap puede ser "max" (mayor arriba) o "min" (menor arriba).
// Min Heap - el menor siempre está arriba class MinHeap { private heap: number[] = []; insert(valor: number): void { this.heap.push(valor); this.bubbleUp(this.heap.length - 1); } extractMin(): number | undefined { if (this.heap.length === 0) return undefined; const min = this.heap[0]; const last = this.heap.pop()!; if (this.heap.length > 0) { this.heap[0] = last; this.bubbleDown(0); } return min; } private bubbleUp(index: number): void { while (index > 0) { const parent = Math.floor((index - 1) / 2); if (this.heap[parent] <= this.heap[index]) break; [this.heap[parent], this.heap[index]] = [this.heap[index], this.heap[parent]]; index = parent; } } } // Uso: Cola de prioridad const cola = new MinHeap(); cola.insert(5); cola.insert(2); cola.insert(8); cola.extractMin(); // 2 (el menor)