C语言第六章作业及上机.doc

  1. 1、本文档共5页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
作业3: 这个作业也是上机实验题 试编写程序: (1) 建立下面这样一棵二叉树 (2) 后序遍历这棵二叉树 (3) 按层次遍历 (4) 求叶子数和深度。 (5) 查找値为‘H’的结点,如存在,则打印出’H’的所有祖先。 #includestdio.h #includemalloc.h #define max 100 typedef struct bnode { char data; struct bnode *lchild,*rchild; }Bnode,*Btree; typedef struct { Btree data[max]; int front ; int rear; }queue; char a[]={#,A,B,C,D,#,E,F,#,G,#,#,H,#}; int length=14; Btree Creatree() { char ch; Btree S; ch=getchar(); if(ch==#) { S=NULL; } else { S=(Btree)malloc(sizeof(Bnode)); S-data=ch; S-lchild=Creatree(); S-rchild=Creatree(); } return S; } void Lastree(Btree S) /*后续遍历*/ { if(S) { Lastree(S-lchild); Lastree(S-rchild); printf(%4c,S-data); } } int leafcount(Btree S) /*求叶子数*/ { if(S==NULL) return 0; else { if(!S-rchild!S-lchild) return 1; else return leafcount(S-lchild)+leafcount(S-rchild); } } int depth(Btree S) /*求深度*/ { if(S==NULL) return 0; else { int m=depth(S-lchild); int n=depth(S-rchild); return(mn?m:n)+1; } } void Serch() /*求祖先*/ { char x; int i,y; printf(enter whats you want serch:\n); scanf(%c,x); printf(put out the anster:\n); for(i=0;ilength;i++) { if(x==a[i]) y=i; } while(y/2!=0) { printf(%4c,a[y/2]); y=y/2; } } void CreatQueue(queue *q) { q-front=-1; q-rear=-1; } int EnQueue(queue *q,Btree c) { q-rear++; if(q-rear=max) { printf(queue overflow!\n); return 0; } q-data[q-rear]=c; return 1; } int DeQueue(queue *q,Btree *ret) { if(q-front==q-rear) return 0; q-front++; *ret=q-data[q-front]; return 1; } void Cengci(Btree S) /*层次遍历*/ { queue tq; Btree res; CreatQueue(tq); EnQueue(tq,S); while(DeQueue(tq,res)==1) { if(res) { printf(%4c,res-data); EnQueue(tq,res-lchild); EnQueue(tq,res-rchild); } } } void main() { Btree t; printf(please enter a tree(end with #):\n); t=Creatree(); printf(please output the tree with Last:\n); Lastree(t); printf(\n); printf(leafs number is %d:\n,leafcount(t)); printf(trees depth is %d:\n,depth(t)); printf(cengcihua:\n); Cengci(t); printf(\n); fflush(stdin); Serch(); printf

文档评论(0)

a13355589 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档