Introduction to MinHeap (original) (raw)

Introduction to Min-Heap

Last Updated : 3 Apr, 2026

A **Min-Heap is a Data Structure with the following properties.

Internal Implementation of Min-Heap Data Structure

A heap can be efficiently represented using an array.

minH2drawio

Operations on Min-heap Data Structure and their Implementation:

Here are some common operations that can be performed on a Heap Data Structure,

Insertion - O(log n) Time and O(n) Space

The insertion operation in a min-heap involves the following steps:

**Illustration:

**Suppose the Heap is a Min-Heap as:

Insertion in Min-Heap

C++ `

#include #include using namespace std;

void insert(vector& heap, int value) { // Add the new element to the end of the heap heap.push_back(value);

// Get the index of the last element
int index = heap.size() - 1;

// Compare the new element with
//its parent and swap if necessary
while (index > 0
       && heap[(index - 1) / 2] > heap[index]) {
    swap(heap[index], heap[(index - 1) / 2]);
    
    // Move up the tree to the
    //parent of the current element
    index = (index - 1) / 2;
}

}

int main() { vectorarr; vectorvalues = {10, 3, 2, 4, 5, 1}; int n = values.size(); for (int i = 0; i < n; i++) { insert(arr, values[i]); cout << "Inserted " << values[i] << " into the min-heap: "; for (int j = 0; j <arr.size(); j++) { cout << arr[j] << " "; } cout << endl; } return 0; }

Java

import java.util.ArrayList;

public class GFG {

public static void insert(ArrayList<Integer> heap, int value) {
    // Add the new element to the end of the heap
    heap.add(value);

    // Get the index of the last element
    int index = heap.size() - 1;
    
    // Compare the new element with
    // its parent and swap if necessary
    while (index > 0 && heap.get((index - 1) / 2) > heap.get(index)) {
        int temp = heap.get(index);
        heap.set(index, heap.get((index - 1) / 2));
        heap.set((index - 1) / 2, temp);
        
        // Move up the tree to the
        // parent of the current element
        index = (index - 1) / 2;
    }
}

public static void main(String[] args) {
    ArrayList<Integer> arr = new ArrayList<>();
    int[] values = {10, 3, 2, 4, 5, 1};
    int n = values.length;
    for (int i = 0; i < n; i++) {
        insert(arr, values[i]);
        System.out.print("Inserted " + values[i] + " into the min-heap: ");
        for (int j = 0; j < arr.size(); j++) {
            System.out.print(arr.get(j) + " ");
        }
        System.out.println();
    }
}

}

Python

def insert(heap, value): # Add the new element to the end of the heap heap.append(value)

# Get the index of the last element
index = len(heap) - 1

# Compare the new element with
# its parent and swap if necessary
while index > 0 and heap[(index - 1) // 2] > heap[index]:
    heap[index], heap[(index - 1) // 2] = heap[(index - 1) // 2], heap[index]
    
    # Move up the tree to the
    # parent of the current element
    index = (index - 1) // 2

if name == "main": arr = [] values = [10, 3, 2, 4, 5, 1] n = len(values) for i in range(n): insert(arr, values[i]) print(f"Inserted {values[i]} into the min-heap: ", end="") for j in range(len(arr)): print(arr[j], end=" ") print()

C#

using System; using System.Collections.Generic;

class GFG { static void insert(List heap, int value) { // Add the new element to the end of the heap heap.Add(value);

    // Get the index of the last element
    int index = heap.Count - 1;
    
    // Compare the new element with
    // its parent and swap if necessary
    while (index > 0 && heap[(index - 1) / 2] > heap[index])
    {
        int temp = heap[index];
        heap[index] = heap[(index - 1) / 2];
        heap[(index - 1) / 2] = temp;
        
        // Move up the tree to the
        // parent of the current element
        index = (index - 1) / 2;
    }
}

static void Main()
{
    List<int> arr = new List<int>();
    int[] values = {10, 3, 2, 4, 5, 1};
    int n = values.Length;
    for (int i = 0; i < n; i++)
    {
        insert(arr, values[i]);
        Console.Write("Inserted " + values[i] + " into the min-heap: ");
        for (int j = 0; j < arr.Count; j++)
        {
            Console.Write(arr[j] + " ");
        }
        Console.WriteLine();
    }
}

}

` JavaScript ``

