#include <malloc.h>
#include <stdio.h>
typedef struct  tree 
{ char   data;
 struct  tree  *lchild,  *rchild;
}pre;
pre * create()
{
struct tree *q;
char ch;
scanf("%c",&ch);
scanf("%d",&ch);
while(ch!='&')
{
if(!(q=(struct tree *)malloc(sizeof(struct tree))))return 0;
else{
q->data=ch;
create(q->lchild);
create(q->rchild);
}
}
return q;
}void midoutput(struct tree *q)//中序遍历输出二叉树
{
midoutput(q->lchild);
printf("%c",q->data);
midoutput(q->rchidl);
}void lastoutput(struct tree *q)
{
lastoutput(q->lchild);
lastoutput(q->rchild);
printf("%c",q->data);
}main()
{
pre *head;
head=create();
midoutput(head);
printf("\n");
lastoutput(head);
return 1;
}
主要想实现的功能是:以先序的方法输入一个二叉树后可以以中序和后序遍历的方式输出但是它总是在输入&后不跳出来,我想使它在输入&后就出来执行          midmidoutput(head);
lastoutput(head);
为什么不行呢?谢谢了!!!

解决方案 »

  1.   

    create()函数中多了一个scanf("%d",&ch);
      

  2.   

    pre * create()
    {
    struct tree *q;
    char ch;
    scanf("%c",&ch);
    scanf("%d",&ch);
    while((ch=getchar())!='&')
    {
    if(!(q=(struct tree *)malloc(sizeof(struct tree))))return 0;
    else{
    q->data=ch;
    create(q->lchild);
    create(q->rchild);
    }
    }
    return q;
    }
    这是常用作法.
      

  3.   

    多了一个scanf,第二个scanf应该放到return前面while后
      

  4.   

    那两个scanf有什么用,不是已经有getchar()了吗?
      

  5.   

    #include <malloc.h>
    #include <stdio.h>
    typedef struct  tree 
    { char   data;
     struct  tree  *lchild,  *rchild;
    }pre;
    pre * create()
    {
    struct tree *q;
    char ch;
    scanf("%c",&ch);
    // scanf("%d",&ch);
    while(ch!='&')
    {
    if(!(q=(struct tree *)malloc(sizeof(struct tree))))return 0;
    else{
    q->data=ch;
                    // create(q->lchild);
    // create(q->rchild);

    }
    }
    return q;
    }void midoutput(struct tree *q)//中序遍历输出二叉树
    {
    midoutput(q->lchild);
    printf("%c",q->data);
    midoutput(q->rchild);
    }void lastoutput(struct tree *q)
    {
    lastoutput(q->lchild);
    lastoutput(q->rchild);
    printf("%c",q->data);
    }main()
    {
    pre *head;
    head=create();
    midoutput(head);
    printf("\n");
    lastoutput(head);
    return 1;
    }给你注释调的create不知道是什么,自己看看代码,应该问题出在那里
      

  6.   

    可以把调试好的程序帖上去好吗?
    不胜感激!!!
    原题是这样的:
    使用二元运算符‘+’,‘-’,‘*’,‘/‘的算术表达式可以用"表达式二叉树",表达二叉树特征如下:
    A)每个运算符都有两个孩子,他们要么是操作数,要么是子表达示
    B)叶子结点中所含的是操作数,非叶子结点是所含的是二元运算符
    C)运算符的左右子树表示的是要对其值并作为操作数之一面使用的子表达式
    D)对表达式二叉树进行先序,中序,后序遍历分别得到表达式的前缀,中缀和后缀表示create()是建立一个二叉树啊
      

  7.   

    朋友zhengstar() 的做法不行啊
    你那就没有循环了
      

  8.   

    关键是你给的代码不全,你写的create没有参数,而你里面调用的带参数,我不知道你到底怎么实现的
      

  9.   

    #include <malloc.h>
    #include <stdio.h>
    typedef struct  tree 
    { char   data;
     struct  tree  *lchild,  *rchild;
    }pre;
    pre * create(struct tree *q)//建立一个二叉树
    {

    char ch;
    while((ch=getchar())!='&')
    {
    if(!(q=(struct tree *)malloc(sizeof(struct tree))))return 0;
    else{
    q->data=ch;
    create(q->lchild);
    create(q->rchild);
    }
    }
    return q;
    }void midoutput(struct tree *q)//中序遍历输出二叉树
    {
    midoutput(q->lchild);
    printf("%c",q->data);
    midoutput(q->rchild);
    }void lastoutput(struct tree *q)//后序遍历输出二叉树{
    lastoutput(q->lchild);
    lastoutput(q->rchild);
    printf("%c",q->data);
    }main()
    {
    pre *head;
    create(head);
    midoutput(head);//中序输出
    printf("\n");
    lastoutput(head);//后序输出
    return 1;
    }
    你再看看,这个带参数的create()
      

  10.   

    你仔细分析一下你的程序逻辑,你的本意是想输入一个‘&’结束整个树的建立吗,但你的程序逻辑实际是输入一个‘&’结束一个左子树的建立,然后返回到父节点的进行右子树的建立,然后再向上返,一直循环下去,知道输入n个‘&’后退到根节点后才能结束