欢迎来到天天文库
浏览记录
ID:38184658
大小:36.00 KB
页数:5页
时间:2019-05-24
《二叉树的树形输出》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include"stdio.h"#include"stdlib.h"typedefcharDataType;typedefstructNode{DataTypedata;structNode*LChild;structNode*RChild;}BinTNode,*BiTree;intdepth=0;voidCreateBiTree(BiTree*bt){charch;ch=getchar();if(ch=='.')*bt=NULL;else{*bt=(BiTree)malloc(sizeof(BinTNode));(*b
2、t)->data=ch;CreateBiTree(&((*bt)->LChild));CreateBiTree(&((*bt)->RChild));}}voidPreOrder(BiTreeroot)//先序遍历{if(root!=NULL){printf("%c",root->data);PreOrder(root->LChild);PreOrder(root->RChild);}}intPostTreeDepth(BiTreebt)//二叉树的深度方法1{inthl,hr,max;if(bt!=NULL){hl=Pos
3、tTreeDepth(bt->LChild);hr=PostTreeDepth(bt->RChild);max=hl>hr?hl:hr;returnmax+1;}else{return0;}}voidPreTreeDepth(BiTreebt,inth){if(bt!=NULL){if(h>depth)depth=h;PreTreeDepth(bt->LChild,h+1);PreTreeDepth(bt->RChild,h+1);}}voidPrintTree(BiTreebt,intnlayer)//按树状打印二叉树{
4、if(bt==NULL){return;}PrintTree(bt->RChild,nlayer+1);for(inti=0;idata);PrintTree(bt->LChild,nlayer+1);}intDegree2Node(BiTreebt)//输出度为二的节点{if(!bt
5、
6、!bt->LChild&&!bt->RChild){return0;}if(bt->LChild&&bt->RChild){returnDegree2N
7、ode(bt->LChild)+Degree2Node(bt->RChild)+1;}else{returnDegree2Node(bt->LChild)+Degree2Node(bt->RChild);}}voidmain(){BiTreebt;intdegree2,h=1,nlayer=1;printf("给予的样本");printf("-----AB.DF..G..C.E.H..------");CreateBiTree(&bt);printf("先序遍历:");PreOrder(bt);printf
8、("");PreTreeDepth(bt,h);printf("输的深度是:%d",depth);printf("");degree2=Degree2Node(bt);printf("度为二的节点是:%d",degree2);printf("");printf("树形输出是:");PrintTree(bt,nlayer);}
此文档下载收益归作者所有