数据构造程序设计报告
学院:
班级:
学号:  XX:
实验名称:二叉树的建立与遍历
一、实验目的:
1.掌握二叉树的二叉链表存储构造;
2.掌握二叉树创立方法;
3.掌握二叉树的先序、中序、后序的递归实现方法。
二、实验内容和要求:
创立二叉树,分别对该二叉树进展先序、中序、后序遍历,并输出遍历结果。
三、叉树的建立与遍历代码如下:
#include <stdio.h>
#include <malloc.h>
struct tnode//结点构造体
{
    char data;
    struct tnode *lchild,*rchild;
};
typedef struct tnode TNODE;
TNODE *creat(void)
{
    TNODE *root,*p;
    TNODE *queue[50];
      int front=0,rear=-1,counter=0;//初始队列中需要的变量front、rear和计数器counter
    char ch;
    printf("建立二叉树,请输入结点:〔#表示虚节点,!表示完毕)\n");
    ch=getchar();
    while(ch!='!')
    {
            if(ch!='#')
              {
                p=(TNODE *)malloc(sizeof(TNODE));
                  p->data=ch;
二叉树的遍历及应用实验报告                  p->lchild=NULL;
                  p->rchild=NULL;
                rear++;
                queue[rear]=p;//把非#的元素入队
                if(rear==0)//如果是第一个元素,那么作为根节点
                {
                    root=p;
                    counter++;
                }
                else
                {
                    if(counter%2==1)//奇数时与其双亲的左子树连接
                    {
                        queue[front]->lchild=p;
                    }
                    if(counter%2==0)//偶数时与其双亲的右子树连接
                    {
                        queue[front]->rchild=p;