L3-1 二叉搜索树的结构 (30 point(s))

二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉搜索树。(摘自百度百科)

给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 2 4 1 3 0 }插入后,得到一棵二叉搜索树,则陈述句如“2是树的根”、“1和4是兄弟结点”、“3和0在同一层上”(指自顶向下的深度相同)、“2是4的双亲结点”、“3是4的左孩子”都是正确的;而“4是2的左孩子”、“1和3是兄弟结点”都是不正确的。

输入格式:

输入在第一行给出一个正整数N(≤100),随后一行给出N个互不相同的整数,数字间以空格分隔,要求将之顺次插入一棵初始为空的二叉搜索树。之后给出一个正整数M(≤100),随后M行,每行给出一句待判断的陈述句。陈述句有以下6种:

  • A is the root,即"A是树的根";
  • A and B are siblings,即"AB是兄弟结点";
  • A is the parent of B,即"AB的双亲结点";
  • A is the left child of B,即"AB的左孩子";
  • A is the right child of B,即"AB的右孩子";
  • A and B are on the same level,即"AB在同一层上"。

题目保证所有给定的整数都在整型范围内。

输出格式:

对每句陈述,如果正确则输出Yes,否则输出No,每句占一行。

输入样例:

5
2 4 1 3 0
8
2 is the root
1 and 4 are siblings
3 and 0 are on the same level
2 is the parent of 4
3 is the left child of 4
1 is the right child of 2
4 and 0 are on the same level
100 is the right child of 3

输出样例:

Yes
Yes
Yes
Yes
Yes
No
No
No

对于这道题,重点在于判断在不在二叉搜索树上,可以用set,也可以用map,建议map,数组能不用就不用,动不动就段错误。

以下用set的,把一些不在最后判断的flag的句子删掉,再把注释打开,就成了用map的方法。

#include<iostream>
#include<set>
#include<map>
const long long INF=1e+6+7;
using namespace std;
set<long long> ansi;
map<long long,long long> bnsi;
struct temp
{
	long long val;
	struct temp* l,* r,* f;
};
typedef struct temp link;
typedef link* Link;
long long bns[INF];
void Union(Link& p,Link& head,Link& pre,long long op)
{
	if(head==NULL){
		bnsi[p->val]=op;
		head=p;
		head->f=pre;
		return ;
	}
	if(p->val>head->val)
	{
		Union(p,head->r,head,op+1);
	}
	else Union(p,head->l,head,op+1);
	return ;
}
Link find(Link& head,long long n)
{
	if(head->val==n)
	return head;
	if(head->val>n)return find(head->l,n);
	else return find(head->r,n);
}
int main()
{
	Link head=(Link)malloc(sizeof(link));
	head=NULL;
	long long sum;
	cin>>sum;
	for(long long n=0;n<sum;n++)
	{
		long long number;
		cin>>number;
		ansi.insert(number);
		Link p=(Link)malloc(sizeof(link));
		p->f=NULL;
		p->l=NULL;
		p->r=NULL;
		p->val=number;
		if(n==0)
		{
			bnsi[number]=1;
			head=p;
		}
		else
		{
			if(p->val>head->val)
			Union(p,head->r,head,2);
			else Union(p,head->l,head,2);
		}
	}
	long long number;
	cin>>number;
	for(long long n=0;n<number;n++)
	{
		long long ui;
		bool flag=true;
		string a;
		cin>>ui>>a;
		if(ansi.find(ui)==ansi.end())flag=false;
		if(a[0]=='a')
		{
			long long io;
			cin>>io>>a>>a;
			if(a[0]=='s')
			{
				if(ansi.find(io)==ansi.end())flag=false;
				if(!flag){
	cout<<"No"<<endl;
	continue;
}
//				if(ansi.find(ui)==ansi.end()||ansi.find(io)==ansi.end())
//				{
//					cout<<"No"<<endl;
//					continue;
//				}
				Link q=(Link)malloc(sizeof(link));
				Link w=(Link)malloc(sizeof(link));
				q=find(head,ui);
				w=find(head,io);
				if(q->f==NULL||w->f==NULL)flag=false;
				else
				{
					if(q->f==w->f)flag=true;
				else flag=false;
				}
			}           
			else
			{
				cin>>a>>a>>a;
				if(ansi.find(io)==ansi.end())flag=false;
				if(!flag){
	cout<<"No"<<endl;
	continue;
}
//				if(ansi.find(ui)==ansi.end()||ansi.find(io)==ansi.end())
//				{
//					cout<<"No"<<endl;
//					continue;
//				}
				if(bnsi[ui]==bnsi[io])flag=true;
				else flag=false;
			}
		}
		else
		{
			cin>>a>>a;
			if(a[0]=='r'&&a[1]=='o')
			{
				if(!flag){
	cout<<"No"<<endl;
	continue;
}
//				if(ansi.find(ui)==ansi.end())
//				{
//					cout<<"No"<<endl;
//					continue;
//				}
				if(bnsi[ui]==1)flag=true;
				else flag=false;
			}
			else if(a[0]=='p')
			{
				long long io;
				cin>>a>>io;
				if(ansi.find(io)==ansi.end())flag=false;
				if(!flag){
	cout<<"No"<<endl;
	continue;
}
//				if(ansi.find(ui)==ansi.end()||ansi.find(io)==ansi.end())
//				{
//					cout<<"No"<<endl;
//					continue;
//				}
				Link p=(Link)malloc(sizeof(link));
				p=find(head,io);
				if(p->f==NULL)flag=false;
				else
				{
					if(p->f->val==ui)flag=true;
					else flag=false;
				}
			}
			else if(a[0]=='l')
			{
				long long io;
				cin>>a>>a>>io;
				if(ansi.find(io)==ansi.end())flag=false;
				if(!flag){
	cout<<"No"<<endl;
	continue;
}
//				if(ansi.find(ui)==ansi.end()||ansi.find(io)==ansi.end())
//				{
//					cout<<"No"<<endl;
//					continue;
//				}
				Link p=(Link)malloc(sizeof(link));
				p=find(head,io);
				if(p->l==NULL)flag=false;
				else
				{
					if(p->l->val==ui)flag=true;
					else flag=false;
				}
			}
			else 
			{
				long long io;
				cin>>a>>a>>io;
				if(ansi.find(io)==ansi.end())flag=false;
				if(!flag){
	cout<<"No"<<endl;
	continue;
}
//				if(ansi.find(ui)==ansi.end()||ansi.find(io)==ansi.end())
//				{
//					cout<<"No"<<endl;
//					continue;
//				}
				Link p=(Link)malloc(sizeof(link));
				p=find(head,io);
				if(p->r==NULL)flag=false;
				else
				{
					if(p->r->val==ui)flag=true;
					else flag=false;
				}
			}
		}
		if(flag)cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
	return 0;
}
//if(ansi.find(ui)==ansi.end())flag=false;
//if(ansi.find(io)==ansi.end())flag=false;
//if(!flag){
//	cout<<"No"<<endl;
//	continue;
//}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值