二叉树的查找,遍历,(满)节点数,树叶的计算

2018-12-02 14:07

实验课题一:将下图中的二叉树用二叉链表表示:

A B D F

1 用三种遍历算法遍历该二叉树,给出对应的输出结果;

2 写一个函数对二叉树搜索,若给出一个结点,根据其是否属于该树,输出true或者false。 3 写函数完成习题4.31(C++版)或4.28(C版教科书)。

#include\#include\

typedef char Elementtype; typedef struct treenode *Tree; struct treenode { Elementtype Element; Tree Left; Tree Right; };

void Pre(Tree t) {

if(t!=NULL) { printf(\ Pre(t->Left); Pre(t->Right); } }

void Aft(Tree t) {

if(t!=NULL) { Pre(t->Left); Pre(t->Right); printf(\

G E H C }

void Mid(Tree t) {

if(t!=NULL) { Pre(t->Left); printf(\ Pre(t->Right); } } }

Tree creat(Tree t) { char c; t=(Tree)malloc(sizeof(struct treenode)); if((c=getchar())!='#') { t->Element=c; t->Left=Pre(); t->Right=Pre(); } else t=NULL; return t; }

int countnode(Tree T) { if(T==NULL) return 0; else return 1+countnode(T->Left)+countnode(T->Right); }

int countleaf(Tree T) { if(T==NULL) return 0; else if(T->Left==NULL&&T->Right==NULL) return 1; return countleaf(T->Left)+countleaf(T->Right);

}

int countfull(Tree T) { if(T==NULL) return 0; else return!(T->Left==NULL||T->Right==NULL)+countfull(T->Left)+countfull(T->Right); }

int Find(Tree T,char c;) { int find;

if(!find&&T!=NULL) { if(T->Element==c) { find=1; } else { Find(T->Left,c); Find(T->Right,c); } } return find; }

void main() { Tree T; char c; T=creat(T); printf(\前序遍历为:\\n\ Pre(T); printf(\ printf(\中序遍历为:\\n\ Mid(T); printf(\ printf(\后序遍历为:\\n\ Aft(T); printf(\ printf(\请输入要检测的字符:\ scanf(\ getchar(); if(Find(T,c)==1) printf(\该字符在树中!\\n\ else

printf(\该字符不在树中!\\n\

printf(\树中的节点数为:%d\\n\ printf(\树中的树叶数为:%d\\n\ printf(\树中的满节点数为:%d\\n\}


二叉树的查找,遍历,(满)节点数,树叶的计算.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:学校塑胶跑道施工方案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: