0% found this document useful (0 votes)
9 views

heap

The document contains C code for implementing Min Heap and Max Heap data structures, including their respective functions for insertion, deletion, and display. It defines structures for both heaps and provides function prototypes for heap operations. The main function demonstrates the usage of these heaps by inserting elements, displaying them, and deleting specific elements.

Uploaded by

Sekhar Muthangi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

heap

The document contains C code for implementing Min Heap and Max Heap data structures, including their respective functions for insertion, deletion, and display. It defines structures for both heaps and provides function prototypes for heap operations. The main function demonstrates the usage of these heaps by inserting elements, displaying them, and deleting specific elements.

Uploaded by

Sekhar Muthangi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <stdio.

h>

#include <stdlib.h>

#define MAX_SIZE 100

// Min Heap

typedef struct {

int size;

int arr[MAX_SIZE];

} MinHeap;

// Max Heap

typedef struct {

int size;

int arr[MAX_SIZE];

} MaxHeap;

// Function prototypes for Min Heap

void minHeapify(MinHeap *heap, int index);

void insertMinHeap(MinHeap *heap, int key);

int deleteMinHeap(MinHeap *heap, int index);

void displayMinHeap(MinHeap *heap);

// Function prototypes for Max Heap

void maxHeapify(MaxHeap *heap, int index);

void insertMaxHeap(MaxHeap *heap, int key);

int deleteMaxHeap(MaxHeap *heap, int index);

void displayMaxHeap(MaxHeap *heap);

// Function to swap two elements

void swap(int *a, int *b) {


int temp = *a;

*a = *b;

*b = temp;

int main() {

MinHeap minHeap;

MaxHeap maxHeap;

minHeap.size = 0;

maxHeap.size = 0;

// Insert elements into Min Heap

insertMinHeap(&minHeap, 3);

insertMinHeap(&minHeap, 1);

insertMinHeap(&minHeap, 6);

insertMinHeap(&minHeap, 5);

insertMinHeap(&minHeap, 9);

insertMinHeap(&minHeap, 8);

// Insert elements into Max Heap

insertMaxHeap(&maxHeap, 3);

insertMaxHeap(&maxHeap, 1);

insertMaxHeap(&maxHeap, 6);

insertMaxHeap(&maxHeap, 5);

insertMaxHeap(&maxHeap, 9);

insertMaxHeap(&maxHeap, 8);

// Display Min Heap

printf("Min Heap:\n");

displayMinHeap(&minHeap);
// Display Max Heap

printf("\nMax Heap:\n");

displayMaxHeap(&maxHeap);

// Delete element from Min Heap

deleteMinHeap(&minHeap, 2);

printf("\nMin Heap after deleting element at index 2:\n");

displayMinHeap(&minHeap);

// Delete element from Max Heap

deleteMaxHeap(&maxHeap, 2);

printf("\nMax Heap after deleting element at index 2:\n");

displayMaxHeap(&maxHeap);

return 0;

// Min Heap Functions

void minHeapify(MinHeap *heap, int index) {

int smallest = index;

int left = 2 * index + 1;

int right = 2 * index + 2;

if (left < heap->size && heap->arr[left] < heap->arr[smallest])

smallest = left;

if (right < heap->size && heap->arr[right] < heap->arr[smallest])

smallest = right;

if (smallest != index) {

swap(&heap->arr[index], &heap->arr[smallest]);
minHeapify(heap, smallest);

void insertMinHeap(MinHeap *heap, int key) {

if (heap->size == MAX_SIZE) {

printf("Heap overflow\n");

return;

int i = heap->size++;

heap->arr[i] = key;

while (i != 0 && heap->arr[(i - 1) / 2] > heap->arr[i]) {

swap(&heap->arr[i], &heap->arr[(i - 1) / 2]);

i = (i - 1) / 2;

int deleteMinHeap(MinHeap *heap, int index) {

if (heap->size <= 0 || index >= heap->size)

return -1;

int root = heap->arr[index];

heap->arr[index] = heap->arr[--heap->size];

minHeapify(heap, index);

return root;

void displayMinHeap(MinHeap *heap) {


for (int i = 0; i < heap->size; ++i)

printf("%d ", heap->arr[i]);

printf("\n");

// Max Heap Functions

void maxHeapify(MaxHeap *heap, int index) {

int largest = index;

int left = 2 * index + 1;

int right = 2 * index + 2;

if (left < heap->size && heap->arr[left] > heap->arr[largest])

largest = left;

if (right < heap->size && heap->arr[right] > heap->arr[largest])

largest = right;

if (largest != index) {

swap(&heap->arr[index], &heap->arr[largest]);

maxHeapify(heap, largest);

void insertMaxHeap(MaxHeap *heap, int key) {

if (heap->size == MAX_SIZE) {

printf("Heap overflow\n");

return;

int i = heap->size++;

heap->arr[i] = key;
while (i != 0 && heap->arr[(i - 1) / 2] < heap->arr[i]) {

swap(&heap->arr[i], &heap->arr[(i - 1) / 2]);

i = (i - 1) / 2;

int deleteMaxHeap(MaxHeap *heap, int index) {

if (heap->size <= 0 || index >= heap->size)

return -1;

int root = heap->arr[index];

heap->arr[index] = heap->arr[--heap->size];

maxHeapify(heap, index);

return root;

void displayMaxHeap(MaxHeap *heap) {

for (int i = 0; i < heap->size; ++i)

printf("%d ", heap->arr[i]);

printf("\n");

You might also like