标题:二叉树的建议与遍历 出错了
取消只看楼主
midle110
Rank: 2
等 级:论坛游民
帖 子:16
专家分:38
注 册:2010-12-15
结帖率:33.33%
已结贴  问题点数:20 回复次数:1 
二叉树的建议与遍历 出错了
二叉树的建议与遍历   出错了 (遍历出错了)
直接上代码吧
#include "malloc.h"
#include "stdio.h"

typedef struct trees
{
    char data;
    struct trees *lchild,*rchild;
}treelist,*treelink;

void maketree(treelink &root);
void display(treelink root);

int main(int argc, char* argv[])
{
    treelist *root;
    root = (treelink)malloc( sizeof(treelist));
    maketree(root);
    display(root);
    return 0;
}

void maketree(treelink &root)
{

    char ch;
    scanf("%c",&ch);
    if (ch == '#')
    {
        root = NULL;
    }
    else
    {
        root = (treelink)malloc(sizeof(treelist));
        root->data = ch;
        maketree(root->lchild);
        maketree(root->rchild);
    }
}


void display(treelink root)
{
    while (root != NULL)
    {
        printf("%c  ",root->data);
        display(root->lchild);
        display(root->rchild);
    }
}
搜索更多相关主题的帖子: display include return 二叉树 
2011-11-16 11:27
midle110
Rank: 2
等 级:论坛游民
帖 子:16
专家分:38
注 册:2010-12-15
得分:0 
程序代码:
#include "malloc.h"
#include "stdio.h"

typedef struct trees
{
    char data;
    struct trees *lchild,*rchild;
}treelist,*treelink;

void maketree(treelink &root);
void display(treelink root);

int main(int argc, char* argv[])
{
    treelist *root;
    root = (treelink)malloc( sizeof(treelist));
    maketree(root);
    display(root);
    return 0;
}

void maketree(treelink &root)
{

    char ch;
    scanf("%c",&ch);
    if (ch == '#')
    {
        root = NULL;
    }
    else
    {
        root = (treelink)malloc(sizeof(treelist));
        root->data = ch;
        maketree(root->lchild);
        maketree(root->rchild);
    }
}


void display(treelink root)
{
    if (root != NULL)   //不是while   自己的代码看了好半天才知道哪里错了  一开始还以为是建树错了,没想到是遍历错了!!!
    {
        printf("%c  ",root->data);
        display(root->lchild);
        display(root->rchild);
    }
} 
算了   我自己查出来了 , 坑爹的错误
2011-11-16 12:06



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-355383-1-1.html




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

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