Data Structure - Min Heap

introduce

Data structures are fundamental tools in computer science for organizing and manipulating data. One such data structure is the minimum heap (also known as min-heap), which is widely used in various fields such as computer science, mathematics, and engineering. This article is an introduction to min-heaps, their properties, and applications.

Big Talk Min Heap

Once upon a time there was a group of small animals who had to manage a pile of food of different sizes. They wanted to find a way to quickly find the smallest food. So, they found something called a minimum heap. A min-heap is like a pile of items lined up in rows, where each item has a number representing its size. The smallest item in the smallest heap is always first. It's like a group of small animals sorting all the food by size, with the smallest food at the top. When the small animals need to find the smallest food, they only need to take the food from the front of the smallest pile to get the smallest food. It's like small animals only need to find the front food to find the smallest food.
However, when the critters take out the smallest food, the minimum pile will change, because the smallest food is no longer at the front. Critters need to rearrange the order of their food items to ensure the smallest food items are still on top. It's like the critters need to rearrange the order of their food to make sure the smallest food is still on top.
Through the minimum pile, small animals can quickly find the smallest food without spending too much time searching the entire food pile.

Min-heap properties

A min-heap is a binary tree data structure in which the value of each parent node is less than or equal to the value of its child nodes. In other words, the root of the tree contains the smallest element in the heap. This unique property of min-heaps makes them useful in a variety of applications, including sorting algorithms, graph algorithms, and data compression.
A min-heap can be implemented as an array or a linked list, where the root of the tree is at the first element of the array. Each element in the array or linked list represents a node in the binary tree, and the left and right children of the node can be found at indices 2i+1 and 2i+2, respectively, where i is the index of the parent node.

Min-heap application

Min-heap is one of the most common applications of sorting algorithms like heapsort. In heapsort, the input array is first converted into a min-heap, then the root element is swapped with the last element of the heap. After each swap, the size of the heap is reduced by one, and the remaining elements are reorganized to maintain the min-heap property. This process continues until the entire array is sorted in ascending order. Another application of min-heaps is in graph algorithms, such as Prim's algorithm for finding the minimum spanning tree (MST) of a graph. In Prim's algorithm, a min-heap is used to store the vertices of the graph, where the priority of each vertex is its distance to the MST. Then, the algorithm repeatedly extracts the smallest vertex from the heap, adds its neighbors to the heap, and updates their distances.

Code

public class MinHeap {
    
    

    private int[] heap;
    private int size;

    public MinHeap(int capacity) {
    
    
        heap = new int[capacity];
    }

    private void siftUp(int index) {
    
    
        int parent = (index - 1) / 2;
        while (index > 0 && heap[index] < heap[parent]) {
    
    
            swap(index, parent);
            index = parent;
            parent = (index - 1) / 2;
        }
    }

    private void siftDown(int index) {
    
    
        int leftChild = index * 2 + 1;
        int rightChild = index * 2 + 2;
        int smallest = index;
        if (leftChild < size && heap[leftChild] < heap[smallest]) {
    
    
            smallest = leftChild;
        }
        if (rightChild < size && heap[rightChild] < heap[smallest]) {
    
    
            smallest = rightChild;
        }
        if (smallest != index) {
    
    
            swap(index, smallest);
            siftDown(smallest);
        }
    }

    private void swap(int i, int j) {
    
    
        int temp = heap[i];
        heap[i] = heap[j];
        heap[j] = temp;
    }

    public void insert(int value) {
    
    
        if (size == heap.length) {
    
    
            throw new IllegalStateException("Heap is full");
        }
        heap[size] = value;
        size++;
        siftUp(size - 1);
    }

    public int deleteMin() {
    
    
        if (size == 0) {
    
    
            throw new IllegalStateException("Heap is empty");
        }
        int minValue = heap[0];
        heap[0] = heap[size - 1];
        size--;
        siftDown(0);
        return minValue;
    }

    public int peek() {
    
    
        if (size == 0) {
    
    
            throw new IllegalStateException("Heap is empty");
        }
        return heap[0];
    }

    public boolean isEmpty() {
    
    
        return size == 0;
    }

    public int size() {
    
    
        return size;
    }
}

public class MinHeapExample {
    public static void main(String[] args) {
        MinHeap heap = new MinHeap(10);
        heap.insert(4);
        heap.insert(2);
        heap.insert(1);
        heap.insert(3);
        heap.insert(5);
        while (!heap.isEmpty()) {
            System.out.print(heap.deleteMin() + " ");
        }
    }
}

output:

1 2 3 4 5

In this example, we created a min-heap and then inserted 5 numbers (4, 2, 1, 3, 5). We then removed the numbers from the heap so they were printed in ascending order.
It turns out that min-heap can indeed find the minimum value quickly, and it can be used to sort numbers.

Summarize

In short, the minimum heap is an indispensable data structure in computer science and has a wide range of applications in various fields. Its unique properties, including that the root contains the smallest element in the heap, make it
an efficient tool for sorting algorithms, graph algorithms, and data compression. Understanding min-heaps and their applications is essential for any computer science student or professional.

Guess you like

Origin blog.csdn.net/yinzhangheng/article/details/129992676