0% found this document useful (0 votes)
13 views5 pages

heap

Uploaded by

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

heap

Uploaded by

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

AIM:- Construct Min and Max Heap using arrays, delete any element and display the content

of the
Heap.

Description:- A heap is a data structure like a tree with some special properties. The basic requirement
of the heap is that the value of a node must be greater than equal to (or smaller than equal to) the value
of its children and the tree should be a complete binary tree.

Program:-

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

typedef struct {

int size;

int arr[MAX];

} Heap;

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

int temp = *a;

*a = *b;

*b = temp;

void minHeapify(Heap *heap, int i) {

int smallest = i;

int left = 2 * i + 1;

int right = 2 * i + 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 != i) {

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

minHeapify(heap, smallest);
}

void maxHeapify(Heap *heap, int i) {

int largest = i;

int left = 2 * i + 1;

int right = 2 * i + 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 != i) {

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

maxHeapify(heap, largest);

void insertMinHeap(Heap *heap, int key) {

if (heap->size == MAX) {

printf("Heap is full\n");

return;

heap->size++;

int i = heap->size - 1;

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;

}
void insertMaxHeap(Heap *heap, int key) {

if (heap->size == MAX) {

printf("Heap is full\n");

return;

heap->size++;

int i = heap->size - 1;

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 deleteElement(Heap *heap, int key, void (*heapify)(Heap*, int)) {

int i;

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

if (heap->arr[i] == key)

break;

if (i == heap->size) {

printf("Element not found\n");

return -1;

heap->arr[i] = heap->arr[heap->size - 1];

heap->size--;

heapify(heap, i);

return 0;

void displayHeap(Heap *heap) {


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

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

printf("\n");

int main() {

Heap minHeap = {0};

Heap maxHeap = {0};

insertMinHeap(&minHeap, 3);

insertMinHeap(&minHeap, 1);

insertMinHeap(&minHeap, 6);

insertMinHeap(&minHeap, 5);

insertMinHeap(&minHeap, 9);

insertMinHeap(&minHeap, 8);

insertMaxHeap(&maxHeap, 3);

insertMaxHeap(&maxHeap, 1);

insertMaxHeap(&maxHeap, 6);

insertMaxHeap(&maxHeap, 5);

insertMaxHeap(&maxHeap, 9);

insertMaxHeap(&maxHeap, 8);

printf("Min Heap: ");

displayHeap(&minHeap);

printf("Max Heap: ");

displayHeap(&maxHeap);

deleteElement(&minHeap, 5, minHeapify);

deleteElement(&maxHeap, 5, maxHeapify);

printf("Min Heap after deletion: ");

displayHeap(&minHeap);

printf("Max Heap after deletion: ");

displayHeap(&maxHeap);
return 0;

Output:-

Min Heap: 1 3 6 5 9 8

Max Heap: 9 8 6 1 5 3

Element not found

Element not found

Min Heap after deletion: 1 3 6 8 9

Max Heap after deletion: 9 8 6 1 3

You might also like