function insert(heap, value) { // Add the new element to the end of the heap heap.push(value);

// Get the index of the last element
let index = heap.length - 1;

// Compare the new element with
// its parent and swap if necessary
while (index > 0 && heap[Math.floor((index - 1) / 2)] > heap[index]) {
    let temp = heap[index];
    heap[index] = heap[Math.floor((index - 1) / 2)];
    heap[Math.floor((index - 1) / 2)] = temp;
    
    // Move up the tree to the
    // parent of the current element
    index = Math.floor((index - 1) / 2);
}

} //Driver Code let arr = []; let values = [10, 3, 2, 4, 5, 1]; let n = values.length;

for (let i = 0; i < n; i++) { insert(arr, values[i]); process.stdout.write(Inserted ${values[i]} into the min-heap: ); for (let j = 0; j < arr.length; j++) { process.stdout.write(arr[j] + " "); } console.log(); }

``

Output

Inserted 10 into the min-heap: 10 Inserted 3 into the min-heap: 3 10 Inserted 2 into the min-heap: 2 10 3 Inserted 4 into the min-heap: 2 4 3 10 Inserted 5 into the min-heap: 2 4 3 10 5 Inserted ...

Deletion - O(log n) Time and O(n) Space

Removing the smallest element (the root) from a Min-Heap involves the following steps:

**Illustration:

