数据结构上机实习报告2.docVIP

  • 2
  • 0
  • 约2.25千字
  • 约 7页
  • 2018-02-06 发布于河南
  • 举报
数据结构上机实习报告2

数据结构上机实习报告 ——二叉树的遍历 报 告 人:邓凯 班 级:计算机86 学 号提交时间:2009、11、19 上机实习题目 建立二叉树,并对其进行先序、中序、后序三种不同的遍历,并测试以下数据 (一)、ab##c### (二)、abc##de#g##f### (三)、abc#de##f##i##g#hj### 三组数据。 二、相关知识或技术(对应DS部分) C语言,C++的一些基本知识,树的建立、遍历以及应用之类的知识。 算法及数据结构设计(算法设计) 先序遍历 void PreOrder(BinTree root)// { if(root!=NULL) { printf(%c,root-data); PreOrder(root-lchild); PreOrder(root-rchild); } } 中序遍历 void I nOrder(BinTree root)/ { if(root!=NULL) { PreOrder(root-lchild); printf(%c,root-data); PreOrder(root-rchild); } }/ 后序遍历 void PostOrder(BinTree root)/ { if(root!=NULL) { PreOrder(root-lchild); PreOrder(root-rchild); } printf(%c,root-data); } 四、Microsoft vs6.0 五、源程序(带注释或说明)、运行结果(数据或屏幕显示、结果分析、讨论T(n)) #includestdio.h #includestdlib.h typedef struct BNode { char data; struct BNode *lchild; struct BNode *rchild; }BTNode; typedef BTNode *BinTree; void CreateBinTree(BinTree *root)//以先序来建立二叉树 { char ch; ch=getchar(); if(ch== )//空格 *root=NULL; //建立空二叉树 else { *root=(BTNode*)malloc(sizeof(BTNode));//开辟空间,生成节点 (*root)-data=ch; CreateBinTree(((*root)-lchild)); CreateBinTree(((*root)-rchild)); } } void PreOrder(BinTree root)//先序遍历 { if(root!=NULL) { printf(%c,root-data); PreOrder(root-lchild); PreOrder(root-rchild); } } void InOrder(BinTree root)//中序遍历 { if(root!=NULL) { PreOrder(root-lchild); printf(%c,root-data); PreOrder(root-rchild); } } void PostOrder(BinTree root)//后序遍历 { if(root!=NULL) { PreOrder(root-lchild); PreOrder(root-rchild); } printf(%c,root-data); } void main() { BinTree root; CreateBinTree(root); printf(先序遍历:); PreOrder(root); printf(\n); printf(中序遍历:); InOrder(root); printf(\n); printf(后序遍历:); PostOrder(root); printf(\n); } 输入ab##c### 运行结果 输入:abc##de#g##f### 运行结果: 输入:abc#de##f##i##g#hj### 运行结果: 六、上机总结 课上的速度飞快,还没来得及看书,一章就过去了。现在听课更还是像以前一样,云里雾里,都快成仙了,不过有些时候还是能听懂,但总也听不完全,因此做这样的实验真可谓是难啊,先得看上半天的书,然后再在网上看有没有类似的程序,忙得不可开交,好不容易找到个,编辑器通不过,只是一个错误却半天也找不到,哎,何苦呢,与其如此还不如自己写,浪费时间,却什

文档评论(0)

1亿VIP精品文档

相关文档