主题:[讨论]急!急!急!构造二叉树链式存储出现错误!!!
我用TC2.0编译、运行。在运行时出现错误:Stack overflow!代码如下;
请高手们指教,谢谢!
#include<stdio.h>
#include<alloc.h>
typedef struct TreeNode
{
char data;
struct TreeNode *lchild,*rchild;
}TreeNode,*BitTree;
BitTree CreateBitTree() /* 构造链式二叉树 */
{
char ch;
int i;
BitTree T;
scanf("%[^\n]",&ch);
printf("success0\n");
/* for(i=0; i < 5; i++)
printf("%c",ch);*/
if(ch == ' ') T = NULL;
/* printf("success0\n");*/
else
{
if(!(T=(TreeNode *)malloc(sizeof(TreeNode)))) exit(0);
T -> data = ch;
printf("success1\n");
CreateBitTree(T->lchild);
printf("success2\n");
CreateBitTree(T->rchild);
printf("success3\n");
/*if(!(T=(TreeNode *)malloc(sizeof(TreeNode)))) return 0; */
}
return T;
}
void VisiteBitTree(BitTree T) /* 输出构造好的链式二叉树 */
{
if( T -> data != ' ')
{
printf("%c",T -> data);
/* printf("\n00\n"); */
if(T -> lchild != NULL)
{
VisiteBitTree(T -> lchild);
/* printf("11\n"); */
}
if(T -> rchild != NULL)
{
VisiteBitTree(T -> rchild);
/*printf("22\n");*/
}
}
}
void main()
{
BitTree T = CreateBitTree();
clrscr();
printf("\n\n");
VisiteBitTree(T);
printf("\n");
}
请高手们指教,谢谢!
#include<stdio.h>
#include<alloc.h>
typedef struct TreeNode
{
char data;
struct TreeNode *lchild,*rchild;
}TreeNode,*BitTree;
BitTree CreateBitTree() /* 构造链式二叉树 */
{
char ch;
int i;
BitTree T;
scanf("%[^\n]",&ch);
printf("success0\n");
/* for(i=0; i < 5; i++)
printf("%c",ch);*/
if(ch == ' ') T = NULL;
/* printf("success0\n");*/
else
{
if(!(T=(TreeNode *)malloc(sizeof(TreeNode)))) exit(0);
T -> data = ch;
printf("success1\n");
CreateBitTree(T->lchild);
printf("success2\n");
CreateBitTree(T->rchild);
printf("success3\n");
/*if(!(T=(TreeNode *)malloc(sizeof(TreeNode)))) return 0; */
}
return T;
}
void VisiteBitTree(BitTree T) /* 输出构造好的链式二叉树 */
{
if( T -> data != ' ')
{
printf("%c",T -> data);
/* printf("\n00\n"); */
if(T -> lchild != NULL)
{
VisiteBitTree(T -> lchild);
/* printf("11\n"); */
}
if(T -> rchild != NULL)
{
VisiteBitTree(T -> rchild);
/*printf("22\n");*/
}
}
}
void main()
{
BitTree T = CreateBitTree();
clrscr();
printf("\n\n");
VisiteBitTree(T);
printf("\n");
}