Implementing Heap Operations (Insertion, Deletion, Heapify)

In the field of computer science, heaps are a commonly used data structure for efficient retrieval of the minimum or maximum element. They are widely used in priority queues, sorting algorithms, graph algorithms, and more. In this article, we will discuss how to implement three fundamental heap operations: insertion, deletion, and heapify, using the Java programming language.

Heap Data Structure

A heap is a complete binary tree that satisfies the heap property. In a min-heap, for example, every parent node has a value smaller than or equal to its children. In a max-heap, the opposite is true, i.e., every parent node has a value greater than or equal to its children.

To create a heap, we can use an array-based implementation. The index 0 of the array will be left unused, and the root of the heap will be stored at index 1. For any given index i, its left child will be at index 2i, and its right child will be at index 2i + 1.

Insertion Operation

The insertion operation in a heap involves adding a new element while maintaining the heap property. To insert a new element, we add it at the end of the array and then "percolate" it up through the heap until the heap property is satisfied.

Here's an implementation of the insert operation for a min-heap:

public class MinHeap {
    private int[] heap;
    private int size;
    private int capacity;

    public MinHeap(int capacity) {
        this.capacity = capacity;
        this.size = 0;
        this.heap = new int[capacity + 1];
    }

    public void insert(int value) {
        if (size >= capacity) {
            throw new IllegalStateException("Heap is full");
        }

        size++;
        int i = size;

        while (i > 1 && value < heap[i / 2]) {
            heap[i] = heap[i / 2];
            i = i / 2;
        }

        heap[i] = value;
    }
}

Deletion Operation

The deletion operation in a heap involves removing the root element while maintaining the heap property. In a min-heap, we always need to remove the smallest element. After removing the root, we replace it with the last element in the array and then "percolate" it down through the heap until the heap property is satisfied.

Here's an implementation of the delete operation for a min-heap:

public class MinHeap {
    // ...

    public int deleteMin() {
        if (size == 0) {
            throw new IllegalStateException("Heap is empty");
        }

        int min = heap[1];
        heap[1] = heap[size];
        size--;

        int i = 1;
        int child;

        while (2 * i <= size) {
            child = 2 * i;

            if (child + 1 <= size && heap[child + 1] < heap[child]) {
                child++;
            }

            if (heap[child] < heap[i]) {
                int temp = heap[i];
                heap[i] = heap[child];
                heap[child] = temp;
            } else {
                break;
            }

            i = child;
        }

        return min;
    }
}

Heapify Operation

Heapify is an operation that rearranges the elements of an array to form a heap. This operation is useful when we want to convert an unordered array into a heap.

Here's an implementation of the heapify operation for a min-heap:

public class MinHeap {
    // ...

    public void heapify() {
        for (int i = size / 2; i >= 1; i--) {
            int j = i;
            int value = heap[i];

            while (2 * j <= size) {
                int child = 2 * j;

                if (child + 1 <= size && heap[child + 1] < heap[child]) {
                    child++;
                }

                if (heap[child] < value) {
                    heap[j] = heap[child];
                } else {
                    break;
                }

                j = child;
            }

            heap[j] = value;
        }
    }
}

Conclusion

Heap operations, such as insertion, deletion, and heapify, are essential for efficiently working with heaps in various algorithms. By implementing these operations in Java, we can utilize the heap data structure to its full potential. Understanding and mastering these operations will enable you to design and implement efficient algorithms that heavily rely on heaps.

Remember, heaps are versatile data structures, and their applications span a wide range of computational problems. So, make sure to practice implementing heap operations and explore their usage in various scenarios to deepen your understanding.


noob to master © copyleft