欢迎来到天天文库
浏览记录
ID:52589129
大小:18.50 KB
页数:3页
时间:2020-03-28
《二叉树的基本操作(C语言、源代码).doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、二叉树的基本操作(源代码)#include"stdio.h"#include"malloc.h"#defineMAX100typedefstructnode{intdate;structnode*lchild,*rchild;}bitnode,*bitree;bitreecreatebitree(bitreebt){intm;printf("m=");scanf("%d",&m);if(m==-1)bt=NULL;else{bt=(bitnode*)malloc(sizeof(bitnode));bt->date=m;bt->lchild=createbitree(bt->l
2、child);bt->rchild=createbitree(bt->rchild);}returnbt;}//树的建立voidleveltraverse(bitreebt){bitreequeue[MAX],p=bt;intrear=0,front=0;if(p!=NULL){queue[++rear]=p;while(frontdate);if(p->lchild!=NULL)queue[++rear]=p->lchild;if(p->rchild!=NULL)queue[++rear]=p
3、->rchild;}}}//树的层次遍历voidpreordertraverse(bitreebt){if(bt!=NULL){printf("%dt",bt->date);preordertraverse(bt->lchild);preordertraverse(bt->rchild);}}//树的先序遍历voidinordertraverse(bitreebt){if(bt!=NULL){inordertraverse(bt->lchild);printf("%dt",bt->date);inordertraverse(bt->rchild);}}//树的中序遍历vo
4、idpostordertraverse(bitreebt){if(bt!=NULL){postordertraverse(bt->lchild);postordertraverse(bt->rchild);printf("%dt",bt->date);}}//树的后序遍历intbitreedepth(bitreebt){inth,lh,rh;if(bt==NULL)h=0;else{lh=bitreedepth(bt->lchild);rh=bitreedepth(bt->rchild);if(lh>=rh)h=lh+1;elseh=rh+1;}returnh;}//求树的
5、深度intbitreeleaf(bitreebt){intm=0;if(!bt)return0;elseif(bt->lchild==NULL&&bt->rchild==NULL)return1;m+=bitreeleaf(bt->lchild)+bitreeleaf(bt->rchild);returnm;}//求树的叶子树intcountbitreenode(bitreebt){if(!bt)return0;elsereturncountbitreenode(bt->lchild)+countbitreenode(bt->rchild)+1;}//求树的节点数voidOp
6、erateBitree(bitreebt){printf("");printf("Theleveltraversalofthebitreeis:t");leveltraverse(bt);printf("");printf("Thepreordertraversalofthebitreeis:t");preordertraverse(bt);printf("");printf("Theinordertraversalofthebitreeis:t");inordertraverse(bt);printf("");printf("T
7、hepostordertraversalofthebitreeis:t");postordertraverse(bt);printf("");printf("Thenumberofthebitreenodeis%d.",countbitreenode(bt));printf("Thedepthofthebitreeis%d.",bitreedepth(bt));printf("Thenumberoftheleavesofthebitreeis%d.",bitreeleaf(
此文档下载收益归作者所有