二叉搜索树 链表
有指针一定要小心中,今天一不小心,浪费了我一个小时去改错。。悲剧。。。
注意指针初始化。。。还有全局变量,局部变量。。指针与取地址。。
#include<stdio.h>
#include<string.h>
//#include<debug.h>
#include<stdlib.h>
typedef struct node
{
struct node *rchild,*lchild;
int ch;
}NODE,*NOE;
void creattree(NOE &T,NOE P)
{
NOE S,Q;
if(P==NULL)
return ;
//printf("*********\n\n");
if(T==NULL)
{
T=P;
}
else
{
S=T;
// printf("*********\n\n");
while(S!=NULL)
{
Q=S;
if(P->ch<S->ch)
{
S=S->lchild;
}
else
{
S=S->rchild;
}
}
if(P->ch>=Q->ch)
Q->rchild=P;
else
Q->lchild=P;
}
}
int comp(NOE q,NOE l)
{
if(q==NULL&&l==NULL)
return 0;
else if((q==NULL&&l!=NULL)||(q!=NULL&&l==NULL))
return 1;
else if(q->ch!=l->ch)
return 1;
else
return comp(q->lchild,l->lchild)+comp(q->rchild,l->rchild);
}
int main( )
{
int N,i,len;
//Debug();
char str[11];
while(scanf("%d",&N),N)
{
getchar();
scanf("%s",str);
len=strlen(str);
NOE V;
V=NULL;
for(i=0;i<len;i++)
{
NOE H;
H=new NODE;
H->ch=str[i]-'0';
H->rchild=NULL;
H->lchild=NULL;
creattree(V,H);
//printf("*********\n\n");
}
while(N--)
{
NOE r;
r=NULL;
char ab[11];
scanf("%s",ab);
int len2=strlen(ab);
for(i=0;i<len2;i++)
{
NODE *t;
t=new NODE;
t->ch=ab[i]-'0';
t->rchild=NULL;
t->lchild=NULL;
creattree(r,t);
}
int res=comp(V,r);
printf(res?"NO\n":"YES\n");
}
}
system("pause");
return 0;
}
posted on 2011-04-16 11:33 more think, more gains 阅读(171) 评论(0) 收藏 举报
浙公网安备 33010602011771号