| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1509 人关注过本帖
标题:关于二叉树的编程错误,大家帮忙调试看看
只看楼主 加入收藏
天之蓝
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-3-19
收藏
 问题点数:0 回复次数:11 
关于二叉树的编程错误,大家帮忙调试看看
已知字符序列为ABD000CE0G00FH00J00\0各字符是按先根序列排列的其中字符D后的00的意义是字符D的结点无左右子树。同理,字符E后0的意义是字符E的结点无左子树但有右子树G,编程建立二叉树(双重链),并按中序遍历输出二叉树
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{   char data;
    struct node *llink,*rlink;
}TNODE;
int i=0;
TNODE *Save(char *m[],TNODE *t)
{
    if(m[i]!='\0'){
        if(m[i]!=0){
            t=(TNODE *)malloc(sizeof(TNODE));
            t->data=m[i];
            Save(m[++i],t->llink);
            Save(m[++i],t->rlink);
         }
        else  t=NULL;
      }
    return(t);
}
void inorder(TNODE *h)
{    TNODE *s[30];
     int top=0;
     do
     {  while(h!=NULL)
        {   s[top++]=h;
            h=h->llink;
            }
         h=s[--top];printf("%c",h->data);
         h=h->rlink;
        }while((top!=0)||(h!=NULL));
        printf("\n");
     }
void main(){
    char *m[20];
    printf("please input a string:\n");
    scanf("%s",m);
    TNODE *t=NULL;
    TNODE *k=NULL;
    k=Save(m,t);
    inorder(k);
    getch();
}
警告 no1.c 13: 不可移动的指针(地址常数)赋值在 Save 函数中
警告 no1.c 14: 指针转换后指向其它类型在 Save 函数中
警告 no1.c 15: 指针转换后指向其它类型在 Save 函数中
错误 no1.c 38: typedef 符号使用不当在 main 函数中
错误 no1.c 38: 未定义的符号't'在 main 函数中
错误 no1.c 39: typedef 符号使用不当在 main 函数中
错误 no1.c 39: 未定义的符号'k'在 main 函数中
警告 no1.c 40: 不可移动的指针(地址常数)转换在 main 函数中
警告 no1.c 40: 不可移动的指针(地址常数)赋值在 main 函数中
警告 no1.c 41: 不可移动的指针(地址常数)转换在 main 函数中

[[it] 本帖最后由 天之蓝 于 2008-10-30 12:41 编辑 [/it]]
搜索更多相关主题的帖子: 二叉树 调试 
2008-10-30 12:39
风居住的街道
Rank: 1
等 级:新手上路
帖 子:374
专家分:0
注 册:2008-10-24
收藏
得分:0 
你那个字符串是什么意思啊……有点看不懂………………
2008-10-30 13:13
天之蓝
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-3-19
收藏
得分:0 
就是输入ABD000CE0G00FH00J00
2008-10-30 13:16
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
你的 TNODE *Save(char *m,TNODE *t) 函数有点问题,试试这个
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{   char data;
    struct node *llink,*rlink;
}TNODE;

int i=0;

TNODE *Save(char *m,TNODE *t)
{
    if(*(m+i) != '\0'){
        if(*(m+i) != 0){
            t=(TNODE *)malloc(sizeof(TNODE));
            t->data=m[i];
            Save((m+(++i)) ,t->llink);
            Save((m+(++i)) ,t->rlink);
         }
        else  t=NULL;
    }
    return(t);
}

void inorder(TNODE *h)
{    TNODE *s[30];
     int top=0;
     do
     {  while(h!=NULL)
        {   s[top++]=h;
            h=h->llink;
            }
         h=s[--top];printf("%c",h->data);
         h=h->rlink;
        }while((top!=0)||(h!=NULL));
        printf("\n");
     }

int main()
{
    char m[20] = "";
    printf("please input a string:\n");
    scanf("%s",m);
    TNODE *t=NULL;
    TNODE *k=NULL;
    k=Save(m,t);
    inorder(k);
    getchar();
    return 0;
}

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2008-10-30 13:16
风居住的街道
Rank: 1
等 级:新手上路
帖 子:374
专家分:0
注 册:2008-10-24
收藏
得分:0 
程序代码:
               A
         B           C
      D           E     F
                   G   H J


是这个意思么?
2008-10-30 13:17
天之蓝
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-3-19
收藏
得分:0 
还是出现这几个错误
错误 no1.c 38: typedef 符号使用不当在 main 函数中
错误 no1.c 38: 未定义的符号't'在 main 函数中
错误 no1.c 39: typedef 符号使用不当在 main 函数中
错误 no1.c 39: 未定义的符号'k'在 main 函数中
2008-10-30 13:22
天之蓝
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-3-19
收藏
得分:0 
是的,就是那个,要求输出中序遍历
2008-10-30 13:25
风居住的街道
Rank: 1
等 级:新手上路
帖 子:374
专家分:0
注 册:2008-10-24
收藏
得分:0 
这个是我写的~~~


程序代码:
#include <stdio.h>
#include <stdlib.h>

typedef struct TNODE
{
    int data;
    struct TNODE *lchild, *rchild;
} *BTREE;

char *create_tree(char *str, BTREE *root)
{
    if (root == NULL)
        return str;
    if (str[0] != '0')
    {
        *root = malloc(sizeof(struct TNODE));
        if (*root == NULL)
            return str;
        (*root)->data = *str++;
        str = create_tree(str, &(*root)->lchild);
        str = create_tree(str, &(*root)->rchild);
        return str;
    }
    *root = NULL;
    return str + 1;
}

void destroy_tree(BTREE root)
{
    if (root != NULL)
    {
        destroy_tree(root->lchild);
        destroy_tree(root->rchild);
        free(root);
    }
}

void inorder_trace(BTREE tree)
{
    if (tree != NULL)
    {
        inorder_trace(tree->lchild);
        putchar(tree->data);
        inorder_trace(tree->rchild);
    }
}

int main(void)
{
    BTREE tree;
    char str[1000];
    while (scanf("%1000s", str) == 1)
    {
        if (*create_tree(str, &tree) == '\0')
        {
            inorder_trace(tree);
            putchar('\n');
        }
        else
            printf("create_tree error\n");
        destroy_tree(tree);
    }
    return 0;
}
2008-10-30 13:55
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
回复 6# 的帖子
我使用VC6
你使用什么编译器?

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2008-10-30 17:34
天之蓝
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-3-19
收藏
得分:0 
wintc1.91
2008-10-30 18:07
快速回复:关于二叉树的编程错误,大家帮忙调试看看
数据加载中...
 
   



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

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