回 帖 发 新 帖 刷新版面

主题:[讨论]请大家看一眼我的二叉树程序

运行时老是出出现“应用程序错误”就是那个弹出的那个窗口,下面是我的程序:
#include <stdlib.h>
#include <stdio.h>

typedef struct Bitree{
     char   data;
     struct Bitree  * lchild ,  * rchild;
}BiTNode;

  
void  CreateBitree(BiTNode *T)
{    
    char  ch;
    
    scanf("%c",&ch);
    if(ch==' ') T=NULL;
    
    else
       {   
           T=(BiTNode*)malloc(sizeof(BiTNode));
           T->data=ch; //生成根结点
           CreateBitree(T->lchild); //生成左子树
        
           CreateBitree(T->rchild); //生成右子树
        }
    
}
void  preorder(BiTNode *root)
{
          if(root!=NULL){
                   printf("%c ",root->data);
                   preorder(root->lchild);
                   preorder(root->rchild);
           }
}
void  inorder(BiTNode *root)
{
          if(root!=NULL){
                   inorder(root->lchild);
                   printf("%c ",root->data);
                   inorder(root->rchild);
           }
}
void  postorder(BiTNode *root)
{
          if(root!=NULL){
                   postorder(root->lchild);
                   postorder(root->rchild);
                   printf("%c ",root->data);
           }
}
int main(void)
{
    BiTNode    T;
    BiTNode   *p;
    p=&T;
    printf("***************************创建二叉树!****************************\n");
    printf("Please input some data end of the character'@' as the end:\n");
    CreateBitree(p); 

     printf("先序遍历的结果为:\n");
     preorder(p);
     printf("中序遍历的结果为:\n");
     inorder(p);
     printf("后序遍历的结果为:\n");
     postorder(p);
     
      return 0;
}
       

回复列表 (共5个回复)

沙发

呵呵,看来这是一个非常普遍的问题啊。请参见:http://www.programfan.com/club/showbbs.asp?id=166939

板凳

帮你改过拉:

#include <stdlib.h>
#include <stdio.h>

typedef struct Bitree{
     char   data;
     struct Bitree  * lchild ,  * rchild;
}BiTNode;

  
BiTNode*  CreateBitree()
{    BiTNode *T;
    char  ch;
    
    scanf("%c",&ch);
    //getchar();
    if(ch==' ') T=NULL;
    
    else
       {   
           T=(BiTNode*)malloc(sizeof(BiTNode));
           T->data=ch; //生成根结点
           T->lchild=CreateBitree(); //生成左子树
        
           T->rchild=CreateBitree(); //生成右子树
        }
    return T;
    
}
void  preorder(BiTNode *root)
{
          if(root!=NULL){
                   printf("%c ",root->data);
                   preorder(root->lchild);
                   preorder(root->rchild);
           }
}
void  inorder(BiTNode *root)
{
          if(root!=NULL){
                   inorder(root->lchild);
                   printf("%c",root->data);
                   inorder(root->rchild);
           }
}
void  postorder(BiTNode *root)
{
          if(root!=NULL){
                   postorder(root->lchild);
                   postorder(root->rchild);
                   printf("%c ",root->data);
           }
}
int main(void)
{
    BiTNode    *T;
    BiTNode   *p;
  
    printf("***************************创建二叉树!****************************\n");
    printf("Please input some data end of the character'@' as the end:\n");
    T=CreateBitree(); 
    p=T;

     printf("先序遍历的结果为:\n");
     preorder(p);
     printf("中序遍历的结果为:\n");
     inorder(p);
     printf("后序遍历的结果为:\n");
     postorder(p);
     
      return 0;
}
       

3 楼

现在可以了,谢谢指教。

4 楼

CreateBitree()
这个必须要有返回值吗?我的问题出在哪里,请教一下。

5 楼

要有返回值,把头指针返回来,这样其它的调用程序才可以得到地址。

我来回复

您尚未登录,请登录后再回复。点此登录或注册