zh_coder 2015-05-16 17:03 采纳率: 0%
浏览 1698

各位前辈帮我看看二叉树层序遍历错在哪里了

#include
#include

struct Nodep /*树结点类型*/
{
char data;
struct Nodep * lired; //左孩子
struct Nodep * rired; //右孩子
};

struct Queue //队列结点
{
Nodep * ch;
struct Queue * next;
};

struct BQueue //队列指针
{
Queue * rear; //队尾指针
Queue * front; //对头指针
};

void input(BQueue * Head) /*初始化队列*/
{
Head->front=Head->rear=(Queue*)malloc(sizeof(Queue));
Head->front->next=NULL;

}

void output(BQueue * Head,Nodep * root) //入队列
{
Queue * s;
s=(Queue*)malloc(sizeof(Queue));
s->ch=root;
s->next=NULL;

Head->rear->next=s;
Head->rear=s;
printf("入队列成功");

}
void Queuebtr(BQueue * Head, Nodep * root) //出队列
{
char ch; //用于存放出队列的元素
Queue * s; //临时变量;
root=s->ch;
s=Head->front->next;
ch=root->data;
Head->front->next=s->next;
if(Head->front==Head->rear)
Head->rear=Head->front;
free(s);
printf("%c",ch);

}

Nodep * gouzao()
{
char ch;
Nodep * root;
scanf("%c",&ch);
if(ch=='0')
root=NULL;
else
{
root=(Nodep *)malloc(sizeof(Nodep));
root->data=ch;
root->lired=gouzao();
root->rired=gouzao();
}
return root;

}

void cengxu(Nodep * root)
{
BQueue * head;
Nodep * roots;
input(head); //初始化队列
roots=root;
output(head,roots); //进队列和根节点

while(head->front!=NULL)
{
Queuebtr(head,roots); //出根节点
if(roots->lired!=NULL)
{
output(head,root->lired); //进左
}
if(roots->rired!=NULL)
{
output(head,root->rired); //进右
}

}
printf("\n");

}

void main()
{
Nodep * root;
root=gouzao();
cengxu(root);
}

  • 写回答

2条回答 默认 最新

  • zh_coder 2015-05-16 17:04
    关注

    不知道为什么能运行 但是结果出错 郁闷死

    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突