自动测试二项堆的插入和弹出_第1页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、自动测试二项堆的插入和弹出ilude stdio.h include stdlib.h include .h define nil 10000 define max 10000 define rand 10000 typef suct _binheap int key; int degree; struct _binheap *parent; struct _binheap *sib; struct _binheap *child; binheap; typedef struct _wrapbinheap struct _wrapbinheap *nt; binheap real; wrapb

2、inheap; wrapbinheap globalrand; wrapbinheap *head; wrapbinheap *tail; wrapbinheap *global_node; vo binheap_init( ) int i; for(i=0;i rand-1;i ) globali.next= globali head= global0; tail= globalrand-1; binheap *mycalloc ( ) binheap *temp= head- real; head=head- next; return temp; void my (binheap *nod

3、e) wrapbinheap *temp = (wrapbinheap *)( (int)node-4); temp- next=0; node- child=node- sib=node- parent=0; tail- next=temp; tail=temp; return ; binheap * neonstruct ( int key) binheap *obj=mycalloc(); obj- key=key; return obj; binheap * nilconstruct () ic binheap *nilobj=null; if(!nilobj) nilobj=myca

4、lloc(); nilobj- key=nil; ee 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 , righ

5、t- sib); return right; void re_link (binheap *left , binheap *right ) right- degree 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_

6、head- sib=binheap_merge(left,right); pre_x=wrap_head; while(1) x=pre_x- if( !x) break; if(! x- sib) pre_x=x; /for foallism else next_x=x- 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- re_link( ne

7、xt_x , x); else next_next_x=next_x- 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- re_link( next_x , x); else pre_x=x; return wrap_head- void binheap_insert (binheap *newobj ,binheap * * head) if(!*head) *head=newobj; e

8、lse *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 print(head- sib,level); binheap *reverse ( binheap *head) binheap *left=null; binheap *right=null; if(!head) return null; while( head- si

9、b ) right=head- 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- binheap * binheap_pop (binheap *_head ) binheap *head=*_head; binheap *re

10、lt=null; binheap *min=null; unsigned int key=(unsigned int )-1; /linux上的随机数都很大 binheap *wrap_head=nilconstruct (); while(head) if (head- key key) key=head- min=head; head=head- wrap_head- sib=*_head; head=wrap_head; while(head) if(head- sib=min) head- sib=min- break; head=head- *_head=wrap_head- res

11、ult=min; binheap_fix_parent(min- child ,null); /a 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- si

12、b=obj) parent_child- sib=replace; break; parent_child=parent_child- 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- if( obj- key parent- key) obj- parent=pa

13、rent- parent; obj- sib=parent- 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( *h

14、ead= 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; p

15、arent=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( nninto insert modenn while(binheap_

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论