Binary Max Heap using ‘C’
A twofold max load is a finished parallel tree that fulfills the maximum pile property. In a maximum stack, for some random hub, the worth of that hub is more noteworthy than or equivalent to the upsides of its youngsters.
Here are the critical elements and tasks of a twofold max pile:
1. Structure:
· A twofold max load is addressed as a paired 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. Max Pile Property:
· For any hub in the load, the worth of that hub is more prominent than or equivalent to the upsides of its kids.
· The greatest worth in the stack is situated at the root hub.
3. Inclusion:
· To embed a component into a maximum store, it is added at the following accessible position, commonly at the base furthest right spot in the tree.
· The store property is then reestablished by more than once contrasting the embedded component and its parent.
· Assuming the parent's worth is not exactly the embedded component, the components are traded.
· The cycle is rehashed until the component is in its right position, fulfilling the maximum load property.
4. Remove Most extreme:
· The concentrate most extreme activity eliminates the greatest component from the root hub of the maximum pile.
· 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 kids, and in the event that any youngster has a bigger worth, they are traded.
· This cycle is rehashed until the component is in its right position, keeping up with the maximum load property.
5. Heapify:
· Heapify is an activity that changes over a variety of components into a maximum load.
· 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 maximum stack property.
· This cycle guarantees that the whole cluster shapes a legitimate max stack.
The time intricacy of inclusion and extraction in a double max stack is O(log n), where n is the quantity of components in the load. Heapify has a period intricacy of O(n).
Twofold max piles are regularly utilized in different applications, for example, need lines, arranging calculations (e.g., load sort), double store based information structures (e.g., paired stack based execution of a need line), and that's only the tip of the iceberg.
In outline, a paired max store is a finished parallel tree that fulfills the maximum stack property, guaranteeing that the worth of every hub is more noteworthy than or equivalent to the upsides of its kids. It gives effective inclusion, extraction, and heapification tasks, making it valuable in various calculations and information structures.
Program:-
#include<stdio.h>
int size=-1;
int main()
{
int
choose,i;
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(0);
}
}
int
left_child(int i)
{
return i+1;
}
int
right_child(int i)
{
return i+2;
}
Output:-
0 Comments