WikiGalaxy

Personalize

Heap Operations: Insertion

Insertion in a Max Heap

Inserting an element into a max heap involves adding the element at the end and then "bubbling up" to maintain the heap property.


import java.util.PriorityQueue;
class MaxHeap {
    public static void main(String[] args) {
        PriorityQueue heap = new PriorityQueue<>((a, b) -> b - a);
        heap.add(10);
        heap.add(5);
        heap.add(15);
        System.out.println(heap);
    }
}
    

Console Output:

[15, 5, 10]

Heap Operations: Deletion

Deletion in a Min Heap

Deleting the root from a min heap involves replacing it with the last element and "bubbling down" to maintain the heap property.


import java.util.PriorityQueue;
class MinHeap {
    public static void main(String[] args) {
        PriorityQueue heap = new PriorityQueue<>();
        heap.add(10);
        heap.add(5);
        heap.add(15);
        heap.poll();
        System.out.println(heap);
    }
}
    

Console Output:

[10, 15]

Heap Operations: Peek

Peeking in a Heap

Peeking retrieves the root element of the heap without removing it, providing a constant time lookup.


import java.util.PriorityQueue;
class HeapPeek {
    public static void main(String[] args) {
        PriorityQueue heap = new PriorityQueue<>();
        heap.add(10);
        heap.add(5);
        heap.add(15);
        System.out.println(heap.peek());
    }
}
    

Console Output:

5

Heap Operations: Heapify

Heapifying an Array

Heapify is the process of converting a binary tree into a heap data structure. This operation is used to build a heap from an unsorted array.


import java.util.Arrays;
import java.util.PriorityQueue;
class HeapifyExample {
    public static void main(String[] args) {
        Integer[] array = {3, 1, 4, 1, 5, 9, 2};
        PriorityQueue heap = new PriorityQueue<>(Arrays.asList(array));
        System.out.println(heap);
    }
}
    

Console Output:

[1, 1, 2, 3, 5, 9, 4]

Heap Operations: Merging Heaps

Merging Two Heaps

Merging two heaps involves combining their elements and re-heapifying to maintain the heap property.


import java.util.PriorityQueue;
class MergeHeaps {
    public static void main(String[] args) {
        PriorityQueue heap1 = new PriorityQueue<>();
        PriorityQueue heap2 = new PriorityQueue<>();
        heap1.add(3);
        heap1.add(1);
        heap2.add(4);
        heap2.add(2);
        heap1.addAll(heap2);
        System.out.println(heap1);
    }
}
    

Console Output:

[1, 2, 4, 3]

Heap Operations: Checking Emptiness

Checking if a Heap is Empty

This operation checks whether a heap is empty, which is useful for determining if further operations can be performed.


import java.util.PriorityQueue;
class CheckHeapEmpty {
    public static void main(String[] args) {
        PriorityQueue heap = new PriorityQueue<>();
        System.out.println(heap.isEmpty());
    }
}
    

Console Output:

true

Heap Operations: Size

Finding the Size of a Heap

The size operation returns the number of elements in the heap, useful for determining its current capacity.


import java.util.PriorityQueue;
class HeapSize {
    public static void main(String[] args) {
        PriorityQueue heap = new PriorityQueue<>();
        heap.add(1);
        heap.add(2);
        System.out.println(heap.size());
    }
}
    

Console Output:

2

Heap Operations: Clear

Clearing a Heap

Clearing a heap removes all elements, effectively resetting it to an empty state.


import java.util.PriorityQueue;
class ClearHeap {
    public static void main(String[] args) {
        PriorityQueue heap = new PriorityQueue<>();
        heap.add(1);
        heap.add(2);
        heap.clear();
        System.out.println(heap.isEmpty());
    }
}
    

Console Output:

true

Heap Operations: Iterating

Iterating Over a Heap

Iterating over a heap allows you to traverse all elements, though the order is not guaranteed to be sorted.


import java.util.PriorityQueue;
class IterateHeap {
    public static void main(String[] args) {
        PriorityQueue heap = new PriorityQueue<>();
        heap.add(1);
        heap.add(3);
        heap.add(2);
        for (Integer num : heap) {
            System.out.print(num + " ");
        }
    }
}
    

Console Output:

1 3 2

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025