欢迎来到天天文库
浏览记录
ID:38165903
大小:33.50 KB
页数:4页
时间:2019-05-24
《数据结构C语言实现二叉树三种遍历》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验课题一:将下图中的二叉树用二叉链表表示:ABCDEFGH1用三种遍历算法遍历该二叉树,给出对应的输出结果;2写一个函数对二叉树搜索,若给出一个结点,根据其是否属于该树,输出true或者false。3写函数完成习题4.31(C++版)或4.28(C版教科书)。#include"stdio.h"#include"malloc.h"typedefstructBiTNode{chardata;structBiTNode*lchild,*rchild;}BiTNode,*BiTree;BiTreeCreate(BiTreeT)
2、{charch;ch=getchar();if(ch=='#')T=NULL;else{T=(BiTNode*)malloc(sizeof(BiTNode));T->data=ch;T->lchild=Create(T->lchild);T->rchild=Create(T->rchild);}returnT;}intnode(BiTreeT){intsum1=0,a,b;if(T){if(T!=NULL)sum1++;a=node(T->lchild);sum1+=a;b=node(T->rchild);sum1+=b
3、;}returnsum1;}intmnode(BiTreeT){intsum2=0,e,f;if(T){if((T->lchild!=NULL)&&(T->rchild!=NULL))sum2++;e=mnode(T->lchild);sum2+=e;f=mnode(T->rchild);sum2+=f;}returnsum2;}voidPreorder(BiTreeT){if(T){printf("%c",T->data);Preorder(T->lchild);Preorder(T->rchild);}}intSum
4、leaf(BiTreeT){intsum=0,m,n;if(T){if((!T->lchild)&&(!T->rchild))sum++;m=Sumleaf(T->lchild);sum+=m;n=Sumleaf(T->rchild);sum+=n;}returnsum;}voidzhongxu(BiTreeT){if(T){zhongxu(T->lchild);printf("%c",T->data);zhongxu(T->rchild);}}voidhouxu(BiTreeT){if(T){houxu(T->lchi
5、ld);houxu(T->rchild);printf("%c",T->data);}}main(){BiTreeT;intsum,sum1,sum3;printf("请输入字符串:");T=Create(T);printf("前序遍历:");Preorder(T);printf("");printf("中序遍历:");zhongxu(T);printf("");printf("后序遍历:");houxu(T);printf("");sum=Sumleaf(T);printf("树叶数为:
6、");printf("%d",sum);printf("");printf("树结点数为:");sum1=node(T);printf("");printf("%d",sum1);printf("");printf("树满结点数为:");sum3=mnode(T);printf("%d",sum3);printf("");}
此文档下载收益归作者所有