《数据结构》树的基本操作
实验四
课程名称:完成日期:姓名:学号:指导教师:实验名称:实验序号:实验成绩:一、实验目的及要求
二、实验环境、实验内容
求出它的深度。
、调试过程及实验结果
五、总结、附录(源程序清单)#include
#include
typedef struct CSNode
char data;
struct CSNode *firstchild, *nextsibling;
CSNode, *CSTree;
void createtree CSTree &T //以二叉树创建树
char ch;
ch getchar ;
if ch ' ' T NULL;
else
if ! T CSNode * malloc sizeof CSNode printf "分配空间出错!" ;
T- data ch;
createtree T- firstchild ;
createtree T- nextsibling ; void visit CSTree T //遍历函数,输出结点
if T
printf "%c",T- data ;
else
printf "树不存在,输出错误!" ; void preroot CSTree T //先根输出
if T
visit T ;
preroot T- firstchild ;
preroot T- nextsibling ; void postroot CSTree T //后根输出相当于二叉树中序遍历
if T
postroot T- firstchild ;
visit T ;
postroot T- nextsibling ; int n 0;
int countleaf CSTree T if T! NULL countleaf T- firstchild ;
if T- firstchild NULL n++;
countleaf T- nextsibling ; return n; int depth CSTree T int firstdepth,nextdepth;
if !T return 0;
else
firstdepth depth T- firstchild ;
nextdepth depth T- nextsibling ; return firstdepth+1 nextdepth?firstdepth+1:nextdepth; int main CSTree T; printf "请输入树的结点,以二叉树的格式创建,空格表示无左右孩子:\n" ;
createtree T ;
printf "\n先根输出树的结点:\n" ;
preroot T ;
printf "\n后根输出树的结点:\n" ;
postroot T ;
printf "\n树的深度是depth %d",depth T ;
printf "\n树的叶子数目是leaf %d\n",countleaf T ;
while 1 ;
return 0; 1