Simple Min Heap Implementation in C++

#include<iostream>
using namespace std ;
#include<malloc.h>

typedef struct HEAP
{
int *array ;
int capacity ;
int size ;
}mheap ;

mheap* create_heap(int ) ;
mheap* insert_heap(mheap *,int ) ;
int parent(int ) ;
void swap(int *,int *) ;
int delete_position(mheap *,int ) ;
int extract_min(mheap *) ;
void min_heapify(mheap *,int ) ;
int left_child(int ) ;
int right_child(int ) ;
void display_heap(mheap *) ;

int main()
{
mheap *h ;
int no,choice,conti,ele,i ;

cout<<"\n enter the capacity of the heap that needed to be created " ;
cin>>no ;
h=create_heap(no) ;

do
{
cout<<"\n 1. insert an element in the heap " ;
cout<<"\n 2. delete an element from the heap " ;
cout<<"\n 3. extract the minimum element from the heap " ;
cout<<"\n 4. display the elements of the heap (as if in an array)" ;
cout<<"\n\n Please enter your choice " ;
cin>>choice ;
switch(choice)
{
case 1 :cout<<"\n how many elements to insert in the heap " ;
cin>>no ;
cout<<"\n enter elements " ;
for(i=0;i<no;i++)
{
cin>>ele ;
h=insert_heap(h,ele) ;
}
break ;
case 2 :cout<<"\n enter the position of element that needed to be deleted " ;
cin>>no ;
ele=delete_position(h,no) ;
if(ele==-1)
cout<<"\n SORRY !!! heap is empty OR no such position is found in the heap  " ;
else
cout<<"\n element that is deleted from position = "<<no<<" from the heap id = "<<ele ; ;
break ;
case 3 :ele=extract_min(h) ;
if(ele==-1)
cout<<"\n SORRY !!! heap is empty " ;
else
cout<<"\n minimum element in the heap(extracted) = "<<ele ;
break ;
case 4 :display_heap(h) ;
break ;
default :cout<<"\n SORRY !!! wrong choice , please try again " ;
}
cout<<"\n please enter 1 to continue " ;
cin>>conti ;
}while(conti==1) ;
return 0 ;
}

mheap* create_heap(int cap)
{
mheap *h ;
h=(mheap*)malloc(sizeof(mheap)) ;
h->capacity=cap ;
h->size=0 ;
h->array=(int*)malloc(h->capacity*sizeof(int)) ;

return h ;
}

mheap* insert_heap(mheap *h,int data)
{
if(h->capacity==h->size)
{
cout<<"\n SORRY !!! heap is full , No more elements can be inserted " ;
return h ;
}

h->size++ ;
int i ;
i=(h->size)-1 ;
h->array[i]=data ;

while(i!=0&&h->array[parent(i)]>h->array[i])
{
swap(&h->array[i],&h->array[parent(i)]) ;
i=parent(i) ;
}
return h ;
}

int parent(int ele)
{
return (ele-1)/2 ;
}

void swap(int *a,int *b)
{
int temp ;
temp=*a ;
*a=*b ;
*b=temp ;
}

int delete_position(mheap *h,int no)
{
if(h->size==0)
return -1 ;cout<<h->size ;
if(no>h->size)
return -1 ;
if(no==1)
return extract_min(h) ;

int ele ;
ele=h->array[no-1] ;
h->array[no-1]=h->array[h->size-1] ;
h->size-- ;
min_heapify(h,no) ;

return ele ;
}

int extract_min(mheap *h)
{
if(h->size==0)
return -1 ;
if(h->size==1)
{
h->size-- ;
return h->array[0] ;
}

int min ;
min=h->array[0] ;
h->array[0]=h->array[h->size-1] ;
h->size-- ;
min_heapify(h,0) ;

return min ;
}

void min_heapify(mheap *h,int p)
{
int left,right,small ;

left=left_child(p) ;
right=right_child(p) ;
small=p ;

if(left<h->size&&h->array[left]<h->array[p])
small=left ;
if(right<h->size&&h->array[right]<h->array[small])
small=right ;

if(small!=p)
{
swap(&h->array[p],&h->array[small]) ;
min_heapify(h,small) ;
}
}

int left_child(int i)
{
return 2*i+1 ;
}

int right_child(int i)
{
return 2*i+2 ;
}

void display_heap(mheap *h)
{
int i ;
if(h->size==0)
{
cout<<"\n No elemenet present in the heap to diaplay " ;
return ;
}

for(i=0;i<h->size;i++)
cout<<h->array[i]<<" " ;
}

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )