I've constructed a Heap by using this code. However, there is one if statement without any codes so I'm trying to avoid using the if statement by giving it to the while statement.
Since the while statement is the opposite with the if. For instance, if(i==1) TRUE while(i!=1) TRUE
I've made the if statement opposite
if((*h).data[temp]<(*h).data[temp*2]&&(*h).data[temp]<(*h).data[temp*2+1])
while((*h).data[temp]>(*h).data[temp*2]&&(*h).data[temp]>(*h).data[temp*2+1])
and the result becomes different.
Here is my real source
int pop(heap* h)
{
int temp=1;
int min=(*h).data[1];
(*h).index--;
//head에 있는 것을 index의 것과 교환
(*h).data[1]=(*h).data[(*h).index];
(*h).data[(*h).index]=NULL;
//root가 가장 작은 경우
while(temp<(*h).capacity)
{
if((*h).data[temp*2]!=NULL&&(*h).data[temp*2+1]!=NULL)
{
if((*h).data[temp]<(*h).data[temp*2]&&(*h).data[temp]<(*h).data[temp*2+1])
{
}
else if((*h).data[temp*2]<(*h).data[temp*2+1])
{
//작은 것을 root와 교환
(*h).data[temp*2]^=(*h).data[temp];
(*h).data[temp]^=(*h).data[temp*2];
(*h).data[temp*2]^=(*h).data[temp];
}
else if((*h).data[temp*2]>(*h).data[temp*2+1])
{
//작을 것을 root와 교환
(*h).data[temp*2+1]^=(*h).data[temp];
(*h).data[temp]^=(*h).data[temp*2+1];
(*h).data[temp*2+1]^=(*h).data[temp];
}
}
else if((*h).data[temp*2]!=NULL)
{
if((*h).data[temp*2]<(*h).data[temp])
{
(*h).data[temp*2]^=(*h).data[temp];
(*h).data[temp]^=(*h).data[temp*2];
(*h).data[temp*2]^=(*h).data[temp];
}
}
temp*=2;
}
return min;
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire