求助刚入门的,二叉树的建立及前后层序遍历,出错啊

hshuai987 2012-10-31 07:30:09
#include "stdlib.h"
#include "stdafx.h"
#include<stdio.h>

struct node
{
int data;
struct node * l,*r;
};


struct node *CreateTree()
{
int x;
scanf("%d",&x);
if(x==0) return NULL;
struct node*t;
t=new struct node;
t->data=x;
printf("输入%d的左",x);
t->l=CreateTree();
printf("输入%d的右",x);
t->r=CreateTree();
return t;
}

void printTree(struct node * t) /*前序遍历*/
{
if(t==NULL) return;
printf("%d",t->data );
printTree(t->l);
printTree(t->r);
}


void PostTree(struct node * t)/*后序遍历*/
{
if(t==NULL) return;
printTree(t->l);
printTree(t->r);
printf("%d",t->data );

}


void LevelTree(struct node * t)/*层次遍历*/
{
InitQueue(Q);
if(t==NULL) return;
while(!QueueEmpty(Q) )
{
DeQueue(Q,t);
visite(t->data);
if(t->l) EnQueue(Q,t->l);
if(t->r) EnQueue(Q,t->r);


}
}


int main(int argc, char* argv[])
{
struct node*root;
printf("输入树的根节点\n");
root=CreateTree();
printTree(root);

printf("是前序遍历\n");
PostTree(root);
printf("是后序遍历\n");
return 0;
}
...全文
200 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
dpdp_2012 2012-11-05
  • 打赏
  • 举报
回复
这里的层次遍历只是个算法思想的伪代码,你还当真了
fengleieee 2012-11-05
  • 打赏
  • 举报
回复
你这个程序有点乱 ,你这段程序主要为了干什么,为什么要用到“stdafx.h"这个头文件。这个头文件一般在windows编程中使用(stdafx.h中没有函数库,只是定义了一些环境参数,使得编译出来的程序能在32位的操作系统环境下运行。 )你是新手吧,我看了一下你的程序运行结果。 : 'Q' : undeclared identifier 'QueueEmpty' : undeclared identifier : undeclared identifier : 'visite' : undeclared identifier 'EnQueue' : undeclared identifier 等等之类的。 都是显示没有定义。你先重写定义部分,把EnQueue,visite这类的函数或者变量定义一下。你必须先定义一个函数,系统才能知道有这么一个东西,然后你再实现这个函数,告诉系统这个函数它应该做什么。最后,在你的主函数中直接使用这个函数,明白吗? 编程是慢慢积累的过程,慢慢来吧,多练练慢慢就会有感觉的。
daiyier 2012-11-05
  • 打赏
  • 举报
回复
引用 2 楼 hshuai987 的回复:
后序运行没问题,主要是层序遍历, : 'Q' : undeclared identifier 'QueueEmpty' : undeclared identifier : undeclared identifier : 'visite' : undeclared identifier 'EnQueue' : undeclared identifier 执行……
在你给出来的这段程序中,这些东西你都没定义,没定义的变量是不能拿来使用的。
ljz_zf 2012-11-05
  • 打赏
  • 举报
回复
不错,学到了。。。
hshuai987 2012-10-31
  • 打赏
  • 举报
回复
后序运行没问题,主要是层序遍历,
: 'Q' : undeclared identifier
'QueueEmpty' : undeclared identifier
: undeclared identifier
: 'visite' : undeclared identifier
'EnQueue' : undeclared identifier
执行 cl.exe 时出错.这些队列如何去定义,谢谢
daiyier 2012-10-31
  • 打赏
  • 举报
回复
就你给出的这段程序,后序遍历有问题,你是在递归的调用前序遍历的函数你应该把PostTree函数修改如下:
void PostTree(struct node * t)/*后序遍历*/
{
if(t==NULL) return;
PostTree(t->l);
PostTree(t->r);
printf("%d",t->data );
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