数据结构树的基本操作_《数据结构》树的基本操作.doc

《数据结构》树的基本操作

实验四

课程名称:完成日期:姓名:学号:指导教师:实验名称:实验序号:实验成绩:一、实验目的及要求

二、实验环境、实验内容

求出它的深度。

、调试过程及实验结果

五、总结、附录(源程序清单)#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值