A Min Heap always keeps the smallest element at the top, making it ideal for scenarios where you need quick access to the smallest item, like scheduling shortest tasks first.
A Max Heap keeps the largest element at the root, so it is perfect for applications like leaderboards where you want to access the highest scores quickly.
MinHeap heap = new MinHeap(); heap.Insert(10); heap.Insert(4); heap.Insert(15); heap.Insert(20); heap.Insert(0); heap.PrintHeap();
After inserting 10, 4, 15, 20, 0, the Min Heap reorganizes to keep the smallest element (0) at the root. The correct order when printed level-wise is 0 -> 4 -> 15 -> 20 -> 10 -> null.
MaxHeap heap = new MaxHeap(); heap.Insert(5); heap.Insert(12); heap.Insert(8); heap.Insert(20); heap.Insert(15); heap.RemoveRoot(); heap.PrintHeap();
Removing root (20) replaces it with last element (15), then heapifies down to maintain Max Heap property. The resulting heap is 15 -> 12 -> 8 -> 5 -> null.
To maintain median efficiently, use a Max Heap for the lower half (to get max of lower half) and a Min Heap for the upper half (to get min of upper half). This allows quick median calculation.
