二叉树基本操作的程序实现_第1页
二叉树基本操作的程序实现_第2页
二叉树基本操作的程序实现_第3页
二叉树基本操作的程序实现_第4页
二叉树基本操作的程序实现_第5页
免费预览已结束,剩余11页可下载查看

下载本文档

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

文档简介

1、二叉树基本操作的程序实现作者:nuciewth 阅读人次:43966 文章来源:本站原创发布时间:2007-6-13条原帖及讨论:http:/ 尸0;左中for(i=0;ilen;i+)(Lai=si+1;Lalen=0;/左前for(i=len+1;istrlen(r);i+)(Rbi-len-1=ri;Rbi-len-1=0;for(i=len+1;ilchild,La,Ra);Pre_In_order(&(*t)-rchild,Lb,Rb);else(*t=NULL;intmain()(Bintreet;charpre30=ABCDEF,in30=CBAEDF;/测试数据prin

2、tf(输入前序遍历序列:);scanf(%s,pre);printf(输入中序遍历序列:);scanf(%s,in);Pre_In_order(&t,pre,in);Posorderl(t);)#include#includetypedefstructnodeintdata;structnode*lchild,*rchild,*next;hufnode;typedefhufnode*linkhuf;/*/*/linkhufCreat_Node(intn)/创建一单链表linkhufhead,pre,p;intx;head=NULL;while(n-)scanf(%d,&x);p

3、=(linkhuf)malloc(sizeof(hufnode);p-data=x;p-lchild=NULL;p-rchild=NULL;if(NULL=head)head=p;pre=head;elsep-next=pre-next;pre-next=p;pre=pre-next;returnhead;/*huffman 树*/linkhufinsert(linkhufroot,linkhufs)/将结点 S 插入到有序(linkhufp1,p2;if(NULL=root)root=s;else(p1=NULL;p2=root;while(p2&p2-datadata)(p1=p2

4、;p2=p2-next;s-next=p2;if(NULL=p1)(root=s;else(p1-next=s;returnroot;voidPreorder1(linkhuft)(if(t!=NULL)(printf(%-6d”,t-data);Preorder1(t-lchild);Preorder1(t-rchild);voidcreathuffman(linkhuf*root)/构造 Huffman 树。(linkhufs,rl,rr;while(*root&(*root)-next)(rl=*root;rr=(*root)-next;*root=rr-next;Huffman

5、root 中。s=(linkhuf)malloc(sizeof(hufnode);s-next=NULL;s-data=rl-data+rr-data;s-lchild=rl;s-rchild=rr;rl-next=rr-next=NULL;*root=insert(*root,s);intmain()linkhufroot;intn;scanf(%d,&n);root=Creat_Node(n);creathuffman(&root);Preorder1(root);printf(n);return0;/*/*按层次顺序建立一棵二叉树/*/#includeBintree.hB

6、intreeLevel_Creat()Bintreeroot,p,s;queuenode;node.front=node.rear=0;charch;ch=getchar();if(ch=)returnNULL;root=(Binnode*)malloc(sizeof(Binnode);/生成根结点root-data=ch;node.datanode.rear+=root;/用队列实现层次遍历while(node.frontdata=ch;p-lchild=s;node.datanode.rear+=s;else(p-lchild=NULL;ch=getchar();if(ch!=)(s=(Binnode*)malloc(sizeof(Binnode);s-data=ch;p-rchild=s;node.datanode.rear+=s;else

温馨提示

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

评论

0/150

提交评论