自动测试二项堆的插入和弹出

本文介绍了一种高效的数据结构——二项堆的C语言实现方法,并详细解释了包括初始化、插入、合并、删除最小元素等核心操作的具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include  <stdio.h>
#include  <stdlib.h>
#include  <time.h>


#define  NIL  10000
#define  MAX  10000
#define   RAND   10000




typedef  struct  _binheap
{
int  key;
int degree;
struct  _binheap  *parent;
struct  _binheap  *sib;
struct  _binheap  *child;
}binheap;
typedef  struct  _wrapbinheap 
{
struct  _wrapbinheap  *next;
binheap  real;
}wrapbinheap;


wrapbinheap  global[RAND];
wrapbinheap  *head;
wrapbinheap  *tail;
wrapbinheap  *global_node;




void  binheap_init( )
{
int  i;
for(i=0;i<RAND-1;i++)
    {
global[i].next=&global[i+1];
    }
head=&global[0];
tail=&global[RAND-1];
}


binheap  *mycalloc ( )
{
binheap  *temp=&head->real;
head=head->next;
return  temp;
}
void  myfree (binheap  *node)
{
wrapbinheap  *temp = (wrapbinheap *)( (int)node-4);
temp->next=0;
node->child=node->sib=node->parent=0;
tail->next=temp;
tail=temp;

return ;
}


binheap  *  newconstruct ( int key)
{
binheap *obj=mycalloc();
obj->key=key;
return  obj;
}
binheap  *  nilconstruct ()
{
static binheap *nilobj=NULL;
if(!nilobj)
    {
nilobj=mycalloc();
nilobj->key=NIL;
    }
else
    {
    } 
return  nilobj;
}
binheap * binheap_merge ( binheap *left , binheap *right )
{
//fuck  sohu

if( !left )
    {
return  right;
    }
if( !right)
    {
return  left;
    }
if( left->degree < right->degree)
    {
left->sib= binheap_merge ( left->sib , right);
return  left;
    }
else
    {  
right->sib= binheap_merge ( left , right->sib);
return  right;
    }
}
void  re_link (binheap *left , binheap *right )
{  
right->degree+=1;
left->sib=right->child;
right->child=left; 
left->parent=right;
}
binheap * binheap_union ( binheap *left , binheap *right )
{
binheap  *pre_x,*x,*next_x,*next_next_x;
binheap  *wrap_head=nilconstruct ();
int label=0;
wrap_head->sib=binheap_merge(left,right);
pre_x=wrap_head; 
while(1)
    {
x=pre_x->sib; 
if( !x)
{
break;
}
if(! x->sib)
{
pre_x=x;  //for  formallism 
}
else
{
next_x=x->sib;
if( x->degree!=next_x->degree )
{
pre_x=x;
}
else
{    
if ( ! next_x->sib)
{
if( x->key > next_x->key)
{
pre_x->sib=next_x;     
re_link ( x ,next_x);

}
else
{   
x->sib=next_x->sib;
re_link( next_x , x);

}
}
else
{
next_next_x=next_x->sib;    
if(  next_x->degree !=next_next_x->degree )
{
if( x->key > next_x->key)
{
pre_x->sib=next_x;     
re_link ( x ,next_x);

}
else
{   
x->sib=next_x->sib;
re_link( next_x , x);

}
}
else
{  
pre_x=x;
}   
}
}
}  
    }
return  wrap_head->sib;
}
void binheap_insert (binheap  *newobj ,binheap * * head)
{
if(!*head)
    {
*head=newobj;
    }
else
    {
*head=binheap_union( newobj , *head);
    }
}
void  print(binheap *head,int level)
{
if(!head)
    {
    }
else
    {
printf("level=%d  key=%d \n",level,head->key);  
print (head->child,level+1);
print(head->sib,level);
    }
}
binheap  *reverse ( binheap  *head)
{
binheap *left=NULL;
binheap  *right=NULL;
if(!head)
    {
return  NULL;
    }
while( head->sib )
    {
right=head->sib;
head->sib=left;
left=head;
head=right;
    }
head->sib=left;
return  head;
}




void  binheap_fix_parent (  binheap * parent_child  ,binheap *parent)
{
while(parent_child)
{
parent_child->parent=parent;
parent_child=parent_child->sib;
}
}




binheap  * binheap_pop (binheap  **_head )
{
binheap  *head=*_head;
binheap  *result=NULL;
binheap  *min=NULL;
unsigned int  key=(unsigned int )-1;    //linux上的随机数都很大
binheap  *wrap_head=nilconstruct ();
while(head)
    {
if (head->key<key)
{
key=head->key;
min=head;
}
head=head->sib;
    }
wrap_head->sib=*_head;
head=wrap_head;
while(head)
    {
if(head->sib==min)
{
head->sib=min->sib;
break;
}
head=head->sib;
    }
*_head=wrap_head->sib;
result=min;
binheap_fix_parent(min->child ,NULL);  //add  by chenbing
head= reverse  ( min->child);
*_head=binheap_union (head ,*_head);

result->sib=NULL;
result->child=NULL;
return  result;
}


void  binheap_re_sib ( binheap *obj , binheap * parent_child ,binheap  *replace  )
{
while(parent_child)
{
if(parent_child->sib==obj)
{
parent_child->sib=replace;
break;
}
parent_child=parent_child->sib;
}
}


void  binheap_adjust ( binheap * obj ,binheap  **head )
{
int  degree;
binheap *parent=obj->parent;
binheap *obj_child=NULL;
binheap *obj_sib=NULL;



while( parent)
{
obj_child=obj->child;
obj_sib=obj->sib;

if( obj->key < parent->key)
{
obj->parent=parent->parent;
obj->sib=parent->sib;

if(obj==parent->child)
{
obj->child=parent;
}
else
{
obj->child=parent->child;
binheap_re_sib ( obj ,parent->child ,parent);
}

if( parent->parent)
{
if( parent==parent->parent->child)
{
parent->parent->child=obj;
}
else
{
binheap_re_sib ( parent ,parent->parent->child ,obj);
}
}
else
{
if( *head== parent)
{
*head=obj;
}
else
{
binheap_re_sib ( parent , *head ,obj);
}

}

parent->parent=obj;
parent->child=obj_child;
parent->sib=obj_sib;
binheap_fix_parent ( obj->child , obj);
binheap_fix_parent ( obj_child , parent);

degree=obj->degree;
obj->degree=parent->degree;
parent->degree=degree;
}
else
{
break;
}
parent=obj->parent;
}
}




void  binheap_dec ( binheap *obj ,int  value ,binheap  **head)
{
obj->key-=value;
binheap_adjust ( obj ,head);
}


void  binheap_delete ( binheap *obj ,binheap **head )
{
binheap *newobj=NULL;
obj->key-=MAX;
binheap_adjust ( obj ,head);
newobj=binheap_pop ( head);
printf(" now delete  %d ",newobj->key+MAX);
}


int main()
{
int i;
int  binheap_count=0;
binheap  *head=NULL;
binheap  *newobj=NULL;
binheap  **result=NULL;


// int  key[]={12,34,67,44,21,345,222,77,55,33};
binheap_init( );
while(1)
{

printf("\n\ninto  insert  mode\n\n");
while(binheap_count <  RAND *3/4)
{
i=rand();
newobj=newconstruct ( i ); 
binheap_insert ( newobj ,&head);
binheap_count++;

}
while(binheap_count >  RAND *1/3)
{
newobj=binheap_pop ( &head);
printf("%d  ", newobj->key);  
myfree (newobj);
binheap_count--;
}

}
// print(head,0);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值