注册 登录
编程论坛 数据结构与算法

在vc6.0为什么这棵树运行不了了??

千马弈 发布于 2012-04-18 19:32, 343 次点击
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#define OK 1
#define ERROR 0
#define  OVERFLOW 1

typedef int Status;
typedef char ElemType;

typedef struct BiTNode{
    ElemType data;
    struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;


Status CreateBiTree(BiTree T)
{
    char ch;
    scanf("%c",&ch);
    if(ch==' ')T=NULL;
    else
    {
    T=(BiTNode*)malloc(sizeof(BiTNode));
    if(!T)exit (OVERFLOW);
   
    T->data=ch;
 CreateBiTree(T->lchild);
 CreateBiTree(T->rchild);
    }
    return OK;
}



Status PreOrderTraverse(BiTree T)
{if(T)
{
    printf("%c",T->data);

PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
return OK;
}


Status DestroyBiTree(BiTree T)
{if(T)
{
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
free(T);

}
return OK;
}

void main()
{
BiTree T;
T=(BiTNode*)malloc(sizeof(BiTNode));
CreateBiTree(T);
PreOrderTraverse(T);
DestroyBiTree(T);
}
2 回复
#2
寒风中的细雨2012-04-18 21:08
程序代码:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#define OK 1
#define ERROR 0
#define  OVERFLOW 1

typedef int Status;
typedef char ElemType;

typedef struct BiTNode{
     ElemType data;
     struct BiTNode *lchild,*rchild;

 }BiTNode,*BiTree;


BiTree CreateBiTree(BiTree T)

 {
     char ch;
     scanf("%c",&ch);
     fflush(stdin);
     if(ch==' ')
     {
         T=NULL;
     }
     else
     {
         T=(BiTNode*)malloc(sizeof(BiTNode));
         if(!T)exit (OVERFLOW);
   
         T->data=ch;
         T->lchild = CreateBiTree(T->lchild);
         T->rchild = CreateBiTree(T->rchild);
     }
     return T;

 }


Status PreOrderTraverse(BiTree T)
{
    if(T)
    {
        printf("%c",T->data);

        PreOrderTraverse(T->lchild);
        PreOrderTraverse(T->rchild);
    }

    return OK;
}


Status DestroyBiTree(BiTree T)
{
    if(T)
    {
        PreOrderTraverse(T->lchild);
        PreOrderTraverse(T->rchild);
        free(T);
    }

    return OK;
}

void main()
{
    BiTree T;
//    T=(BiTNode*)malloc(sizeof(BiTNode));
    T = CreateBiTree(T);
    PreOrderTraverse(T);
    DestroyBiTree(T);
}
#3
寒风中的细雨2012-04-18 21:22
要是你的main函数不做修改   

 CreateBiTree(BiTree T)中就要为左右孩子分配空间而不是为结点自身
1