C++ `

#include #include using namespace std;

void insert(vector& heap, int value) { // Add the new element to the end of the heap heap.push_back(value);

// Get the index of the last element
int index = heap.size() - 1;

// Compare the new element with its parent and swap if
// necessary
while (index > 0
       && heap[(index - 1) / 2] > heap[index]) {
    swap(heap[index], heap[(index - 1) / 2]);
    
    // Move up the tree to the parent of the current
    // element
    index = (index - 1) / 2;
}

}

// Function to delete a node from the min-heap void deleteMin(vector& heap, int value) { // Find the index of the element to be deleted int index = -1; for (int i = 0; i < heap.size(); i++) { if (heap[i] == value) { index = i; break; } } // If the element is not found, return if (index == -1) { return; }

// Replace the element to be deleted with the last
// element
heap[index] = heap[heap.size() - 1];

// Remove the last element
heap.pop_back();

// Heapify the tree starting from the element at the
// deleted index
while (true) {
    int left_child = 2 * index + 1;
    int right_child = 2 * index + 2;
    int smallest = index;
    if (left_child < heap.size()
        && heap[left_child] < heap[smallest]) {
        smallest = left_child;
    }
    if (right_child < heap.size()
        && heap[right_child] < heap[smallest]) {
        smallest = right_child;
    }
    if (smallest != index) {
        swap(heap[index], heap[smallest]);
        index = smallest;
    }
    else {
        break;
    }
}

}

int main() { vectorarr; vectorvalues = { 13, 16, 31, 41, 51, 100 }; int n = values.size(); for (int i = 0; i < n; i++) { insert(arr, values[i]); } cout << "Initial heap: "; for (int j = 0; j < arr.size(); j++) { cout << arr[j] << " "; } cout << endl;

deleteMin(arr, 13);
cout << "Heap after deleting 13: ";
for (int j = 0; j < arr.size(); j++) {
    cout << arr[j] << " ";
}
cout << endl;

return 0;

}

Java

import java.util.ArrayList;

public class GFG {

public static void insert(ArrayList<Integer> heap, int value) {
    // Add the new element to the end of the heap
    heap.add(value);

    // Get the index of the last element
    int index = heap.size() - 1;

    // Compare the new element with its parent and swap if
    // necessary
    while (index > 0 && heap.get((index - 1) / 2) > heap.get(index)) {
        int temp = heap.get(index);
        heap.set(index, heap.get((index - 1) / 2));
        heap.set((index - 1) / 2, temp);

        // Move up the tree to the parent of the current
        // element
        index = (index - 1) / 2;
    }
}

// Function to delete a node from the min-heap
public static void deleteMin(ArrayList<Integer> heap, int value) {
    // Find the index of the element to be deleted
    int index = -1;
    for (int i = 0; i < heap.size(); i++) {
        if (heap.get(i) == value) {
            index = i;
            break;
        }
    }

    // If the element is not found, return
    if (index == -1) {
        return;
    }

    // Replace the element to be deleted with the last
    // element
    heap.set(index, heap.get(heap.size() - 1));

    // Remove the last element
    heap.remove(heap.size() - 1);

    // Heapify the tree starting from the element at the
    // deleted index
    while (true) {
        int left_child = 2 * index + 1;
        int right_child = 2 * index + 2;
        int smallest = index;

        if (left_child < heap.size() && heap.get(left_child) < heap.get(smallest)) {
            smallest = left_child;
        }
        if (right_child < heap.size() && heap.get(right_child) < heap.get(smallest)) {
            smallest = right_child;
        }
        if (smallest != index) {
            int temp = heap.get(index);
            heap.set(index, heap.get(smallest));
            heap.set(smallest, temp);
            index = smallest;
        } else {
            break;
        }
    }
}

public static void main(String[] args) {
    ArrayList<Integer> arr = new ArrayList<>();
    int[] values = {13, 16, 31, 41, 51, 100};
    int n = values.length;

    for (int i = 0; i < n; i++) {
        insert(arr, values[i]);
    }

    System.out.print("Initial heap: ");
    for (int j = 0; j < arr.size(); j++) {
        System.out.print(arr.get(j) + " ");
    }
    System.out.println();

    deleteMin(arr, 13);
    System.out.print("Heap after deleting 13: ");
    for (int j = 0; j < arr.size(); j++) {
        System.out.print(arr.get(j) + " ");
    }
    System.out.println();
}

}

Python

def insert(heap, value): # Add the new element to the end of the heap heap.append(value)

# Get the index of the last element
index = len(heap) - 1

# Compare the new element with its parent and swap if
# necessary
while index > 0 and heap[(index - 1) // 2] > heap[index]:
    heap[index], heap[(index - 1) // 2] = heap[(index - 1) // 2], heap[index]

    # Move up the tree to the parent of the current
    # element
    index = (index - 1) // 2

Function to delete a node from the min-heap

def deleteMin(heap, value):

# Find the index of the element to be deleted
index = -1
for i in range(len(heap)):
    if heap[i] == value:
        index = i
        break

# If the element is not found, return
if index == -1:
    return

# Replace the element to be deleted with the last
# element
heap[index] = heap[-1]

# Remove the last element
heap.pop()

# Heapify the tree starting from the element at the
# deleted index
while True:
    left_child = 2 * index + 1
    right_child = 2 * index + 2
    smallest = index

    if left_child < len(heap) and heap[left_child] < heap[smallest]:
        smallest = left_child
    if right_child < len(heap) and heap[right_child] < heap[smallest]:
        smallest = right_child

    if smallest != index:
        heap[index], heap[smallest] = heap[smallest], heap[index]
        index = smallest
    else:
        break

if name == "main": arr = [] values = [13, 16, 31, 41, 51, 100] n = len(values)

for i in range(n):
    insert(arr, values[i])

print("Initial heap: ", end="")
for val in arr:
    print(val, end=" ")
print()

deleteMin(arr, 13)
print("Heap after deleting 13: ", end="")
for val in arr:
    print(val, end=" ")
print()

C#

using System; using System.Collections.Generic;

class GFG { static void insert(List heap, int value) { // Add the new element to the end of the heap heap.Add(value);

    // Get the index of the last element
    int index = heap.Count - 1;

    // Compare the new element with its parent and swap if
    // necessary
    while (index > 0 && heap[(index - 1) / 2] > heap[index])
    {
        int temp = heap[index];
        heap[index] = heap[(index - 1) / 2];
        heap[(index - 1) / 2] = temp;

        // Move up the tree to the parent of the current
        // element
        index = (index - 1) / 2;
    }
}

// Function to delete a node from the min-heap
static void deleteMin(List<int> heap, int value)
{
    // Find the index of the element to be deleted
    int index = -1;
    for (int i = 0; i < heap.Count; i++)
    {
        if (heap[i] == value)
        {
            index = i;
            break;
        }
    }

    // If the element is not found, return
    if (index == -1)
    {
        return;
    }

    // Replace the element to be deleted with the last
    // element
    heap[index] = heap[heap.Count - 1];

    // Remove the last element
    heap.RemoveAt(heap.Count - 1);

    // Heapify the tree starting from the element at the
    // deleted index
    while (true)
    {
        int left_child = 2 * index + 1;
        int right_child = 2 * index + 2;
        int smallest = index;

        if (left_child < heap.Count && heap[left_child] < heap[smallest])
        {
            smallest = left_child;
        }
        if (right_child < heap.Count && heap[right_child] < heap[smallest])
        {
            smallest = right_child;
        }

        if (smallest != index)
        {
            int temp = heap[index];
            heap[index] = heap[smallest];
            heap[smallest] = temp;
            index = smallest;
        }
        else
        {
            break;
        }
    }
}

static void Main()
{
    List<int> arr = new List<int>();
    int[] values = { 13, 16, 31, 41, 51, 100 };
    int n = values.Length;

    for (int i = 0; i < n; i++)
    {
        insert(arr, values[i]);
    }

    Console.Write("Initial heap: ");
    for (int j = 0; j < arr.Count; j++)
    {
        Console.Write(arr[j] + " ");
    }
    Console.WriteLine();

    deleteMin(arr, 13);
    Console.Write("Heap after deleting 13: ");
    for (int j = 0; j < arr.Count; j++)
    {
        Console.Write(arr[j] + " ");
    }
    Console.WriteLine();
}

}

JavaScript

function insert(heap, value) {

// Add the new element to the end of the heap
heap.push(value);

// Get the index of the last element
let index = heap.length - 1;

// Compare the new element with its parent and swap if
// necessary
while (index > 0 && heap[Math.floor((index - 1) / 2)] > heap[index]) {
    let temp = heap[index];
    heap[index] = heap[Math.floor((index - 1) / 2)];
    heap[Math.floor((index - 1) / 2)] = temp;

    // Move up the tree to the parent of the current
    // element
    index = Math.floor((index - 1) / 2);
}

}

// Function to delete a node from the min-heap function deleteMin(heap, value) { // Find the index of the element to be deleted let index = -1; for (let i = 0; i < heap.length; i++) { if (heap[i] === value) { index = i; break; } }

// If the element is not found, return
if (index === -1) return;

// Replace the element to be deleted with the last
// element
heap[index] = heap[heap.length - 1];

// Remove the last element
heap.pop();

// Heapify the tree starting from the element at the
// deleted index
while (true) {
    let left_child = 2 * index + 1;
    let right_child = 2 * index + 2;
    let smallest = index;

    if (left_child < heap.length && heap[left_child] < heap[smallest]) {
        smallest = left_child;
    }
    if (right_child < heap.length && heap[right_child] < heap[smallest]) {
        smallest = right_child;
    }

    if (smallest !== index) {
        let temp = heap[index];
        heap[index] = heap[smallest];
        heap[smallest] = temp;
        index = smallest;
    } else {
        break;
    }
}

}

// Driver code let arr = []; const values = [13, 16, 31, 41, 51, 100]; const n = values.length;

for (let i = 0; i < n; i++) { insert(arr, values[i]); }

process.stdout.write("Initial heap: "); for (let j = 0; j < arr.length; j++) { process.stdout.write(arr[j] + " "); } console.log();

deleteMin(arr, 13); process.stdout.write("Heap after deleting 13: "); for (let j = 0; j < arr.length; j++) { process.stdout.write(arr[j] + " "); } console.log();

`

