HEAP DATA STRUCTURE
Name: Om Lohade
Class: IT – C
PRN: 12320123
Roll no.: SEDA 3
C program to create max & min heap data structure and perform insertion
Heap data structure is a complete binary tree that satisfies the heap property,
where any given node is
always greater than its child node/s and the key of the root node is the
largest among all other nodes. This property is also called max heap
property.
always smaller than the child node/s and the key of the root node is the
smallest among all other nodes. This property is also called min heap
property.
Max Heap Code:
#include <stdio.h>
int size = 0;
void swap(int *a, int *b)
{
int temp = *a;
*a=*b;
*b=temp;
}
void heapify(int array[],int size, int i)
{
if(size==1)
printf("Only 1 element");
else
{
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if (l < size && array[l] > array[largest])
largest = l;
if (r < size && array[r] > array[largest])
largest = r;
if(largest!=i)
{
swap(&array[i], &array[largest]);
heapify(array,size,largest);
}
}
}
void printArray(int array[], int size)
{
for (int i = 0; i < size; ++i)
printf("%d ", array[i]);
printf("\n");
}
void insert(int array[], int newnum)
{
if(size==0)
{
array[0] = newnum;
size+=1;
}
else
{
array[size] = newnum;
size+=1;
for(int i= size/2 - 1; i>=0;i--)
heapify(array,size,i);
}
}
int main()
{
int array[10];
insert(array, 3);
insert(array, 4);
insert(array, 9);
insert(array, 5);
insert(array, 2);
printf("Max-Heap array: ");
printArray(array, size);
Min Heap Code:
#include <stdio.h>
#include <stdlib.h>
struct Heap
{
int* arr;
int size;
int capacity;
};
typedef struct Heap heap;
heap* createHeap(int capacity, int* nums);
void insertHelper(heap* h, int index);
void heapify(heap* h, int index);
int extractMin(heap* h);
void insert(heap* h, int data);
heap* createHeap(int capacity, int* nums)
{
heap* h = (heap*)malloc(sizeof(heap));
if (h == NULL) {
printf("Memory error");
return NULL;
}
h->size = 0;
h->capacity = capacity;
h->arr = (int*)malloc(capacity * sizeof(int));
if (h->arr == NULL) {
printf("Memory error");
return NULL;
}
int i;
for (i = 0; i < capacity; i++) {
h->arr[i] = nums[i];
}
h->size = i;
i = (h->size - 2) / 2;
while (i >= 0) {
heapify(h, i);
i--;
}
return h;
}
void insertHelper(heap* h, int index)
{
int parent = (index - 1) / 2;
if (h->arr[parent] > h->arr[index]) {
int temp = h->arr[parent];
h->arr[parent] = h->arr[index];
h->arr[index] = temp;
insertHelper(h, parent);
}
}
void heapify(heap* h, int index)
{
int left = index * 2 + 1;
int right = index * 2 + 2;
int min = index;
if (left >= h->size || left < 0)
left = -1;
if (right >= h->size || right < 0)
right = -1;
if (left != -1 && h->arr[left] < h->arr[index])
min = left;
if (right != -1 && h->arr[right] < h->arr[min])
min = right;
if (min != index) {
int temp = h->arr[min];
h->arr[min] = h->arr[index];
h->arr[index] = temp;
heapify(h, min);
}
}
void insert(heap* h, int data)
{
if (h->size < h->capacity)
{
h->arr[h->size] = data;
insertHelper(h, h->size);
h->size++;
}
}
void printHeap(heap* h)
{
for (int i = 0; i < h->size; i++) {
printf("%d ", h->arr[i]);
}
printf("\n");
}
int main()
{
int arr[9] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
heap* hp = createHeap(9, arr);
printf("Min-Heap array: ");
printHeap(hp);
return 0;
}