网上找的板子,改了下就过了,就T了一发,后来发现数组开小了,
这我就郁闷了,数组开小了不应该是RE吗,怎么变成T了。
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef struct BiTNode
{
int value;
struct BiTNode *lchild,*rchild;
}*BiTree;
bool LT(int a,int b) //LessThan小于
{
if(a<b)
return true;
else
return false;
}
/*
在根指针root所指向的二叉排序树中递归地查找其关键字等于data的数据元素,若查找成功,则指针p指向该数据元素结点,并返回true,
否则指针p指向查找路径上访问的最后一个结点并返回false指针,指针f指向root的双亲,其初始调用值NULL
*/
bool SearchBST(BiTree root,int data,BiTree f,BiTree &p)
{
if(!root)
{
p=f;
return false;
}
else if(data==root->value)
{
p=root;
return true;
}
else if(data<root->value)
{
return SearchBST(root->lchild,data,root,p);
}
else if(data>root->value)
{
return SearchBST(root->rchild,data,root,p);
}
}
//当二叉排序树root中不存在关键字等于data的数据元素时,插入data
inline void InsertBST(BiTree &root,int data)
{
BiTree p,s;
if(!SearchBST(root,data,NULL,p))
{
s=(struct BiTNode *)malloc(sizeof(BiTNode));
s->value=data;
s->lchild=s->rchild=NULL;
if(p==NULL)
root=s;
else if(LT(data,p->value))
p->lchild=s;
else
p->rchild=s;
}
return ;
}
void DeleteBST(BiTree root)
{
if(root)
{
DeleteBST(root->lchild); //释放左子树
DeleteBST(root->rchild); //释放右子树
free(root); //释放根结点
}
}
void dfs(BiTree root,int data)
{
if(!root)
{
return ;
}
else if(data==root->value)
{
return;
}
else if(data<root->value)
{
cout<<'E';
dfs(root->lchild,data);
}
else if(data>root->value)
{
cout<<'W';
dfs(root->rchild,data);
}
}
int main(void)
{
int i,a[1010],n;
int t;
cin>>t;
while(t--)
{
cin>>n;
BiTree root;
root=NULL;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
InsertBST(root,a[i]);
}
int q;
cin>>q;
while(q--)
{
int x;
cin>>x;
BiTree p;
if(root->value==x)
{
cout<<endl;
continue;
}
dfs(root,x);
cout<<endl;
}
DeleteBST(root);
}
return 0;
}