Output

Initial heap: 13 16 31 41 51 100 Heap after deleting 13: 16 41 31 100 51

Peek operation - O(1) Time and O(n) Space

To access the minimum element (i.e., the root of the heap), the value of the root node is returned.

Min Heap Data Structure

C++ `

#include #include using namespace std;

// Function to insert an element into min-heap void insert(vector& heap, int value) {

// Add the new element at the end
heap.push_back(value);

// Heapify-up to maintain min-heap property
int index = heap.size() - 1;
while (index > 0 && heap[(index - 1) / 2] > heap[index]) {
    swap(heap[index], heap[(index - 1) / 2]);
    index = (index - 1) / 2;
}

}

// Function to get the peak element of min-heap int top(const vector& heap) { if (!heap.empty())

// Root element
    return heap[0]; 
return -1; 

}

int main() { vector minHeap;

// Insert elements into the min-heap
insert(minHeap, 51);
insert(minHeap, 41);
insert(minHeap, 31);
insert(minHeap, 16);
insert(minHeap, 13);

// Get the peak element (smallest in min-heap)
int peakElement = top(minHeap);
cout << "Peak element: " << peakElement << endl;

return 0;

}

Java

import java.util.ArrayList;

public class GFG {

// Function to insert an element into min-heap
public static void insert(ArrayList<Integer> heap, int value) {
    
    // Add the new element at the end
    heap.add(value);

    // Heapify-up to maintain min-heap property
    int index = heap.size() - 1;
    while (index > 0 && heap.get((index - 1) / 2) > heap.get(index)) {
        int temp = heap.get(index);
        heap.set(index, heap.get((index - 1) / 2));
        heap.set((index - 1) / 2, temp);
        index = (index - 1) / 2;
    }
}

// Function to get the peak element of min-heap
public static int top(ArrayList<Integer> heap) {
    if (!heap.isEmpty())
        // Root element
        return heap.get(0); 
    return -1; 
}

public static void main(String[] args) {
    ArrayList<Integer> minHeap = new ArrayList<>();

    // Insert elements into the min-heap
    insert(minHeap, 51);
    insert(minHeap, 41);
    insert(minHeap, 31);
    insert(minHeap, 16);
    insert(minHeap, 13);

    // Get the peak element (smallest in min-heap)
    int peakElement = top(minHeap);
    System.out.println("Peak element: " + peakElement);
}

}

Python

Function to insert an element into min-heap

def insert(heap, value):

# Add the new element at the end
heap.append(value)

# Heapify-up to maintain min-heap property
index = len(heap) - 1
while index > 0 and heap[(index - 1) // 2] > heap[index]:
    heap[index], heap[(index - 1) //
                      2] = heap[(index - 1) // 2], heap[index]
    index = (index - 1) // 2

Function to get the peak element of min-heap

def top(heap): if heap: # Root element return heap[0] return -1

if name == "main": minHeap = [] # Insert elements into the min-heap insert(minHeap, 51); insert(minHeap, 41); insert(minHeap, 31); insert(minHeap, 16); insert(minHeap, 13);

# Get the peak element (smallest in min-heap)
peakElement = top(minHeap)
print("Peak element:", peakElement)

C#

using System; using System.Collections.Generic;

class GFG { // Function to insert an element into min-heap static void insert(List heap, int value) { // Add the new element at the end heap.Add(value);

    // Heapify-up to maintain min-heap property
    int index = heap.Count - 1;
    while (index > 0 && heap[(index - 1) / 2] > heap[index])
    {
        int temp = heap[index];
        heap[index] = heap[(index - 1) / 2];
        heap[(index - 1) / 2] = temp;
        index = (index - 1) / 2;
    }
}

// Function to get the peak element of min-heap
static int top(List<int> heap)
{
    if (heap.Count > 0)
        // Root element
        return heap[0];
    return -1;
}

static void Main()
{
    List<int> minHeap = new List<int>();

    // Insert elements into the min-heap
   insert(minHeap, 51);
   insert(minHeap, 51);
   insert(minHeap, 41);
   insert(minHeap, 31);
   insert(minHeap, 16);
   insert(minHeap, 13);

    // Get the peak element (smallest in min-heap)
    int peakElement = top(minHeap);
    Console.WriteLine("Peak element: " + peakElement);
}

}

JavaScript

// Function to insert an element into min-heap function insert(heap, value) { // Add the new element at the end heap.push(value);

// Heapify-up to maintain min-heap property
let index = heap.length - 1;
while (index > 0 && heap[Math.floor((index - 1) / 2)] > heap[index]) {
    let temp = heap[index];
    heap[index] = heap[Math.floor((index - 1) / 2)];
    heap[Math.floor((index - 1) / 2)] = temp;
    index = Math.floor((index - 1) / 2);
}

}

// Function to get the peak element of min-heap function top(heap) { if (heap.length > 0) { // Root element return heap[0]; } return -1; }

// driver code let minHeap = [];

// Insert elements into the min-heap insert(minHeap, 51); insert(minHeap, 41); insert(minHeap, 31); insert(minHeap, 16); insert(minHeap, 13);

// Get the peak element (smallest in min-heap) let peakElement = top(minHeap); console.log("Peak element:", peakElement);

`

