| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 547 人关注过本帖
标题:急!!!我的层次遍历二叉树错在哪???
只看楼主 加入收藏
lizjohn
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2010-10-28
结帖率:70.59%
收藏
已结贴  问题点数:20 回复次数:6 
急!!!我的层次遍历二叉树错在哪???
我的层次遍历二叉树错在哪
#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-19 08:32
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:20 
#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;
};                           


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

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

void enqueue( struct link*q, struct node*e)                      //入队
{
    struct line*p;
    p=(struct line*)malloc(sizeof(struct line));
    p->ch=e;
    p->next=NULL;
    if( q->front == 0 )
    {
        q->rear = q->front = p;
    }
    else
    {
        q->rear->next = p;
        q->rear = p;
    }
}

struct node* dequeue(struct link*q)                         //出队
{
    struct node*e;
    struct line*p;
    p = q->front;
    e = p->ch;
    q->front = p->next;
    printf(" %c", e->data);
    return e;
}


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

void show_tree(struct node*T)
{
    if(T)
    {
        printf(" %c", T->data);
        show_tree(T->lchild);
        show_tree(T->rchild);
    }
}
void main()
{
    struct link*q = 0;/*定义队列指针*/
    struct node*p ;//= (struct node*)malloc(sizeof(struct node));
    struct node*T = 0;
    T = createbitree(T);
    show_tree(T);
    printf("\n");
    q = initqueue(q);
   
    printf("按层次遍历树中结点其输出顺序为: \n");
    enqueue(q,T);
    while(!queueempty(*q))
    {
        p = dequeue(q);
        if(p->lchild!=NULL)
            enqueue(q,p->lchild);
        if(p->rchild!=NULL)
            enqueue(q,p->rchild);
    }
    printf("\n");
}
2010-11-19 13:23
lizjohn
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2010-10-28
收藏
得分:0 
很感谢你的解答,等这么久第一个解答的人,好感动!!!
但是好像还是没对,这下连建树函数都执行不准确了。
请在帮我看一下,顺便问几个问题:
#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;
};                           


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

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

void enqueue( struct link*q, struct node*e)                      //入队
{
    struct line*p;
    p=(struct line*)malloc(sizeof(struct line));
    p->ch=e;
    p->next=NULL;
    if( q->front == 0 )
    {
        q->rear = q->front = p;
    }
    else
    {
        q->rear->next = p;
        q->rear = p;
    }
}

struct node* dequeue(struct link*q)                         //出队
{
    struct node*e;
    struct line*p;
    p = q->front;
    e = p->ch;
    q->front = p->next;
    printf(" %c", e->data);
    return e;
}


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

void show_tree(struct node*T)
{
    if(T)
    {
        printf(" %c", T->data);
        show_tree(T->lchild);
        show_tree(T->rchild);
    }
}
void main()
{
    struct link*q = 0;/*定义队列指针*/   这里等于0是干什么?可以这样表达吗
    struct node*p ;//= (struct node*)malloc(sizeof(struct node));
    struct node*T = 0;还有这里
    T = createbitree(T);
    show_tree(T);
    printf("\n");
    q = initqueue(q);
   
    printf("按层次遍历树中结点其输出顺序为: \n");
    enqueue(q,T);
    while(!queueempty(*q))
    {
        p = dequeue(q);
        if(p->lchild!=NULL)
            enqueue(q,p->lchild);
        if(p->rchild!=NULL)
            enqueue(q,p->rchild);
    }
    printf("\n");
}
2010-11-19 16:54
lizjohn
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2010-10-28
收藏
得分:0 
回复 2楼 寒风中的细雨
刚才的问题,好像和输入有关系
2010-11-19 17:01
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
本来是不想截图的
图片附件: 游客没有浏览图片的权限,请 登录注册
2010-11-19 18:59
Imtheone
Rank: 2
等 级:论坛游民
帖 子:78
专家分:34
注 册:2010-4-23
收藏
得分:0 
路过看看
2010-11-19 22:00
aiyinsitan
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:183
专家分:177
注 册:2010-4-22
收藏
得分:0 
回复 5楼 寒风中的细雨
为什么我的运行也是没结果的啊??
  难道是编译器的问题
2010-11-19 22:16
快速回复:急!!!我的层次遍历二叉树错在哪???
数据加载中...
 
   



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

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