Binary Min Heap using ‘C

A double min stack is a finished paired tree that fulfills the min pile property. In a min stack, for some random hub, the worth of that hub is not exactly or equivalent to the upsides of its kids.


Here are the critical highlights and tasks of a double min stack:


1. Structure:


·        A paired min pile is addressed as a double tree, where every hub contains a worth.


·        The tree is finished, meaning all levels are filled with the exception of perhaps the last level, which is filled from left to right.


2. Min Pile Property:


·        For any hub in the pile, the worth of that hub is not exactly or equivalent to the upsides of its kids.


·        The base worth in the pile is situated at the root hub.


3. Addition:


·        To embed a component into a min stack, it is added at the following accessible position, regularly at the base furthest right spot in the tree.


·        The store property is then reestablished by over and again contrasting the embedded component and its parent.


·        Assuming that the parent's worth is more prominent than the embedded component, the components are traded.


·        The cycle is rehashed until the component is in its right position, fulfilling the min pile property.


4. Remove Least:


·        The concentrate least activity eliminates the base component from the root hub of the min stack.


·        The last component in the stack is moved to the root position, and the pile property is reestablished by "rising down" the component.


·        The component is contrasted and its youngsters, and in the event that any kid has a more modest worth, they are traded.


·        This cycle is rehashed until the component is in its right position, keeping up with the min load property.


5. Heapify:


·        Heapify is an activity that changes over a variety of components into a min pile.


·        Beginning from the last non-leaf hub and going up to the root, the components are "rose down" to their right situations to fulfill the min stack property.


·        This interaction guarantees that the whole exhibit frames a legitimate min store.


The time intricacy of inclusion and extraction in a double min stack is O(log n), where n is the quantity of components in the store. Heapify has a period intricacy of O(n).


Paired min piles track down different applications, for example, need lines, arranging calculations (e.g., load sort), diagram calculations (e.g., Dijkstra's calculation), and that's only the tip of the iceberg.


In synopsis, a twofold min pile is a finished double tree that fulfills the min stack property, guaranteeing that the worth of every hub is not exactly or equivalent to the upsides of its youngsters. It gives effective inclusion, extraction, and heapification tasks, making it helpful in various calculations and information structures.

Top of Form

Program:-

#include<stdio.h>

int heap[40];

int size=-1;

 

int main()

{

int choose;

printf("1.insert 2.display 3.lastdel 4.rootdel 5.exit\n");

while(1)

{

printf("enter your choice");

scanf("%d",&choose);

switch(choose)

{

case 1:insert();

        break;

case 2:display();

        break;

case 3:lastdel();

        break;

case 4:rootdel();

        break;

case 5:exit(0);

       break;

}

}

}

 

int insert()

{

int p;

printf("enter element");

scanf("%d",&p);

size=size+1;

heap[size]=p;

moveup(size);

}

 

int moveup(int i)

{

while(i>0)

{

if(heap[parent(i)]>heap[i])

{

int temp;

temp=heap[parent(i)];

heap[parent(i)]=heap[i];

heap[i]=temp;

}

i=i/2;

}

}

 

int parent(int i)

{

return (i-1)/2;

}

 

 

int display()

{

          int i;

for(i=0;i<=size;i++)

{

printf("%d\t",heap[i]);

}

}

 

int lastdel()

{

heap[size]=NULL;

size=size-1;

}

 

int rootdel()

{

int r=heap[0];

heap[0]=heap[size];

size=size-1;

movedown(0);

}

int movedown(int k)

{

int index=k;

int left=left_child(k);

if(left<=size && heap[left]<heap[index])

{

index=left;

}

int right= right_child(k);

if(right<=size && heap[right]<heap[index]){

index=right;

}

if(k!=index)

{

int temp;

temp=heap[index];

heap[index]=heap[k];

heap[k]=temp;

movedown(index);

}

}

 

 

int left_child(int i)

{

return i+1;

 

}

 

int right_child(int i)

{

return i+2;

}

Output:-