Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

绿色网站无广告
会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux集群 > Architecture > » 正文

二项堆(减小关键字的值和删除指定关键字)

来源: chenbingchenbing 分享至:

注意观察注释掉的代码


#include  <stdio.h>
#include  <stdlib.h>
#define  NIL  9999
typedef  struct  _binheap
{
int  key;
int degree;
struct  _binheap  *parent;
struct  _binheap  *sib;
struct  _binheap  *child;
}binheap;
binheap  *  newconstruct ( int key)
{
binheap *obj=(binheap*)calloc (1 ,sizeof (binheap));
obj->key=key;
return  obj;
}
binheap  *  nilconstruct ()
{
static binheap *nilobj=NULL;
if(!nilobj)
    {
nilobj=(binheap*)calloc (1 ,sizeof (binheap));
nilobj->key=NIL;
    }
else
    {
    } 
return  nilobj;
}
binheap * binheap_merge ( binheap *left , binheap *right )
{
//funck  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 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  * pop (binheap  **_head )
{
binheap  *head=*_head;
binheap  *result=NULL;
binheap  *min=NULL;
int  key=9999999;
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 )
{
#define  MAX  10000
binheap *newobj=NULL;
obj->key-=MAX;
binheap_adjust ( obj ,head);
newobj=pop ( head);
printf(" now delete  %d ",newobj->key+MAX);
}


int main()
{
int i;
binheap  *head=NULL;
binheap  *newobj=NULL;
binheap  **result=NULL;
int  key[]={12,34,67,44,21,345,222,77,55,33};
result=(binheap **) calloc (  sizeof (key)/sizeof (int ),sizeof (binheap *));
for(i=0;i<sizeof (key) /sizeof (int) ;i++)
    {
newobj=newconstruct (key[i]); 
result[i]=newobj;
insert ( newobj , &head);  
    }
print(head,0);
//pop the result 

// binheap_dec (result[9] ,30 ,&head);
// binheap_dec (result[8] ,50 ,&head);
// binheap_dec (result[7] ,70 ,&head);
// binheap_dec (result[6] ,220 ,&head);
// binheap_dec (result[5] ,340 ,&head);
// binheap_dec (result[4] ,20 ,&head);
// binheap_dec (result[3] ,40 ,&head);
binheap_dec (result[2] ,60 ,&head);
// binheap_dec (result[1] ,30 ,&head);
// binheap_dec (result[0] ,10 ,&head);
printf("adjust \n");
print(head,0);

//test  delete
binheap_delete ( result[6] ,&head);
printf("delete \n");
print(head,0);

binheap_delete ( result[5] ,&head);
printf("delete \n");
print(head,0);

binheap_delete ( result[1] ,&head);
printf("delete \n");
print(head,0);

binheap_delete ( result[8] ,&head);
printf("delete \n");
print(head,0);

binheap_delete ( result[7] ,&head);
printf("delete \n");
print(head,0);

binheap_delete ( result[3] ,&head);
printf("delete \n");
print(head,0);

while(head)
    {
newobj=pop ( &head);
printf("%d  \n", newobj->key);  
    }


}



Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史