当前位置:文档之家› 数据结构实验二叉树的遍历

数据结构实验二叉树的遍历

南昌大学实验报告
学生姓名:李木子学号:8000113146 专业班级:软工133 实验类型:□验证□综合□设计□创新实验日期:实验成绩:
一、实验项目名称
二叉树的遍历
二、实验目的
学会链式二叉树的结构体定义,创建与前序中序后序遍历
三、实验基本原理
四、主要仪器设备及耗材
电脑,VC6.0
五、实验步骤
/**************************************/
/* 链式二叉树的创建与遍历 */
/**************************************/
/**************************************/
/* 链式二叉树的结构体定义 */
/**************************************/
#include<stdio.h>
#include<malloc.h>
typedef char datatype ;
typedef struct BinTreeNode{
datatype data ;
struct BinTreeNode *lchild ;
struct BinTreeNode *rchild ;
} BinTreeNode ;
/**************************************/
/* 链式二叉树函数声明 */
/**************************************/
BinTreeNode * CreateTree(void);
void PreOrder( BinTreeNode * t );
void InOrder( BinTreeNode * t );
void PostOrder( BinTreeNode * t );
/**************************************/
/* 链式二叉树创建函数 */
/**************************************/
BinTreeNode * CreateTree(void)
{
char ch ;
BinTreeNode * t ;
ch=getchar();
if(ch=='#')
t=NULL;
else if( ch=='\n');
else
{
t=( BinTreeNode*)malloc(sizeof( BinTreeNode ));
t->data =ch ;
t->lchild=CreateTree();
t->rchild=CreateTree();
}
return t ;
}
/**************************************/
/* 链式二叉树递归前序遍历函数 */
/**************************************/
void PreOrder( BinTreeNode * t )
{
if(t)
{
printf("%c\t",t->data);
PreOrder( t->lchild );
PreOrder( t->rchild );
}
}
/**************************************/ /* 链式二叉树递归中序遍历函数 */
/**************************************/ void InOrder( BinTreeNode * t )
{
if(t)
{
InOrder( t->lchild );
printf("%c\t",t->data);
InOrder( t->rchild );
}
}
/**************************************/ /* 链式二叉树递归后序遍历函数 */
/**************************************/ void PostOrder( BinTreeNode * t )
{
if(t)
{
PostOrder( t->lchild );
PostOrder( t->rchild );
printf("%c\t",t->data);
}
}
/**************************************/ /* 主函数 */
/**************************************/ int main()
BinTreeNode * t ;
printf("创建二叉树\n");
t=CreateTree();
printf("前序遍历二叉树\n");
PreOrder( t );
printf("\n");
printf("中序遍历二叉树\n");
InOrder( t );
printf("\n");
printf("后序遍历二叉树\n");
PostOrder( t );
printf("\n");
return0;
}
六、实验数据及处理结果
七、思考讨论题或体会或对改进实验的认识
八、参考资料
[1]《数据结构(c语言版)(第三版)》,李云清,人民邮电出版社
[2]《C语言程序设计》,苏小红,高等教育出版社。

相关主题