| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 375 人关注过本帖
标题:我的层次遍历二叉树错在哪
只看楼主 加入收藏
lizjohn
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2010-10-28
结帖率:70.59%
收藏
已结贴  问题点数:20 回复次数:1 
我的层次遍历二叉树错在哪
#include<stdio.h>
#include<stdlib.h>
struct node                                    //树结构体
{
    char data;
    struct node *lchild,*rchild;
};                        
struct line                                    //队结构体
{
    struct node*ch;
    struct line *next;
};                  
struct link                                         //队首,队尾
{
    struct line *front;
    struct line *rear;
   
};                           


void createbitree(struct node*T)                     //建树     
{
    char ch;
    scanf("%c",&ch);
    if(ch==' ')
        T=NULL;
    else
    {
        if(!(T=(struct node*)malloc(sizeof(struct node))))
            exit(0);
        T->data=ch;
        createbitree(T->lchild);
        createbitree(T->rchild);
    }
   
}

   
void initqueue( struct link*q)                              //初始化队  
{
    q->front=q->rear=(struct line*)malloc(sizeof(struct line));
    if(!q->front)
        exit(0);
    q->front->next=NULL;

   
   
}


void enqueue( struct link*q, struct node*e)                      //入队
{
    struct line*p;
    p=(struct line*)malloc(sizeof(struct line));
    p->ch=e;
    p->next=NULL;
    q->rear->next=p;
    q->rear=p;
}
void dequeue(struct link*q,struct node*e)                         //出队
{
   
    char data;
    struct line*p;
    p=q->front->next;
    e=p->ch;
data=e->data;
    q->front->next=p->next;
    if(q->rear==p)
        q->rear=q->front;
    free(q);
    printf("%c ",data);
}


int queueempty(struct link q)                           //判断队是否为空
{
    if(q.front->next==NULL)
        return 1;
    else return 0;
}


void main()
{
 struct link*q;
struct node*p;
 truct node*T;
 createbitree(T);
 initqueue(q);
 p=T;
enqueue(q,p);
while(queueempty(*q)!=1)
{
dequeue(q,p);
if(p->lchild!=NULL)
enqueue(q,p->lchild);
if(p->rchild!=NULL)
enqueue(q,p->rchild);
}
printf("\n");

 printf("按层次遍历树中结点其输出顺序为: \n");
 
}
语法上已经没问题,但不知算法上有什么错,请高手帮看一下
搜索更多相关主题的帖子: 二叉树 遍历 
2010-11-18 21:58
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:20 
这个 我有贴错吗?
2010-11-19 13:28
快速回复:我的层次遍历二叉树错在哪
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.010894 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved