0% found this document useful (0 votes)
10 views3 pages

Heap Tree 1

The document is a C program that implements a max heap and a min heap data structure with functionalities to insert, delete, and display elements. It includes functions for swapping elements, inserting values into the heaps, and maintaining the heap properties during deletions. The program provides a user interface for interacting with the heaps through a menu-driven approach.

Uploaded by

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

Heap Tree 1

The document is a C program that implements a max heap and a min heap data structure with functionalities to insert, delete, and display elements. It includes functions for swapping elements, inserting values into the heaps, and maintaining the heap properties during deletions. The program provides a user interface for interacting with the heaps through a menu-driven approach.

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>
#define MAX 100
int maxHeap[MAX], minHeap[MAX];
int maxSize = 0, minSize = 0;
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void insertMaxHeap(int H[], int *size, int K) {
*size = *size + 1;
int N = *size;
H[N] = K;

while (N != 1 && H[N] > H[N/2]) {


swap(&H[N], &H[N/2]);
N = N / 2;
}
}
void insertMinHeap(int H[], int *size, int K) {
*size = *size + 1;
int N = *size;
H[N] = K;
while (N != 1 && H[N] < H[N/2]) {
swap(&H[N], &H[N/2]);
N = N / 2;
}
}
void walkDownMax(int H[], int i, int N) {
while (i <= N/2) {
int L = 2 * i;
int R = 2 * i + 1;
int M = i;
if (L <= N && H[L] > H[M]) M = L;
if (R <= N && H[R] > H[M]) M = R;
if (M != i) {
swap(&H[i], &H[M]);
i = M;
} else {
return;
}
}
}
void walkDownMin(int H[], int i, int N) {
while (i <= N/2) {
int L = 2 * i;
int R = 2 * i + 1;
int M = i;
if (L <= N && H[L] < H[M]) M = L;
if (R <= N && H[R] < H[M]) M = R;
if (M != i) {
swap(&H[i], &H[M]);
i = M;
} else {
return;
}
}
}
void deleteMaxHeap(int H[], int *size) {
if (*size == 0) {
printf("Max Heap is empty!\n");
return;
}
printf("Deleted from Max Heap: %d\n", H[1]);
H[1] = H[*size];
*size = *size - 1;
walkDownMax(H, 1, *size);
}
void deleteMinHeap(int H[], int *size) {
if (*size == 0) {
printf("Min Heap is empty!\n");
return;
}
printf("Deleted from Min Heap: %d\n", H[1]);
H[1] = H[*size];
*size = *size - 1;
walkDownMin(H, 1, *size);
}
void displayHeap(int H[], int size, char *name) {
if (size == 0) {
printf("%s is empty!\n", name);
return;
}
printf("%s: ", name);
for (int i = 1; i <= size; i++) {
printf("%d ", H[i]);
}
printf("\n");
}
int main() {
int choice, val;
while (1) {
printf("\n=== Heap Menu ===\n");
printf("1. Insert into Max Heap\n");
printf("2. Insert into Min Heap\n");
printf("3. Delete from Max Heap\n");
printf("4. Delete from Min Heap\n");
printf("5. Display Max Heap\n");
printf("6. Display Min Heap\n");
printf("7. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d", &val);
insertMaxHeap(maxHeap, &maxSize, val);
break;
case 2:
printf("Enter value: ");
scanf("%d", &val);
insertMinHeap(minHeap, &minSize, val);
break;
case 3:
deleteMaxHeap(maxHeap, &maxSize);
break;
case 4:
deleteMinHeap(minHeap, &minSize);
break;
case 5:
displayHeap(maxHeap, maxSize, "Max Heap");
break;
case 6:
displayHeap(minHeap, minSize, "Min Heap");
break;
case 7:
exit(0);
default:
printf("Invalid choice!\n");
}
}

return 0;
}

You might also like