动态堆

来源:互联网 发布:手机淘宝联盟怎么提现 编辑:程序博客网 时间:2024/04/30 14:47

在一个最小堆中减少其中元素的值,这个堆能够动态地维护结构。这样就不需要多次对程序进行排序。

[0]=  0  1 ||[1]=  1  2 ||[2]=  4  3 ||[3]=  9  9 ||[4]=  16  10 ||[5]=  5  6 ||[6]=  16  7 ||[7]=  9  8 ||[8]=  4  4 ||[9]=  1  5 ||
[0]=  0  2 ||[1]=  1  5 ||[2]=  4  3 ||[3]=  9  9 ||[4]=  -3  1 ||[5]=  5  6 ||[6]=  16  7 ||[7]=  9  8 ||[8]=  4  4 ||[9]=  1  10 ||
[0]=  0  2 ||[1]=  1  5 ||[2]=  4  7 ||[3]=  9  9 ||[4]=  -3  3 ||[5]=  5  6 ||[6]=  -174  1 ||[7]=  9  8 ||[8]=  4  4 ||[9]=  1  10 ||
-174  1
-3  3
0  2
1  10
1  5
4  7
4  4
5  6
9  8
9  9

 

¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥

#include  <stdio.h>
#include <stdlib.h>
#define N   150
typedef  struct  wrap_data
{
 int  data;
 int  pos;
}wrap_data;
wrap_data * min_heap[N]={0};
int count=1;
int left(i)
{
 return  2*i;
}
int right(i)
{
 return  2*i+1;
}

void insert_help(wrap_data *point_a)
 //void insert_help(int a)
{
 wrap_data * temp;
 int exchange;
 int  label=1,sign=0;
 if(point_a->data>min_heap[label]->data )
 {
  min_heap[label]=point_a; 
 }
 else
 {
  return ;
 }
 while(left(label)<=count-1)
 {
  if(right(label)<=count-1)
  {
   if(min_heap [left (label)]->data <  min_heap [right (label)]->data )
    exchange=left (label) ;
   else
    exchange= right (label);   
  }
  else
   exchange=left (label) ;   
  if(min_heap[exchange]->data < min_heap[label]->data )
  {
   temp=min_heap[exchange];
   min_heap[exchange]=min_heap[label];
   min_heap[label]=temp;  
  }
  else
   break;
  label=exchange;
 }   
}
void insert(wrap_data  *point_a)
 //void insert(int    a)
{
 wrap_data * temp;
 int  pos_temp;
 int p, num;
 if(count==N)
 {
  insert_help(point_a);
  return ;
 }
 min_heap[count]=point_a;
 point_a->pos=count;
 num=count;
 while(num/2)
 {
  p=num/2; 
  if(min_heap[num]->data<min_heap[p]->data)
  {
   temp=min_heap[num];min_heap[num]=min_heap[p];min_heap[p]=temp;
   pos_temp=min_heap[num]->pos;min_heap[num]->pos=min_heap[p]->pos;min_heap[p]->pos=pos_temp;
  }
  else
  {
   break;
  }
  num=p;
 }
 count++;
 if(count>N)count=N;
}

//update  similar  to  insert  ,just  a  little  different
void update(int pos, int  minus )
{
 wrap_data * temp;
 int num,p;
 int  pos_temp;
 num=pos;
 min_heap[num]->data-=minus;  //add  by  chenbing  2011.11.7
 while(num/2)
 {  
  p=num/2; 
  if(min_heap[num]->data<min_heap[p]->data)
  {  
   temp=min_heap[num];min_heap[num]=min_heap[p];min_heap[p]=temp;
   pos_temp=min_heap[num]->pos;min_heap[num]->pos=min_heap[p]->pos;min_heap[p]->pos=pos_temp;
  }  
  else
  {  
   break;
  }  
  num=p;
 }  
}
wrap_data * pop()
{
 wrap_data * temp,*result=min_heap[1];
 int exchange;
 int  label=1;
 min_heap[label]=min_heap[count-1];
 while(left(label)<=count-1)
 {
  if(right(label)<=count-1)
  {
   if(min_heap [left (label)]->data <  min_heap [right (label)]->data )
    exchange=left (label) ;
   else
    exchange= right (label);   
  }
  else
   exchange=left (label) ;

  if(min_heap[exchange]->data<min_heap[label]->data)
  {
   temp=min_heap[exchange];min_heap[exchange]=min_heap[label];min_heap[label]=temp;
  }
  else
   break;
  label=exchange;
 }
 count--;
 return  result;
}
int  empty()
{
 return  !(count-1);
}

int  main()
{

 wrap_data *result;
 int i=0;
 wrap_data *a[100]={0};
 for(i=0;i<10;i++)
 {
  a[i]=(wrap_data*) calloc (1, sizeof (wrap_data ));
  a[i]->data=i*i%20;
  insert(a[i]);

 }

 for (i=0;i<10;i++)
 {
  printf ("[%d]=  %d  %d ||",i,a[i]->data, a[i]->pos );
 }
 printf("\n");

 update (  a[4]->pos ,  19);

 for (i=0;i<10;i++)
 {
  printf ("[%d]=  %d  %d ||",i,a[i]->data, a[i]->pos );
 }
 printf("\n");

 update (  a[6]->pos ,  190);

 for (i=0;i<10;i++)
 {
  printf ("[%d]=  %d  %d ||",i,a[i]->data, a[i]->pos );
 }
 printf("\n");

 while(!empty())
 {
  result=pop();
  printf("%d  %d\n",result->data,result->pos);
  //  printf("%d \n",result->data);
 }

}


 

 

原创粉丝点击