TC 段错误
我用TC写的一个程序,没有错误也没有警告,当时交作业的时候系统提示有 段错误 。 我找不到错在哪里 ,希望各路大神搭救一下,谢谢啦!程序是比较两棵二叉树是否一样。
源代码如下
#include<stdio.h>
#include<stdlib.h>
struct tree{
struct tree *left ;
int initial; /*the number of initial sequence */
int test; /*the number of sequence needing to be tested*/
struct tree *right ;
} ;
struct tree *root;
struct tree *Insert(int tmp, struct tree *T ); /*insert tmp to T->initial and return T*/
struct tree *Judge(int tmp, struct tree *T ); /*insert tmp to T->test and judge */
struct tree *Emptytest(struct tree *T); /*make the empty in order to read the next sequence*/
int result=0 , pri[10] , flag=1;
int main()
{
int nmb , line , i , j , p ,tmp;
root=NULL;
for(i=0;i<10;i++)
pri[i]=0;
while(1){
fflush(stdin);
scanf( "%d" , &nmb ) ; /* read in the total number of integers */
if(nmb==0)
break;
scanf( "%d" , &line ) ;
fflush(stdin);
for( i=0 ; i<nmb ; i++ ) /*read in the initial integers and insert them to root*/
{
scanf( "%d", &tmp ) ;
root=Insert( tmp , root ) ;
}
for( j=0 ; j<line ; j++ ) /* read in the sequences to be tested*/
{ fflush(stdin);
for( i=0 ; i<nmb ; i++ )
{
scanf( "%d", &tmp ) ;
root=Judge( tmp , root ) ; /* every time read in a integer then we test it */
if(flag==0) /* the two trees are different then flag will be made equal to 0 */
break;
}
if(i==nmb) /* the for loop above come to an end without break , the trees are the same */
{
pri[result++]=1; /* we need to print one YES when the program finishes */
for(p=0 ; p<nmb && root->test!=0 ; p++) /* finish testing a sequence and we make every Node->test empty so we can read in the next sequence */
root=Emptytest(root);
}
else {
pri[result++]=0; /* we need to print one NO when the program finishes */
for(p=0;p<nmb&&root->test!=0;p++) /* finish testing a sequence and we make every Node->test empty so we can read in the next sequence */
root=Emptytest(root);
flag=1; /* initialize the flag before a following judge */
}
}
}
for(i=0;i<result-1;i++) /* print out the result */
{
if(pri[i]==1)
printf("YES\n");
else printf("NO\n");
}
if(pri[i]==1)
printf("YES");
else printf("NO");
return 0 ;
}
struct tree * Insert(int tmp, struct tree *T ) /* insert tmp to a binary tree T */
{
if( T==NULL )
{
T=(struct tree *)malloc(sizeof(struct tree));
if( T==NULL )
{
printf("Error:Out of space!");
exit(0);
}
else /* create a new node */
{
T->initial = tmp ;
T->test=0;
T->left=T->right = NULL ;
}
}
else
if( tmp < T->initial ) /* we always attach the smaller integer to the left end */
T->left = Insert( tmp , T->left ) ;
else
if( tmp > T->initial ) /* we always attach the larger integer to the right end */
T->right = Insert( tmp , T->right ) ;
return T ;
}
struct tree * Judge(int tmp, struct tree *T )
{
if(T->test==0) /* T->test is empty and a integer can be placed here */
{
T->test=tmp;
if((T->test)!=(T->initial)) /* two trees are different at this place */
{
flag=0;
return T ;
}
}
else
if( tmp < T->initial ) /* we insert tmp and compare tmp with T->initial just like buiding a binary tree */
{
if(T->left==NULL)
{
flag=0;
return T ;
}
else T->left = Judge( tmp , T->left ) ;
}
else
if( tmp > T->initial )
{
if(T->right==NULL)
{
flag=0;
return T ;
}
else T->right = Judge( tmp , T->right ) ;
}
return T ;
}
struct tree *Emptytest(struct tree *T)
{
if(T->left->test!=0 && T->left!=NULL) /* node T has a left child */
T->left=Emptytest(T->left); /* before we empty T ,we meke its child empty recursively */
else if(T->right->test!=0 && T->right!=NULL) /* node T has a right child */
T->right=Emptytest(T->right);
else T->test=0 ; /* make T empty if it has no child */
return T ;
}