您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 二叉树的查找,遍历,(满)节点数,树叶的计算
实验课题一:将下图中的二叉树用二叉链表表示:1用三种遍历算法遍历该二叉树,给出对应的输出结果;2写一个函数对二叉树搜索,若给出一个结点,根据其是否属于该树,输出true或者false。3写函数完成习题4.31(C++版)或4.28(C版教科书)。#includestdio.h#includestdlib.htypedefcharElementtype;typedefstructtreenode*Tree;structtreenode{ElementtypeElement;TreeLeft;TreeRight;};voidPre(Treet){if(t!=NULL){printf(%c,t-Element);Pre(t-Left);Pre(t-Right);}}voidAft(Treet){if(t!=NULL){Pre(t-Left);Pre(t-Right);printf(%c,t-Element);ABCDEFGH}voidMid(Treet){if(t!=NULL){Pre(t-Left);printf(%c,t-Element);Pre(t-Right);}}}Treecreat(Treet){charc;t=(Tree)malloc(sizeof(structtreenode));if((c=getchar())!='#'){t-Element=c;t-Left=Pre();t-Right=Pre();}elset=NULL;returnt;}intcountnode(TreeT){if(T==NULL)return0;elsereturn1+countnode(T-Left)+countnode(T-Right);}intcountleaf(TreeT){if(T==NULL)return0;elseif(T-Left==NULL&&T-Right==NULL)return1;returncountleaf(T-Left)+countleaf(T-Right);}intcountfull(TreeT){if(T==NULL)return0;elsereturn!(T-Left==NULL||T-Right==NULL)+countfull(T-Left)+countfull(T-Right);}intFind(TreeT,charc;){intfind;if(!find&&T!=NULL){if(T-Element==c){find=1;}else{Find(T-Left,c);Find(T-Right,c);}}returnfind;}voidmain(){TreeT;charc;T=creat(T);printf(前序遍历为:\n);Pre(T);printf(\n);printf(中序遍历为:\n);Mid(T);printf(\n);printf(后序遍历为:\n);Aft(T);printf(\n);printf(请输入要检测的字符:);scanf(%c,&c);getchar();if(Find(T,c)==1)printf(True该字符在树中!\n);elseprintf(False该字符不在树中!\n);printf(树中的节点数为:%d\n,ountnode(T));printf(树中的树叶数为:%d\n,ountleaf(T));printf(树中的满节点数为:%d\n,countfull(T));}
本文标题:二叉树的查找,遍历,(满)节点数,树叶的计算
链接地址:https://www.777doc.com/doc-5090680 .html