min heap Algorithm

The min heap algorithm is a specialized data structure that is particularly useful for priority queue operations, where elements need to be extracted based on their priority or value. Min heap is a complete binary tree, which means that it has all levels completely filled except possibly for the last level, and the last level is filled from left to right. The key property of a min heap is that the value of each node in the tree is greater than or equal to the value of its parent node, with the minimum value being at the root of the tree. This structure allows for efficient insertion and extraction of the minimum value in the heap. The main operations performed in a min heap include inserting a new element, extracting the minimum element, and decreasing the value of an element. When inserting a new element, the element is first added to the end of the heap, and then it is percolated up the tree by swapping it with its parent node until it is in the correct position. When extracting the minimum element, the root node is removed, and the last element in the heap is placed at the root. This element is then percolated down the tree by swapping it with its smaller child node until it is in the correct position. Decreasing the value of an element involves updating the value and percolating it up the tree as needed. Overall, the min heap algorithm provides an efficient way to maintain a collection of elements while still being able to access and manipulate the minimum value quickly.
#include<stdio.h>
#include<stdlib.h>

typedef struct min_heap{
	int *p;
	int size;
	int count;
}Heap;

Heap* create_heap(Heap* heap); /*Creates a min_heap structure and returns a pointer to the struct*/
void down_heapify(Heap* heap, int index);/*Pushes an element downwards in the heap to find its correct position*/
void up_heapify(Heap* heap, int index);/*Pushes an element upwards in the heap to find its correct position*/
void push(Heap* heap, int x);/*Inserts an element in the heap*/
void pop(Heap* heap);/*Removes the top element from the heap*/
int top(Heap* heap);/*Returns the top element of the heap or returns INT_MIN if heap is empty*/
int empty(Heap* heap);/*Checks if heap is empty*/
int size(Heap* heap);/*Returns the size of heap*/

int main(){
	Heap* head = create_heap(head);
	push(head, 10);
	printf("Pushing element : 10\n");
	push(head, 3);
	printf("Pushing element : 3\n");
	push(head, 2);
	printf("Pushing element : 2\n");
	push(head, 8);
	printf("Pushing element : 8\n");
	printf("Top element = %d \n", top(head));
	push(head, 1);
	printf("Pushing element : 1\n");
	push(head, 7);
	printf("Pushing element : 7\n");
	printf("Top element = %d \n", top(head));
	pop(head);
	printf("Popping an element.\n");
	printf("Top element = %d \n", top(head));
	pop(head);
	printf("Popping an element.\n");
	printf("Top element = %d \n", top(head));
	printf("\n");
	return 0;
}
Heap* create_heap(Heap* heap){
	heap = (Heap *)malloc(sizeof(Heap));
	heap->size = 1;
	heap->p = (int *)malloc(heap->size*sizeof(int));
	heap->count = 0;
	return heap;
}

void down_heapify(Heap* heap, int index){
	if(index>=heap->count)return;
	int left = index*2+1;
	int right = index*2+2;
	int leftflag = 0, rightflag = 0;
	
	int minimum = *((heap->p)+index);
	if(left<heap->count && minimum>*((heap->p)+left)){
		minimum = *((heap->p)+left);
		leftflag = 1;
	}
	if(right<heap->count && minimum>*((heap->p)+right)){
		minimum = *((heap->p)+right);
		leftflag = 0;
		rightflag = 1;
	}
	if(leftflag){
		*((heap->p)+left) = *((heap->p)+index);
	    *((heap->p)+index) = minimum;
	    down_heapify(heap, left);
	}
	if(rightflag){
		*((heap->p)+right) = *((heap->p)+index);
	    *((heap->p)+index) = minimum;
	    down_heapify(heap, right);
	}
}
void up_heapify(Heap* heap, int index){
	int parent = (index-1)/2;
	if(parent<0)return;
	if(*((heap->p)+index)<*((heap->p)+parent)){
		int temp = *((heap->p)+index);
		*((heap->p)+index) = *((heap->p)+parent);
		*((heap->p)+parent) = temp;
		up_heapify(heap, parent);
	}
}

void push(Heap* heap, int x){
	if(heap->count>=heap->size)return;
	*((heap->p)+heap->count) = x;
	heap->count++;
	if(4*heap->count >= 3*heap->size){
		heap->size *= 2;
		(heap->p) = (int *)realloc((heap->p), (heap->size)*sizeof(int));
	}
	up_heapify(heap, heap->count - 1);
}
void pop(Heap* heap){
	if(heap->count==0)return;
	heap->count--;
	int temp = *((heap->p)+heap->count);
	*((heap->p)+heap->count) = *(heap->p);
	*(heap->p) = temp;
	down_heapify(heap, 0);
	if(4*heap->count<=heap->size){
		heap->size /= 2;
		(heap->p) = (int *)realloc((heap->p), (heap->size)*sizeof(int));
	}
}
int top(Heap* heap){
	if(heap->count!=0)return *(heap->p);
	else return INT_MIN;
}
int empty(Heap* heap){
	if(heap->count!=0)return 0;
	else return 1;
}
int size(Heap* heap){
	return heap->count;
}

LANGUAGE:

DARK MODE: