下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026-2027学年贵州省安顺市三联高级中学(衡秀高级中学)高二(上)期中数学试卷(含答案)
- 2026年执业药师考试《药学专业知识(一)》考试卷(带答案解析)
- 2026-2027学年广东省深圳市实验学校高中园高三上学期第一次综合质量检测数学试卷(含答案)
- 2025-2026学年江苏省苏州市初三(上)数学期末模拟卷(一)含答案
- 2026年基本公共卫生服务专项培训考试试题附答案
- 2026年培训试题及答案
- 福建省三明市五县联考2026-2027学年高一上学期期中学情调研生物试卷(含答案)
- 2026-高中教育学综合练习42模拟题及答案
- 2026-大学高等数学综合练习20考试题及答案
- 2026武汉东湖新技术开发区面向复旦大学引进储备干部考试模拟试题及答案解析
- 银川创投集团全资子公司银川商功人工智能科技有限公司公开招聘人工智能工程师等专业技术人员笔试备考试题及答案解析
- 血管内导管相关性血流感染预防与诊治指南
- 2026榆林能源集团有限公司招聘工作人员笔试参考题库及答案
- 麻醉应急预案措施方案(3篇)
- 2027贵州航天电器秋季招聘284人考试参考题库及答案解析
- 2026年秋季开学高三拒绝躺平心理减压课件
- 考研形近易混词汇 (二)
- 矿产资源勘查实施方案编写提纲及解读
- 2026年艾滋病知识竞赛试题及答案
- 《水的净化》课件
- 2026年6月英语六级真题(第一套)及答案解析
评论
0/150
提交评论