Operations on Binary Min Heap

PROBLEM :

Your task is to implement the three methods insertKey,  deleteKey,  and extractMin on a Binary Min Heap

Example
The first line of the input contains an integer 'T' denoting the number of test cases. Then T test cases follow.
First line of each test case contains an integer Q denoting the number of queries .

A Query Q is of 3 Types
1.  1  x  (a query of this type means to insert an element in the min heap with value x )
2.  2  x  (a query of this type means to remove an element at position x from the min heap)
3. 3  (a query like this removes the min element from the min heap and prints it ).

The second line of each test case contains Q queries seperated by space.

Output:
The output for each test case will  be space separated integers having -1  if the heap is empty else the min element of the heap from the stack.
You are required to complete the 3 methods insertKey which takes one argument the value to be inserted , deleteKey which takes one argument the position from where element is to be deleted and extractMin which returns the minimum element in the heap .

Constraints:
1<=T<=100
1<=Q<=100
1<=x<=100

Example:
Input
1
7
1 4 1 2 3 1 6 2 0 3 3

Output
2 6 - 1

Explanation:
In the first test case for query
1 4      the heap will have  {4}
1 2      the heap will be { 2 4 }
3         removes min element from heap ie 2 and prints it now heap is {4}
1 6      inserts 6 to heap now heap is {4 6}
2 0      delete element at position 0 of heap now heap is {6}
3         remove min element from heap ie 6 and prints it  now the heap is empty {}
3         since heap is empty thus no min element exist so -1 is printed .

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/*The structure of the class is
class MinHeap
{
    int *harr;
    int capacity;
    int heap_size;

public:
MinHeap(int cap=100) {heap_size = 0; capacity = cap; harr = new int[cap];}

    int extractMin();
    void deleteKey(int i);
    void insertKey(int k);
};*/

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

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

int rightchild(int i)
{
    return (2*i + 2);
}
   
void swap(int *a,int *b)
{
    int temp ;
    temp=(*a) ;
    (*a)=(*b) ;
    (*b)=temp ;
}


void MinHeapify(int curr,int heap_size,int harr[])
{
    int left=leftchild(curr) ;
    int right=rightchild(curr) ;
   
    int min=curr ;
    if(left<heap_size&&harr[min]>harr[left])
        min=left ;
    if(right<heap_size&&harr[min]>harr[right])
        min=right ;
       
    if(min!=curr)
    {
        swap(&harr[curr],&harr[min]) ;
        MinHeapify(min,heap_size,harr) ;
    }
}

/* Removes min element from min heap and returns it */
int MinHeap ::  extractMin()
{
    if(heap_size<=0)
        return -1 ;
       
    if(heap_size==1)
    {
        int min=harr[0] ;
        heap_size-- ;
        return min ;
    }
   
    int min=harr[0] ;
    harr[0]=harr[heap_size-1] ;
    heap_size-- ;
    MinHeapify(0,heap_size,harr) ;
   
    return min ;
}

/* Removes element from position x in the min heap  */
void MinHeap :: deleteKey(int x)
{
    if(heap_size<=x) return ;
   
    harr[x]=INT_MIN ;
    while(x!=0&&harr[parent(x)]>harr[x])
    {
        swap(&harr[parent(x)],&harr[x]) ;
        x=parent(x) ;
    }
    extractMin() ;
}

/* Inserts an element at position x into the min heap*/
void MinHeap ::insertKey(int x)
{
    heap_size++ ;
   
    int pos=heap_size-1 ;
    harr[pos]=x ;
   
    while(pos!=0&&harr[parent(pos)]>harr[pos])
    {
        swap(&harr[parent(pos)],&harr[pos]) ;
        pos=parent(pos) ;
    }
}

--------------------------------------------------------------------------------

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 )