二叉树的遍历学习心得数据结构程序设计报告学院:班级:学号:姓名:实验名称:二叉树的建立与遍历一、实验目的:1
掌握二叉树的二叉链表存储结构;2
掌握二叉树创建方法;3
掌握二叉树的先序、中序、后序的递归实现方法
二、实验内容和要求:创建二叉树,分别对该二叉树进行先序、中序、后序遍历,并输出遍历结果
三、叉树的建立与遍历代码如下:includeXincludestructtnode//结点结构体{};typedefstructtnodetnode;tnode*creat(void){tnode*root,*p;tnode*queue[50];chardata;structtnode*lchild,*rchild;intfront=0,rear=-1,counter=0;//初始队列中需要的变量front、rear和计数器countercharch;printf("建立二叉树,请输入结点:(X表示虚节点,
表示结束)\n");ch=getchar;while(ch
'){if(ch
='X'){p=(tnode*)malloc(sizeof(tnode));p->data=ch;p->lchild=null;p->rchild=null;rear++;queue[rear]=p;//把非X的元素入队if(rear==0)//如果是第一个元素,则作为根节点{}else{if(counter%2==1)//奇数时与其双亲的左子树连接{}第1页共4页if(counter%2==0)//偶数时与其双亲的右子树连接{queue[front]->rchild=p;queue[front]->lchild=p;root=p;counter++;}}}}front++;counter++;else//为X时,计数,但不连接结点{if(counter%2==0)front++;counte