L3-016 二叉搜索树的结构 (30分)
二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉搜索树。(摘自百度百科)
给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 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,即"A和B是兄弟结点";
A is the parent of B,即"A是B的双亲结点";
A is the left child of B,即"A是B的左孩子";
A is the right child of B,即"A是B的右孩子";
A and B are on the same level,即"A和B在同一层上"。
题目保证所有给定的整数都在整型范围内。
输出格式:
对每句陈述,如果正确则输出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
**思路:**先建树,再对根节点进行dfs,用map进行相应参数的存储。(用数组存储会出现段错误,因为要判断的数可能很大,也会是负数)
#include<string.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<iomanip>
#include<cstdlib>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
const ll INF = 1ll << 62;
const double PI = acos(-1);
const double eps = 1e-7;
const int mod = 998244353;
#define speed {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); }
struct node
{
int value;
node *l, *r;
node(int value = 0, node *l = NULL, node *r = NULL) :value(value), l(l), r(r) {}
};
struct Point//用来存储儿子节点初始化为inf
{
int x,y;
Point(){x=inf;y=inf;}
Point(int x,int y):x(x),y(y){}
};
void buildtree(node *&root, int x)//建树
{
if (root == NULL)root = new node(x);
else
{
if (x > root->value)buildtree(root->r, x);//较大值往右插入
if (x < root->value)buildtree(root->l, x);
}
}
map<int,int>savedeep;//用来存储每个节点的深度,用来判断两个节点是否在同一层
map<int,Point>son;//用来存儿子节点,用来判断左节点、右节点、是否是父亲节点
map<int,int>Map;//用来判断是否是兄弟节点,不能用数组类似哈希的方法判断,有些数是负数会造成段错误,前面两个同理
void dfs(node *root, int deep)
{
if (root == NULL)return;//到子节点了
savedeep[root->value]=deep;//存深度
if(root->l!=NULL)son[root->value].x=root->l->value;//存入左儿子
if(root->r!=NULL)son[root->value].y=root->r->value;
if(son[root->value].x!=inf&&son[root->value].y!=inf)//要判断两个是否是兄弟节点的前提是两个节点都要存在
{
Map[root->l->value]=root->r->value;
Map[root->r->value]=root->l->value;
}
dfs(root->l, deep + 1);
dfs(root->r, deep + 1);
}
void Free(node *root)//释放空间
{
if (root == NULL)return;
Free(root->l);
Free(root->r);
delete root;
}
int main()
{
int n,a[200];//用来存原数组
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
node *root;
root =new node(a[0]);//给根节点赋初值
for(int i=1;i<n;i++)
buildtree(root,a[i]);
dfs(root,1);
int k;
cin>>k;
getchar();//读入回车
while(k--)
{
int x,y;
string s;
getline(cin,s);
if(s.find("root")!=string::npos)//用来判断这句话里有没有root,用来判断这项操作的目的是什么
{
sscanf(s.c_str(),"%d is the root",&x);
if(x==a[0])printf("Yes\n");
else printf("No\n");
}
if(s.find("siblings")!=string::npos)
{
sscanf(s.c_str(),"%d and %d are siblings",&x,&y);
if(Map.find(x)==Map.end())printf("No\n");//要判断键值是否存在,题目会给你树上没有的数字,后面同理
else
{
if(Map[x]!=y)printf("No\n");
else printf("Yes\n");
}
}
if(s.find("parent")!=string::npos)
{
int f=0;
sscanf(s.c_str(),"%d is the parent of %d",&x,&y);
if(son.find(x)==son.end())printf("No\n");
else
{
if(son[x].x==y||son[x].y==y)f=1;
if(f==1)printf("Yes\n");
else printf("No\n");
}
}
if(s.find("left")!=string::npos)
{
int f=0;
sscanf(s.c_str(),"%d is the left child of %d",&x,&y);
if(son.find(y)==son.end())printf("No\n");
else
{
if(son[y].x==x||son[y].y==x)f=1;
if(f==1&&x<y)printf("Yes\n");//左节点要比父亲节点的权值小
else printf("No\n");
}
}
if(s.find("right")!=string::npos)
{
int f=0;
sscanf(s.c_str(),"%d is the right child of %d",&x,&y);
if(son.find(y)==son.end())printf("No\n");
else
{
if(son[y].x==x||son[y].y==x)f=1;
if(f==1&&x>y)printf("Yes\n");
else printf("No\n");
}
}
if(s.find("same")!=string::npos)
{
sscanf(s.c_str(),"%d and %d are on the same level",&x,&y);
if(savedeep.find(x)==savedeep.end()||savedeep.find(y)==savedeep.end())printf("No\n");
else if(savedeep[x]==savedeep[y])printf("Yes\n");
else printf("No\n");
}
}
Free(root);
system("pause");
return 0;
}