Heapify - O(n) Time and O(log n) Space

The heapify operation is used to place an element in its correct position within the heap so that the heap property is maintained.
A heapify operation can also be used to create a min heap from an unsorted array. This is done by starting at the last non-leaf node and repeatedly performing the "heapify down" operation until all nodes satisfy the heap property.

Heapify operation in Min Heap

C++ `

#include #include using namespace std;

void heapify(vector &arr, int i, int n) { int smallest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;

// If left child exists and is smaller than root
if (l < n && arr[l] < arr[smallest])
    smallest = l;

// If right child exists and is smaller than smallest so far
if (r < n && arr[r] < arr[smallest])
    smallest = r;

// If smallest is not root, 
//swap and continue heapifying
if (smallest != i) {
    swap(arr[i], arr[smallest]);
     // Recursively heapify 
    heapify(arr, smallest, n);
}

}

int main() { vector arr = {2, 3, 10, 4, 5, 1}; cout << "Original array: "; for (int i = 0; i < arr.size(); i++) cout << arr[i] << " ";

// Build min-heap: perform heapify from last
//non-leaf node up to root
for (int i = arr.size()/2 - 1; i >= 0; i--)
    heapify(arr, i, arr.size());

// Print array after min-heapify
cout << "\nMin-Heap after heapify operation: ";
for (int i = 0; i < arr.size(); i++)
    cout << arr[i] << " ";

return 0;

}

Java

import java.util.ArrayList; import java.util.Arrays;

public class GFG {

public static void heapify(int[] arr, int i, int n) {
    int smallest = i;        
    int l = 2 * i + 1;      
    int r = 2 * i + 2;  
    
    // If left child exists and is smaller than root
    if (l < n && arr[l] < arr[smallest])
        smallest = l;

    // If right child exists and is smaller than smallest so far
    if (r < n && arr[r] < arr[smallest])
        smallest = r;

    // If smallest is not root, swap and continue heapifying
    if (smallest != i) {
        int temp = arr[i];
        arr[i] = arr[smallest];
        arr[smallest] = temp;

        // Recursively heapify
        heapify(arr, smallest, n);
    }
}

public static ArrayList<Integer> buildMinHeap(int[] arr) {
    int n = arr.length;
    // Build min-heap
    for (int i = n / 2 - 1; i >= 0; i--)
        heapify(arr, i, n);

    // Convert array to ArrayList before returning
    ArrayList<Integer> result = new ArrayList<>();
    for (int x : arr)
        result.add(x);

    return result;
}

public static void main(String[] args) {
    int[] arr = {2, 3, 10, 4, 5, 1};

    System.out.print("Original array: ");
    for (int x : arr)
        System.out.print(x + " ");
        
          // Build min-heap: perform heapify from last
     //non-leaf node up to root
    ArrayList<Integer> minHeap = buildMinHeap(arr);
      // Print array after min-heapify
    System.out.print("\nMin-Heap after heapify operation: ");
    for (int x : minHeap)
        System.out.print(x + " ");
}

}

Python

def heapify(arr, i, n): smallest = i l = 2 * i + 1 r = 2 * i + 2

# If left child exists and is smaller than root
if l < n and arr[l] < arr[smallest]:
    smallest = l

# If right child exists and is smaller than smallest so far
if r < n and arr[r] < arr[smallest]:
    smallest = r

# If smallest is not root,
# swap and continue heapifying
if smallest != i:
    arr[i], arr[smallest] = arr[smallest], arr[i]
    # Recursively heapify
    heapify(arr, smallest, n)

if name == "main": arr = [2, 3, 10, 4, 5, 1]

# Print original array
print("Original array: ", " ".join(str(x) for x in arr))

# Build min-heap: perform heapify from last non-leaf node up to root
for i in range(len(arr) // 2 - 1, -1, -1):
    heapify(arr, i, len(arr))


print("Min-Heap after heapify operation: ", " ".join(str(x) for x in arr))

C#

using System; using System.Collections.Generic;

class GFG { static void heapify(int[] arr, int i, int n) { int smallest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;

    // If left child exists and is smaller than root
    if (l < n && arr[l] < arr[smallest])
        smallest = l;

    // If right child exists and is smaller than smallest so far
    if (r < n && arr[r] < arr[smallest])
        smallest = r;

    // If smallest is not root, 
    // swap and continue heapifying
    if (smallest != i)
    {
        int temp = arr[i];
        arr[i] = arr[smallest];
        arr[smallest] = temp;
        // Recursively heapify 
        heapify(arr, smallest, n);
    }
}

static List<int> buildMinHeap(int[] arr)
{
    int n = arr.Length;

    // Build min-heap: perform heapify from last
    // non-leaf node up to root
    for (int i = n / 2 - 1; i >= 0; i--)
        heapify(arr, i, n);

    // Convert array to List<int>
    List<int> result = new List<int>(arr);
    return result;
}

static void Main()
{
    int[] arr = { 2, 3, 10, 4, 5, 1 };

    Console.Write("Original array: ");
    for (int i = 0; i < arr.Length; i++)
        Console.Write(arr[i] + " ");

    List<int> minHeap = buildMinHeap(arr);

    Console.Write("\nMin-Heap after heapify operation: ");
    for (int i = 0; i < minHeap.Count; i++)
        Console.Write(minHeap[i] + " ");
}

}

JavaScript

function heapify(arr, i, n) { let smallest = i;
let l = 2 * i + 1;
let r = 2 * i + 2;

// If left child exists and is smaller than root
if (l < n && arr[l] < arr[smallest])
    smallest = l;

// If right child exists and is smaller than smallest so far
if (r < n && arr[r] < arr[smallest])
    smallest = r;

// If smallest is not root, 
// swap and continue heapifying
if (smallest !== i) {
    let temp = arr[i];
    arr[i] = arr[smallest];
    arr[smallest] = temp;
    // Recursively heapify 
    heapify(arr, smallest, n);
}

}

// Driver code let arr = [ 2, 3, 10, 4, 5, 1];

console.log("Original array: " + arr.join(" "));

// Build min-heap: perform heapify from last // non-leaf node up to root for (let i = Math.floor(arr.length / 2) - 1; i >= 0; i--) { heapify(arr, i, arr.length); }

console.log("Min-Heap after heapify operation: " + arr.join(" "));

`

Output

Original array: 2 3 10 4 5 1 Min-Heap after heapify operation: 1 3 2 4 5 10

Min-Heap Vs Max-Heap

420046868

Applications of Min-Heap Data Structure

Advantages of Min-heap Data Structure