#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);
为什么不行呢?谢谢了!!!
#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);
为什么不行呢?谢谢了!!!
解决方案 »
- 请教高人 代码编译没问题但是调试出现异常退出
- vc 关于richedit 中gif播放控件的问题
- ADO中如何批处理SQL
- how to register .ocx files of ActiveX in run time?
- 我打开chm文件时出了这样的错误“不能打开文件:mk:@MSITStore:D:\book\vbapi.chm“怎么办?
- 在NT/2000下如何编程增加用户
- 关于VC里的API的问题,谢谢各位。
- 那位帮忙解决下面帖子问题,再送100分
- 怎样学vc编程
- MFC调用google,百度等搜索引擎,搜索内容
- 高兴呀,当爸爸了,送给大家点利事包
- 如何在响应某个事件之后让我的picture控件中的图画变成另外一幅?
{
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;
}
这是常用作法.
#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不知道是什么,自己看看代码,应该问题出在那里
不胜感激!!!
原题是这样的:
使用二元运算符‘+’,‘-’,‘*’,‘/‘的算术表达式可以用"表达式二叉树",表达二叉树特征如下:
A)每个运算符都有两个孩子,他们要么是操作数,要么是子表达示
B)叶子结点中所含的是操作数,非叶子结点是所含的是二元运算符
C)运算符的左右子树表示的是要对其值并作为操作数之一面使用的子表达式
D)对表达式二叉树进行先序,中序,后序遍历分别得到表达式的前缀,中缀和后缀表示create()是建立一个二叉树啊
你那就没有循环了
#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()