爱问知识人 爱问教育 医院库

通过一个C程序实现先序、中序和后序遍历三种遍历算法。要求:加注析。急啊。

首页

通过一个C程序实现先序、中序和后序遍历三种遍历算法。要求:加注析。急啊。

要在VC2005能运行的啊。要解析。

提交回答

全部答案

    2018-05-21 04:49:02
  •   嘿嘿,上个学期学了数据结构,你这问题我们课本有,打给你吧。#include 
    #include
    typedef struct node         //二叉树结点类型的定义

    char data;
      struct node *lchild; //定义结点的左孩子指针
      struct node *rchild; //定义结点的右孩子指针
    } BinTNode;    BinTNode *CreateBinTree()  //输入二叉树的先序遍历序列,创建二叉链表
    {  
      BinTNode *t;
      char ch;
      ch=getchar();
      if (ch=='0')             //如果读入0,创建空树           
      t=NULL;
      else
      { 
      t=(BinTNode *)malloc(sizeof(BinTNode)); //申请根结点*t空间
      t->data=ch;                                  //将结点数据ch放入跟结点的数据域
      t->lchild=CreateBinTree();       //建左子树
      t->rchild=CreateBinTree();       //建右子树
      }
        return t;
    }void ListBinTree(BinTNode *t)                  //用广义表表示二叉树
    {
      if (t!=NULL)
      {
      printf("%c",t->data);
      if (t->lchild!=NULL||t->rchild!=NULL)
      {
       printf("(");
       ListBinTree(t->lchild);
       if (t->rchild!=NULL)
       printf(",");
       ListBinTree(t->rchild);
       printf(")");
      }
      }
    }void preorder(BinTNode *t)   //对二叉树进行先序遍历
    {
      if(t!=NULL)
      {
       printf("data);
        preorder(t->lchild);
       preorder(t->rchild);
     }
    } void inorder(BinTNode *t) //对二叉树进行中序遍历
    {
      if(t!=NULL)
       {
        inorder(t->lchild);
         printf("data);
        inorder(t->rchild);
      }
    } void postorder(BinTNode *t) //对二叉树进行后序遍历
    {
      if(t!=NULL)
      {
       postorder(t->lchild);
        postorder(t->rchild);
        printf("data);   
      }
    } void main()
    {
      BinTNode *t=NULL;
      printf("请输入先序序列,虚结点用0表示(以图6。
      14二叉树为例): ");
      t=CreateBinTree();
      printf("广义表表示的二叉树的输出: ");
      ListBinTree(t);                          //调用二叉树的广义表表示函数
      printf(" 二叉树的前序遍历结果为: ");
      preorder(t);                             //调用二叉树先序遍历函数
      printf(" 二叉树的中序遍历结果为: ");
      inorder(t);                              //调用二叉树中序遍历函数
      printf(" 二叉树的后序遍历结果为: ");
      postorder(t);                            //调用二叉树后序遍历函数
     printf(" ");  
    }程序运行结果:
    请输入先序序列,虚结点用0表示(以图6。
      14二叉树为例):
    ABD00E00CFG0000(回车)
    广义表表示的二叉树的输出:
    A ( B ( D,E),C ( F (G) ) )
    二叉树的前序遍历结果为:
    A B D E C F G
    二叉树的中序遍历结果为:
    D B E A G F C
    二叉树的后序遍历结果为:
    D E B G F C A。
      

    你***

    2018-05-21 04:49:02

类似问题

换一换

相关推荐

正在加载...
最新资料 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):