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

Min-Max Heap (Insert, Del, Search)

The document presents a C program that implements a Min-Max Heap data structure, allowing for insertion, deletion of minimum and maximum elements, and searching for keys. It includes functions for managing the heap and demonstrates its usage with sample data. The output shows the heap's state after various operations, including successful searches and deletions.

Uploaded by

mdshakeer811
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)
41 views5 pages

Min-Max Heap (Insert, Del, Search)

The document presents a C program that implements a Min-Max Heap data structure, allowing for insertion, deletion of minimum and maximum elements, and searching for keys. It includes functions for managing the heap and demonstrates its usage with sample data. The output shows the heap's state after various operations, including successful searches and deletions.

Uploaded by

mdshakeer811
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

--C PROGRAM TO PERFORM INSERT,DELETE,SEARCHING FOR A KEY USING MIN-MAX

HEAP:--
PROGRAM:
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100


struct MinMaxHeap {
int heap[MAX_SIZE];
int size;
};

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


int temp = *a;
*a = *b;
*b = temp;
}

int parent(int i) {
return (i - 1) / 2;
}
int isMinLevel(int i) {
int level = 0;

while ((1 << level) - 1 <= i) {


level++;
}
return level % 2 == 0;
}

void insert(struct MinMaxHeap* mmHeap, int key) {


if (mmHeap->size >= MAX_SIZE) {
printf("Heap overflow\n");
return;
}
mmHeap->heap[mmHeap->size] = key;
int current = mmHeap->size;
while (current > 0 && ((isMinLevel(current) && key < mmHeap->heap[parent(current)]) ||

(!isMinLevel(current) && key > mmHeap->heap[parent(current)]))) {


swap(&mmHeap->heap[current], &mmHeap->heap[parent(current)]);
current = parent(current);
}
mmHeap->size++;

}
void deleteMin(struct MinMaxHeap* mmHeap) {
if (mmHeap->size == 0) {
printf("Heap underflow\n");
return;

}
mmHeap->heap[0] = mmHeap->heap[mmHeap->size - 1];
mmHeap->size--;
// Heapify from the root
int current = 0;

int minChild = 2 * current + 1;


while (minChild < mmHeap->size) {
int otherChild = minChild + 1;
for (int i = 0; i < 2 && i + minChild < mmHeap->size; i++) {
if ((isMinLevel(current) && mmHeap->heap[minChild + i] < mmHeap-
>heap[minChild]) ||
(!isMinLevel(current) && mmHeap->heap[minChild + i] > mmHeap-
>heap[minChild])) {

minChild += i;
}
}
if ((isMinLevel(current) && mmHeap->heap[minChild] < mmHeap->heap[current]) ||
(!isMinLevel(current) && mmHeap->heap[minChild] > mmHeap->heap[current])) {
swap(&mmHeap->heap[current], &mmHeap->heap[minChild]);
current = minChild;

minChild = 2 * current + 1;
} else {
break;
}
}

}
void deleteMax(struct MinMaxHeap* mmHeap) {
if (mmHeap->size <= 1) {
printf("Heap underflow\n");
return;

}
int maxChild = 2;
if (mmHeap->size > 2 && mmHeap->heap[2] < mmHeap->heap[1]) {
maxChild = 1;
}

mmHeap->heap[maxChild] = mmHeap->heap[mmHeap->size - 1];


mmHeap->size--;
// Heapify from the removed max child
int current = maxChild;
while (current < mmHeap->size) {

int maxGrandchild = 4 * current + 1;


for (int i = 0; i < 4 && i + maxGrandchild < mmHeap->size; i++) {
if ((isMinLevel(current) && mmHeap->heap[maxGrandchild + i] > mmHeap-
>heap[maxGrandchild]) ||
(!isMinLevel(current) && mmHeap->heap[maxGrandchild + i] < mmHeap-
>heap[maxGrandchild])) {
maxGrandchild += i;
}
}
if ((isMinLevel(current) && mmHeap->heap[maxGrandchild] > mmHeap->heap[current])
||
(!isMinLevel(current) && mmHeap->heap[maxGrandchild] < mmHeap-
>heap[current])) {

swap(&mmHeap->heap[current], &mmHeap->heap[maxGrandchild]);
current = maxGrandchild;
} else {
break;
}

}
}
int search(struct MinMaxHeap* mmHeap, int key) {
for (int i = 0; i < mmHeap->size; i++) {
if (mmHeap->heap[i] == key) {

return 1; // Key found


}
}
return 0; // Key not found
}

void display(struct MinMaxHeap* mmHeap) {


printf("MinMax Heap: ");
for (int i = 0; i < mmHeap->size; i++) {
printf("%d ", mmHeap->heap[i]);
}

printf("\n");
}
int main() {
struct MinMaxHeap mmHeap;
mmHeap.size = 0;

insert(&mmHeap, 3);

insert(&mmHeap, 7);
insert(&mmHeap, 1);
insert(&mmHeap, 5);
insert(&mmHeap, 9);
insert(&mmHeap, 2);

insert(&mmHeap, 6);
insert(&mmHeap, 8);
insert(&mmHeap, 4);
display(&mmHeap);
printf("\nSearching for keys:\n");

printf("Key 5: %s\n", search(&mmHeap, 5) ? "Found" : "Not found");


printf("Key 10: %s\n", search(&mmHeap, 10) ? "Found" : "Not found");
printf("\nDeleting min and max:\n");
deleteMin(&mmHeap);
deleteMax(&mmHeap);

display(&mmHeap);
return 0;
}
OUTPUT:
MinMax Heap: 1 9 6 4 7 2 3 8 5

Searching for keys:


Key 5: Found
Key 10: Not found
Deleting min and max:
MinMax Heap: 9 4 5 8 7 2 3

You might also like