| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 326 人关注过本帖
标题:为什么这个树的层次遍历不对呢?想了半天了。。
只看楼主 加入收藏
adgvcxz
Rank: 2
等 级:论坛游民
帖 子:23
专家分:52
注 册:2009-6-26
结帖率:83.33%
收藏
 问题点数:0 回复次数:1 
为什么这个树的层次遍历不对呢?想了半天了。。
程序代码:
#include <stdio.h>
#include <malloc.h>
struct tree{
    char data;
    struct tree *lchild,*rchild;
};
struct tree *create(struct tree *tree){
    char ch;
    scanf(" %c",&ch);
    if(ch=='#')
        tree=NULL;
    else{   
        tree=(struct tree *)malloc(sizeof(struct tree));
        tree->data=ch;
        tree->lchild=create(tree->lchild);
        tree->rchild=create(tree->rchild);
    }
    return tree;
}
void levelorder(struct tree *tree){
    struct tree *a[100];
    int rear=0,front=0;
    if(tree){
        a[rear++]=tree;
        while(front!=rear){
            printf("%2c",a[front]->data);
            if(a[front]->lchild)
                a[rear++]=tree->lchild;
            if(a[front]->rchild)
                a[rear++]=tree->rchild;
            front++;
        }
    }
}
void main(){
    struct tree *t;
    printf("输入节点值(按照先序遍历输入)");
    t=create(t);
    printf("按层遍历(队列):");
    levelorder(t);
}
大家帮我看看吧  谢谢。
2012-03-12 14:19
adgvcxz
Rank: 2
等 级:论坛游民
帖 子:23
专家分:52
注 册:2009-6-26
收藏
得分:0 
哎,没有帮我看看吗。。
2012-03-12 17:23
快速回复:为什么这个树的层次遍历不对呢?想了半天了。。
数据加载中...
 
   



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

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